小白求解答!!如何将DS1302的时间数据存入AT24C02中,再调用出来显示在LCD12864上?
2019-07-15 08:35发布
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
我这边可以将数据写入AT24C02中,这证明我的模块不是坏的。我现在想把DS1302的时间先存入AT24C02,然后再调用出来显示在液晶屏上。我写了以下代码,还是不行。能帮我看一下吗?
unsigned char table4[9]={'0','0',':','0','0',':','0','0',' '};
void display_time()
{
uchar num,tmp0,tmp1,tmp2;
uchar Second,Min,Hour;
uchar Second0,Min0,Hour0;
uchar Second1,Min1,Hour1;
EEPROM_Software_Reset();
Second = ds1302_read(0x81);//读秒
Min = ds1302_read(0x83);//读分
Hour = ds1302_read(0x85);//读时
EEPROM_Byte_Write(0xa0,0x00,Second);
EEPROM_Byte_Write(0xb0,0x00,Min);
EEPROM_Byte_Write(0xc0,0x00,Hour);
tmp0 = EEPROM_CurrentAddr_Read(0xa1);
tmp1 = EEPROM_CurrentAddr_Read(0xb1);
tmp2 = EEPROM_CurrentAddr_Read(0xc1);
Second0 = (tmp0 & 0x0f) + 0x30;//秒的个位
Second1 = ((tmp0 & 0x7f) >> 4) + 0x30;//秒的十位
Min0 = (tmp1 & 0x0f) + 0x30;//分的个位
Min1 = ((tmp1& 0x7f) >> 4) + 0x30;//分的十位
Hour0 = (tmp2 & 0x0f) + 0x30;//秒的个位
Hour1 = ((tmp2 & 0x7f ) >> 4) + 0x30;//时的十位
table4[6]= Second1;
table4[7]= Second0;
table4[3]= Min1;
table4[4]= Min0;
table4[0]= Hour1;
table4[1]= Hour0;
lcd_pos(3,1); //显示在第四行第2个字符
for(num=0;num<8;num++)//显示长度
{
lcd_wdat(table4[num]);
}
}
//////////////////AT24C02//////////////////
void EEPROM_Software_Reset(void)
{
uchar i;
IIC_Start();
//9 clocl
scl = 0;
sda = 1;
for(i=0;i<9;i++)
{
scl = 0;
_nop_();//delay
_nop_();
scl = 1;
_nop_();//delay
scl = 0;
_nop_();//delay
_nop_();
}
IIC_Start();
IIC_Stop();
}
void IIC_Start(void)
{
scl = 0;
sda = 1;
scl = 1;
_nop_();
sda = 0;
nop_();
}
void IIC_Stop(void)
{
scl = 0;
sda = 0;
scl = 1;
_nop_();
sda = 1;
}
void IIC_Write_Byte(uchar dataSend)
{
uchar i;
for(i=0;i<8;i++)
{
scl = 0;
if(dataSend & 0x80)sda = 1;//´Ó×î¸ßλ¿ªÊ¼Ð´
else sda = 0;
_nop_();
scl = 1;
_nop_();
scl = 0;
_nop_();
dataSend <<= 1;
}
}
uchar IIC_Read_Byte(void)
{
uchar dataRead,i;
for(i=0;i<8;i++)
{
dataRead <<= 1;
scl = 0;
sda = 1;
_nop_();
_nop_();
scl = 1;
_nop_();
if(sda)dataRead |= 0x01;
_nop_();
scl = 0;
_nop_();
_nop_();
}
return dataRead;
}
bit IIC_Ack(void)
{
bit flag;
scl = 0;
sda = 1;
_nop_();
_nop_();
scl = 1;
_nop_();
flag = sda;//¶ÁSDA
_nop_();
scl = 0;
_nop_();
_nop_();
return flag;
}
void IIC_NoAck(void)
{
scl = 0;
sda = 1;
_nop_();
_nop_();
scl = 1;
_nop_();
_nop_();
scl = 0;
_nop_();
_nop_();
}
void EEPROM_Byte_Write(uchar devAddr,uchar wordAddr,uchar dataSend)
{
IIC_Start();
IIC_Write_Byte(devAddr);
IIC_Ack();
IIC_Write_Byte(wordAddr);
IIC_Ack();
IIC_Write_Byte(dataSend);
IIC_Ack();
IIC_Stop();
Delay5ms();
}
uchar EEPROM_CurrentAddr_Read(uchar devAddr)
{
uchar dataRead;
IIC_Start();
IIC_Write_Byte(devAddr);
IIC_ACK();
dataRead = IIC_Read_Byte();
IIC_NoACK();
ICC_Stop();
return dataRead;
}
一周热门 更多>