??? 03/12/04 16:12 Read: times |
#66597 - RE: stepper motor control Responding to: ???'s previous message |
here's the code i had written in C. can u tell me what modifications i need to do to make step delay and counting a timer 0 interrupt
/*main.c*/ #include <reg51xa.h> #include <stdio.h> #include <stdlib.h> sbit target=P3^2; /*set pin 3.2 as target */ sbit notA=P2^0; /*set pin 1.0 as output */ sbit isA=P2^1; /*set pin 1.1 as output */ sbit notB=P2^2; /*set pin 1.2 as output */ sbit isB=P2^3; /*set pin 1.3 as output */ char step[]={12,6,3,9};/*load the step sequence*/ char binbuf2,binbuf3,binbuf4,direction; int y=1;int i; int dir; long int st=0; /*initialise step count */ int t=0;int z=1; void sendserial(unsigned char);/*function for serial communication*/ void convert(); /*function for conversion */ void delay(); /*function for delay */ void delay(){ int g, h; for(g=0; g<5000; g++) for(h=0; h<5000; h++) g=g+1; } void main() { TMOD = 0x20; /* configure timer for the correct baud rate */ TH1 = 0xe6; /* 1200 bps for 12 MHz clock */ TCON = 0x00; /* Set timer to not running */ SCON = 0x50; /* Set Serial IO to receive and normal mode */ TR1 = 1; /* start timer to Receive */ /* clockwise movement */ st=0; direction='C'; ACC=direction; sendserial(ACC); i = 0; while(st!=100) { P2=step[i]; delay(); i++; if(i == 4) i = 0; st=st+1; if(target == 1){ t=t+1; ACC=st; convert(); sendserial(binbuf4); sendserial(binbuf3); sendserial(binbuf2); if(t==2) /* if no. of pulses = 2 */ { ACC=t; convert(); sendserial(binbuf4); sendserial(binbuf3); sendserial(binbuf2); } if(t==6) /* if no. of pulses = 6 */ { ACC=t; convert(); sendserial(binbuf4); sendserial(binbuf3); sendserial(binbuf2); } } } /* counter clockwise movement */ direction='A'; ACC=direction; sendserial(ACC); st=0; t=0; i = 3; while(st!=100){ P2=step[i]; delay(); i--; if(i < 0) i = 3; st=st+1; if(target == 1){ t=t+1; ACC=st; convert(); sendserial(binbuf4); sendserial(binbuf3); sendserial(binbuf2); if(t==4) /* if no. of pulses = 4 */ { ACC=t; convert(); sendserial(binbuf4); sendserial(binbuf3); sendserial(binbuf2); } if(t==6) /* if no. of pulses = 6 */ { ACC=t; convert(); sendserial(binbuf4); sendserial(binbuf3); sendserial(binbuf2); } } } ACC='E'; sendserial(ACC); ACC='N'; sendserial(ACC); ACC='D'; sendserial(ACC); } void serial_init() { SCON = 0x50; /* mode 1, 8-bit uart, enable receiver */ PCON|= 0x80; /* PCON^7 = SMOD doubles the baud rate i.e. k=2 */ TMOD = 0x20; /* timer 1, mode 2, 8-bit reload */ TH1 = 0xFD; /* reload value for 19200 baud */ TR1 = 1; IE = 0x90; /* Enable Serial Int */ } void sendserial( unsigned char ch ) interrupt 4 { TI = 0; SBUF = ch; while ( !TI ); } |