基础技能综合实训案例
#include <REGX52.H>
sfr AUXR = 0X8e;
sbit S4 = P3^3;
sbit S5 = P3^2;
sbit L8 = P0^7;
sbit L7 = P0^6;
unsigned char hour = 0;
unsigned char minute = 0;
unsigned char second = 0;
unsigned char stat_led = 0xff;
unsigned char command = 0x00;
unsigned char code tables[18]= {
0xC0, // 1100 0000 // 0
0xf9, // 1111 1001 1
0xa4, // 1010 0100 2
0xb0, // 1011 0000 3
0x99, // 1001 1001 4
0x92, // 1001 0010 5
0x82, // 1000 0010 6
0xf8, // 1111 1000 7
0x80, // 8
0x90, // 9 1001 0000
0x88, // A 1000 1000
0x80, // b 1000 0011
0xc6, // c 1100 0110
0xc0, // d 1010 0001
0x86, // E 1000 0110
0x8e, // F 1000 1110
0xbf, // - 1011 1111
0x7f
};
void SMG_Display(void);
void Delay_ms(unsigned int xms) {
unsigned int i,j;
for(i = 0; i < xms;i++) {
for(j = 0; j<299;j++);
}
}
void _74HC138(unsigned char n) {
switch(n) {
case 4: P2 = (P2 & 0x1f) | 0x80;
break;
case 5: P2 = (P2 & 0x1f) | 0xa0;
break;
case 6: P2 = (P2 & 0x1f) | 0xc0;
break;
case 7: P2 = (P2 & 0x1f) | 0xe0;
break;
case 0: P2 = (P2 & 0x1f) | 0x00;
break;
}
}
void System_Init(void) {
_74HC138(5);
P0 = 0x00;
_74HC138(4);
P0 = 0xff;
}
void SMG_duanma(unsigned char Location,unsigned char Value) {
_74HC138(6);
P0 = 0x01 << Location;
_74HC138(7);
P0 = Value;
}
void LED_Init(){
unsigned char i;
_74HC138(4);
for(i = 0; i < 8;i++) {
P0 = 0xfe << i; // 1111 1110 << i ==
Delay_ms(500);
}
for(i = 0; i < 8 ;i++) {
P0 = ~(0xfe << i); // 0000 0001
Delay_ms(500);
}
_74HC138(0);
}
void SMG_Init() {
unsigned char i;
_74HC138(7);
P0 = 0x00;
for(i = 0; i < 8;i++) {
_74HC138(6); // 1111 1110 1111 1100
P0 = ~(0xfe << i); // 0000 0001 0000 0011 0000 0111
Delay_ms(500); //
}
for(i = 0; i < 8;i++) { //1111 1110 1111 1100 1111 1000
_74HC138(6);
P0 = 0xfe <<i;
Delay_ms(500);
}
}
void LocalCleak(void) {
if(S5 == 0) {
SMG_Display();
if(S5 == 0) {
while(S5 == 0) {
SMG_Display();
} // 7
_74HC138(4);
stat_led = (stat_led | 0x40) & (~stat_led | 0xbf);
P0 = stat_led;
_74HC138(0);
}
}
if(S4 == 0) {
SMG_Display();
if(S4 ==0) {
while(S4 ==0) {
SMG_Display();
}
_74HC138(4);
stat_led = (stat_led | 0x80) & (~stat_led | 0x7f);
P0 = stat_led;
_74HC138(0);
}
}
}
void SMG_Delay(unsigned int t) {
while(t--);
}
void SMG_Display(void) {
SMG_duanma(0,tables[hour/10]);
SMG_Delay(500);
SMG_duanma(1,tables[hour%10]);
SMG_Delay(500);
SMG_duanma(2,tables[16]);
SMG_Delay(500);
SMG_duanma(3,tables[second/10]);
SMG_Delay(500);
SMG_duanma(4,tables[second%10]);
SMG_Delay(500);
SMG_duanma(5,tables[16]);
SMG_Delay(500);
SMG_duanma(6,tables[minute/10]);
SMG_Delay(500);
SMG_duanma(7,tables[minute%10]);
SMG_Delay(500);
}
void Timer0Init(void) {
TMOD = 0x21;
// 20ms
TH0 = (25535-20000) / 256;
TL0 = (25535-20000) % 256;
TR0 = 1;
ET0 = 1;
EA = 1;
}
unsigned char count = 0;
void Timer0Rountine () interrupt 1 {
TH0 = (25535-20000) / 256;
TL0 = (25535-20000) % 256;
count++;
if(count == 20) {
count = 0;
minute++;
if(minute == 60) {
minute = 0;
second ++;
if(second == 60) {
second= 0;
hour++;
if(hour == 99){
hour=0;
}
}
}
}
}
void UART_Init(void) {
TMOD = 0x21;
SCON = 0x50;
AUXR = 0x00;
TH1 = 0xfd;
TL1 = 0xfd;
ES = 1;
EA = 1;
TR1 = 1;
}
void SendByte(unsigned char dat) {
SBUF = dat;
while(TI == 0);
TI = 0;
}
void UART1_Routine(void) interrupt 4 {
if(RI == 1) {
RI = 0;
command = SBUF;
}
}
void UART_work(void) {
if(command != 0x00) {
_74HC138(4);
switch(command & 0xf0) { //1010 0001
case 0xa0: // 保留高四位 0101 1110 & 0000 1111
P0 = (P0 & 0xf0) | (~command & 0x0f);
command = 0;
break;
case 0xb0:
SendByte((hour/10 << 4)|(hour % 10));
SendByte((second/10 << 4)|(second % 10));
SendByte((minute/10 << 4)|(minute % 10));
command = 0;
break;
}
}
}
void main(void) {
System_Init();
LED_Init();
SMG_Init();
Timer0Init();
UART_Init();
while(1) {
UART_work();
SMG_Display();
LocalCleak();
}
}