📄 ds1307.h
字号:
#include <reg52.h>
#include <intrins.h>
sbit SCL=P1^0;
sbit SDA=P1^1;
#define ACK 0
#define NACK 1
#define WRITE 0xd0
#define READ 0xd1
unsigned char Sec, Min, Hrs;
extern unsigned char led_buf[6];
unsigned char code zimo[16]={0xc0,0xf9,0xa4,0xb0, // 0, 1, 2, 3
0x99,0x92,0x82,0xf8,0x80,0x90};// 4, 5, 6, 7, 8, 9
void delayms(unsigned char ms)
// 延时子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
//I2C启始标志
void I2c_start(void)
{
SDA = 1;
SCL = 1;
_nop_();
_nop_();
SDA = 0;
_nop_();
_nop_();
SCL = 0;
_nop_();
}
//I2C停止标志
void I2c_stop(void)
{
SDA = 0;
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
SDA = 1;
_nop_();
}
unsigned char I2c_read(unsigned char ack)
{
unsigned char i,read_data;
for(i = 0; i < 8; i++)
{
SCL = 1;
_nop_();
read_data <<= 1;
read_data |= (unsigned char)SDA;
SCL = 0;
_nop_();
}
_nop_();
SCL = 0;
if(ack == NACK) SDA = 1; /* sda = 1 if next cycle is reset */
else
SDA=0;
_nop_();
SCL = 1;
_nop_();
SCL = 0;
_nop_();
SDA = 1;
_nop_();
return(read_data);
}
void I2c_write(unsigned char write_data)
{
unsigned char i;
bit ack_bit;
for(i = 0; i < 8; i++) // 循环移入8个位
{
SDA = (bit)(write_data & 0x80);
SCL = 1;
_nop_();
_nop_();
SCL = 0;
_nop_();
_nop_();
write_data <<= 1;
}
SDA = 1; // 读取应答
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
SCL = 0;
_nop_();
_nop_();
ack_bit = SDA;
}
void Init_Rtc(unsigned char sec,unsigned char min,unsigned char hour)
{
I2c_start();
I2c_write(WRITE);
I2c_write(0x00);
I2c_write(sec);
I2c_write(min);
I2c_write(hour);
I2c_stop();
delayms(10);
}
void WritetoRam(unsigned char add,unsigned char dat)
{
I2c_start();
I2c_write(WRITE);
I2c_write(add);
I2c_write(dat);
I2c_stop();
}
void Read_time(void)
{
I2c_start();
I2c_write(WRITE);
I2c_write(0x00);
I2c_start();
I2c_write(READ);
Sec = I2c_read(ACK);
Min = I2c_read(ACK);
Hrs = I2c_read(NACK);
I2c_stop();
delayms(10);
}
void disp_time(void)
{
unsigned char disp_temp[3];
unsigned char temp_h,temp_l,i,j;
Read_time();
disp_temp[0]=Sec;
disp_temp[1]=Min;
disp_temp[2]=Hrs;
do
{
temp_h=(disp_temp[j]&0xf0)>>4;
temp_l=disp_temp[j]&0x0f;
led_buf[i]=zimo[temp_l];
i++;
led_buf[i]=zimo[temp_h];
i++;
j++;
}while(j<3);
j=0;
i=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -