您的位置 首页 ADAS

根据51单片机家具门屉耐用性自动检测设备程序设计

includereg52h>defineuintunsignedintdefineucharunsignedchardefineZKB6占空比初始值设定defin

#include

#define uint unsigned int
#define uchar unsigned char
#define ZKB 6 //占空比初始值设定
#define max_time 100 //设置超出时刻
sbit gd1=P1^0; //光电开关被挡住是高电平,没有被挡住是低电平
sbit gd2=P1^1; //光电开关被挡住是高电平,没有被挡住是低电平
sbit en=P1^2; //步进电机使能端,高电平作业,低电平不作业
sbit zx=P1^3; //高电平步进电机正转,低电平步进电机回转
sbit clock=P1^4; //步进电机频率端口
sbit lcdrs=P2^3; //LCD1602端口界说
sbit lcdrw=P2^4; //LCD1602端口界说
sbit lcden=P2^5; //LCD1602端口界说
uchar k=0;
uchar j=0;
uchar flag=0; //标志位
uint avr_time; //界说均匀时刻
uchar num; //界说液晶显现循环变量
uint zheng=0,fan=0; //正回转次数置零
uint shijian[51]; //界说时刻数组
uchar code table2[]=”forward: “; //LCD1602显现数组
uchar code table3[]=”backward: “; //LCD1602显现数组
uchar code table4[]=” error! “; //LCD1602显现数组
void timer_init()
{
TMOD=0x11; //定时器形式设置
TH0=0xFF; //写入定时器0初始值
TL0=0x9C;
TH1=(65536-50000)/256; //写入定时器1初始值
TL1=(65536-50000)%6;
ET0=1; //答应定时器0中止
TR0=1; //发动定时器0
ET1=1; //答应定时器1中止
TR1=1; //发动定时器1
EA=1; //答应总中止
}
uint data_pro(void)
{
uchar i;
uint avr=0;
for(i=1;i<51;i++)
{
avr+=shijian[i];
}
avr=avr/50;
return avr;
}
void delay_50us(uint t)
{
uint y;
for(;t>0;t–)
for(y=19;y>0;y–);
}
void write_com(uchar com)
{
lcdrs=0;
lcdrw=0;
lcden=0;
P0=com;
delay_50us(10);
lcden=1;
delay_50us(20);
lcden=0;
}
void write_data(uchar date)
{
lcden=0;
lcdrs=1;
lcdrw=0;
P0=date;
delay_50us(10);
lcden=1;
delay_50us(20);
lcden=0;
}
void init()
{
write_com(0x38);
delay_50us(100);
write_com(0x01);
delay_50us(100);
write_com(0x06);
delay_50us(100);
write_com(0x0f);
delay_50us(100);
}
void write_zheng(uchar add,uchar date)
{
uchar qian,bai,shi,ge;
qian=date/1000;
bai=date00/100;
shi=date0/10;
ge=date;
write_com(0x80+add);
write_data(0x30+qian);
write_data(0x30+bai);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_fan(uchar add,uchar date)
{
uchar qian,bai,shi,ge;
qian=date/1000;
bai=date00/100;
shi=date0/10;
ge=date;
write_com(0x80+0x40+add);
write_data(0x30+qian);
write_data(0x30+bai);
write_data(0x30+shi);
write_data(0x30+ge);
}
void main()
{
timer_init(); //定时器初始化
delay_50us(300); //液晶初始化前延时一段时刻
init(); //液晶初始化
write_com(0x80); //写地址
for(num=0;num<16;num++) //显现
{
write_data(table2[num]);
delay_50us(100);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_data(table3[num]);
delay_50us(100);
}
write_zheng(12,zheng); //初始显现零
write_fan(12,fan); //初始显现零
en=1; //上电后,电机立刻发动
zx=1; //正转
zheng++; //正转次数加1
write_zheng(12,zheng); //显现修改后,正转次数
while(1)
{
if(gd2==1) //碰到光电开关2
{
zx=0; //回转
fan++; //回转次数加1
write_fan(12,fan); //显现修改后,回转次数
}
if(gd1==1) //碰到光电开关1
{
TR1=0; //关定时器1
en=0; //中止电机滚动
shijian[j++]=k; //记载一次时刻
if(j==51) //记载完50次,求均匀值
{
avr_time=data_pro();
}
if(flag==0) //标志位
{
k=0; //中止数据清0
TH1=(65536-50000)/256; //重装定时器初值
TL1=(65536-50000)%6;
TR1=1; //再次发动定时器1
zx=1; //正转
en=1; //电机发动
zheng++; //正转次数加1
write_zheng(12,zheng); //显现修改后,正转次数
}
}
if(j>51) //记载完50次后,每次都要比较是否超出规则的时刻
{
if((k-avr_time)>max_time) //假如大于额外时刻
{
en=0; //电机中止滚动
write_com(0x80+0x40);//液晶显现过错
for(num=0;num<16;num++)
{
write_data(table4[num]);
delay_50us(100);
}
}
}
}
}
void timer0() interrupt 1
{
static uchar i=0;
TH0=0xFF;
TL0=0x9C;
++i;
if (i>10) i=0;
if (i<=ZKB)
clock=0;
else
clock=1;
}
void timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%6;
k++;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/adas/260855.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部