|
|
- #include <reg51.h>
- unsigned char code SEG_TABLE[] = {
- 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x40
- };
- unsigned char sec = 0, min = 0, hour = 0;
- unsigned char disp_buf[8];
- unsigned int timer_cnt = 0;
- void Delay_Short() {
- unsigned char i;
- for(i = 0; i < 20; i++);
- }
- void Timer0_Init() {
- TMOD |= 0x01;
- TH0 = 0xFF;
- TL0 = 0x94;
- ET0 = 1;
- EA = 1;
- TR0 = 1;
- }
- void Timer0_ISR() interrupt 1 {
- TH0 = 0xFF;
- TL0 = 0xC0;
- timer_cnt++;
-
- if(timer_cnt >= 10000) {
- timer_cnt = 0;
- sec++;
- if(sec >= 60) {
- sec = 0;
- min++;
- if(min >= 60) {
- min = 0;
- hour++;
- if(hour >= 24) {
- hour = 0;
- }
- }
- }
- }
- }
- void Update_DispBuf() {
- disp_buf[0] = sec % 10;
- disp_buf[1] = sec / 10;
- disp_buf[2] = 10;
- disp_buf[3] = min % 10;
- disp_buf[4] = min / 10;
- disp_buf[5] = 10;
- disp_buf[6] = hour % 10;
- disp_buf[7] = hour / 10;
- }
- void Scan_Display() {
- static unsigned char bit_idx = 0;
- P2 = 0xFF;
- P0 = SEG_TABLE[disp_buf[bit_idx]];
- P2 = ~(1 << bit_idx);
- Delay_Short();
- bit_idx = (bit_idx + 1) % 8;
- }
- void main() {
- Timer0_Init();
- while(1) {
- Update_DispBuf();
- Scan_Display();
- }
- }
复制代码
|
评分
-
查看全部评分
|