普中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); // 清屏
- }
复制代码
|