init.c
void System_Init()
{
P0 = 0x00; //关闭蜂鸣器和继电器
P2 = P2 & 0x1f | 0xa0;
P2 &= 0x1f;
P0 = 0x00; //关闭LED
P2 = P2 & 0x1f | 0x80;
P2 &= 0x1f;
}
led.c
#include <LED.H>
idata unsigned char temp_1 = 0x00;
idata unsigned char temp_old_1 = 0xff;
void Led_Disp(unsigned char *ucLed)
{
temp_1=0x00;
temp_1 = (ucLed[0] << 0) | (ucLed[1] << 1) | (ucLed[2] << 2) | (ucLed[3] << 3) |
(ucLed[4] << 4) | (ucLed[5] << 5) | (ucLed[6] << 6) | (ucLed[7] << 7);
if (temp_1 != temp_old_1)
{
P0 = ~temp_1;
P2 = P2 & 0x1f | 0x80;
P2 &= 0x1f;
temp_old_1 = temp_1;
}
}
seg.c
#include <Seg.H>
unsigned char Seg_dula[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
unsigned char Seg_wela[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
void Seg_Dis(unsigned char wela, unsigned char dula,unsigned char piont)
{
P0 = 0xff;
P2 = P2 & 0x1f | 0xe0;
P2 &= 0x1f;
P0 = Seg_wela[wela];
P2 = P2 & 0x1f | 0xc0;
P2 &= 0x1f;
P0 = Seg_dula[dula];
if(piont) P0 &= 0x7f;
P2 = P2 & 0x1f | 0xe0;
P2 &= 0x1f;
}
key.c
#include <Key.H>
unsigned char Key_Read()
{
unsigned char temp = 0;
P44 = 0;P42 = 1;P35 = 1;P34 = 1;
if(P33 == 0) temp = 4;
if(P32 == 0) temp = 5;
if(P31 == 0) temp = 6;
if(P30 == 0) temp = 7;
P44 = 1;P42 = 0;P35 = 1;P34 = 1;
if(P33 == 0) temp = 8;
if(P32 == 0) temp = 9;
if(P31 == 0) temp = 10;
if(P30 == 0) temp = 11;
P44 = 1;P42 = 1;P35 = 0;P34 = 1;
if(P33 == 0) temp = 12;
if(P32 == 0) temp = 13;
if(P31 == 0) temp = 14;
if(P30 == 0) temp = 15;
P44 = 1;P42 = 1;P35 = 1;P34 = 0;
if(P33 == 0) temp = 16;
if(P32 == 0) temp = 17;
if(P31 == 0) temp = 18;
if(P30 == 0) temp = 19;
return temp;
}
main
unsigned char Key_Slow_down;
unsigned char Seg_Slow_down;
unsigned char Key_Val, Key_Old, Key_Down, Key_Up;
unsigned char Seg_Pos;
unsigned char Seg_Buf[] = {10,10,10,10,10,10,10,10};
unsigned char Seg_Point[] = {0,0,0,0,0,0,0,0};
unsigned char ucLED[8] = {0,0,0,0,0,0,0,0};
void Key_Proc()
{
if(Key_Slow_down) return;
Key_Slow_down = 1;
Key_Val = Key_Read();
Key_Down = Key_Val & (Key_Val ^ Key_Old);
Key_Up = ~Key_Val & (Key_Val ^ Key_Old);
Key_Old = Key_Val;
}
void Seg_Proc()
{
if(Seg_Slow_down) return;
Seg_Slow_down = 1;
}
void LED_Proc()
{
}
void Timer0Init(void) //0毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x05;
TL0 = 0x00; //设置定时初值
TH0 = 0x00; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void Timer1Init(void) //1毫秒@12.000MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x18; //设置定时初值
TH1 = 0xFC; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1;
EA = 1;
}
void Timer1Sever() interrupt 3
{
if(++Key_Slow_down == 10) Key_Slow_down = 0;
if(++Seg_Slow_down == 100) Seg_Slow_down = 0;
if(++Seg_Pos == 8) Seg_Pos = 0;
Seg_Dis(Seg_Pos, Seg_Buf[Seg_Pos], Seg_Point[Seg_Pos]);
Led_Disp(ucLED);
}
void main()
{
System_Init();
Timer0Init();
Timer1Init();
while(1)
{
Key_Proc();
Seg_Proc();
LED_Proc();
}
}