题目
?
代码
1、底层代码是直接运用官方提供的指导书配套的代码。
2、为了便于修改,此次编程运用了模块化编写。
4、比赛提供的onewire.c和onewire.h的代码需要做修改
onewire.h的unsigned char?rd_temperature()改为unsigned int?rd_temperature()
onewire.c的延时要延长,比赛板子的运行速度比51的要快
主函数
unsigned int Delay_Key,Delay_Seg,Delay_1s;
unsigned char Seg_Code[8],Seg_Buf[12],smgwei;
unsigned char Seg_Mode=0,Work_Mode=1,Time_Mode,time;
unsigned int pwm;
void Key_Work(void);
void Seg_Work(void);
void main(void)
{
banzi_Init();
Timer0Init();
Timer1Init();
rd_temperature();
while(1)
{
Seg_Work();
Key_Work();
}
}
按键处理(BTN矩阵案件)
void Key_Work(void)
{
unsigned char Key_Num,Key_Old,Key_Down,Key_Up;
static unsigned char Flag_Key_Up=1;
if(Delay_Key) return;
Delay_Key=1;
Key_Num = Key_Scan();
Key_Down = Key_Num & (Key_Num ^ Key_Old);
Key_Up = ~Key_Num & (Key_Num ^ Key_Old);
Key_Old = Key_Num;
if(Key_Down && Flag_Key_Up==1)
{
Flag_Key_Up=0;
switch(Key_Down)
{
case(4):
Work_Mode++;
if(Work_Mode>=4) Work_Mode=1;break;
case(5):
Time_Mode++;
if(Time_Mode>=3) Time_Mode=0;
if(Time_Mode==0) time=0;
else if(Time_Mode==1) time=60;
else if(Time_Mode==2) time=120;break;
case(9):
time=0;
Time_Mode=0;
P34=0;break;
case(8):
Seg_Mode++;
if(Seg_Mode==2) Seg_Mode=0;
}
}
if(Key_Up)
Flag_Key_Up=1;
}
?显示处理
void Seg_Work(void)
{
if(Delay_Seg) return;
Delay_Seg=1;
if(Seg_Mode==0)
sprintf(Seg_Buf,"-%d- %04d",(int)Work_Mode,(int)time);
else if(Seg_Mode==1)
sprintf(Seg_Buf,"-4- %02dC",rd_temperature()/16);
Seg_Tran(Seg_Buf,Seg_Code);
}
中断处理
void Time0() interrupt 1 //处理输出PWM,和LED
{
TL0 = 0x9C; //100us
TH0 = 0xFF;
pwm++;
if(pwm>=10) pwm=0;
if(time>0)
{
if(Work_Mode==1)
{
if(pwm<=2) P34=1;
else P34=0;
Led_Dis(1);
}
else if(Work_Mode==2)
{
if(pwm<=3) P34=1;
else P34=0;
Led_Dis(2);
}
else if(Work_Mode==3)
{
if(pwm<=7) P34=1;
else P34=0;
Led_Dis(4);
}
}
if(time==0)
{
Led_Dis(0);
P34=0;
}
}
void Time1() interrupt 3 //处理定时
{
TL1 = 0x18; //1ms
TH1 = 0xFC;
if(time>0)
{
Delay_1s++;
if(Delay_1s==1000)
{
Delay_1s=0;
time = time - 1;
}
}
if(++Delay_Seg==500) Delay_Seg=0;
if(++Delay_Key==11) Delay_Key=0;
if(++smgwei==8) smgwei=0;
Seg_Dis(Seg_Code,smgwei);
}
|