DS1302时钟 新手
时间:10-02
整理:3721RD
点击:
#include <stc15f2k60s2.h>
#include <intrins.h>
#define DIG P2
#define uint unsigned int
#define uchar unsigned char
sbit RST=P0^0;
sbit CLK=P0^2;
sbit IO=P0^1; //1302 èy???ú
sbit SEC1=P3^4;
sbit SEC2=P3^3;
sbit DU1=P3^5;
sbit MIN1=P3^7;
sbit MIN2=P3^6;
sbit DU2=P4^1;
sbit HOU1=P4^4;
sbit HOU2=P4^2; //êy??1ü??ó|òy??
uchar code DIG_CODE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,};
uchar code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d}; //?áè?μ??·
uchar code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c}; //D′μ??·
uchar TIME[7] = {0x49, 0x59, 0x23, 0x07, 0x05, 0x06, 0x16}; //3?ê??μ
uchar LED[9]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //êy??1ü??á?
uint i = 0;
void Ds1302Write(unsigned char addr, unsigned char dat);
void Ds1302Init();
void Ds1302ReadTime();
void datapros();
void display();
void main()
{
Ds1302Init();
while(1)
{
datapros();
display();
}
}
void datapros()
{
Ds1302ReadTime();
LED[0]=DIG_CODE[TIME[0]/16];
LED[1]=DIG_CODE[TIME[0]&0x0f];
LED[3]=DIG_CODE[TIME[1]/16];
LED[4]=DIG_CODE[TIME[1]&0x0f];
LED[6]=DIG_CODE[TIME[2]/16];
LED[7]=DIG_CODE[TIME[2]&0x0f];
}
void display()
{
DIG=0x00;
if (i==0)
{
SEC1=0;SEC2=1;DU1=1;MIN1=1;MIN2=1;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=LED[0];
}
else if(i==1)
{
SEC1=1;SEC2=0;DU1=1;MIN1=1;MIN2=1;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=LED[1];
}
else if(i==2)
{
SEC1=1;SEC2=1;DU1=0;MIN1=1;MIN2=1;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=0x40;
}
else if(i==3)
{
SEC1=1;SEC2=1;DU1=1;MIN1=0;MIN2=1;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=LED[3];
}
else if(i==4)
{
SEC1=1;SEC2=1;DU1=1;MIN1=1;MIN2=0;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=LED[4];
}
else if(i==5)
{
SEC1=1;SEC2=1;DU1=1;MIN1=1;MIN2=1;DU2=0;HOU1=1;HOU2=1;
i++;
DIG=0x40;
}
else if(i==6)
{
SEC1=1;SEC2=1;DU1=1;MIN1=1;MIN2=1;DU2=1;HOU1=0;HOU2=1;
i++;
DIG=LED[6];
}
else if(i==7)
{
SEC1=1;SEC2=1;DU1=1;MIN1=1;MIN2=1;DU2=1;HOU1=1;HOU2=0;
i=0;
DIG=LED[7];
}
}
void Ds1302Write(unsigned char addr, unsigned char dat)
{
unsigned char n;
RST = 0;
_nop_();
CLK = 0; //??SCLK??μíμ????£
_nop_();
RST = 1; //??RST????μ????£
_nop_();
for (n=0; n<8; n++) //?aê?′??í°???μ??·?üá?
{
IO = addr & 0x01; //êy?Y′óμí???aê?′??í
addr >>= 1;
CLK = 1; //êy?Y?úé?éy??ê±£?DS1302?áè?êy?Y
_nop_();
CLK = 0;
_nop_();
}
for (n=0; n<8; n++) //D′è?8??êy?Y
{
IO = dat & 0x01;
dat >>= 1;
CLK = 1; //êy?Y?úé?éy?óê±£?DS1302?áè?êy?Y
_nop_();
CLK = 0;
_nop_();
}
RST = 0; //?áê?′??íêy?Y
_nop_();
}
unsigned char Ds1302Read(unsigned char addr)
{
unsigned char n,dat,dat1;
RST = 0;
_nop_();
CLK = 0; //??SCLK??μíμ????£
_nop_();
RST = 1; //??RST????μ????£
_nop_();
for(n=0; n<8; n++) //?aê?′??íμ??·?üá?
{
IO = addr & 0x01; //êy?Y′??í
addr >>= 1;
CLK = 1; //êy?Y?úé?éy??ê±£?DS1302?áè?êy?Y
_nop_();
CLK = 0; //????êy?Y
_nop_();
}
_nop_();
for(n=0; n<8; n++) //?áè?êy?Y
{
dat1 = IO; //′ó×?μí???aê??óê?
dat = (dat>>1) | (dat1<<7);
CLK = 1;
_nop_();
CLK = 0; //D′êy?Y
_nop_();
}
RST = 0;
_nop_(); //DS1302?′??μ??è?¨ê±??
CLK = 1;
_nop_();
IO = 0;
_nop_();
IO = 1;
_nop_();
return dat;
}
void Ds1302Init()
{
unsigned char i;
Ds1302Write(0x8E,0X00); //è¥3yD′±£?¤
for (i=0; i<7; i++) //D′è?ê±?óD?o?£o·???ê±è????ü?ê
{
Ds1302Write(WRITE_RTC_ADDR[i],TIME[i]);
}
Ds1302Write(0x8E,0x80); //′ò?aD′±£?¤
}
void Ds1302ReadTime()
{
unsigned char i;
for (i=0; i<7; i++) //?áè?7??×??úμ?ê±?óD?o?£o·???ê±è????ü?ê
{
TIME[i] = Ds1302Read(READ_RTC_ADDR[i]);
}
}
#include <intrins.h>
#define DIG P2
#define uint unsigned int
#define uchar unsigned char
sbit RST=P0^0;
sbit CLK=P0^2;
sbit IO=P0^1; //1302 èy???ú
sbit SEC1=P3^4;
sbit SEC2=P3^3;
sbit DU1=P3^5;
sbit MIN1=P3^7;
sbit MIN2=P3^6;
sbit DU2=P4^1;
sbit HOU1=P4^4;
sbit HOU2=P4^2; //êy??1ü??ó|òy??
uchar code DIG_CODE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,};
uchar code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d}; //?áè?μ??·
uchar code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c}; //D′μ??·
uchar TIME[7] = {0x49, 0x59, 0x23, 0x07, 0x05, 0x06, 0x16}; //3?ê??μ
uchar LED[9]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //êy??1ü??á?
uint i = 0;
void Ds1302Write(unsigned char addr, unsigned char dat);
void Ds1302Init();
void Ds1302ReadTime();
void datapros();
void display();
void main()
{
Ds1302Init();
while(1)
{
datapros();
display();
}
}
void datapros()
{
Ds1302ReadTime();
LED[0]=DIG_CODE[TIME[0]/16];
LED[1]=DIG_CODE[TIME[0]&0x0f];
LED[3]=DIG_CODE[TIME[1]/16];
LED[4]=DIG_CODE[TIME[1]&0x0f];
LED[6]=DIG_CODE[TIME[2]/16];
LED[7]=DIG_CODE[TIME[2]&0x0f];
}
void display()
{
DIG=0x00;
if (i==0)
{
SEC1=0;SEC2=1;DU1=1;MIN1=1;MIN2=1;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=LED[0];
}
else if(i==1)
{
SEC1=1;SEC2=0;DU1=1;MIN1=1;MIN2=1;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=LED[1];
}
else if(i==2)
{
SEC1=1;SEC2=1;DU1=0;MIN1=1;MIN2=1;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=0x40;
}
else if(i==3)
{
SEC1=1;SEC2=1;DU1=1;MIN1=0;MIN2=1;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=LED[3];
}
else if(i==4)
{
SEC1=1;SEC2=1;DU1=1;MIN1=1;MIN2=0;DU2=1;HOU1=1;HOU2=1;
i++;
DIG=LED[4];
}
else if(i==5)
{
SEC1=1;SEC2=1;DU1=1;MIN1=1;MIN2=1;DU2=0;HOU1=1;HOU2=1;
i++;
DIG=0x40;
}
else if(i==6)
{
SEC1=1;SEC2=1;DU1=1;MIN1=1;MIN2=1;DU2=1;HOU1=0;HOU2=1;
i++;
DIG=LED[6];
}
else if(i==7)
{
SEC1=1;SEC2=1;DU1=1;MIN1=1;MIN2=1;DU2=1;HOU1=1;HOU2=0;
i=0;
DIG=LED[7];
}
}
void Ds1302Write(unsigned char addr, unsigned char dat)
{
unsigned char n;
RST = 0;
_nop_();
CLK = 0; //??SCLK??μíμ????£
_nop_();
RST = 1; //??RST????μ????£
_nop_();
for (n=0; n<8; n++) //?aê?′??í°???μ??·?üá?
{
IO = addr & 0x01; //êy?Y′óμí???aê?′??í
addr >>= 1;
CLK = 1; //êy?Y?úé?éy??ê±£?DS1302?áè?êy?Y
_nop_();
CLK = 0;
_nop_();
}
for (n=0; n<8; n++) //D′è?8??êy?Y
{
IO = dat & 0x01;
dat >>= 1;
CLK = 1; //êy?Y?úé?éy?óê±£?DS1302?áè?êy?Y
_nop_();
CLK = 0;
_nop_();
}
RST = 0; //?áê?′??íêy?Y
_nop_();
}
unsigned char Ds1302Read(unsigned char addr)
{
unsigned char n,dat,dat1;
RST = 0;
_nop_();
CLK = 0; //??SCLK??μíμ????£
_nop_();
RST = 1; //??RST????μ????£
_nop_();
for(n=0; n<8; n++) //?aê?′??íμ??·?üá?
{
IO = addr & 0x01; //êy?Y′??í
addr >>= 1;
CLK = 1; //êy?Y?úé?éy??ê±£?DS1302?áè?êy?Y
_nop_();
CLK = 0; //????êy?Y
_nop_();
}
_nop_();
for(n=0; n<8; n++) //?áè?êy?Y
{
dat1 = IO; //′ó×?μí???aê??óê?
dat = (dat>>1) | (dat1<<7);
CLK = 1;
_nop_();
CLK = 0; //D′êy?Y
_nop_();
}
RST = 0;
_nop_(); //DS1302?′??μ??è?¨ê±??
CLK = 1;
_nop_();
IO = 0;
_nop_();
IO = 1;
_nop_();
return dat;
}
void Ds1302Init()
{
unsigned char i;
Ds1302Write(0x8E,0X00); //è¥3yD′±£?¤
for (i=0; i<7; i++) //D′è?ê±?óD?o?£o·???ê±è????ü?ê
{
Ds1302Write(WRITE_RTC_ADDR[i],TIME[i]);
}
Ds1302Write(0x8E,0x80); //′ò?aD′±£?¤
}
void Ds1302ReadTime()
{
unsigned char i;
for (i=0; i<7; i++) //?áè?7??×??úμ?ê±?óD?o?£o·???ê±è????ü?ê
{
TIME[i] = Ds1302Read(READ_RTC_ADDR[i]);
}
}
请问?你的问题是什么呢?
直接DS1323吧
没什么 我感觉15的 和51的没多大差别啊
谢谢分享的好资料