#include <8052.h> sfr at 0xD7 PLLCON; sfr at 0xEF ADCCON1; sfr at 0xD8 ADCCON2; volatile int _n; /* Timer 0 Interrupt Routine */ void ET0_int() interrupt 1 { _n--; } void wait(int w) { int i; TH0 = 129; // 16777216 / 12 / 127 / 110 = 100.08 Hz TL0 = 129; TR0 = 1; ET0 = 1; for (i = w; i > 0; i--) { _n = 110; while (_n > 0) ; } TR0 = 0; ET0 = 0; } void stop(int i) { P3 = P3 | 0x3C; wait(i); } void forward(int i) { P3 = P3 & 0xC3; P3 = P3 | 0x14; wait(i); } void back(int i) { P3 = P3 & 0xC3; P3 = P3 | 0x28; wait(i); } void right(int i) { P3 = P3 & 0xC3; P3 = P3 | 0x18; wait(i); } void left(int i) { P3 = P3 & 0xC3; P3 = P3 | 0x24; wait(i); } void init(void) { PLLCON = 0x00; // core clock = 16.777216 MHz SCON = 0x52; // 8bit, no_parity, 1 stop_bit TMOD = 0x22; // Timer1=8 bit_mode, Timer0=8 bit_mode TH1 = 0xF7; // ..for 9600 baud.. TR1 = 1; // ..(assuming 16.777216 MHz clock) PCON = 0x80; // double baud rate ADCCON1 = 0xAC; // power up ADC & not EXT_Reference ADCCON2 = 0x00; // sellect chan#0 P1 = 0x03; // Light OFF P3 = 0x03; // all motor off /* interrupt enable */ IE = 0x80; } void main(void) { init(); forward(200); left(50); back(100); stop(0); }