|
楼主 |
发表于 2023-5-9 16:26:38
|
显示全部楼层
void ds1302_write_byte(unsigned char address,unsigned char data)
{
unsigned char i;
CE = 0;
_nop_();
CE = 1;
_nop_();
SCLK = 0;
_nop_();
for (i = 0;i<8;i++)
{
IO = address&0x01;
address= address>>1;
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
for (i = 0;i<8;i++)
{
IO = dat&0x01;
dat = dat>>1;
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
CE = 0;
_nop_();
}
*/
unsigned char ds1302_read_byte(unsigned char address){
unsigned char i=0,temp=0,value=0;
CE = 0;
_nop_();
CE = 1;
_nop_();
SCLK = 0;
_nop_();
for (i = 0;i<8;i++)
{
IO = address&0x01;
address = address>>1;
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
for (i = 0;i<8;i++)
{ /
temp = IO;
value=(temp<<7)|(value>>1);
SCLK=1;
_nop_();
SCLK=0;
_nop_();
}
CE = 0;
_nop_();
SCLK=1;
_nop_();
IO = 0;
_nop_();
IO = 1;
_nop_();
return value;
}
unsigned char gREAD_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
unsigned char gWRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};
unsigned char gDS1302_TIME[7] = {0x25, 0x22, 0x17, 0x31, 0x08, 0x03, 0x22};
void ds1302_init()
{
unsigned char i;
ds1302_write_byte(0x8E,0X00);
for (i = 0;i<7;i++){
ds1302_write_byte(gWRITE_RTC_ADDRESS[i],gDS1302_TIME[i]);
}
ds1302_write_byte(0x8E,0X80);
}
*/
void ds1302_read_time(void)
{
unsigned char i=0;
for(i=0;i<7;i++)
{
gDS1302_TIME[i]=ds1302_read_byte(gREAD_RTC_ADDRESS[i]);
#include "ds1302.h"
#include "LCD1602.h"
extern unsigned char gDS1302_TIME[7];
void main(){
unsigned char time_buf[7];
unsigned char i;
ds1302_init();
LCD_Init();/
while(1)
{
ds1302_read_time();
for (i = 0 ; i< 7 ; i++)
{
time_buf[i]=(gDS1302_TIME[i]/16)*10+gDS1302_TIME[i]%16;
}
LCD_ShowNum(1,1,20,2);
LCD_ShowNum(1,3,time_buf[6],2);
LCD_ShowChar(1,5,'-');
LCD_ShowNum(1,6,time_buf[4],2);
LCD_ShowChar(1,8,'-');
LCD_ShowNum(1,9,time_buf[3],2);
LCD_ShowNum(1,15,time_buf[5],1);
LCD_ShowNum(2,1,time_buf[2],2);
LCD_ShowChar(2,3,':');
LCD_ShowNum(2,4,time_buf[1],2);
LCD_ShowChar(2,6,':');
LCD_ShowNum(2,7,time_buf[0],2);
}
}
|
|