您的位置:首页 > 其它

使用F2812调试DS1302实时时钟芯片经验

2011-06-09 14:20 393 查看
最近在调试DS1302忙一天终于搞定,特此写点心得共享下希望各位少走弯路,最重要的是要保住芯片手册,基本上软件上需要注意的问题芯片手册都能发现。

1. 在上电时,RST 必须为逻辑0 直至Vcc>2.0V。同时SCLK RST 驱动至逻辑1 状态时必须为逻辑0

2. 数据输入是在SCLK的上升沿,数据输出是在SCLK的下降沿
3. 传输方式:低位先传输。
4. 时钟运行:秒寄存器的最高位(BIT7)是作为时钟控制位,当为逻辑1时,时钟停止,为0时钟运行。所以如果要时钟运行的话就必须将秒寄存器的最高位清0
5. 12/24小时模式:小时寄存器的第7位(最高位)是选择12小时制还是24小时制。当为逻辑1时选择12小时,逻辑0选择24小时。当为12小时制时,小时寄存器的第5位用来表示上午AM和下午PM,逻辑1的时候表示PM,逻辑0的时候表示AM。
6. 在企图对DS1302操作之前,必须将WP清零。
7. 读写模式:有两种模式,可以单字节读写,以及连续多字节读写(burst mode)。在连续多字节读写时,即burst mode,地址是必须从地址0的0位开始的。如果对时钟操作,即CLOCK操作,则必须连续写满8个字节。若是对RAM操作,则可以不必须写满31个字节。

