本帖最后由 KeepYn 于 2021-4-12 21:41 编辑
- /*1、串口发送00,电机停止转动,指示灯1点亮;
- 2、串口发送01,电机全速正转,指示灯2点亮;
- 3、串口发送02,电机全速反转,指示灯3点亮;
- 4、串口发送03,电机50%速度正转,指示灯2闪烁;
- 5、串口发送04,电机50%速度反转,指示灯3闪烁;
- 数码管代替指示灯(数码管的正向反向流动,流动速度的快慢代替指示灯)指示灯不亮不闪烁
- */
- #include <reg51.h> //头文件 #预处理命令符 inclue预处理命令
- #define uint unsigned int
- #define uchar unsigned char
- uchar a,b,i; //定义全局变量a的值域,以便下面函数使用
- uchar SendBuf[]="The signal is normal";
- //定义数组,储存返回语句
- uchar code DMZ50[]={0x01,0x01,0x01,0x01,0x02,0x04,0x08,0x08,0x08,0x08,0x10,0x20}; //流水显示的段码,定义为正
- uchar code DMF50[]={0x20,0x10,0x08,0x08,0x08,0x08,0x04,0x02,0x01,0x01,0x01,0x01}; //流水显示的段码,定义为反
- uchar code DM0[]={0x49,0x36,0x49,0x36,0x49,0x36,0x49,0x36,0x49,0x36,0x49,0x36,0x49,0x36,0x49,0x36};
- //流水显示的段码,停
- uchar code DMZ[] = {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x02,0x04,
- 0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x10,0x20};
- uchar code DMF[] = {0x20,0x10,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,
- 0x04,0x02,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01};
- sbit WA=P2^2;
- sbit WB=P2^3; //74HC138译码器位选管脚(数码管用)
- sbit WC=P2^4;
- /*蜂鸣器*/
- sbit BZ=P2^5;
- //L298N引脚定义
- sbit ena = P1^0; //l298n电机驱动的相关引脚
- sbit in1 = P1^1;
- sbit in2 = P1^2;
- sbit in3 = P1^3;
- sbit in4 = P1^4;
- sbit enb = P1^5;
- uchar MA = 0,MB = 0; //pwm控制用
- uchar PWMA = 20;
- uchar PWMB = 20;
- uchar cmd; //串口接收的命令sbuf
- void delay(uint z); //声明延时函数
- /*数码管*/
- void showDigital()
- {
- switch(cmd)
- {
- case(0):
- for(i=0;i<16;i++)
- {
- switch(i)
- { case(8):
- WA = 0;WB = 0;WC = 0;break;
- case(9):
- WA = 1;WB = 0;WC = 0;break;
- case(10):
- WA = 0;WB = 1;WC = 0;break;
- case(11):
- WA = 1;WB = 1;WC = 0;break;
- case(12):
- WA = 0;WB = 0;WC = 1;break;
- case(13):
- WA = 1;WB = 0;WC = 1;break;
- case(14):
- WA = 0;WB = 1;WC = 1;break;
- case(15):
- WA = 1;WB = 1;WC = 1;break;
- case(7):
- WA = 0;WB = 0;WC = 0;break;
- case(6):
- WA = 1;WB = 0;WC = 0;break;
- case(5):
- WA = 0;WB = 1;WC = 0;break;
- case(4):
- WA = 1;WB = 1;WC = 0;break;
- case(3):
- WA = 0;WB = 0;WC = 1;break;
- case(2):
- WA = 1;WB = 0;WC = 1;break;
- case(1):
- WA = 0;WB = 1;WC = 1;break;
- case(0):
- WA = 1;WB = 1;WC = 1;break;
- }
- P0 = DM0[i];
- delay(250);
- }
- break;
- case(3):
- for(i=0;i<12;i++)
- {
- switch(i)
- {
- case(11):
- WA = 1;WB = 1;WC = 1;break;
- case(10):
- WA = 1;WB = 1;WC = 1;break;
- case(9):
- WA = 1;WB = 1;WC = 1;break;
- case(8):
- WA = 0;WB = 1;WC = 1;break;
- case(7):
- WA = 1;WB = 0;WC = 1;break;
- case(6):
- WA = 0;WB = 0;WC = 1;break;
- case(5):
- WA = 0;WB = 0;WC = 1;break;
- case(4):
- WA = 0;WB = 0;WC = 1;break;
- case(3):
- WA = 0;WB = 0;WC = 1;break;
- case(2):
- WA = 1;WB = 0;WC = 1;break;
- case(1):
- WA = 0;WB = 1;WC = 1;break;
- case(0):
- WA = 1;WB = 1;WC = 1;break;
- }
- P0 = DMZ50[i];
- delay(50);
- }
- break;
- case(4):
- for(i=0;i<12;i++)
- {
- switch(i)
- {
- case(0):
- WA = 1;WB = 1;WC = 1;break;
- case(1):
- WA = 1;WB = 1;WC = 1;break;
- case(2):
- WA = 1;WB = 1;WC = 1;break;
- case(3):
- WA = 0;WB = 1;WC = 1;break;
- case(4):
- WA = 1;WB = 0;WC = 1;break;
- case(5):
- WA = 0;WB = 0;WC = 1;break;
- case(6):
- WA = 0;WB = 0;WC = 1;break;
- case(7):
- WA = 0;WB = 0;WC = 1;break;
- case(8):
- WA = 0;WB = 0;WC = 1;break;
- case(9):
- WA = 1;WB = 0;WC = 1;break;
- case(10):
- WA = 0;WB = 1;WC = 1;break;
- case(11):
- WA = 1;WB = 1;WC = 1;break;
- }
- P0 = DMF50[i];
- delay(50);
- }
- break;
- case(1):
- for(i=0;i<20;i++)
- {
- switch(i)
- { case(19):
- WA = 1;WB = 1;WC = 1;break;
- case(18):
- WA = 1;WB = 1;WC = 1;break;
- case(17):
- WA = 1;WB = 1;WC = 1;break;
- case(16):
- WA = 0;WB = 1;WC = 1;break;
- case(15):
- WA = 1;WB = 0;WC = 1;break;
- case(14):
- WA = 0;WB = 0;WC = 1;break;
- case(13):
- WA = 1;WB = 1;WC = 0;break;
- case(12):
- WA = 0;WB = 1;WC = 0;break;
- case(11):
- WA = 1;WB = 0;WC = 0;break;
- case(10):
- WA = 0;WB = 0;WC = 0;break;
- case(9):
- WA = 0;WB = 0;WC = 0;break;
- case(8):
- WA = 0;WB = 0;WC = 0;break;
- case(7):
- WA = 0;WB = 0;WC = 0;break;
- case(6):
- WA = 1;WB = 0;WC = 0;break;
- case(5):
- WA = 0;WB = 1;WC = 0;break;
- case(4):
- WA = 1;WB = 1;WC = 0;break;
- case(3):
- WA = 0;WB = 0;WC = 1;break;
- case(2):
- WA = 1;WB = 0;WC = 1;break;
- case(1):
- WA = 0;WB = 1;WC = 1;break;
- case(0):
- WA = 1;WB = 1;WC = 1;break;
- }
- P0 = DMZ[i];
- delay(25);
- }
- break;
- case(2):
- for(i=0;i<20;i++)
- {
- switch(i)
- { case(0):
- WA = 1;WB = 1;WC = 1;break;
- case(1):
- WA = 1;WB = 1;WC = 1;break;
- case(2):
- WA = 1;WB = 1;WC = 1;break;
- case(3):
- WA = 0;WB = 1;WC = 1;break;
- case(4):
- WA = 1;WB = 0;WC = 1;break;
- case(5):
- WA = 0;WB = 0;WC = 1;break;
- case(6):
- WA = 1;WB = 1;WC = 0;break;
- case(7):
- WA = 0;WB = 1;WC = 0;break;
- case(8):
- WA = 1;WB = 0;WC = 0;break;
- case(9):
- WA = 0;WB = 0;WC = 0;break;
- case(10):
- WA = 0;WB = 0;WC = 0;break;
- case(11):
- WA = 0;WB = 0;WC = 0;break;
- case(12):
- WA = 0;WB = 0;WC = 0;break;
- case(13):
- WA = 1;WB = 0;WC = 0;break;
- case(14):
- WA = 0;WB = 1;WC = 0;break;
- case(15):
- WA = 1;WB = 1;WC = 0;break;
- case(16):
- WA = 0;WB = 0;WC = 1;break;
- case(17):
- WA = 1;WB = 0;WC = 1;break;
- case(18):
- WA = 0;WB = 1;WC = 1;break;
- case(19):
- WA = 1;WB = 1;WC = 1;break;
- }
- P0 = DMF[i];
- delay(25);
- }
- break;
- }
- }
- /*全速正转*/
- void forward()
- {
- in1=1; //l298n手册,真值
- in2=0;
- in3=1;
- in4=0;
- PWMA = 20;
- PWMB = 20;
- }
- /*全速反转*/
- void fallback()
- {
- in1=0;
- in2=1;
- in3=0;
- in4=1;
- PWMA = 20;
- PWMB = 20;
- }
- /*50占空比正转*/
- void quick()
- {
- in1=1;
- in2=0;
- PWMA = 10;
- in3=1;
- in4=0;
- PWMB = 10;
- }
- /*50占空比反转*/
- void slow()
- {
- in1=0;
- in2=1;
- PWMA = 10;
- in3=0;
- in4=1;
- PWMB = 10;
- }
- /*停*/
- void stop()
- {
- in1=0;
- in2=0;
- in3=0;
- in4=0;
- }
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=115;y>0;y--);
- }
- void SendOneByte(uchar *str) //串口发送数据
- {
- while(*str !='\0')
- {
- SBUF=*str;
- while(!TI);
- TI=0;
- str++;
- }
- }
- void main() //定义主函数,返回值为空
- {
- //串口定时器 定时器T0
- TMOD = 0x21; //T0定时器为工作方式一,T1定时器为工作方式二
- PCON = 0x00; //串口初始化相关,波特率
- SCON = 0x50; //串口初始化相关,串口工作方式一,允许接收
- TH1 = 0xFD; //设置初值
- TL1 = 0xFD; //设置初值
- TR1 = 1; //开启定时器T1
- TH0 = 0xF4; //设置初值
- TL0 = 0x48; //设置初值
- TR0 = 1; //开启定时器T0
- ES = 1; //开放串口中断
- PT0 = 1; //定时器0中断优先
- ET0 = 1; //开放定时器T0中断
- EA = 1; //开放总中断
- SendOneByte("Receiving from 8051...\r\n"); //发送字符串,结尾回车换行
- while(1)
- {
- showDigital();
- SendOneByte("\r\n");
- delay(500);
- SendOneByte(SendBuf);
- }
- }
- void time0_int() interrupt 1
- {
- TR0 = 0;
- TH0 = (65536-500)/256;
- TL0 = (65536-500)%256;
- MA++;
- if(MA < PWMA)
- {
- ena = 1; //使用ena来产生pwm波控制A端电机
- }
- else
- ena = 0;
- if(MA == 40)
- {
- MA = 0;
- }
- MB++;
- if(MB < PWMB)
- {
- enb = 1; //使用enb来产生pwm波控制B端电机
- }
- else
- enb = 0;
- if(MB == 40)
- {
- MB = 0;
- }
- TR0 = 1;
- }
- void UARTInterrupt(void) interrupt 4
- {
- if(RI)
- {
- RI = 0;
- cmd = SBUF;
- switch(cmd)
- {
- case 1:
- forward();
- break;
- case 2:
- fallback();
- break;
- case 3:
- quick();
- break;
- case 4:
- slow();
- break;
- case 0:
- stop();
- default:
- break;
- }
- }
- }
复制代码 |