一个限速代码,求各位大神帮忙改错。
#ifndef _Max7219_H_#define _Max7219_H_
#include<intrins.h>
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define DECODE_MODE 0x09 //译码方式
#define INTENSITY 0x0A
#define SCAN_LIMIT 0x0B
#define SHUT_DOWN 0x0C
#define DISPLAY_TEST 0x0D
#define delay1us _nop_();
#define delay2us {delay1us;delay1us;}
#define delay4us {delay2us;delay2us;}
#define delay5us {delay4us;delay1us;}
#define delay12us {delay4us;delay4us;delay4us;}
sbit din=P2^0;
sbit load=P2^1;
sbit clk=P2^2;
void Max7219_Wr_byte(uchar wrdat);
void Max7219_Wr_data(uchar addr,uchar dat);
void Init_Max7219(void);
void Disp_speed(uint speed);
void Disp_set_speed(uint set_speed);
//初始化MAX7219
void Max7219_Init(void)
{
Max7219_Wr_data(SHUT_DOWN, 0x01);
Max7219_Wr_data(DISPLAY_TEST, 0x00);
Max7219_Wr_data(DECODE_MODE, 0xff);
Max7219_Wr_data(SCAN_LIMIT, 0x07);
Max7219_Wr_data(INTENSITY, 0x0a);
}
void Max7219_Wr_byte(uchar wrdat)
{
bit dat;
uchar i;
for (i=0;i<8;i++)
{
clk=0;
dat=(bit)(wrdat&0x80);
if(dat)
din=1;
else
din=0;
wrdat<<=1;
clk=1;
}
}
void Max7219_Wr_data(uchar addr,uchar dat)
{
load=0;
Max7219_Wr_byte(addr);
Max7219_Wr_byte(dat);
load=1;
}
void Disp_speed(uint speed)
{
uchar speed_buf[4];
uchar i;
speed_buf[0]=speed%10;
speed_buf[1]=speed/10%10|0x80;
speed_buf[2]=speed/100%10;
speed_buf[3]=speed/1000;
if(0==speed_buf[3])
{
speed_buf[3]=0x0f;
if(0==speed_buf[2])
speed_buf[2]=0x0f;
}
for(i=1;i<=4;i++)
{
Max7219_Wr_data(i,speed_buf[i-1]);
}
}
void Disp_set_speed(uint set_speed)
{
uchar speed_buf[4];
uchar i;
speed_buf[0]=set_speed%10;
speed_buf[1]=set_speed/10%10|0x80;
speed_buf[2]=set_speed/100%10;
speed_buf[3]=set_speed/1000;
if(0==speed_buf[3])
{
speed_buf[3]=0x0f;
if(0==speed_buf[2])
speed_buf[2]=0x0f;
}
for(i=1;i<=4;i++)
{
Max7219_Wr_data(i+4,speed_buf[i-1]);
}
}
#endif
#include <reg51.h>
sbit KEY0=P2^3;
sbit KEY1=P2^4;
sbit KEY2=P2^5;
sbit KEY3=P2^6;
sbit SPK=P2^7;
sbit LED_GREEN=P1^6;
sbit LED=P1^7;
uint set_speed;
uint speed;
uchar sec_cnt;
void Init_8051(void);
void Delay(uchar time);
uint Get_key(void);
void Warn_ring(void);
void main(void)
{
Init_8051();
Max7219_Init();
while(1)
{
set_speed=Get_key();
Disp_speed(speed);
Disp_set_speed(set_speed);
Warn_ring();
}
}
void Delay(uchar time)
{
uchar t1,t2;
for(t1=time;t1>0;t1--)
for(t2=248;t2>0;t2--);
}
//初始化
void Init_8051(void)
{
EA=0;
TCON=0x00;
TMOD=0x51;
TL0=0xe0; //定时20ms
TH0=0xb1; //T1计数13个脉冲
TL1=0xf3;
TH1=0xff;
TR0=1;
TR1=1;
IE=0x8a;
SPK=0;
LED=0;
LED_GREEN=1;
}
//定时器0函数,定时50*20ms=1s
void Timer0_speed(void) interrupt 1
{
ET0=0;
TR0=0;
TL0=0xe0;
TH0=0xb1;
TR0=1;
sec_cnt++;
ET0=1;
}
//计数中断 计数10次 半径d=20cm
//speed=0.2*60*10/sec_cnt*36 km/h
void Counter_speed(void) interrupt 3
{ float a=6250;
ET1=0;
TR1=0;
TL1=0xf3;
TH1=0xff;
TR1=1;
speed=4.52*a/sec_cnt;
sec_cnt=0;
ET1=1;
}
//键盘处理函数
uint Get_key(void)
{
static uint key_value;
if(!KEY0)
{
Delay(40);
if(!KEY0)
{
while(KEY3)
{
if(!KEY1)
{
Delay(40);
if(0==KEY1)
{
while(0==KEY1)
{
Disp_set_speed(key_value);
}
key_value+=10;
if(key_value==9999)
key_value=0;
}
}
if(!KEY2)
{
Delay(40);
if(0==KEY2)
{
while(0==KEY2)
{
Disp_set_speed(key_value);
}
key_value-=10;
if(key_value==0)
key_value=9999;
}
}
}
}
}
return (key_value);
}
//报警函数
void Warn_ring(void)
{
if(speed > set_speed)
{
LED=1;
SPK=0;
Delay(1);
SPK=1;
}
else
LED=0;
}