本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
大家帮忙看一下这个ds1302的问题在哪?
时间显示不稳定,数字乱码。
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DS=P1^0;
sbit LCD_RS = P0^0; //寄存器选择输入
sbit LCD_RW = P0^1; //液晶读/写控制
sbit LCD_EN = P0^2; //液晶使能控制
sbit scl=P1^1;
sbit sda=P1^3;
sbit rst=P1^2;
uint temp;
uchar table[6];
uchar table1[18];
uchar inittime[8] ={0,0,0,19,10,1,9,}; //秒分时日月星期年
////////////////////////////////////////////////////////////////////////////////// 延时程序
void delay(uint ms)
{
uint i,j;
for (j=0;j<ms;j++)
for (i=0;i<120;i++);
}
void delayus(uint us)
{
uint i,j;
for (j=0;j<us;j++)
for (i=0;i<12;i++);
}
/////////////////////////////////////////////////////////////////////////////////////ds18b20程序
void dsreset(void)
{ uchar X;
DS=1;
DS=0;
delayus(600);
DS=1;
delayus(30);
X=DS;
delayus(90);
delayus(400);
}
bit tmpreadbit(void)
{ bit dat;
DS=1;
DS=0;
DS=1;
dat=DS;
delayus(60);
return (dat);
}
uchar tmpread(void)
{
uchar i,temp,dat;
dat=0;
for(i=1;i<=8;i++)
{
temp=tmpreadbit();
dat=(temp<<7)|(dat>>1);
}
return(dat);
}
void tmpwritebyte(uchar dat)
{
uchar j;
bit testb;
for(j=1;j<=8;j++)
{ DS=1;
testb=dat&0x01;
dat=dat>>1;
if(testb)
{
DS=0;
DS=1;
delayus(60);
}
else
{
DS=0;
delayus(1);
DS=1;
delayus(1);
}
}
}
void tmp()
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0x44);
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8;
temp=temp|a;
table[0]=' ';
if(b&0xf8) {temp=~temp+1;table[0]='-';}
tt=temp*0.0625;
temp=tt*10+0.5;
table[1] = (uchar)(temp/100+'0');
table[2] = (uchar)(temp%100/10+'0');
table[3] = '.';
table[4] = (uchar)(temp%100%10+'0');
table[5] = '\0';
}
//////////////////////////////////////////////////////////////////12864显示程序
bit lcd_busy()
{
bit result;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
delayus(1);
result = (bit)(P2&0x80);
LCD_EN = 0;
return(result);
}
void lcd_wcmd(uchar cmd)
{
while(lcd_busy());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
delayus(1);
P2 = cmd;
delayus(1);
LCD_EN = 1;
delayus(1);
LCD_EN = 0;
}
void lcd_wdat(uchar dat)
{
while(lcd_busy());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
delayus(1);
P2 = dat;
delayus(1);
LCD_EN = 1;
delayus(1);
LCD_EN = 0;
}
void lcd_position(uchar X,uchar Y)
{
uchar pos;
if (X==0)
{X=0x80;}
else if (X==1)
{X=0x90;}
else if (X==2)
{X=0x88;}
else if (X==3)
{X=0x98;}
pos = X+Y ;
lcd_wcmd(pos);
}
void lcd_init()
{
lcd_wcmd(0x34); //扩充指令操作
delay(5);
lcd_wcmd(0x30); //基本指令操作
delay(5);
lcd_wcmd(0x0C); //显示开,关光标
delay(5);
lcd_wcmd(0x01); //清除LCD的显示内容
delay(5);
}
void display(uchar x,uchar y,uchar *p)
{
lcd_position(x,y);
while(*p != '\0')
{
lcd_wdat(*p);
p++;
}; }
/////////////////////////////////////////////////////////////////////////////////////////ds1302程序
void DS1302_InputByte(uchar value)
{
uchar i;
for (i=8;i>0;i--)
{
sda=value&0x01;
scl=1;
scl=0;
value=value>>1;
}
}
uchar DS1302_OutputByte(void) //实时时钟读取一字节(内部函数)
{
uchar value,i;
value=0x00;
for (i=8;i>0;i--)
{
value= value>>1 ;
if (sda==1)
{
value= value|0x80;
}
scl=1;
scl=0;
}
return value;
}
void Write_DS1302(uchar Addr,uchar ucDa) //Addr: DS1302地址, ucData: 要写的数据
{
rst = 0;
scl = 0;
rst = 1;
DS1302_InputByte(Addr);
DS1302_InputByte(ucDa);
scl = 1;
rst = 0;
}
uchar Read_DS1302(uchar Addr)//读取DS1302某地址的数据
{
unsigned char ucData;
rst = 0;
scl = 0;
rst = 1;
DS1302_InputByte(Addr|0x01);
ucData = DS1302_OutputByte();
scl = 1;
rst = 0;
return(ucData);
}
void settime(uchar *p)
{uchar i;
uchar addr=0x80;
Write_DS1302(0x8e,0x00);
for(i=0;i<7;i++)
{Write_DS1302(addr,*p);
addr+=2;
p++;}
Write_DS1302(0x8e,0x80);
}
void gettime()
{uchar i,j;
uchar currenttime[8];
uchar addr=0x81;
for(i=0;i<7;i++)
{j=Read_DS1302(addr);
currenttime[i]=((j&0x70)>>4)*10 +(j&0x0F);
addr+=2;
}
table1[0] = (uchar)(currenttime[6]/10+'0');
table1[1] = (uchar)(currenttime[6]%10+'0');
table1[2] = (uchar)(currenttime[5]+'0');
table1[3] = (uchar)(currenttime[4]/10+'0');
table1[4] = (uchar)(currenttime[4]%10+'0');
table1[5] = (uchar)(currenttime[3]/10+'0');
table1[6] = (uchar)(currenttime[3]%10+'0');
table1[7] = (uchar)(currenttime[2]/10+'0');
table1[8] = (uchar)(currenttime[2]%10+'0');
table1[9] = (uchar)(currenttime[1]/10+ '0');
table1[10] = (uchar)(currenttime[1]%10+ '0');
}
void Init_DS1302(void)//初始化芯片
{ uchar Second;
rst=0;
scl=0;
Second=Read_DS1302(0x80);
if(Second&0x80!=0x80)
{ Write_DS1302(0x8e,0x00);
Write_DS1302(0x80,0); }
} |