各位大佬,我是個業余愛好者,基礎差,,請教大家了,,
我想讓1206上顯示,時間(小時:分種,秒),不知道哪里錯了,1206顯示秒的地方,不是一秒一變化,同時進入一秒
的IF中不見leds變化,1206,顯示的部分全刪了,進入一秒的IF leds是有變化的,,一個個加的測試,也像一到1206寫
命令處和1206寫數據的地方,, leds一秒一變化,就不正常了,,(1206顯示秒的地方,不是一秒一變化,)
謝謝謝謝
單片機源程序如下:
#include <reg52.h>
unsigned char ms;
//unsigned char ml;
sbit sm=P2^6; //RS 數據/命令選擇。H/L.
sbit dx=P2^5; // R/W讀寫選擇。H/L.
sbit eo=P2^7; // 使能
sbit leds=P2^0;
sbit ledf=P2^1;
sbit ledh=P2^2;
unsigned char t;
unsigned char code yihang[]="BeiJingShiJian";
unsigned char code erhang[]="0123456789";
/* 開定時器T0.定時5MS RST15F204 */
void InitTimer0()
{
TMOD = 0x01; //設定定時器0工作方式(16位定時器 )
TH0 = 0xEC; //
TL0 = 0x78; //
EA = 1; // 開總中斷
ET0 = 1; // 開定時器0中斷
TR0 = 1; // 啟動定時器0
}
void Lcd1602_Delay1ms(unsigned int c) //誤差 0us
{
unsigned char a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
/*寫命令比如光標顯示與不顯示,需不需要移屏,在液晶什么位置顯示 */
void xieml (unsigned char ml)
{
eo=0;
dx=0; // R/W讀寫選擇。H/L..
sm=0; //RS 數據/命令選擇。H/L.
P0=ml;
Lcd1602_Delay1ms(2);
eo=1;
Lcd1602_Delay1ms(2);
eo=0;
}
/*寫數據比如在液晶顯示什么容 */
void xiesj(unsigned char sj)
{
eo=0;
dx=0; // R/W讀寫選擇。H/L.
sm=1; //RS 數據/命令選擇。H/L.
P0=sj;
Lcd1602_Delay1ms(2);
eo=1;
Lcd1602_Delay1ms(2);
eo=0;
}
/*1206初始化 */
void chushiha()
{
xieml(0X38);
xieml(0X0c);
// xieml(0X06);
xieml(0X01);
}
/*主函數,寫入固定時間,驅動某個繼電器 */
void main()
{
unsigned char n,s,f,h,sh,sg,fh,fg,hh,hg;
InitTimer0();
chushiha();
while(1)
{
if(ms==200)
{
ms=0; s=s+1; leds=~leds; //為了檢測觀察程序,加了一秒leds亮滅一次。
}
if(s==59)
{
s=0; f=f+1;ledf=~ledf; //為了檢測觀察程序,加了一分leds亮滅一次。
}
if(f==59)
{
f=0; h=h+1; ledh=~ledh;
}
if(h==11)
{
h=0;
}
sg=s%10; sh=s/10; // sg秒的個位數,sh秒十位數。
fg=f%10; fh=f/10; // fg分的個位數,fh分十位數。
hg=h%10; hh=h/10; // hg時的個位數,hh時十位數。
xieml(0X80+0X01);
for(n=0;n<14;n++)
{
xiesj(yihang[n]); //1206第一行顯示BeiJingShiJian。
}
xieml(0X80+0X44);
xiesj(erhang[hh]); //1206第二行顯示時間時的十位數。
xieml(0X80+0X45);
xiesj(erhang[hg]); //1206第二行顯示時間時的個位數。
xieml(0X80+0X46);
xiesj(':');
xieml(0X80+0X47);
xiesj(erhang[fh]); //1206第二行顯示時間分的十位數。
xieml(0X80+0X48);
xiesj(erhang[fg]); //1206第二行顯示時間分的個位數。
xieml(0X80+0X49);
xiesj(',');
xieml(0X80+0X4A);
xiesj(erhang[sh]); //1206第二行顯示時間秒的十位數。
xieml(0X80+0X4B);
xiesj(erhang[sg]); //1206第二行顯示時間秒的個位數。
}
}
void T0_time() interrupt 1
{
TH0 = 0xEC; //
TL0 = 0x78;
ms++;
// if(ms==200){ms=0;led=~led;}
}
|