完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
通过串口给单片机发送指令控制电机不同的转动状态,此程序是配合普中51-单核-A4开发板写的。请按需服用。欢迎留言讨论。 /*1、串口发送00,电机停止转动,指示灯1点亮; 2、串口发送01,电机全速正转,指示灯2点亮; 3、串口发送02,电机全速反转,指示灯3点亮; 4、串口发送03,电机50%速度正转,指示灯2闪烁; 5、串口发送04,电机50%速度反转,指示灯3闪烁; 数码管代替指示灯(数码管的正向反向流动,流动速度的快慢代替指示灯)指示灯不亮不闪烁 */ #include #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}; ***it WA=P2^2; ***it WB=P2^3; //74HC138译码器位选管脚(数码管用) ***it WC=P2^4; /*蜂鸣器*/ ***it BZ=P2^5; //L298N引脚定义 ***it ena = P1^0; //l298n电机驱动的相关引脚 ***it in1 = P1^1; ***it in2 = P1^2; ***it in3 = P1^3; ***it in4 = P1^4; ***it enb = P1^5; uchar MA = 0,MB = 0; //pwm控制用 uchar PWMA = 20; uchar PWMB = 20; uchar cmd; //串口接收的命令***uf 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; 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; 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; 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; 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; 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 !=' |