Oliver Hobson

Multiplexed unipolar stepper motor control

Discussion created by Oliver Hobson on Aug 30, 2011
Latest reply on Sep 2, 2011 by bigmac

Hi

 

I am a newcomer to microcontrollers and have been struggling with the coding for the control of 6 UAG2 unipolar stepper motors that are multiplexed through a 74LS151N using the GT16A  microcontroller. I am trying to recive the motor instruction through SCI and in that data it identifies which motor needs to be selected and in which direction it should rotate for x amount of steps. I don't think I am going about this in the correct manner and am desperately in need of help especially since I am guessing I need to use the TPM module

 

The wiring diagram is attached and the code is below:

 

The SCI interrupt to receive the data:

 

interrupt 20void SCI_interrupt (void) {    asm ("nop");    if (SCI2S1_RDRF) {            // SCI 2 receive interrupt        if (PacketReceiveIndex == 0 && SCI2D == 0x23      // ascii #                                  || SCI2D == 0x41    // ascii A                                  || SCI2D == 0x42    // ascii B                                  || SCI2D == 0x43    // ascii C                                  || SCI2D == 0x44    // ascii D                                  || SCI2D == 0x45    // ascii E                                  || SCI2D == 0x46    // ascii F                                  || SCI2D == 0x61    // ascii a                                  || SCI2D == 0x62    // ascii b                                  || SCI2D == 0x63    // ascii c                                  || SCI2D == 0x64    // ascii d                                  || SCI2D == 0x65    // ascii e                                  || SCI2D == 0x66) { // ascii f            StartReceived = 1;      }        else if (StartReceived == 1 && SCI2D == 0x23) {   // ascii #       PacketReceive[PacketReceiveIndex] = SCI2D;          if (PacketReceiveIndex == 15) {         // done??              StartReceived = 0;                    // reset for next        PacketReceiveIndex = 0;               // reset for next        SCI2C2_TIE = 0;                PiezoMove (PacketReceive);                                                }             else PacketReceiveIndex++;              // not done, increment index        }         else if (StartReceived == 1 && SCI2D == 0x41      // ascii A                                  || SCI2D == 0x42    // ascii B                                  || SCI2D == 0x43    // ascii C                                  || SCI2D == 0x44    // ascii D                                  || SCI2D == 0x45    // ascii E                                  || SCI2D == 0x46    // ascii F                                  || SCI2D == 0x61    // ascii a                                  || SCI2D == 0x62    // ascii b                                  || SCI2D == 0x63    // ascii c                                  || SCI2D == 0x64    // ascii d                                  || SCI2D == 0x65    // ascii e                                  || SCI2D == 0x66) { // ascii f                                        PacketReceive[PacketReceiveIndex] = SCI2D;          if (PacketReceiveIndex == 15) {         // done??              StartReceived = 0;                    // reset for next        PacketReceiveIndex = 0;               // reset for next        SCI2C2_TIE = 0;      }    }  }       }

 and the actual motor control code:

void motorStep (unsigned char Data[]) {  int loopCount = 0;  int i = 0;  unsigned char motorData[4];  motorData[0] = 0x09;  motorData[1] = 0x0A;    motorData[2] = 0x06;  motorData[3] = 0x05;    if (Data[0-7] == 0x41) {     // motor 1 forward: ascii A        // select motor with multiplexer    PTBD = 0x00;                           // Send 0,0,0 to mux        for (loopCount = 0; loopCount <= Data[8-15]; loopCount++) {            PTDD = motorData[i++];                       loopCount++;      asm ("nop");            if (i == 3) i = 0;           }     }    else if (Data[0-7] == 0x61) {     // motor 1 backward: ascii a      // select motor with multiplexer    PTBD = 0x00;                     // Send 0,0,0 to mux          for (loopCount = 0; loopCount <= Data[8-15]; loopCount++) {            PTDD = motorData[i--];      loopCount++;      asm ("nop");            if (i == 0) i = 3;              }  }       else if (Data[0-7] == 0x42) {     // motor 2 forward: ascii B    // select motor with multiplexer    PTBD = 0x01;                           // Send 0,0,1 to mux        for (loopCount = 0; loopCount <= Data[8-15]; loopCount++) {            PTDD = motorData[i++];                       loopCount++;      asm ("nop");            if (i == 3) i = 0;           }     }    else if (Data[0-7] == 0x62) {     // motor 2 backward: ascii b      // select motor with multiplexer    PTBD = 0x01;                     // Send 0,0,1 to mux          for (loopCount = 0; loopCount <= Data[8-15]; loopCount++) {            PTDD = motorData[i--];      loopCount++;      asm ("nop");            if (i == 0) i = 3;              }  }    else if (Data[0-7] == 0x43) {     // motor 3 forward: ascii C        // select motor with multiplexer    PTBD = 0x02;                           // Send 0,1,0 to mux        for (loopCount = 0; loopCount <= Data[8-15]; loopCount++) {            PTDD = motorData[i++];                       loopCount++;      asm ("nop");            if (i == 3) i = 0;           }     }    else if (Data[0-7] == 0x63) {     // motor 3 backward: ascii c      // select motor with multiplexer    PTBD = 0x02;                     // Send 0,1,0 to mux          for (loopCount = 0; loopCount <= Data[8-15]; loopCount++) {            PTDD = motorData[i--];      loopCount++;      asm ("nop");            if (i == 0) i = 3;              }  }    else if (Data[0-7] == 0x44) {     // motor 4 forward: ascii D        // select motor with multiplexer    PTBD = 0x03;                           // Send 0,1,1 to mux        for (loopCount = 0; loopCount <= Data[8-15]; loopCount++) {            PTDD = motorData[i++];                       loopCount++;      asm ("nop");            if (i == 3) i = 0;           }     }    else if (Data[0-7] == 0x64) {     // motor 4 backward: ascii d      // select motor with multiplexer    PTBD = 0x03;                     // Send 0,1,1 to mux          for (loopCount = 0; loopCount <= Data[8-15]; loopCount++) {            PTDD = motorData[i--];      loopCount++;      asm ("nop");            if (i == 0) i = 3;              }  }    else if (Data[0-7] == 0x45) {     // motor 5 forward: ascii E           // select motor with multiplexer    PTBD = 0x04;                           // Send 1,0,0 to mux        for (loopCount = 0; loopCount <= Data[8-15]; loopCount++) {            PTDD = motorData[i++];                       loopCount++;      asm ("nop");            if (i == 3) i = 0;           }     }    else if (Data[0-7] == 0x65) {     // motor 5 backward: ascii e      // select motor with multiplexer    PTBD = 0x04;                     // Send 1,0,0 to mux          for (loopCount = 0; loopCount <= Data[8-15]; loopCount++) {            PTDD = motorData[i--];      loopCount++;      asm ("nop");            if (i == 0) i = 3;              }  }    else if (Data[0-7] == 0x46) {     // motor 6 forward: ascii F           // select motor with multiplexer    PTBD = 0x05;                           // Send 1,0,1 to mux        for (loopCount = 0; loopCount <= Data[8-15]; loopCount++) {            PTDD = motorData[i++];                       loopCount++;      asm ("nop");            if (i == 3) i = 0;           }     }    else if (Data[0-7] == 0x66) {     // motor 6 backward: ascii f      // select motor with multiplexer    PTBD = 0x05;                     // Send 1,0,1 to mux          for (loopCount = 0; loopCount <= Data[8-15]; loopCount++) {            PTDD = motorData[i--];      loopCount++;      asm ("nop");            if (i == 0) i = 3;              }  }  }

 Thanks

Outcomes