制作出来的实物图如下:
#include <REGX52.H>unsigned char count=0; unsigned char pwm=5; //占空比设定1-10 sbit Apwm12=P3^4; //PWM输出 sbit Apwm34=P3^5; //PWM输出 sbit Bpwm12=P3^6; //PWM输出 sbit Bpwm34=P3^7; //PWM输出 void tingzhi() //停止 { P1=0x00; //开机等待 } void qianjin() //前进:A1=1 A2=0,A3=0 A4=1,B1=1 B2=0,B3=1 B4=0 { P1=0X59; } void houtui() //后退:A1=0 A2=1,A3=1 A4=0,B1=0 B2=1,B3=0 B4=1 { P1=0XA6; } void zuoyi() //左移 { P1=0X6A; } void youyi() //右移 { P1=0X95; } void nsz() //逆时针 { P1=0X56; } void ssz() //顺时针 { P1=0XA9; } void zuoshang() //左上 { P1=0X48; } void zuoxia() //左下 { P1=0X22; } void youshang() //右上 { P1=0X11; } void youxia() //右下 { P1=0X84; } void zuozhuan() //左转 { P1=0X50; } void youzhuan()//右转 { P1=0X09; } void Timer0_Int() //T0初始化 { TMOD=0X01; TH0= 0XFc; //1ms定时 TL0= 0X66; TR0= 1; ET0= 1; EA = 1; } void timer0()interrupt 1 //T0中断程序 { count++; TR0= 0; TH0= 0XFc; //重载1ms定时 TL0= 0X66; if(count<=pwm) { Apwm12=1; Apwm34=1; Bpwm12=1; Bpwm34=1; } else { Apwm12=0; Apwm34=0; Bpwm12=0; Bpwm34=0; } if(count>=10) { count=0; //10ms清零 } TR0= 1; //开启计时 } void UART_INIT() //串口初始化 { SM0 = 0; SM1 = 1; //串口工作方式1 REN = 1; //允许串口接收 EA = 1; //开总中断 ES = 1; //开串口中断 TMOD = 0x20; //8位自动重装模式 TH1 = 0xfd; TL1 = 0xfd; //9600波特率 TR1 = 1; //启动定时器1 } //串口中断 void UART_SER() interrupt 4 { if(RI) { RI = 0;//清除接收标志 switch(SBUF) { case 0x00: tingzhi(); break; //停止 case 0x01: qianjin(); break; //前进 case 0x02: houtui(); break; //后退 case 0x03: zuoyi(); break; //左移 case 0x04: youyi(); break; //右移 case 0x05: nsz(); break; //逆时针 case 0x06: ssz(); break; //顺时针 case 0x07: zuoshang(); break; //左上 case 0x08: youshang(); break; //右上 case 0x09: zuoxia(); break; //左下 case 0x10: youxia(); break; //右下 case 0x11: zuozhuan(); break; //左转 case 0x12: youzhuan(); break; //右转 } } } void main() { tingzhi(); Timer0_Int(); UART_INIT();//串口初始化 while(1); }
复制代码