普中51開發板ds1302+lcd1602出現問號,不能正確計時,請問我代碼有什么問題,實在是找不出來了
現在可以確定的是驅動lcd1602的代碼沒啥問題
感謝大佬的幫助!
單片機代碼和現象如下:
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;
- }
- }
復制代碼 lcd1602.c- #include <REG52.H>
- #define LCD1602_DB P0
- sbit LCD1602_RS = P2 ^ 6;
- sbit LCD1602_RW = P2 ^ 5;
- sbit LCD1602_EN = P2 ^ 7;
- /*等待液晶準備好*/
- void lcdWaitReady()
- {
- unsigned char sta;
- LCD1602_DB = 0xFF;
- LCD1602_RS = 0;
- LCD1602_RW = 1;
- do
- {
- LCD1602_EN = 1;
- sta = LCD1602_DB; // 讀取狀態字
- LCD1602_EN = 0;
- } while (sta & 0x80); // bit_7為1時表示液晶正忙,重復檢測直到其等于1為止
- }
- /*向LCD1602液晶寫入一字節命令,cmd為待寫入命令值*/
- void lcdWriteCmd(unsigned char cmd)
- {
- lcdWaitReady();
- LCD1602_RS = 0;
- LCD1602_RW = 0;
- LCD1602_DB = cmd;
- LCD1602_EN = 1;
- LCD1602_EN = 0;
- }
- /*向LCD1602液晶寫入一字節命令,dat為待寫入命令*/
- void lcdWriteDta(unsigned char dat)
- {
- lcdWaitReady();
- LCD1602_RS = 1;
- LCD1602_RW = 0;
- LCD1602_DB = dat;
- LCD1602_EN = 1;
- LCD1602_EN = 0;
- }
- /*設置顯示RAM起始地址,亦即光標位置,(x,y)為對應屏幕上的字符坐標*/
- void lcdSetCursor(unsigned char x, unsigned char y)
- {
- unsigned char addr;
- if (y == 0)
- { // 由輸入的屏幕坐標計算顯示RAM的地址
- addr = 0x00 + x; // 第一行字符地址從0x00起始
- }
- else
- {
- addr = 0x40 + x; // 第二行字符地址從0x40起始
- }
- lcdWriteCmd(addr | 0x80); // 設置RAM地址
- }
- /*在液晶上顯示字符串,(x,y)為對應屏幕上的起始坐標,str為字符串指針*/
- void lcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
- {
- lcdSetCursor(x, y); // 設置起始地址
- while (*str != '\0')
- { // 連續寫入字符串數據,直到檢測到結束符
- lcdWriteDta(*str++); // 先取str指向的數據,然后str自加1
- }
- }
- /*區域清除,清除從(x,y)坐標起始的len個字符*/
- 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點陣,8位數據接口
- lcdWriteCmd(0x0C); // 顯示器開,光標關閉
- lcdWriteCmd(0x06); // 文字不動,地址自動+1
- lcdWriteCmd(0x01); // 清屏
- }
復制代碼
|