DS1302给LCD1602显示数据,LCD除了20外其余均显示0,调试无果求解答!
时间:10-02
整理:3721RD
点击:
函数如下:
main.c
#include <reg51.h>
#include <intrins.h>
sbit DS1302_CE=P3^5;
sbit DS1302_CK=P3^6;
sbit DS1302_IO=P3^4;
unsigned char T0RH=0;T0RL=0;
bit flag200ms=0;
unsigned char LED[]= {0x3f,0x06,0x5B,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char DS1302SingleRead(unsigned char reg);
void InitLCD1602();
void LCDshowstr(unsigned char x,unsigned char y,unsigned char *p);
void DS1302SingleWrite(unsigned char reg,unsigned char dat);
void ConfigeTimer0(unsigned char ms)
{
unsigned int tmp;
tmp=65536-12000000*ms/12/1000;
T0RH=(unsigned char )(tmp>>8);
T0RL=(unsigned char )tmp;
TMOD&=0xF0;
TMOD|=0x01;
TH0=T0RH;
TL0=T0RL;
ET0=1;
TR0=1;
}
unsigned char DS1302BitRead()
{
unsigned char dat=0;
unsigned char mask;
for (mask=0;mask<8;mask++,dat<<1)
{
if (DS1302_IO!=0)
{dat=dat|0x01;}
DS1302_CK=1;
_nop_();
DS1302_CK=0;
_nop_();
}
return dat;
}
void DS1302BitWrite(unsigned char dat)
{
unsigned char mask;
for (mask=0;mask<8;mask++,dat>>1)
{
if ((dat&0x01)!=0)
{DS1302_IO=1;}
else
{DS1302_IO=0;}
DS1302_CK=1;
_nop_();
DS1302_CK=0;
_nop_();
}
}
void InitDS1302()
{
unsigned char code InitTime[7]={0x00,0x30,0x09,0x18,0x03,0x06,0x13};
unsigned char i;
DS1302_CK=0;
DS1302_CE=0;
i=DS1302SingleRead(0);
if ((i&0x80)!=0)
{
DS1302SingleWrite(7,0x00);
for(i=0;i<7;i++)
{
DS1302SingleWrite(i,InitTime[i]);
}
}
}
unsigned char DS1302SingleRead(unsigned char reg)
{
unsigned char dat;
DS1302_IO=0xff;
DS1302_CE=1;
DS1302BitWrite((reg<<1)+0x81);
dat=DS1302BitRead();
DS1302_CE=0;
return dat;
}
void DS1302SingleWrite(unsigned char reg,unsigned char dat)
{
DS1302_CE=1;
DS1302BitWrite((reg<<1)|0x80);
DS1302BitWrite(dat);
DS1302_CE=0;
}
void main()
{
unsigned char Psec=0xaa;
unsigned char Time[8];
unsigned char str[10];
unsigned char i;
EA=1;
ConfigeTimer0(2);
InitLCD1602();
InitDS1302();
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]='w';
str[1]='e';
str[2]='e';
str[3]='k';
str[4]=(Time[5]&0x0f)+'0';
str[5]='\0';
LCDshowstr(11,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 interruptTimer0() interrupt 1
{
static unsigned char tmp=0;
TH0=T0RH;
TL0=T0RL;
tmp++;
if (tmp>=100)
{
tmp=0;
flag200ms=1;
}
}
LCD1602.c ///这块调试没问题,上面不晓得哪里错了,求指教!
#include<reg51.h>
#define LCD1602_DB P0
sbit LCD1602_RS=P2^6;
sbit LCD1602_RW=P2^5;
sbit LCD1602_E=P2^7;
void LCDwriteReady();
void LCDshowstr(unsigned char x,unsigned char y,unsigned char *p);
/* 向LCD1602液晶写入一字节命令,cmd-待写入命令值 */
void LCDwritecmd(unsigned char cnt)
{
LCDwriteReady();
LCD1602_RS=0;
LCD1602_RW=0;
LCD1602_DB=cnt;
LCD1602_E=1;
LCD1602_E=0;
}
/* 设置显示RAM起始地址,亦即光标位置,(x,y)-对应屏幕上的字符坐标 */
void LCDsetCursor(unsigned char x,unsigned char y)
{
unsigned char c;
if (y==0)
{
c=0x00+x;
}else
{
c=0x40+x;
}
LCDwritecmd(c|0x80);
}
/* 向LCD1602液晶写入一字节数据,dat-待写入数据值 */
void LCDwritedat(unsigned char dat)
{
LCDwriteReady();
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_DB=dat;
LCD1602_E=1;
LCD1602_E=0;
}
/* 在液晶上显示字符串,(x,y)-对应屏幕上的起始坐标,
str-字符串指针,len-需显示的字符长度 */
void LCDshowstr(unsigned char x,unsigned char y,unsigned char *p)
{
LCDsetCursor(x,y);
while(*p!='\0')
{
LCDwritedat(*p++);
}
}
/* 等待液晶准备好 */
void LCDwriteReady()
{
unsigned char cmd;
LCD1602_RS=0;
LCD1602_RW=1;
do
{
LCD1602_E=1;
LCD1602_DB=0xff;
cmd=LCD1602_DB;
}while(cmd&0x80);
}
/* 初始化1602液晶 */
void InitLCD1602()
{
LCDwritecmd(0x38);
LCDwritecmd(0x0c);
LCDwritecmd(0x06);
LCDwritecmd(0x01);
}
main.c
#include <reg51.h>
#include <intrins.h>
sbit DS1302_CE=P3^5;
sbit DS1302_CK=P3^6;
sbit DS1302_IO=P3^4;
unsigned char T0RH=0;T0RL=0;
bit flag200ms=0;
unsigned char LED[]= {0x3f,0x06,0x5B,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char DS1302SingleRead(unsigned char reg);
void InitLCD1602();
void LCDshowstr(unsigned char x,unsigned char y,unsigned char *p);
void DS1302SingleWrite(unsigned char reg,unsigned char dat);
void ConfigeTimer0(unsigned char ms)
{
unsigned int tmp;
tmp=65536-12000000*ms/12/1000;
T0RH=(unsigned char )(tmp>>8);
T0RL=(unsigned char )tmp;
TMOD&=0xF0;
TMOD|=0x01;
TH0=T0RH;
TL0=T0RL;
ET0=1;
TR0=1;
}
unsigned char DS1302BitRead()
{
unsigned char dat=0;
unsigned char mask;
for (mask=0;mask<8;mask++,dat<<1)
{
if (DS1302_IO!=0)
{dat=dat|0x01;}
DS1302_CK=1;
_nop_();
DS1302_CK=0;
_nop_();
}
return dat;
}
void DS1302BitWrite(unsigned char dat)
{
unsigned char mask;
for (mask=0;mask<8;mask++,dat>>1)
{
if ((dat&0x01)!=0)
{DS1302_IO=1;}
else
{DS1302_IO=0;}
DS1302_CK=1;
_nop_();
DS1302_CK=0;
_nop_();
}
}
void InitDS1302()
{
unsigned char code InitTime[7]={0x00,0x30,0x09,0x18,0x03,0x06,0x13};
unsigned char i;
DS1302_CK=0;
DS1302_CE=0;
i=DS1302SingleRead(0);
if ((i&0x80)!=0)
{
DS1302SingleWrite(7,0x00);
for(i=0;i<7;i++)
{
DS1302SingleWrite(i,InitTime[i]);
}
}
}
unsigned char DS1302SingleRead(unsigned char reg)
{
unsigned char dat;
DS1302_IO=0xff;
DS1302_CE=1;
DS1302BitWrite((reg<<1)+0x81);
dat=DS1302BitRead();
DS1302_CE=0;
return dat;
}
void DS1302SingleWrite(unsigned char reg,unsigned char dat)
{
DS1302_CE=1;
DS1302BitWrite((reg<<1)|0x80);
DS1302BitWrite(dat);
DS1302_CE=0;
}
void main()
{
unsigned char Psec=0xaa;
unsigned char Time[8];
unsigned char str[10];
unsigned char i;
EA=1;
ConfigeTimer0(2);
InitLCD1602();
InitDS1302();
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]='w';
str[1]='e';
str[2]='e';
str[3]='k';
str[4]=(Time[5]&0x0f)+'0';
str[5]='\0';
LCDshowstr(11,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 interruptTimer0() interrupt 1
{
static unsigned char tmp=0;
TH0=T0RH;
TL0=T0RL;
tmp++;
if (tmp>=100)
{
tmp=0;
flag200ms=1;
}
}
LCD1602.c ///这块调试没问题,上面不晓得哪里错了,求指教!
#include<reg51.h>
#define LCD1602_DB P0
sbit LCD1602_RS=P2^6;
sbit LCD1602_RW=P2^5;
sbit LCD1602_E=P2^7;
void LCDwriteReady();
void LCDshowstr(unsigned char x,unsigned char y,unsigned char *p);
/* 向LCD1602液晶写入一字节命令,cmd-待写入命令值 */
void LCDwritecmd(unsigned char cnt)
{
LCDwriteReady();
LCD1602_RS=0;
LCD1602_RW=0;
LCD1602_DB=cnt;
LCD1602_E=1;
LCD1602_E=0;
}
/* 设置显示RAM起始地址,亦即光标位置,(x,y)-对应屏幕上的字符坐标 */
void LCDsetCursor(unsigned char x,unsigned char y)
{
unsigned char c;
if (y==0)
{
c=0x00+x;
}else
{
c=0x40+x;
}
LCDwritecmd(c|0x80);
}
/* 向LCD1602液晶写入一字节数据,dat-待写入数据值 */
void LCDwritedat(unsigned char dat)
{
LCDwriteReady();
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_DB=dat;
LCD1602_E=1;
LCD1602_E=0;
}
/* 在液晶上显示字符串,(x,y)-对应屏幕上的起始坐标,
str-字符串指针,len-需显示的字符长度 */
void LCDshowstr(unsigned char x,unsigned char y,unsigned char *p)
{
LCDsetCursor(x,y);
while(*p!='\0')
{
LCDwritedat(*p++);
}
}
/* 等待液晶准备好 */
void LCDwriteReady()
{
unsigned char cmd;
LCD1602_RS=0;
LCD1602_RW=1;
do
{
LCD1602_E=1;
LCD1602_DB=0xff;
cmd=LCD1602_DB;
}while(cmd&0x80);
}
/* 初始化1602液晶 */
void InitLCD1602()
{
LCDwritecmd(0x38);
LCDwritecmd(0x0c);
LCDwritecmd(0x06);
LCDwritecmd(0x01);
}
我给你做了吧
首先先确定显示程序正确,你的显示内容里除了20以外,还有其他的符号和英文,先确定这些现实正确,其他的就是确定ds1302的数据是否正确,以及ds1302的数据转换为显示数据是否正确