用軟件實現時鐘計數,下載到開發板上,數碼管上數字一閃一閃的,剛開始學,不知為什么?求助。程序如下:
#include<reg52.h> #define uchar unsigned char #define uint unsigned char uchar code led_7seg[]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60}; uchar hour=23,minute=59,second=50,n=0; uint count=0;
void init_timer0(void) //timer0每2MS中斷一次 { TMOD=0x01; TH0=(65536-1000)/256; TL1=(65536-1000)%256; ET0=1; TR0=1; EA=1; } void d1_10ms(void) { uchar i; for(i=200;i>0;i--){} } uchar kbscan(void) { uchar sccode,recode; P1=0xf0; if((P1&0xf0)!=0xf0) { d1_10ms(); if((P1&0xf0)!=0xf0) { sccode=0xfe; while((sccode&0x10)!=0) { P1=sccode; if((P1&0xf0)!=0xf0) { recode=(P1&0xf0)|0xf0; return((~sccode)+(~recode)); } else sccode=(sccode<<1)|0x01; } } } return(0); } void timer0_int(void) interrupt 1 using 1 { TH0=(65536-1000)/256; TL0=(65536-1000)%256; count++; switch(n&&0x07) { case 0:{P0=led_7seg[hour/10];P2=0xfe;}break; case 1:{P0=led_7seg[hour%10];P2=0xfd;}break; //case 2:{P0=led_7seg[10];P2=0xfb;}break; case 3:{P0=led_7seg[minute/10];P2=0xf7;}break; case 4:{P0=led_7seg[minute%10];P2=0xef;}break; //case 5:{P0=led_7seg[10];P2=0xdf;}break; case 6:{P0=led_7seg[second/10];P2=0xbf;}break; case 7:{P0=led_7seg[second%10];P2=0x7f;}break; default:break; } if(count==500) //1MS時間到調整顯示時間 { count=0; second++; if(second==60) { second=0; minute++; } if(minute==60) { minute=0; hour++; if(hour==24) { hour=0; } } }
} void main(void) { uchar key; init_timer0(); while(1) { key=kbscan(); switch(key) { case 0x11:{if(hour<23)hour++;else hour=0;}break; case 0x21:{if(minute<59)minute++;else minute=0;}break; default:break; } } }
[此貼子已經被作者于2010-1-12 23:50:00編輯過]
|