📄 timechip.c
字号:
#include "timechip.h"
void delay_us(unsigned char a)
{
unsigned char b;
for(b=a;b>0;b--)
{
asm("nop");
}
}
void Start(void)
{
DIR_IN;
asm("nop");
SDA_0;
asm("nop");
SCL_1;
asm("nop");
delay_us(0x20);
//delay_us(0xc);
DIR_OUT;
asm("nop");
delay_us(0xc);
SCL_0;
delay_us(0xc);
}
void Stop(void)
{
DIR_OUT;
asm("nop");
delay_us(0xc);
SCL_1;
delay_us(0xc);
DIR_IN;
delay_us(0xc);
}
void SendAck(void)
{
DIR_OUT;
delay_us(0xc);
SCL_1;
delay_us(0xc);
SCL_0;
delay_us(0xc);
DIR_IN;
}
void SendNoAck(void)
{
DIR_IN;
delay_us(0x23);
SCL_1;
delay_us(0x23);
SCL_0;
delay_us(0x23);
}
unsigned char RxByte(void)
{
unsigned char a,b;
DIR_IN; // 置 SDA 为输入
for(a=8;a>0;a--)
{
b <<= 1;
SCL_1;
delay_us(0x0c);
if(SDA_IN)
b |= 0x01;
else
b &= 0xfe;
SCL_0;
delay_us(0xc);
}
return b;
}
unsigned char TxByte(unsigned char a)
{
unsigned char b;
for(b=8;b>0;b--)
{
if(a&0x80)
{
asm("nop");
DIR_IN; // 置方向为输入,外面为 1
}
else
{
asm("nop");
DIR_OUT; // 置方向为输出,输出0
}
delay_us(0xc);
SCL_1;
delay_us(0xc);
SCL_0;
a <<= 1;
asm("nop");
}
asm("nop");
DIR_IN;
delay_us(0xc);
SCL_1;
delay_us(0xc);
if(SDA_IN)
{
b = 0xff;
}
else
{
b = 0;
}
SCL_0;
delay_us(0x04);
return b;
}
void init5372(void)
{
Start();
TxByte(0x64);
TxByte(0x70);
TxByte(0);
TxByte(0);
TxByte(0);
TxByte(0);
TxByte(0);
TxByte(0);
TxByte(0);
TxByte(0);
TxByte(0x28);
Stop();
delay_us(50);
}
void Read5372(void)
{
//RTC temp1;
uint8_t i;
Error_C_1;
if(Rd_Over | Rd_Read | ET_5372_Se)
{
asm("nop");
return;
}
ET_5372_Se_1;
if(Tx_Len)
{
asm("nop");
return;
}
asm("nop");
Start();
TxByte(0x64);
TxByte(0x04);
for(i=0; i<7;i++)
{
asm("nop");
TIME_RTC_BK[i]=TIME_RTC[i];
}
TIME_RTC[0] = RxByte(); //rtc.sec = RxByte();
SendAck();
TIME_RTC[1] = RxByte(); //rtc.minute = RxByte();
SendAck();
TIME_RTC[2] = RxByte(); //rtc.hour = RxByte();
SendAck();
TIME_RTC[3] = RxByte(); //rtc.week = RxByte();
SendAck();
TIME_RTC[4] = RxByte(); //rtc.day = RxByte();
SendAck();
TIME_RTC[5] = RxByte(); //rtc.month = RxByte();
SendAck();
TIME_RTC[6] = RxByte(); //rtc.year = RxByte();
// SendAck();
// Time_Error = RxByte(); //error
SendNoAck();
Stop();
}
void Read5372A(void)
{
//RTC temp1;
uint8_t i;
Error_C_1;
if(Rd_Over | Rd_Read | ET_5372_Se)
{
return;
}
ET_5372_Se_1;
if(Tx_Len)
{
return;
}
Start();
TxByte(0x64);
TxByte(0x04);
for(i=0; i<7;i++)
{
TIME_RTC_BK[i]=TIME_RTC[i];
}
TIME_RTC[0] = RxByte(); //rtc.sec = RxByte();
SendAck();
TIME_RTC[1] = RxByte(); //rtc.minute = RxByte();
SendAck();
TIME_RTC[2] = RxByte(); //rtc.hour = RxByte();
SendAck();
TIME_RTC[3] = RxByte(); //rtc.week = RxByte();
SendAck();
TIME_RTC[4] = RxByte(); //rtc.day = RxByte();
SendAck();
TIME_RTC[5] = RxByte(); //rtc.month = RxByte();
SendAck();
TIME_RTC[6] = RxByte(); //rtc.year = RxByte();
SendAck();
Time_Error = RxByte(); //error
SendNoAck();
Stop();
}
uint8_t show816_H(uint8_t u)
{
uint8_t b,x;
x= (u >>4) &0x0f;
if(x >9)
b = x + 0x37;
else
b = x + 0x30;
return(b);
}
uint8_t show816_L(uint8_t u)
{
uint8_t b,x;
x = u & 0x0f;
if( x >9)
b = x +0x37;
else
b = x +0x30;
return(b);
}
void Write5372(void)
{
// unsigned char a;
Start();
TxByte(0x64);
asm("nop");
TxByte(0x00);
asm("nop");
TxByte(TIME_TMP[0]); //sec
asm("nop");
TxByte(TIME_TMP[1]); //min
asm("nop");
TxByte(TIME_TMP[2]); //h
asm("nop");
TxByte(TIME_TMP[3]); //w
asm("nop");
TxByte(TIME_TMP[4]); //d
asm("nop");
TxByte(TIME_TMP[5]); //m
asm("nop");
//TIME_TMP[6]=8;
TxByte(TIME_TMP[6]); //year
asm("nop");
// TxByte(Time_Error); //year
Stop();
asm("nop");
// delay_us(70);
}
void Write5372A(void)
{
// unsigned char a;
Start();
asm("nop");
TxByte(0x64);
asm("nop");
TxByte(0x00);
asm("nop");
TxByte(TIME_TMP[0]); //sec
asm("nop");
TxByte(TIME_TMP[1]); //min
asm("nop");
TxByte(TIME_TMP[2]); //h
asm("nop");
TxByte(TIME_TMP[3]); //w
asm("nop");
TxByte(TIME_TMP[4]); //d
asm("nop");
TxByte(TIME_TMP[5]); //m
//TIME_TMP[6]=8;
TxByte(TIME_TMP[6]); //year
asm("nop");
TxByte(Time_Error); //year
asm("nop");
Stop();
asm("nop");
//delay_us(70);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -