📄 ds1307.c
字号:
#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 dispbuf[7];
unsigned char code zimo[16]={0xc0,0xf9,0xa4,0xb0, // 0, 1, 2, 3
0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};// 4, 5, 6, 7, 8, 9
unsigned char code disflag[6]={0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char led_buf[6];
unsigned char n,flag;
unsigned char Sec, Min, Hrs;
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 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;
}
int main()
{
// unsigned char disp_temp[3];
// unsigned char temp_h,temp_l,i,j;
P0 = 0xff;
P2 = 0xff;
TMOD = 0x01;
TH0 = 0xfe;
TL0 = 0x0c;
IE = 0x82;
flag=0;
n=0;
/* I2c_start();
I2c_write(WRITE);
I2c_write(0x00);
I2c_write(0x00);
I2c_stop();*/
Init_Rtc(0x00,0x50,0x03);
TR0=1;
while(1)
{
/* 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;*/
disp_time();
}
}
void timer0(void) interrupt 1
{
TH0=0xfe;
TL0=0x0c;
// KeyDelaytime++;
if(flag<7)
{
P0=led_buf[n];
P2=disflag[n];
}
else
{
P2=0x00;
P0=0;
}
flag++;
if(flag==8)
{
n++;
if(n==6)
{
n=0;
}
flag=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -