我们用T0来发出不同占空比的脉冲来控制电机转速,然后用T1去计T0发出脉冲的数量来达到我们所需要的步数,达到步数后电机停止并且蜂鸣器响。我们现在的问题是用键盘在液晶屏上控制转速和步数后,发送给我们的两个定时器作为初值去运转电机,但是设置的转速一旦大于30(r/min),电机就会一直转下去不会停止,蜂鸣器也不会叫,而且不论转速怎么提升,电机的转动速度都差不多不会变,只有在转速设定小于30的时候才能正常实现我们的功能,用设定转速行走我们所规定的步数后电机停止并且蜂鸣器报警。求51hei论坛大佬进行指点!!!
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit LCD_RS = P2^0;
- sbit LCD_RW = P2^1;
- sbit LCD_EN = P2^2;
- sbit S1=P2^3; //定义键-----进入设置界面
- sbit S2=P2^4; //定义键-----调整光标位置
- sbit S3=P2^5; //定义键-----调整光标位置数值
- sbit S4=P2^6; //定义键-----保存设置值
- sbit S5=P2^7; //定义键-----发送键
- uchar code table1[]="z:0 v:000 b:000";
- uchar code table0[]=" 000";
- uchar S1num,S2num,S3num;
- uint z,v1,v2,v3,b1,b2,b3,sv;
- uchar i;
- void delay(uint z) //延时函数
- {
- int x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
-
- void LCD_write_data(uint dat) //写数据
- {
- LCD_RS = 1;
- LCD_RW = 0;
- LCD_EN = 0;
- P1=dat;
- delay(5);
- LCD_EN = 1;
- delay(5);
- LCD_EN = 0;
- }
-
- void LCD_write_com(uint com) //写指令
- {
- LCD_RS = 0;
- LCD_RW = 0;
- LCD_EN = 0;
- P1=com;
- delay(5);
- LCD_EN = 1;
- delay(5);
- LCD_EN = 0;
- }
-
- void init()//1602初始化
- {
- z=0;
- v1=0;
- v2=0;
- v3=0;
- b1=0;
- b2=0;
- b3=0;
- sv=0;
- LCD_write_com(0x38);
- LCD_write_com(0x01);
- LCD_write_com(0x06);
- LCD_write_com(0x0c);
- }
- void write_v1(uchar add,uint dat)//写转速v函数
-
- {
- uchar bai;
- bai=dat;
- LCD_write_com(0xc0+add);
- LCD_write_data(0x30+bai);
- }
- void write_v2(uchar add,uint dat)//写转速v函数
-
- {
- uchar shi;
- shi=dat;
- LCD_write_com(0xc0+add);
- LCD_write_data(0x30+shi);
- }
- void write_v3(uchar add,uint dat)//写转速v函数
-
- {
- uchar ge;
- ge=dat;
- LCD_write_com(0xc0+add);
- LCD_write_data(0x30+ge);
- }
- void write_b1(uchar add,uint dat)//写步数b函数
-
- {
- uint bai;
- bai=dat;
- LCD_write_com(0xc0+add);
- LCD_write_data(0x30+bai);
- }
- void write_b2(uchar add,uint dat)//写步数b函数
-
- {
- uint shi;
- shi=dat;
- LCD_write_com(0xc0+add);
- LCD_write_data(0x30+shi);
- }
- void write_b3(uchar add,uint dat)//写步数b函数
-
- {
- uint ge;
- ge=dat;
- LCD_write_com(0xc0+add);
- LCD_write_data(0x30+ge);
- }
- void write_z(uchar add,uint dat)//写转向z函数
-
- {
- uint ge;
- ge=dat;
- LCD_write_com(0xc0+add);
- LCD_write_data(0x30+ge);
- }
- void write_sv(uchar add,uint dat)//写实际转速sv函数
-
- {
- uint bai,shi,ge;
- bai=dat/100;
- shi=dat%100/10;
- ge=dat%10;
- LCD_write_com(0x80+add);
- LCD_write_data(0x30+bai);
- LCD_write_data(0x30+shi);
- LCD_write_data(0x30+ge);
- }
- void display()//1602初始化显示函数
- {
- init();
- LCD_write_com(0x80|0x10);
- for(i=0;i<11;i++)
- {
- LCD_write_data(table0[i]);
- delay(5);
- }
- LCD_write_com(0xc0);
- for(i=0;i<15;i++)
- {
- LCD_write_data(table1[i]);
- delay(5);
- }
- write_z(2,z);
- write_v1(6,v1);
- write_v2(7,v2);
- write_v3(8,v3);
- write_b1(12,b1);
- write_b2(13,b2);
- write_b3(14,b3);
- write_sv(6,sv);
- S1num=0;
- S2num=0;
- S3num=0;
- }
- void keyscan()//键盘扫描函数
- {
- if(S1==0)//按键S1按下时
- {
- S1num=1;
- delay(20);
- if(S1==0)
- {
- LCD_write_com(0xc0+2);//光标到转向位置处
- LCD_write_com(0x0f);//光标闪烁
- }
- }
- if(S1num!=0)//只有按键S1按下后,S2,S3,S4按键操作才有效
- {
- if(S2==0)//按键S2按下时
- {
-
- delay(20);
- if(S2==0)
- {
- S2num++;
- while(!S2);//松手检测
- if(S2num==0)
- {
- LCD_write_com(0xc0+2);//光标到指定位置
- }
- if(S2num==1)
- {
- LCD_write_com(0xc0+6);//光标到指定位置
- }
- if(S2num==2)
- {
- LCD_write_com(0xc0+7);
- }
- if(S2num==3)
- {
- LCD_write_com(0xc0+8);
- }
- if(S2num==4)
- {
- LCD_write_com(0xc0+12);
- }
- if(S2num==5)
- {
- LCD_write_com(0xc0+13);
- }
- if(S2num==6)
- {
- LCD_write_com(0xc0+14);
- }
- if(S2num==7)
- {
- LCD_write_com(0xc0+2);
- S2num=0;
- }
- }
- }
- if(S3==0)//按键S3按下时
- {
- delay(20);
- if(S3==0)
- {
- while(!S3);
- if(S2num==0)
- {
- z++;
- if(z==2)
- {
- z=0;
- }
- write_z(2,z);
- LCD_write_com(0xc0+2);
- }
- if(S2num==1)
- {
- v1++;
- if(v1==10)
- {
- v1=0;
- }
- write_v1(6,v1);
- LCD_write_com(0xc0+6);
- }
- if(S2num==2)
- {
- v2++;
- if(v2==10)
- {
- v2=0;
- }
- write_v2(7,v2);
- LCD_write_com(0xc0+7);
- }
- if(S2num==3)
- {
- v3++;
- if(v3==10)
- {
- v3=0;
- }
- write_v3(8,v3);
- LCD_write_com(0xc0+8);
- }
- if(S2num==4)
- {
- b1++;
- if(b1==10)
- {
- b1=0;
- }
- write_b1(12,b1);
- LCD_write_com(0xc0+12);
- }
- if(S2num==5)
- {
- b2++;
- if(b2==10)
- {
- b2=0;;
- }
- write_b2(13,b2);
- LCD_write_com(0xc0+13);
- }
- if(S2num==6)
- {
- b3++;
- if(b3==10)
- {
- b3=0;
- }
- write_b3(14,b3);
- LCD_write_com(0xc0+14);
- }
- }
- }
- if(S4==0)//按键S4按下时
-
- {
- delay(5);
- if(S4==0)
- {
- S1num=0;
- S2num=0;
- S3num=0;
- LCD_write_com(0x0c);
- }
- }
- }
- }
- void main()
- {
- display();//1602显示
-
- while(1)
- {
- keyscan();//不断检测键是否按下
- }
- }
复制代码
所有资料51hei提供下载:
最新.zip
(265.78 KB, 下载次数: 29)
|