小弟學單片機的時間不長,自己寫了個51單片機控制1602液晶顯示器顯示電子表的程序,但是調(diào)試程序的時候用按鍵調(diào)時間的時候老是在秒的后面還顯示出兩個數(shù)字,不知道這是為什么,秒后面并沒有讓他顯示,還是顯示了數(shù)字。希望高手不吝賜教;源程序如下:
#include<reg52.h> sbit RS=P1^0; sbit RW=P1^1; sbit EN=P1^2; sbit K1=P3^2; sbit K2=P3^3; sbit K3=P3^4; unsigned int second,minute,hour; unsigned int cnt; unsigned int ms; unsigned char x=0x88; unsigned char y=0x85; unsigned char z=0x82; //*********延時****************** void yanshi(ms) { unsigned int i; while(ms--) for(i=120;i>0;i--); } //*********寫命令********* write_cmd(unsigned char cmd) { RS=0; RW=0; P0=cmd; yanshi(5); EN=1; yanshi(5); EN=0; } //*****************寫數(shù)據(jù)*********** write_data(unsigned char date) { RS=1; RW=0; P0=date; yanshi(5); EN=1; yanshi(5); EN=0; } //*******初始化************* void init() { EN=0; write_cmd(0x38); write_cmd(0x0e); write_cmd(0x06); write_cmd(0x01); write_cmd(0x80); write_cmd(0x84); write_data(':'); write_cmd(0x87); write_data(':'); } //*********************寫時間******* write_SFM(unsigned char date) { unsigned char shi,fen; shi=date/10; fen=date%10; write_data(shi+0x30); write_data(fen+0x30); } //*********主函數(shù)入口********** main() { TMOD=0x02; TH0=0xa4; TL0=0xa4; ET0=1; EA=1; TR0=1; init(); while(1) { if(K1==0) { yanshi(50); if(K1==0) { second++; if(second==60) { second=0; } write_cmd(x); write_SFM(second); while(K1==0); }
} if(K2==0) { yanshi(50); if(K2==0) { minute++; if(minute==60) { minute=0; } write_cmd(0x85); write_SFM(minute); while(K2==0); }
} if(K3==0) { yanshi(50); if(K3==0) { hour++; if(hour==24) { hour=0; } write_cmd(z); write_SFM(hour); while(K3==0); }
}
} } void t0() interrupt 1 { TH0=0xa4; TL0=0xa4; cnt++; if(cnt==10000) { cnt=0; second++; write_cmd(x); write_SFM(second); if(second==60) { second=0; minute++; write_cmd(y); write_SFM(minute); if(minute==60) { minute=0; hour++; write_cmd(z); write_SFM(hour); if(hour==24) { hour=0; } } }
} }
[此貼子已經(jīng)被作者于2009-10-11 13:35:51編輯過]
|