蓝桥杯(单片机赛道)知识点整理—DS1302部分

功能介绍

DS1302有两个功能:一个是时钟显示功能,一个是静态RAM功能。

采用三线接口与CPU进行通信。

Only three wires are required to communicate with the clock/RAM:

  1. CE(CE signal must be high during a read or a write.)
  2. I/O (data line)
  3. SCLK (serial clock)

控制命令

控制命令:

第七位默认为1,为0,写进去也是无效的;

第六位是RAM和CLOCK的选择位,此处用到的是CLOCK时钟的功能;

第5位到第1位决定了寄存器的五位地址;

最低位为0或1,可以进行写读的选择,写是0,读是1

数据输入

在输入的控制命令输入完成后,紧跟着输入的是一个数据命令,在紧跟着8位控制命令字节的下一个8位SCLK脉冲的上升沿,数据被写入到DS1302中。

在输入一个读取的控制命令后,紧跟的是读出一个数据,在紧跟8位的控制字节指令的下一个SCLK脉冲的下降沿读出DS1302的数据。

注意的是在控制命令的最后一个上升沿的下一个下降沿便开始进行数据的读取了,具体如下图所示。

寄存器介绍

通过对相应的寄存器进行操作来对设置DS1302的初始化时间。

寄存器表如下,每一个寄存器有八位。

12/24的操作寄存器:

Bit 7 of the hours register is defined as the 12- or 24- hour mode-select bit. When high, the 12-hour mode is selected. In the 12-hour mode, bit 5 is the AM/PM bit with logic high being PM.

其中,第二个寄存器的CH位,当为1时,DS1302将进入停止状态。

写保护位寄存器,再写入之前需要清0,将WP位置为0即可。

补充充电寄存器

bits 4 to 7:control the selection of the trickle charger. Only a pattern of 1010 enables the trickle charger.

bits 2 and 3:  select whether one diode or two diodes are connected between VCC2 and VCC1. If DS is 01, one diode is selected or if DS is 10, two diodes are selected. If DS is 00 or 11, the trickle charger is disabled independently of TCS.

bits 0 and 1:  select the resistor that is connected between VCC2 and VCC1. The resistor and diodes are selected by the RS and DS bits as shown in Table 2.

代码实现

RST即是CE引脚

主函数代码:

#include 
#include 

unsigned char smg_num[]={                       //标准字库
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black  -     H    J    K    L    N    o   P    U     t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1


sbit DS1302_SCLK = P1^7;
sbit DS1302_IO = P2^3;
sbit DS1302_CE = P1^3;
/* 千万注意,此处需要加上最后一位,即写保护位 */
unsigned char DS1302_command[]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e};//seconds;minute;hour;date;month;day(1 to 7);year;write_protect
unsigned char time_stable[]={0x50,0x59,0x92,0x31,0x10,0x01,0x22}; 
unsigned char time_now[7];
void DS1302_Init()
{
	DS1302_SCLK = 0;//ds1302的时钟置零
	DS1302_CE = 0;//CE线置零
}

/*根据时序图进行操作
command: 传入的是控制字节 包括了第七位默认为1;
第六位:ram和clk的选择位
第五到第一位:寄存器的地址位
第零位:写读选择位
*/
void DS1302_WriteByte(unsigned char command, unsigned char Data) //写入一个字节
{
	unsigned char i;
	DS1302_CE = 1;
	for(i=0; i<=7; i++) //先写入控制指令
	{
		DS1302_IO = command&(0X01<

数码管显示代码

#include "reg52.h"

void Delay(unsigned char t)
{
	while(t--);
}

void Init138(unsigned char channel)
{
	switch(channel)
	{
		case 4:
			P2 = (P2 & 0X1F)|0X80;
			break;
		case 5:
			P2 = (P2 & 0X1F)|0Xa0;
			break;
		case 6:
			P2 = (P2 & 0X1F)|0Xc0;
			break;
		case 7:
			P2 = (P2 & 0X1F)|0Xe0;
			break;
	}
	P2 = (P2 & 0X1F)|0X00;
}

void SMG_Bit(unsigned char pos,unsigned char dat)
{
	P0 = 0X01<

你可能感兴趣的:(笔记,单片机,蓝桥杯,嵌入式硬件)