51单片机DS1302数字时钟
作者:互联网
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit RST = P1^0;
sbit IO = P1^1;
sbit SCLK = P1^2;
sbit HC595_SER = P2^7; //b 8 d c e e f 8 f e
sbit HC595_RCLK = P2^5; //11 8 13 12 14 14 16 8 16 14
sbit HC595_SRCLK = P2^6; //1011 1000 1101 1100 1110 1110 1111 1000 1111 1110
//unsigned char code led_dian[]={0xbF,0x86,0xdB,0xcF,0xe6,0xeD,0xfD,0x87,0xfF,0xeF};
unsigned char code led_duan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char hour,hour1,hour2;
unsigned char min,min1,min2;
unsigned char sec,sec1,sec2;
uchar code digital[] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
void Delay_Ms(uchar q)
{
uchar p;
while(q--)
{
for(p=0;p<113;p++);
}
}
//数据发送子程序
void send_595(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(dat&0x80)
HC595_SER = 1;
else
HC595_SER = 0;
HC595_SRCLK = 0;
HC595_SRCLK = 1;
dat = dat<<1;
}
}
//
void write_byte(uchar date)
{
uchar i;
SCLK = 0;
for(i=0;i<8;i++)
{
SCLK = 0;
IO = date & 0x01;
SCLK = 1;
date >>= 1;
}
}
//
void write_ds1302(uchar addr,uchar date)
{
uchar num;
RST = 0;
SCLK = 0;
RST = 1;
write_byte(addr);
num = (date/10<<4)|(date%10);
write_byte(num);
RST = 0;
SCLK = 1;
}
//
uchar read_byte()
{
uchar i;
uchar dat = 0x00;
for(i=0;i<8;i++)
{
SCLK = 0;
dat >>= 1;
if(IO)
{
dat |= 0X80;
}
SCLK = 1;
SCLK = 0;
}
return dat;
}
//
uchar read_ds1302(uchar addr)
{
uchar dat;
uchar dat1;
uchar dat2;
RST = 0;
SCLK = 0;
RST = 1;
write_byte(addr);
dat = read_byte();
RST = 0;
SCLK = 1;
dat1 = dat/16;
dat2 = dat%16;
dat = dat1*10+dat2; //转换为10进制
return dat;
}
//
void init_ds1302() //偶数
{
write_ds1302(0x8e,0x00);
write_ds1302(0x84,23); //时
write_ds1302(0x82,59); //分
write_ds1302(0x80,50); //秒
write_ds1302(0x8e,0x80);
}
//
void get_time() //奇数
{
write_ds1302(0x8e,0x00);
hour = read_ds1302(0x85); //时
min = read_ds1302(0x83); //分
sec = read_ds1302(0x81); //秒
write_ds1302(0x8e,0x80);
}
void main()
{
init_ds1302();
Delay_Ms(5);
while(1)
{
get_time();
hour1 = hour/10;
hour2 = hour%10;
min1 = min/10;
min2 = min%10;
sec1 = sec/10;
sec2 = sec%10;
send_595(0xfe);
send_595(led_duan[hour1]);
HC595_RCLK = 0;
HC595_RCLK = 1;
send_595(0xfd);
send_595(led_duan[hour2]);
HC595_RCLK = 0;
HC595_RCLK = 1;
send_595(0xfb);
send_595(0x40);
HC595_RCLK = 0;
HC595_RCLK = 1;
send_595(0xf7);
send_595(led_duan[min1]);
HC595_RCLK = 0;
HC595_RCLK = 1;
send_595(0xef);
send_595(led_duan[min2]);
HC595_RCLK = 0;
HC595_RCLK = 1;
send_595(0xdf);
send_595(0x40);
HC595_RCLK = 0;
HC595_RCLK = 1;
send_595(0xbf);
send_595(led_duan[sec1]);
HC595_RCLK = 0;
HC595_RCLK = 1;
send_595(0x7f);
send_595(led_duan[sec2]);
HC595_RCLK = 0;
HC595_RCLK = 1;
}
}
标签:ds1302,595,uchar,DS1302,51,send,HC595,单片机,RCLK 来源: https://blog.csdn.net/qq_43588817/article/details/88661335