标题:
这个单片机程序咋样做到电机反转?求指导
[打印本页]
作者:
626370550
时间:
2020-4-3 12:39
标题:
这个单片机程序咋样做到电机反转?求指导
捕获.PNG
(83.88 KB, 下载次数: 25)
下载附件
2020-4-3 12:37 上传
总共6个按键,左边第一个是开窗,显示屏显示hand1,下面接着的是关窗,显示hand2,后面是停止键,显示hand0,此时电机应该要停下来,右边3个第一个和第二个都是限位开关,比如此时是hand1状态,按一下这两个中的任何一个都会变为hand0,就代表电机停下来的意思,右边最后一个键是模式选择键,可以调到语音模式,此时要是闭合最上面的第一个刀开关,显示sound1,开窗,闭合第二个就显示sound2,关窗,两个都打开就是sound0,代表停止
单片机源程序如下:
#include<reg52.h>
#include<intrins.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit E=P2^7;
sbit RW=P2^6;
sbit RS=P2^5;
sbit K2=P3^4;
sbit K1=P2^4;
sbit xianwei1=P2^0;
sbit xianwei2=P2^1;
sbit S1=P2^2;
sbit S2=P2^3;
unsigned char code F_Rotation[8]={0x08,0x18,0x10,0x30,0x20,0x60,0x40,0x48};
unsigned char code B_Rotation[8]={0x48,0x40,0x60,0x20,0x30,0x10,0x18,0x08};
uchar xwflag=0;
uchar FB_flag=0;
uchar a1=0,a2=0;
uchar b1=0,b2=0;
uchar c1=0,c2=0;
uchar d1=0,d2=0;
uchar moshi=0;
void Delay(uint i)
{
uchar x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
void delays()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
bit Busy(void)
{
bit busy_flag=0;
RS=0;
RW=1;
E=1;
delays();
busy_flag=(bit)(P0 & 0x80);
E=0;
return busy_flag;
}
void wcmd(uchar del)
{
while(Busy());
RS=0;
RW=0;
E=0;
delays();
P0=del;
delays();
E=1;
delays();
E=0;
}
void wdata (uchar del)
{
while(Busy());
RS=1;
RW=0;
E=0;
delays();
P0=del;
delays();
E=1;
delays();
E=0;
}
void L1602_init(void)
{
wcmd(0x38);
wcmd(0x0c);
wcmd(0x06);
wcmd(0x01);
}
void L1602_char(uchar hang, uchar lie, char sign)
{
uchar a;
if(hang==1) a=0x80;
if(hang==2) a=0xc0;
a=a+lie -1;
wcmd(a);
wdata(sign);
}
void L1602_string(uchar hang, uchar lie, uchar *p )
{
uchar a,b=0;
if(hang==1) a=0x80;
if(hang==2) a=0xc0;
a=a+lie -1;
while(1)
{
wcmd(a++);
if((*p=='\0') || (b==16)) break;
b++;
wdata(*p);
p++;
}
}
void main(void)
{
unsigned char i;
IT0=1;
EX0=1;
PX0=1;
IT1=1;
EX1=1;
PX1=1;
EA=1;
L1602_init();
Delay(100);
L1602_char(1,1,'M');
L1602_char(1,2,'o');
L1602_char(1,3,'d');
L1602_char(1,4,'e');
L1602_char(1,5,':');
while(1)
{
L1602_char(2,6,FB_flag%10+48);
if(i>7) i=0;
if(FB_flag==1) { P1=F_Rotation[i]; }
else if(FB_flag==2) { P1= B_Rotation[i]; }
else{ P1=0xff; }
i++;
Delay(100);
if(K1==0){ a1=1; a2=0; }
if( a1==1&&a2==0)
{
FB_flag=0;
a2=1;
}
if(K1==1){ a1=0;a2=0; }
if( K2==0) { d1=1;d2=0; }
if(d1==1&&d2==0)
{
FB_flag=1;
xwflag=0;
d2=1;
}
if(K2==1) { d1=0;d2=0; }
if(moshi==0)
{
L1602_char(1,6,'H');
L1602_char(1,7,'a');
L1602_char(1,8,'n');
L1602_char(1,9,'d');
L1602_char(1,10,' ');
L1602_char(1,16, FB_flag%10+48);
L1602_char(2,16,' ');
}
if(moshi==1)
{
L1602_char(1,6,'S');
L1602_char(1,7,'o');
L1602_char(1,8,'u');
L1602_char(1,9,'n');
L1602_char(1,10,'d');
if(S1==0) L1602_char(2,1,0+48); else L1602_char(2,1,1+48);
if(S2==0) L1602_char(2,2,0+48); else L1602_char(2,2,1+48);
if(S1==1&&S2==0) { xwflag=0;FB_flag=1; L1602_char(1,16,FB_flag%10+48) ;}
if(S1==0&&S2==1) { xwflag=0;FB_flag=2; L1602_char(1,16,FB_flag%10+48) ;}
if(S1==1&&S2==1) { FB_flag=0; L1602_char(1,16,FB_flag%10+48) ;}
L1602_char(2,16,' ');
}
if(xianwei1==0) { b1=1;b2=0;}
if(b1==1&&b2==0)
{
FB_flag=0;
xwflag=1;
b2=1;
}
if(xianwei1==1) { b1=0;b2=0;}
if(xianwei2==0) { c1=1;c2=0; }
if(c1==1&&c2==0)
{
FB_flag=0;
xwflag=2;
c2=1;
}
if(xianwei2==1) { c1=0;c2=0; }
if(xwflag!=0) FB_flag=0;
}
}
void it0() interrupt 0
{
moshi++;
if(moshi==3) moshi=0;
FB_flag=0;
}
void it1() interrupt 2
{
FB_flag=2;
xwflag=0;
}
复制代码
全部资料51hei下载地址:
仿真.zip
(21.64 KB, 下载次数: 3)
2020-4-3 12:39 上传
点击文件名下载附件
作者:
626370550
时间:
2020-4-3 12:40
我感觉是程序的错误,但不知道错在哪,
作者:
626370550
时间:
2020-4-3 16:55
总共6个按键,左边第一个是开窗,显示屏显示hand1,下面接着的是关窗,显示hand2,后面是停止键,显示hand0,此时电机应该要停下来,右边3个第一个和第二个都是限位开关,比如此时是hand1状态,按一下这两个中的任何一个都会变为hand0,就代表电机停下来的意思,右边最后一个键是模式选择键,可以调到语音模式,此时要是闭合最上面的第一个刀开关,显示sound1,开窗,闭合第二个就显示sound2,关窗,两个都打开就是sound0,代表停止
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1