#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit PW1=P1^0 ;
sbit PW2=P1^1 ; //控制电机的两个输入
sbit PW3=P1^2 ; //输出pwm
sbit accelerate=P0^2 ; //调速按键
sbit stop=P0^3 ; //停止按键
sbit left=P0^4 ; //左转按键
sbit right=P0^5 ; //右转按键
sbit detect=P3^2; //检测脉冲
sbit lcdrs=P0^0;
sbit lcden=P0^1;
#define Da P2
uint temp; //保存检测到的电平数据以便比较
uint count; //用于计数
uint aa,bb; //用于计数
uint speed; //用来计算转速
uint a=25000;
uint t0=25000,t1=25000; //初始时占空比为50%
uchar flag=1; //此标志用于选择不同的装载初值
uchar dflag; //左右转标志
uchar sflag=1; //用来标志速度档位
#define right_turn PW1=0;PW2=1;PW3=0; //顺时针转动
#define left_turn PW1=1;PW2=0;PW3=0; //逆向转动
#define end_turn PW1=1;PW2=1;PW3=1; //停转
void keyscan(); //键盘扫描
void delay(uchar z);
void time_init(); //定时器的初始化
void write_com(uchar com); //液晶写指令
void write_data(uchar date); //液晶写数据
void lcd_init(); //液晶初始化
void display(uint rate); //显示速度
void int0_init(); //定时器0初始化
void judge_derection();
void main()
{
time_init(); //定时器的初始化
lcd_init(); //液晶初始化
int0_init(); //定时器0初始化
while(1)
{
}
}
//定时器0用来计算转速
//定时器1用来产生PWM
void time_init()
{
TMOD=0x11; //两个定时器都设定为工作方式1 十六位定时计数器
EA=1; //开启总中断
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
TH1=(65536-a)/256;
TL1=(65536-a)%256;
ET1=1;
TR1=0;
}
void int0_init()
{
EX0=1;//外部中断源可以申请中断
IT0=1;//外部中断源下降沿触发
}
void timer0() interrupt 1 using 0
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256; //装载初值
keyscan(); //键盘扫描程序
aa++;
if(aa==5)
{
aa=0;
temp=count*0.5*60*2*2*100/24; //计算转速,每分转多少圈
count=0; //重新开始计数脉冲数
display(temp); //把计算得的结果显示出来
}
}
void timer1() interrupt 3 using 0
{
if(flag)
{
flag=0;
end_turn;
a=t0; //t0的大小决定着低电平延续时间
TH1=(65536-a)/256;
TL1=(65536-a)%256; //重装载初值
}
else
{
flag=1; //这个标志起到交替输出高低电平的作用
if(dflag==0)
{
right_turn; //右转
}
else
{
left_turn; //左转
}
a=t1; //t1的大小决定着高电平延续时间
TH1=(65536-a)/256;
TL1=(65536-a)%256; //重装载初值
}
}
/*******外部中断*******************/
void service_int0() interrupt 0
{
count++; //来一个下降沿沿就计一个脉冲数
}
/*******显示函数***********/
void display(uint rate)
{
uchar wan,qian, bai,shi,ge;
wan=rate/10000;
qian=rate/1000%10;
bai=rate/100%10;
shi=rate/10%10;
ge=rate%10;
write_com(0x80);
write_data('0'+wan);
write_data('0'+qian);
write_data('0'+bai);
write_data('.');
write_data('0'+shi);
write_data('0'+ge);
}
/******延时函数********/
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--) ;
}
/************写指令************/
void write_com(uchar com)
{
lcdrs=0;
Da=com;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
/************写数据**********/
void write_data(uchar date)
{
lcdrs=1;
Da=date;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
/************液晶初始化**********/
void lcd_init()
{
lcden=0;
write_com(0x38) ; //初始化
write_com(0x0c) ; //打开光标 0x0c不显示光标 0x0e光标不闪,0x0f光标闪
write_com(0x01) ; //清显示
write_com(0x80+0x40);
write_data('S');
write_data(':');
write_data('0');
write_data(' ');
write_data(' ');
write_data('P');
write_data(':');
write_data('0');
}
/***********键盘扫描程序**********/
void keyscan()
{
if(stop==0)
{
TR1=0; //关闭定时器0 即可停止转动
end_turn; // 停止供电
write_com(0x80+0x40);
write_data('S');
write_data(':');
write_data('0');
write_data(' ');
write_data(' ');
write_data('P');
write_data(':');
write_data('0');
}
if(left==0)
{
TR1=1;
dflag=1; //转向标志置位则左转
write_com(0x80+0x40+10);
//write_data('0'+sflag);
write_data('L');
write_com(0x80+0x40);
write_data('S');
write_data(':');
write_data('1');
write_data(' ');
write_data(' ');
write_data('P');
write_data(':');
write_data('1');
}
if(right==0)
{
TR1=1;
dflag=0; //转向标志复位则右转
write_com(0x80+0x40+10);
write_data('R');
write_com(0x80+0x40);
write_data('S');
write_data(':');
write_data('1');
write_data(' ');
write_data(' ');
write_data('P');
write_data(':');
write_data('1');
//write_data('0'+sflag);
}
if(accelerate==0)
{
delay(10) ; //延时消抖
if(accelerate==0)
{
while(accelerate==0) ; //等待松手
sflag++;
if(sflag==2)
{
t0=20000;
t1=30000; //占空比为百分之60
write_com(0x80+0x40);
write_data('S');
write_data(':');
write_data('2');
write_data(' ');
write_data(' ');
write_data('P');
write_data(':');
write_data('2');
}
if(sflag==3)
{
t0=15000;
t1=35000; //占空比为百分之70
write_com(0x80+0x40);
write_data('S');
write_data(':');
write_data('3');
write_data(' ');
write_data(' ');
write_data('P');
write_data(':');
write_data('3');
}
if(sflag==4)
{
t0=10000;
t1=40000; //占空比为百分之80
write_com(0x80+0x40);
write_data('S');
write_data(':');
write_data('4');
write_data(' ');
write_data(' ');
write_data('P');
write_data(':');
write_data('4');
}
if(sflag==5)
{
t0=5000;
t1=45000; //占空比为百分之90
write_com(0x80+0x40);
write_data('S');
write_data(':');
write_data('5');
write_data(' ');
write_data(' ');
write_data('P');
write_data(':');
write_data('5');
}
if(sflag>=6)
{
sflag=0;
t0=25000;
t1=25000;
write_com(0x80+0x40);
write_data('S');
write_data(':');
write_data('1');
write_data(' ');
write_data(' ');
write_data('P');
write_data(':');
write_data('1');
}
}
}
}
评论0