|
普中51開發(fā)板ds1302+lcd1602出現(xiàn)問號(hào),不能正確計(jì)時(shí),請(qǐng)問我代碼有什么問題,實(shí)在是找不出來了
現(xiàn)在可以確定的是驅(qū)動(dòng)lcd1602的代碼沒啥問題
感謝大佬的幫助!
單片機(jī)代碼和現(xiàn)象如下:
main.c- #include <REG52.H>
- #include <INTRINS.H>
- #define delay() \
- { \
- _nop_(); \
- }
- sbit DS1302_CE = P3 ^ 5;
- sbit DS1302_IO = P3 ^ 4;
- sbit DS1302_SCLK = P3 ^ 6;
- unsigned char T0RH = 0;
- unsigned char T0RL = 0;
- bit flag200ms = 0;
- void configTimer0(unsigned int ms);
- void init_DS1302();
- extern void lcdShowStr(unsigned char x, unsigned char y, unsigned char *str);//在lcd1602的x列,y行輸出str
- extern void initLCD1602();//初始化lcd1602
- unsigned char DS1302SingleRead(unsigned char reg);
- void main()
- {
- unsigned char i;
- unsigned char psec = 0xAA;
- unsigned char time[8];
- unsigned char str[12];
- EA = 1;
- configTimer0(1);
- init_DS1302();
- initLCD1602();
- while (1)
- {
- if (flag200ms)
- {
- flag200ms = 0;
- for (i = 0; i < 7; i++)
- {
- time[i] = DS1302SingleRead(i);
- }
- if (psec != time[0])
- {
- 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];
- }
- }
- }
- }
- void DS1302ByteWrite(unsigned char dat)
- {
- unsigned char mask;
- for (mask = 0x01; mask != 0; mask <<= 1)
- {
- if (mask & dat)
- {
- DS1302_IO = 1;
- delay();
- }
- else
- {
- DS1302_IO = 0;
- delay();
- }
- DS1302_SCLK = 1;
- delay();
- DS1302_SCLK = 0;
- delay();
- }
- DS1302_IO = 1;
- }
- unsigned char DS1302ByteRead()
- {
- unsigned char mask;
- unsigned char dat = 0;
- DS1302_IO = 1;
- for (mask = 0x01; mask != 0; mask <<= 1)
- {
- if (DS1302_IO)
- {
- dat |= mask;
- }
- DS1302_SCLK = 1;
- delay();
- DS1302_SCLK = 0;
- delay();
- }
- return dat;
- }
- void DS1302SingleWrite(unsigned char reg, unsigned char dat)
- {
- DS1302_CE = 1;
- delay();
- DS1302ByteWrite((reg << 1) | 0x80);
- DS1302ByteWrite(dat);
- DS1302_CE = 0;
- delay();
- }
- unsigned char DS1302SingleRead(unsigned char reg)
- {
- unsigned char dat;
- DS1302_CE = 1;
- delay();
- DS1302ByteWrite((reg << 1) | 0x81);
- dat = DS1302ByteRead();
- DS1302_CE = 0;
- delay();
- return dat;
- }
- void init_DS1302()
- {
- unsigned char i;
- unsigned char code initTime[] = {
- 0x00, 0x30, 0x12, 0x08, 0x10, 0x02, 0x13};
- DS1302_CE = 0;
- delay();
- DS1302_SCLK = 0;
- delay();
- i = DS1302SingleRead(0);
- if (i & 0x80)
- {
- DS1302SingleWrite(7, 0x00);
- for (i = 0; i < 7; i++)
- {
- DS1302SingleWrite(i, initTime[i]);
- }
- }
- }
- void configTimer0(unsigned int ms)
- {
- unsigned long tmp;
- tmp = 11059200 / 12;
- tmp = (tmp * ms) / 1000;
- tmp = 65536 - tmp;
- tmp += 0;
- T0RH = (unsigned char)(tmp >> 8);
- T0RL = (unsigned char)tmp;
- TMOD &= 0xF0;
- TMOD |= 0x01;
- TH0 = T0RH;
- TL0 = T0RL;
- ET0 = 1;
- TR0 = 1;
- }
- void interruptTimer0() interrupt 1
- {
- static unsigned char timer200ms = 0;
- TH0 = T0RH;
- TL0 = T0RL;
- timer200ms++;
- if (timer200ms >= 200)
- {
- timer200ms = 0;
- flag200ms = 1;
- }
- }
復(fù)制代碼 lcd1602.c- #include <REG52.H>
- #define LCD1602_DB P0
- sbit LCD1602_RS = P2 ^ 6;
- sbit LCD1602_RW = P2 ^ 5;
- sbit LCD1602_EN = P2 ^ 7;
- /*等待液晶準(zhǔn)備好*/
- void lcdWaitReady()
- {
- unsigned char sta;
- LCD1602_DB = 0xFF;
- LCD1602_RS = 0;
- LCD1602_RW = 1;
- do
- {
- LCD1602_EN = 1;
- sta = LCD1602_DB; // 讀取狀態(tài)字
- LCD1602_EN = 0;
- } while (sta & 0x80); // bit_7為1時(shí)表示液晶正忙,重復(fù)檢測(cè)直到其等于1為止
- }
- /*向LCD1602液晶寫入一字節(jié)命令,cmd為待寫入命令值*/
- void lcdWriteCmd(unsigned char cmd)
- {
- lcdWaitReady();
- LCD1602_RS = 0;
- LCD1602_RW = 0;
- LCD1602_DB = cmd;
- LCD1602_EN = 1;
- LCD1602_EN = 0;
- }
- /*向LCD1602液晶寫入一字節(jié)命令,dat為待寫入命令*/
- void lcdWriteDta(unsigned char dat)
- {
- lcdWaitReady();
- LCD1602_RS = 1;
- LCD1602_RW = 0;
- LCD1602_DB = dat;
- LCD1602_EN = 1;
- LCD1602_EN = 0;
- }
- /*設(shè)置顯示RAM起始地址,亦即光標(biāo)位置,(x,y)為對(duì)應(yīng)屏幕上的字符坐標(biāo)*/
- void lcdSetCursor(unsigned char x, unsigned char y)
- {
- unsigned char addr;
- if (y == 0)
- { // 由輸入的屏幕坐標(biāo)計(jì)算顯示RAM的地址
- addr = 0x00 + x; // 第一行字符地址從0x00起始
- }
- else
- {
- addr = 0x40 + x; // 第二行字符地址從0x40起始
- }
- lcdWriteCmd(addr | 0x80); // 設(shè)置RAM地址
- }
- /*在液晶上顯示字符串,(x,y)為對(duì)應(yīng)屏幕上的起始坐標(biāo),str為字符串指針*/
- void lcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
- {
- lcdSetCursor(x, y); // 設(shè)置起始地址
- while (*str != '\0')
- { // 連續(xù)寫入字符串?dāng)?shù)據(jù),直到檢測(cè)到結(jié)束符
- lcdWriteDta(*str++); // 先取str指向的數(shù)據(jù),然后str自加1
- }
- }
- /*區(qū)域清除,清除從(x,y)坐標(biāo)起始的len個(gè)字符*/
- void lcdAreaClear(unsigned char x, unsigned char y, unsigned char len)
- {
- lcdSetCursor(x, y);
- while (len--)
- {
- lcdWriteDta(' ');
- }
- }
- /*清屏指令*/
- void lcdFullClear()
- {
- lcdWriteCmd(0x01);
- }
- /*初始化1602液晶*/
- void initLCD1602()
- {
- lcdWriteCmd(0x38); // 16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)接口
- lcdWriteCmd(0x0C); // 顯示器開,光標(biāo)關(guān)閉
- lcdWriteCmd(0x06); // 文字不動(dòng),地址自動(dòng)+1
- lcdWriteCmd(0x01); // 清屏
- }
復(fù)制代碼
|
|