dev.nlited.com

>>

Code

<<<< prev
next >>>>

2016-04-29 20:22:34 chip Page 1680 📢 PUBLIC

The most precise way to control the speed of the motor is to drive the STEP signal from a hardware timer. If I just need steady-state speed control I can avoid the overhead of using interrupts by using a PWM timer to output a pulse on the STEP pin. This has the advantage of being completely "fire and forget", I configure the timer and just let it go. The downside is that I need to have some sort of external timer or sensor to let me know when to change the speed.


PWM_X: unsigned int StepMode= 3; //0=Full step, 3= 1/8 microstep int main(void) { int n1; WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer PortConfig(); PWM_Config(1000); for(n1=10;n1--;) __delay_cycles(32767); PWM_Config(180); return 0; } void PortConfig(void) { P1SEL= P1SEL & ~0x70; //P1.4 as gpio (MTR_STEP_X) P1DIR= P1DIR | 0x70; //P1.4 as output P1OUT= (P1OUT & 0x70)|0x40; //MTR_DIR_X=1= Clockwise P2SEL= 0x00; //P2.0:7 as gpio P2DIR= (P2DIR & ~0x02)|0x10; //P2.1 as input, 2.4 as output. P2REN= 0x02; //Enable pull resistor on P2.1 P2OUT|= 0x02; //Set P2.1 pull resistor to up. P3SEL= (P3SEL & ~0x80)|0x00; P3DIR= (P3DIR & ~0x80)|0x80; P3OUT= (P3OUT & ~0x80)|0x80; P4DIR|= 0x88; //P4.3,7 as output P4SEL&= ~0x88; //P4.3,7 as gpio P4OUT|= 0x88; //Turn LED2 on //Set stepping mode, P8.1-2. P8SEL= P8SEL & ~0x06; P8DIR= P8DIR|0x06; P8REN|= 0x06; P8OUT= (P8OUT & ~0x06)|0x06; } void PWM_Config(unsigned int period) { unsigned int pulse; period= period*(8>>StepMode); pulse= period/10; TA0CCTL3= OUTMOD_7; //Toggle/Reset TA0CTL= TASSEL_2 + MC_1; //Use SM clock (1us) TA0CCR0= period-pulse; //Period (minimum 180) TA0CCR3= pulse; //Pulse width (minimum 10) P1DIR|= 0x10; //Output P1SEL|= 0x10; //Select TA0.3 }

The maximum speed with the NEMA-8 motor is 180/20, which is a 172us period and 19us pulse, 5.85KHz. Any shorter than this and the motor will stall. In 1/8 microstepping mode (1600 steps per revolution) this is 3.6RPM. This also gives the actual SM clock, 1.05MHz (0.95us). This is off from the theoretical 1MHz due to the sloppy 32KHz crystal. In full step mode, the minimum period is about 1440 (180*8). This is nowhere close to the A3967 specification of 500KHz (2us).

The Fixed Off-Time is defined as RtCt (20K*680pF= 14us). The RC Blanking is defined as 1400Ct (1400*680pF= 980ns). This adds up to 15us. If this is applied to every step, this would limit the stepping frequency to 67KHz.

Both STEP_X (P1.4) and STEP_Y (P1.5) use TA0, so they will both run at the same speed in PWM mode. STEP_Z (P2.4) uses TA2. I changed STEP_Y to use TA1.1 (P2.0) in CtrlMtr so they can all be run independently.



WebV7 (C)2018 nlited | Rendered by tikope in 155.941ms | 18.219.81.129