#include<max7219.h>
#include<reg51.h>
typedef unsigned int uint;
sbit KEY0=P2^2;
sbit KEY1=P2^0;
sbit KEY2=P2^1;
sbit KEY3=P2^3;
sbit SPK=P1^0;
sbit LED_GREEN=P2^7;
sbit LED_RED=P2^6;
uint set_speed;
uint speed;
uint cnt1=0,cnt2=0;
void Init_8051(void);
uint Get_key(void);
void Warning(void);
void Delay(long int time);
//主函数
void main(void)
{
Init_8051();
Max7219_Init();
while(1)
{
set_speed=Get_key();//按键数据放入设定速度
Disp_set_speed(set_speed);
Disp_speed(speed);
Warning();
}
}
void Init_8051(void)
{
EA=0;//总中断
TMOD=0X51;//设定16位计数器的工作方式
TH0=(65536-50000)/256;//给定时器赋值
TL0=(65536-50000)%256;
TR0=1;//打开定时器
ET0=1;
EA=1;
TL1=0;//计数器赋值
TH1=0;
TR0=1;
SPK=0;
LED_GREEN=0;//点亮绿灯
LED_RED=1;//红灯关
}
void Timer0_speed(void)interput 1
{
cnt1++;
TH0=(65536-50000)/256;//给定时器赋值
TL0=(65536-50000)/256;
if(cnt1==20)
{
TR0=0;
TR1=0;
cnt2=TH1*256+TL1;
speed=cnt2;
TH1=0;
TL1=0;
cnt1=0;
TR0=1;
TR1=1;
}
}
void Disp_speed(uint speed)
{
uchar speed_buf[4];
uchar i;
speed_buf[0]=speed%10;
speed_buf[1]=speed/10%10;
speed_buf[2]=speed/100%10;
speed_buf[3]=speed/1000;
if(speed_buf[3]==0)
{
speed_but[3]==0x0f;
if(speed_buf[2]==0)
speed_buf[2]=0x0f;
}
for(i=1;i<5;i++)
{
Max7219_Wr_data(i+4,speed_buf[i-1]);
}
}
void Warning(void)
{
if(speed>set_speed)
{
LED_RED=0;//
LED_GREED=1;//
SPK=0;
Delay(1);
SPK=1;//
}
else
LED_RED=1;
}
uint Get_key(void)
{
statio uint key_value;
if(!KEY0)
{
Delay(40);//
if(!KEY0)
}
while(Key3==1)
{
if(!KEY1)
{
Delay(40);
If(Key1==0)
{
while(key1==0)
{
Disp_set_speed(key_value);
}
key_value+=10;
if(key_value==9999)
key_value=0;
}
}
}
}
|