当前位置: 代码迷 >> 单片机 >> 时钟芯片RTC4543终于调试成功,分享给大家,希望有用到的人少走弯路吧,该如何解决
  详细解决方案

时钟芯片RTC4543终于调试成功,分享给大家,希望有用到的人少走弯路吧,该如何解决

热度:153   发布时间:2016-04-28 16:33:03.0
时钟芯片RTC4543终于调试成功,分享给大家,希望有用到的人少走弯路吧
闲话不多说,先贴代码
common.h
#ifndef __COMMON_H__
#define __COMMON_H__

typedef unsigned char uint8;
typedef signed char int8;
typedef unsigned int uint16;
typedef signed int int16;
typedef unsigned long uint32;
typedef signed long int32;
typedef float fp32;
#endif


RTC4543.h
#ifndef _RTC4543DRIVER_H_
#define _RTC4543DRIVER_H_

#include "common.h"

/****************************************************************************
* 名 称:RTC_Delay
* 功 能:延时
* 入口参数:延时时间
* 出口参数:无
* 注 意:
****************************************************************************/
static void RTC_Delay(uint8 rtcdelaytime);
/****************************************************************************
* 名 称:RTC_Write
* 功 能:对RTC4543进行写入
* 入口参数:dat写入的数据,nbit写的位数,一般为8或4
* 出口参数:无
* 注 意:先写低位
****************************************************************************/
static void RTC_Write(uint8 dat,uint8 nbit);
/****************************************************************************
* 名 称:RTC_Read
* 功 能:对RTC4543进行读出
* 入口参数:nbit读的位数,一般为8或4
* 出口参数:无
* 注 意:先读低位
****************************************************************************/
static uint8 RTC_Read(uint8 nbit);
/****************************************************************************
* 名 称:Set_time()
* 功 能:RTC设置时间
* 入口参数:*P设置芯片 的时间
* 出口参数:无
* 注 意:设置的日期时间为曼彻斯特码,即BCD码,高4位代表10位,低4位代表个位
****************************************************************************/
void RTC_Settime(uint8 *p);
/****************************************************************************
* 名 称:Read_time()
* 功 能:RTC读取时间
* 入口参数:无
* 出口参数:*time
* 注 意:读取的日期时间为曼彻斯特码,即BCD码,高4位代表10位,低4位代表个位
****************************************************************************/
uint8* RTC_Readtime(void);
#endif

RTC4543.c
#include "RTC4543.h"
#include "STC12C5A60S2.h"
enum{
SETUP = 1,
CLR = 0,
};
sbit RTC_CE = P1^1;//RTC4543控制线
sbit RTC_CLK = P1^2;
sbit RTC_DATA = P1^3;
sbit RTC_WR = P1^4;
/****************************************************************************
* 名 称:RTC_Delay
* 功 能:延时
* 入口参数:延时时间
* 出口参数:无
* 注 意:
****************************************************************************/
static void RTC_Delay(uint8 rtcdelaytime)
{
uint8 i;
for(;rtcdelaytime > 0; rtcdelaytime--)
for(i = 10;i>0;i--);
}
/****************************************************************************
* 名 称:RTC_Write
* 功 能:对RTC4543进行写入
* 入口参数:dat写入的数据,nbit写的位数,一般为8或4
* 出口参数:无
* 注 意:先写低位
****************************************************************************/
static void RTC_Write(uint8 dat,uint8 nbit)
{
for(;nbit > 0; nbit--)
{
RTC_CLK = CLR;
if((dat&0x01) == 1)
{
RTC_DATA = 1;
}
else
{
RTC_DATA = 0;
}
RTC_Delay(1);
RTC_CLK = SETUP;
RTC_Delay(1);
dat >>= 1;
}
}
/****************************************************************************
* 名 称:RTC_Read
* 功 能:对RTC4543进行读出
* 入口参数:nbit读的位数,一般为8或4
* 出口参数:无
* 注 意:先读低位
****************************************************************************/
static uint8 RTC_Read(uint8 nbit)
{
uint8 dat =0;
for(;nbit > 0; nbit--)
{
RTC_CLK = CLR;
if(RTC_DATA == 1)
{
dat |= 0x80;
}
else
{
dat |= 0x00;
}
dat >>= 1;
RTC_CLK = SETUP;
RTC_Delay(1);

}