一、硬件电路:
1.DS1302内部电路:
(1)虽然电路看起来比较复杂,但是我们只需要知道它是由一个时钟寄存器,一个31*8RAM和部分引脚就行,时钟电路设置初始时间和储存时间数据(掉电消除),RAM用来存放数据(掉电保存)。
(2)CE(RST):复位引脚,低电平有效
I/O:双向数据引脚
SCLK:时钟引脚,用于同步数据传输
![](https://img-blog.csdnimg.cn/img_convert/054c42741a72960e942f5c32684a37f5.png)
![](https://img-blog.csdnimg.cn/img_convert/123da0f5d3e826c49679e311c7d2cdb7.png)
(3)蓝桥杯板子上的电路:
![](https://img-blog.csdnimg.cn/img_convert/786a949cdf8a83fdd52611549c29fe24.png)
2.相关寄存器:
(1)时钟寄存器
![](https://img-blog.csdnimg.cn/img_convert/53b9567e7de37f72162f6982711eff0b.png)
(a)CH:当CH置“1”时,禁用晶振,DS1302不再计数
![](https://img-blog.csdnimg.cn/img_convert/278fd51682879e4b2fcd39d2ffe1c82f.png)
(b)10 Second:表示秒十位
(c)RANGE:表示前面数据的范围
(d)12/24:表示选择12小时制还是24小时制,置“1”时为12小时制
(e)AM/PM:在12小时制中,表示是前12小时还是后12小时;在24小时制中,和后一位表示时十位
(d)Day:表示星期几
(c)WP:写保护,置“1”时,对写入进行保护,不允许外部向DS1302写入数据
(2)RAM:
![](https://img-blog.csdnimg.cn/img_convert/fe07b64e511e7fbd4274ed7c3e9333aa.png)
一共有31个8位的存储单元,命令控制字为C0H-FDH,其中奇数为读操作,偶数为写操作
3.蓝桥杯板子上的电路:
![](https://img-blog.csdnimg.cn/img_convert/7981823546429d294cac63b230a1cbb2.png)
二、操作DS1302:(三线SPI)
1.写字节:(低位先行)
![](https://img-blog.csdnimg.cn/img_convert/b025e43fdf8c3aff0416e43994cde959.png)
(1)R/W表示读取/写入
(2)R/C表示RAM/CRC
2.读字节:(低位先行)
![](https://img-blog.csdnimg.cn/img_convert/6277986214e317daadd1bc78653c2ee5.png)
注意:上升沿时,写入数据;下降沿时,读取数据
三、DS1302实验:
用两个独立按键控制两个功能,功能一:用定时器从0开始计时;功能二:一开始设置DS1302的时间为23小时 59分钟 50秒,之后并每秒读取DS1302的时间
代码思路:
(1)定时器——》独立按键——》数码管显示——》DS1302
官方库函数+自己写的设置与读取函数:
#ifndef __DS1302_H
#define __DS1302_H
#include "stc15f2k60s2.h"
#include <intrins.h>
#define u8 unsigned char
#define u16 unsigned int
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3;
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
void Ds1302_Set(u8* s_time);
void Ds1302_Read(u8* r_time);
#endif
#include "ds1302.h"
//
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK = 0;
SDA = temp&0x01;
temp>>=1;
SCK=1;
}
}
//
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
//
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
return (temp);
}
/****************************************************/
/*
初始化时分秒
*/
void Ds1302_Set(u8* s_time)
{
u8 i;
//关闭写保护
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<3;i++)
Write_Ds1302_Byte(0x80+i*2,((s_time[i]/10)<<4)|(s_time[i]%10));
//开启写保护
Write_Ds1302_Byte(0x8e,0x80);
}
/*
读取时间
*/
void Ds1302_Read(u8* r_time)
{
u8 i,r_temp;
for(i=0;i<3;i++)
{
r_temp = Read_Ds1302_Byte(0x81+i*2);
r_time[i] = (r_temp>>4)*10+(r_temp&0x0f);
}
}
主函数:
#include "stc15f2k60s2.h"
#include <stdio.h>
#include "ds1302.h"
#define u8 unsigned char
#define u16 unsigned int
code unsigned char Seg_Table[] =
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0x88, //A
0x83, //b
0xc6, //C
0xa1, //d
0x86, //E
0x8e //F
};
u8 key_dealy;
u8 last_led = 0xff;
u8 mode=1;
//数码管
u8 COT[9],COD[8],PSI;
u16 seg_dealy;
//DS1302
u8 r_time[3],s_time[3]={50,59,23};
u16 ms_count;
u8 sec,minute,hour;
void SEG_Proc();
void All_Close();
void Timer0_Init(void);
void Key_Proc();
void main()
{
All_Close();
Timer0_Init();
Ds1302_Set(s_time);
while(1)
{
Key_Proc();
SEG_Proc();
}
}
/***************独立按键********************/
/*
独立按键按键检测
*/
u8 D_Key()
{
u8 i=0;
for(i=0;i<4;i++)
{
if((P3 & (0x08>>i)) == 0)
return i+4;
}
return 0;
}
/****************数码管**********************/
void SEG_TSL(u8* input,u8* output)
{
u8 i=0;
for(i=0;i<8;i++)
{
switch(input[i])
{
case '0':output[i] = Seg_Table[0];break;
case '1':output[i] = Seg_Table[1];break;
case '2':output[i] = Seg_Table[2];break;
case '3':output[i] = Seg_Table[3];break;
case '4':output[i] = Seg_Table[4];break;
case '5':output[i] = Seg_Table[5];break;
case '6':output[i] = Seg_Table[6];break;
case '7':output[i] = Seg_Table[7];break;
case '8':output[i] = Seg_Table[8];break;
case '9':output[i] = Seg_Table[9];break;
case '-':output[i] = (~0x40);break;
default:output[i] = 0xff;
}
}
}
void SEG_Show(u8 COD,u8 PSI)
{
//消隐
P0 = 0xff;
P2 = P2 & 0x1f | (0x70<<1);
P2 &= 0x1f;
//位选
P0 = 0x01<<PSI;
P2 = P2 & 0x1f | (0x60<<1);
P2 &= 0x1f;
//段选
P0 = COD;
P2 = P2 & 0x1f | (0x70<<1);
P2 &= 0x1f;
}
/****************定时器**********************/
/*
初始化定时器0
*/
void Timer0_Init(void) //1毫秒@12MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x18; //设置定时初始值
TH0 = 0xFC; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1; //使能定时器0中断
EA = 1;
}
void Timer0_Isr(void) interrupt 1
{
if(key_dealy++ == 20)key_dealy = 0;
if(seg_dealy++ == 1000)seg_dealy = 0;
ms_count++;
if(ms_count == 1000)
{
ms_count = 0;
sec++;
if(sec == 60)
{
sec = 0;
minute++;
if(minute == 60)
{
minute = 0;
hour++;
if(hour == 24)
hour = 0;
}
}
}
SEG_Show(COD[PSI],PSI);
if(PSI++ == 7) PSI = 0;
}
/**************LED*************************/
/*
使某个LED灯电平翻转而不影响其他LED
*/
void LEE_Reverve(u8 L_x)
{
P0 = last_led ^ (0x01<<(L_x-1));
last_led = P0;
P2 = P2 & 0x1f | (0x40<<1);
P2 &= 0x1f;
}
/***********************************/
/*
关闭所有设备
*/
void All_Close()
{
//关闭蜂鸣器和继电器
P0 = 0x00;
P2 = P2 & 0x1f | (0x50<<1);
P2 &= 0x1f;
//关闭LED
P0 = 0xff;
P2 = P2 & 0x1f | (0x40<<1);
P2 &= 0x1f;
}
void Key_Proc()
{
static u8 key_old;
u8 key_down,key_now;
if(key_dealy) return;
key_dealy = 1;
key_now = D_Key();
key_down = key_now & (key_now ^ key_old);
key_old = key_now;
if(key_down == 4)
mode = 1;
if(key_down == 5)
mode = 2;
}
void SEG_Proc()
{
if(seg_dealy) return;
seg_dealy = 1;
if(mode == 1)
{
sprintf(COT,"%02u-%02u-%02u",(u16)hour,(u16)minute,(u16)sec);
SEG_TSL(COT,COD);
}
if(mode == 2)
{
Ds1302_Read(r_time);
sprintf(COT,"%02u-%02u-%02u",(u16)r_time[2],(u16)r_time[1],(u16)r_time[0]);
SEG_TSL(COT,COD);
}
}