8. DS1302包含了实时时钟日历和31个字节的RAM,这里需要注意的是实时时钟日历的8个寄存器和31个字节的RAM是独立的,没有任何关系,不要混淆。31个RAM字节可以做其他用途,如闹钟等。因为DS1302有电池供电,所以RAM的内容不会丢失,相当于EEPROM了。用户可以用来实现特别的要求。
9. 时序必须严格按照其数据手册上来。
10.源代码如下:
/******************** (C) COPYRIGHT 2011 TOGEST LTD********************
* File Name : DPS28_I2C.c
* Author : He ZongNan
* Version : V1.1.0
* Last Modify : 05/30/2011
* Description : I2C drv source file;
* ------------------------------------------------------------------------------
* Modification history
* 2011-06-03 Created by He ZongNan
* --------------------------------------------------------------------
********************************************************************************/
#include "../drv_inc/DSP28_Device.h"
struct TIMER_REG TimerRegs;
/*******************************************************************************
* Function Name : InitDS1302
* Description : init the ds1302
* Input : None;
* Output : None;
* Return : None;
*******************************************************************************/
void InitDS1302(void)
{
Uint8 Sec = 0;
EALLOW;
GpioMuxRegs.GPAMUX.bit.PWM3_GPIOA2 = 0; //0: select GPIO function
GpioMuxRegs.GPAMUX.bit.PWM2_GPIOA1 = 0; //0: select GPIO function
GpioMuxRegs.GPAMUX.bit.PWM1_GPIOA0 = 0; //0: select GPIO function
GpioMuxRegs.GPADIR.bit.GPIOA2 = 1;
GpioMuxRegs.GPADIR.bit.GPIOA1 = 1;
GpioMuxRegs.GPADIR.bit.GPIOA0 = 1;
DS1302_CE = 0;
DS1302_CK = 0;
EDIS;
W1302(0x8E, 0x00);
W1302(0x80, 0x00);
}
/*******************************************************************************
* Function Name : SET_DA
* Description : set da
* Input : None;
* Output : None;
* Return : None;
*******************************************************************************/
void SET_DA(Uint16 flag)
{
if (flag == F_IN)
{
EALLOW;
DA_IN;
EDIS;
}
else
{
EALLOW;
DA_OUT;
EDIS;
}
}
/*******************************************************************************
* Function Name : DS1302InputByte
* Description : input one byte
* Input : Uint8 DA;
* Output : None;
* Return : None;
*******************************************************************************/
void DS1302InputByte(Uint8 DA)
{
union UINT16_REG REG;
REG.all = DA;
SET_DA(F_OUT);
DS1302_DA = REG.bit.BITS0;DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;
DS1302_DA = REG.bit.BITS1;DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;

DS1302_DA = REG.bit.BITS2;DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;
DS1302_DA = REG.bit.BITS3;DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;
DS1302_DA = REG.bit.BITS4;DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;

DS1302_DA = REG.bit.BITS5;DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;
DS1302_DA = REG.bit.BITS6;DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;
DS1302_DA = REG.bit.BITS7;DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;
SET_DA(F_IN);
}
/*******************************************************************************
* Function Name : DS1302OutputByte
* Description : input one byte
* Input : Uint8 DA;
* Output : None;
* Return : None;
*******************************************************************************/
Uint8 DS1302OutputByte(void)
{
union UINT16_REG REG;
REG.all = 0;
SET_DA(F_IN);

REG.bit.BITS0 = DS1302_DA; DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;
REG.bit.BITS1 = DS1302_DA; DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;

REG.bit.BITS2 = DS1302_DA; DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;

REG.bit.BITS3 = DS1302_DA; DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;

REG.bit.BITS4 = DS1302_DA; DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;

REG.bit.BITS5 = DS1302_DA;DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;

REG.bit.BITS6 = DS1302_DA;DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;

REG.bit.BITS7 = DS1302_DA;DELAY1;
DS1302_CK = 1;DELAY1;
DS1302_CK = 0;DELAY1;
return (REG.all);

}
/*******************************************************************************
* Function Name : W1302
* Description : input one byte
* Input : Uint8 DA;
* Output : None;
* Return : None;
*******************************************************************************/
void W1302(Uint8 Addr, Uint8 Da)
{
if(Addr % 2 == 1)
{
return;
}
DS1302_CE = 0; DELAY4;
DS1302_CK = 0; DELAY1;
DS1302_CE = 1; DELAY4;
DS1302InputByte(Addr);
DS1302InputByte(Da);
DS1302_CK = 1; DELAY1;
DS1302_CE = 0; DELAY4;
}
/*******************************************************************************
* Function Name : R1302
* Description : input one byte
* Input : Uint8 DA;
* Output : None;
* Return : None;
*******************************************************************************/
Uint8 R1302(Uint8 Addr)
{
Uint8 Da;
if(Addr % 2 == 0)
{
return 0;
}
DS1302_CE = 0; DELAY4;
DS1302_CK = 0; DELAY1;
DS1302_CE = 1; DELAY4;

DS1302InputByte(Addr);
Da = DS1302OutputByte();
DS1302_CK = 1; DELAY1;
DS1302_CE = 0; DELAY10;
return Da;
}
/*******************************************************************************
* Function Name : SET_TIMER
* Description : input one byte
* Input : Uint8 DA;
* Output : None;
* Return : None;
*******************************************************************************/
void SET_TIMER(struct TIMER_REG *pTM)
{
W1302(0x8e, 0x00); //写操作
DS1302_CE = 0; DELAY4;
DS1302_CK = 0; DELAY1;
DS1302_CE = 1; DELAY4;
DS1302InputByte(0xbe); // 0xbe: 时钟多字节写命令
DS1302InputByte((*pTM).Second);
DS1302InputByte((*pTM).Minute);
DS1302InputByte((*pTM).Hour);
DS1302InputByte((*pTM).Day);
DS1302InputByte((*pTM).Month);
DS1302InputByte((*pTM).Week);
DS1302InputByte((*pTM).Year);
DS1302InputByte((*pTM).Control);
DS1302_CK = 1; DELAY1;
DS1302_CE = 0; DELAY4;
}
/*******************************************************************************
* Function Name : GET_TIMER
* Description : input one byte
* Input : Uint8 DA;
* Output : None;
* Return : None;
*******************************************************************************/
void GET_TIMER(struct TIMER_REG *pTM)
{
DS1302_CE = 0; DELAY4;
DS1302_CK = 0; DELAY1;
DS1302_CE = 1; DELAY4;
DS1302InputByte(0xbf); //0xbf:时钟多字节读命令
(*pTM).Second = DS1302OutputByte();
(*pTM).Minute = DS1302OutputByte();
(*pTM).Hour = DS1302OutputByte();
(*pTM).Day = DS1302OutputByte();
(*pTM).Month = DS1302OutputByte();
(*pTM).Week = DS1302OutputByte();
(*pTM).Year = DS1302OutputByte();

DS1302_CK = 1; DELAY1;
DS1302_CE = 0; DELAY4;
}
/******************************************************************************
** End Of File
******************************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: