|
|
樓主這個(gè)程序不完整,缺少按鍵調(diào)整程序。把一些抄寫錯(cuò)誤糾正后通過編譯,仿真時(shí)沒有發(fā)現(xiàn)樓主所言的頻閃問題。
- #include <reg52.h>
- #define LCD1602_DB P0
- sbit LCD1602_RS = P2^6;
- sbit LCD1602_RW = P2^5;
- sbit LCD1602_E = P2^7;
- sbit DS1302_CE = P1^7;
- sbit DS1302_CK = P3^5;
- sbit DS1302_IO = P3^4;
- bit flag200ms = 0; //200ms定時(shí)標(biāo)志
- unsigned char T0RH = 0; //T0重載值的高字節(jié)
- unsigned char T0RL = 0; //T0重載的低字節(jié)
- void ConfigTimer0(unsigned int ms);
- void InitDS1302();
- void DS1302BurstRead(unsigned char *dat);
- void InitLcd1602();
- void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
- void delay(unsigned int t)
- {
- while(--t);
- }
- void LcdWaitReady()
- {
- unsigned char sta;
-
- LCD1602_DB = 0xFF;
- LCD1602_RS = 0;
- LCD1602_RW = 1;
- do {
- delay(500);
- LCD1602_E = 1;
- sta = LCD1602_DB;
- LCD1602_E = 0;
- } while (sta & 0x80); //我覺得這個(gè)do while 可能要改
- }
- void LcdWriteCmd(unsigned char cmd)
- {
- LcdWaitReady();
- LCD1602_RS = 0;
- LCD1602_RW = 0;
- LCD1602_DB = cmd;
- LCD1602_E = 1;
- LCD1602_E = 0;
- }
- void LcdWriteDat(unsigned char dat)
- {
- LcdWaitReady();
- LCD1602_RS = 1;
- LCD1602_RW = 0;
- LCD1602_DB = dat;
- LCD1602_E = 1;
- LCD1602_E = 0;
- }
- void LcdSetCursor(unsigned char x, unsigned char y)
- {
- unsigned char addr;
-
- if (y == 0)
- addr = 0x00 + x;
- else
- addr = 0x40 + x;
- LcdWriteCmd(addr | 0x80);
- }
- void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
- {
- LcdSetCursor(x, y);
- while (*str != '\0')
- {
- LcdWriteDat(*str++);
- }
- }
- void InitLcd1602()
- {
- LcdWriteCmd(0x38);
- LcdWriteCmd(0x0C);
- LcdWriteCmd(0x06); //文字不動(dòng),地址自動(dòng)+1
- LcdWriteCmd(0x01); //清屏
- }
- void main()
- {
- unsigned char psec=0xAA;//秒備份,初值A(chǔ)A確保首次讀取時(shí)間后會(huì)刷新顯示
- unsigned char time[8]; //當(dāng)前時(shí)間數(shù)組
- unsigned char str[12]; //字符串轉(zhuǎn)換緩沖區(qū)
- EA = 1; //開總中斷
- ConfigTimer0(1); //T0定時(shí)1ms
- InitDS1302(); //初始化實(shí)時(shí)時(shí)鐘
- InitLcd1602(); //初始化液晶
-
- while (1)
- {
- if (flag200ms) //每200ms讀取依次時(shí)間
- {
- flag200ms = 0;
- DS1302BurstRead(time); //讀取DS1302當(dāng)前時(shí)間
- if (psec != time[0]) //檢測(cè)到時(shí)間有變化時(shí)刷新顯示
- {
- str[0] = '2';
- str[1] = '0';
- str[2] = (time[6] >> 4) + '0';
- str[3] = (time[6]&0x0F) + '0';
- str[4] = '-';
- str[5] = (time[4] >> 4) + '0';
- str[6] = (time[4]&0x0F) + '0';
- str[7] = '-';
- str[8] = (time[3] >> 4) + '0';
- str[9] = (time[3]&0x0F) + '0';
- str[10] = '\0';
- LcdShowStr(0, 0, str);
- //顯示到液晶的第一行
- str[0] = (time[5]&0x0F) + '0';
- str[1] = '\0';
- LcdShowStr(11, 0, "week");
- LcdShowStr(15, 0, str); //顯示到液晶的第一行
-
- str[0] = (time[2] >> 4) + '0';
- str[1] = (time[2]&0x0F) + '0';
- str[2] = ':';
- str[3] = (time[1] >> 4) + '0';
- str[4] = (time[1]&0x0F) + '0';
- str[5] = ':';
- str[6] = (time[0] >> 4) + '0';
- str[7] = (time[0]&0x0F) + '0';
- str[8] = '\0';
- LcdShowStr(4, 1, str); //顯示到第二行
-
- psec = time[0]; //用當(dāng)前值更新上次秒數(shù)
- }
- }
- }
- }
- /* 發(fā)送一個(gè)字節(jié)到DS1302通信總線上 */
- void DS1302ByteWrite(unsigned char dat)
- {
- unsigned char mask;
-
- for (mask=0x01; mask!=0; mask<<=1) //低位在前,逐位輸出
- {
- if ((mask&dat) != 0) //首先輸出該位數(shù)據(jù)
- DS1302_IO = 1;
- else
- DS1302_IO = 0;
- DS1302_CK = 1; //拉高時(shí)鐘
- DS1302_CK = 0; //拉低時(shí)鐘,
- }
- DS1302_IO = 1; //確保釋放IO
- }
- /* 由DS1302讀取一個(gè)字節(jié)*/
- unsigned char DS1302ByteRead()
- {
- unsigned char mask;
- unsigned char dat = 0;
-
- for (mask=0x01; mask!=0; mask<<=1) //低位在前,逐位輸出
- {
- if (DS1302_IO != 0) //讀取IO引腳,并設(shè)置對(duì)應(yīng)位
- {
- dat |= mask;
- }
- DS1302_CK = 1; //拉高時(shí)鐘
- DS1302_CK = 0; //拉低時(shí)鐘,完成一個(gè)位
- }
- return dat;
- }
- /* reg-寄存器地址,dat-待寫入字節(jié) */
- void DS1302SingleWrite(unsigned char reg, unsigned char dat)
- {
- DS1302_CE = 1;
- DS1302ByteWrite((reg<<1)|0x80);
- DS1302ByteWrite(dat);
- DS1302_CE = 0;
- }
- /* reg-寄存器地址,返回值-讀到的字節(jié) */
- unsigned char DS1302SingleRead(unsigned char reg)
- {
- unsigned char dat;
-
- DS1302_CE = 1;
- DS1302ByteWrite((reg<<1)|0x81);
- dat = DS1302ByteRead();
- DS1302_CE = 0;
- return dat;
- }
- void DS1302BurstWrite(unsigned char *dat)
- {
- unsigned char i;
- DS1302_CE = 1;
- DS1302ByteWrite(0xBE);
- for(i=0; i<8; i++)
- {
- DS1302ByteWrite(dat[i]);
- }
- DS1302_CE = 0;
- }
- void DS1302BurstRead(unsigned char *dat)
- {
- unsigned char i;
- DS1302_CE = 1;
- DS1302ByteWrite(0xBF);
- for(i=0; i<8; i++)
- {
- dat[i] = DS1302ByteRead();
- }
- DS1302_CE = 0;
- }
- /* DS1302初始化,掉電則重新設(shè)置初試時(shí)間*/
- void InitDS1302()
- {
- unsigned char dat;
- unsigned char code InitTime[] = {0x00,0x30,0x00, 0x16, 0x05, 0x04, 0x19}; //2019年5月16日 星期四12:30:00
-
- DS1302_CE = 0;
- DS1302_CK = 0;
- dat = DS1302SingleRead(0);
- if ((dat & 0x80) != 0) //秒寄存器最高位CH值判斷DS1302是否已停止
- {
- DS1302SingleWrite(7, 0x00); //撤銷寫保護(hù)以允許寫入數(shù)據(jù)
- DS1302BurstWrite(InitTime); //設(shè)置DS1302為默認(rèn)的初始時(shí)間
- }
- }
- /* 配置并啟動(dòng)T0,ms-T0定時(shí)時(shí)間 */
- void ConfigTimer0(unsigned int ms)
- {
- unsigned long tmp; //臨時(shí)變量
-
- tmp = 11059200 / 12;
- tmp = (tmp * ms) /1000;
- tmp = 65536 - tmp;
- tmp = tmp + 12; //補(bǔ)償中斷響應(yīng)延時(shí)造成的誤差
- T0RH = (unsigned char)(tmp>>8);
- T0RL = (unsigned char)tmp;
- TMOD &= 0xF0; //清零T0的控制位
- TMOD |= 0x01;
- TH0 = T0RH;
- TL0 = T0RL;
- ET0 = 1;
- TR0 = 1;
- }
- /* T0中斷服務(wù)函數(shù),200ms定時(shí)*/
- void InterruptTimer0() interrupt 1
- {
- static unsigned char tmr200ms = 0;
-
- TH0 = T0RH; //重新加載重載值
- TL0 = T0RL;
- tmr200ms++;
- if (tmr200ms >= 200) //定時(shí)200ms
- {
- tmr200ms = 0;
- flag200ms = 1;
- }
- }
復(fù)制代碼
|
|