/**********************************************************************/ /* */ /* Demo program for CUHK ARM board v.14 */ /* */ /**********************************************************************/ /* */ /* ARM CPU: Philips LPC2131 */ /* Crystal clock: 11.0592 MHz */ /* Serial port baudrate: 57600 */ /* Programming tool: KEIL uVision3 RealView for ARM */ /* */ /**********************************************************************/ #include extern void init_timer(void); #define PWM_FREQ 276480 //set PWM frequency to 50 Hz #define L_DIR 0x00010000 //set p0.16 as left motor direction #define L_DIRinv 0x00020000 //set p0.17 as inverted left motor direction #define R_DIR 0x00040000 //set p0.18 as right motor direction #define R_DIRinv 0x00080000 //set p0.19 as inverted right motor direction #define TEST_PIN 0x00010000 //set p1.16 as Test pin #define LWheelSen 0x00000040 //set p0.6 as left wheel sensor input #define RWheelSen 0x00000008 //set p0.3 as right wheel sensor input #define NEWLINE sendchar(0x0a); sendchar(0x0d) //define global variables long timeval; int lcur,rcur,lold,rold,lcount,rcount; //=============================== void Init_Serial_A(void) { U0LCR = 0x83; //8 bit length ,DLAB=1 U0DLL = 0x0F; //57600;3+Fpclk/(16*baudrate); U0DLM = 0x00; U0LCR = 0x03; //DLAB=0 } char getchar(void) { volatile char ch = '0'; while ((U0LSR&0x1)==0) // wait until receive a byte ; ch = U0RBR; // receive character return ch; } void sendchar(char ch) { while( (U0LSR&0x40)==0 ); U0THR = ch; // Transmit next character } int print(char *p) { while(*p!='\0') { sendchar(*p++); } return(0); } void putint(int count) { sendchar('0' + count/10000); sendchar('0' + (count/1000) % 10); sendchar('0' + (count/100) % 10); sendchar('0' + (count/10) % 10); sendchar('0' + count % 10); } int read_sensor(int channel) { int temp; ADCR=0x1<>=6; temp&=0x3ff;//TEMP IS 0-3V PRESICION IS 1024 return (temp*33); } int main(void) { char cin; long leftPWM,rightPWM; PINSEL0 = 0x00000005; // set p0.0 to TXD0, p0.1 to RXD0 and the rest to GPIO PINSEL1 = 0x00400000; // set p0.27 to ad0.0 and the rest to GPIO PINSEL1 |= 0x01000000; // set p0.28 to ad0.1 PINSEL1 |= 0x04000000; // set p0.29 to ad0.2 PINSEL1 |= 0x10000000; // set p0.30 to ad0.3 PINSEL1 |= 0x00040000; // set p0.25 to ad0.4 PINSEL1 |= 0x00000400; // set p0.21 to PWM5 PINSEL0 |= 0x00008000; // set p0.7 to PWM2 Init_Serial_A(); // Init COM port init_timer(); // Init TIMER 0 NEWLINE; print("======================================================================="); NEWLINE; print("* *"); NEWLINE; print("* CUHK Computer Science and Engineering Department *"); NEWLINE; print("* LPC2131 ARM Board (ver.14) *"); NEWLINE; print("* *"); NEWLINE; print("======================================================================="); NEWLINE; print("* *"); NEWLINE; print("* f - forward 100 steps (~23cm); b - backward 100 steps (~23cm) *"); NEWLINE; print("* *"); NEWLINE; print("* F - continue forward; B - continue backward *"); NEWLINE; print("* *"); NEWLINE; print("* s - stop; p - show sensor values (Unit:0.1mV) *"); NEWLINE; print("* *"); NEWLINE; print("* z - decrease speed; x - increase speed *"); NEWLINE; print("* *"); NEWLINE; print("======================================================================="); NEWLINE; NEWLINE; PWMPCR=0x2000; // enable pwm5 PWMPCR|=0x0400; // enable pwm2 PWMMCR=0x0002; PWMMR0 = PWM_FREQ; //set PWM frequency to 50 Hz //set robot to full speed leftPWM=PWM_FREQ; rightPWM=PWM_FREQ; PWMMR2 = leftPWM; //set left motor PWM width to full speed PWMMR5 = rightPWM; //set right motor PWM width to full speed PWMLER = 0x25; //enable match 0,2,5 latch to effective PWMTCR=0x09; //set p0.16-p0.19 as output IO0DIR|=L_DIR; IO0DIR|=L_DIRinv; IO0DIR|=R_DIR; IO0DIR|=R_DIRinv; IO1DIR|=TEST_PIN; // p1.16 as Outputs //initialize variables lcur=0; rcur=0; lold=0; rold=0; lcount=0; rcount=0; //==================== print("Front = "); putint(read_sensor(0)); print("; Left = "); putint(read_sensor(1)); print("; Right = "); putint(read_sensor(2)); NEWLINE; print(" Back Left = "); putint(read_sensor(3)); print("; Back Right = "); putint(read_sensor(4)); NEWLINE; NEWLINE; while(1) { cin=getchar(); if((cin=='f')||(cin=='F')) { //set robot to move forward 100 steps IO0SET|=L_DIR; IO0CLR|=L_DIRinv; IO0SET|=R_DIRinv; IO0CLR|=R_DIR; if(cin=='f') { lcount=0; //reset left step count rcount=0; //reset right step count //stop when stepcount reach 100 steps while((lcount<=100)||(rcount<=100)) { if(lcount>=100) { IO0CLR|=L_DIRinv; //stop left motor IO0CLR|=L_DIR; lcount=0xff; } if(rcount>=100) { IO0CLR|=R_DIRinv; //stop right motor IO0CLR|=R_DIR; rcount=0xff; } } } } else if ((cin=='b')||(cin=='B')) { //set robot to move backward 100 steps IO0SET|=L_DIRinv; IO0CLR|=L_DIR; IO0SET|=R_DIR; IO0CLR|=R_DIRinv; if(cin=='b') { lcount=0; //reset left step count rcount=0; //reset right step count //stop when stepcount reach 100 steps while((lcount<=100)||(rcount<=100)) { if(lcount>=100) { IO0CLR|=L_DIRinv; //stop left motor IO0CLR|=L_DIR; } if(rcount>=100) { IO0CLR|=R_DIRinv; //stop right motor IO0CLR|=R_DIR; } } } } else if (cin=='s') { //set robot to stop IO0CLR|=L_DIRinv; IO0CLR|=L_DIR; IO0CLR|=R_DIR; IO0CLR|=R_DIRinv; } else if (cin=='z') { //decrease speed leftPWM=leftPWM-1000; rightPWM=rightPWM-1000; if(leftPWM<0) { leftPWM=0; rightPWM=0; } PWMMR2=leftPWM; PWMMR5=rightPWM; PWMLER = 0x24; //enable match 2,5 latch to effective } else if (cin=='x') { //increase speed leftPWM=leftPWM+1000; rightPWM=rightPWM+1000; if(leftPWM>PWM_FREQ) { leftPWM=PWM_FREQ; rightPWM=PWM_FREQ; } PWMMR2=leftPWM; PWMMR5=rightPWM; PWMLER = 0x24; //enable match 2,5 latch to effective } else if (cin=='p') { print("Front = "); putint(read_sensor(0)); print("; Left = "); putint(read_sensor(1)); print("; Right = "); putint(read_sensor(2)); NEWLINE; print(" Back Left = "); putint(read_sensor(3)); print("; Back Right = "); putint(read_sensor(4)); NEWLINE; NEWLINE; } } } void __irq IRQ_Exception() { timeval++; //generate square wave at test pin if((timeval%2)==0) IO1SET|=TEST_PIN; else IO1CLR|=TEST_PIN; //================= //get the current wheel sensor values lcur=IO0PIN & LWheelSen; rcur=IO0PIN & RWheelSen; //count the number of switching if(lcur!=lold) { lcount++; lold=lcur; } if(rcur!=rold) { rcount++; rold=rcur; } T0IR = 1; // Clear interrupt flag VICVectAddr = 0; // Acknowledge Interrupt } /* Setup the Timer Counter 0 Interrupt */ void init_timer (void) { T0PR = 0; // set prescaler to 0 T0MR0 =13800; // set interrupt interval to 1mS T0MCR = 3; // Interrupt and Reset on MR0 T0TCR = 1; // Timer0 Enable VICVectAddr0 = (unsigned long)IRQ_Exception; // set interrupt vector in 0 VICVectCntl0 = 0x20 | 4; // use it for Timer 0 Interrupt VICIntEnable = 0x00000010; // Enable Timer0 Interrupt }