⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtc.c

📁 RTC 1302 module c file
💻 C
字号:
/*
**********************************************************************************************
* File:		RTC.c
* Contents:
*           The RTC 1302 module c file
*           use GPIO to control Rtc_IO and Rtc_SCLK, Rtc_CE communicate with DS1302
*
* Copyright (c) 2006 Fameg, Inc. All rights reserved
***********************************************************************************************
*/


#include "common.h"


//Pin operation
#if 0
void  WritePinSCLK(){Rtc_SCLK=1;	}

void ClearPinSCLK(){Rtc_SCLK=0;}

void  setIOin(){}
void  setIOout(){}

void WritePinIO(){Rtc_IO=1;}
void ClearPinIO(){Rtc_IO=0;}

BOOL ReadPinIO(){return(Rtc_IO);}
//Delay
void Delay_0_5us(unsigned char cnt)
{
	//  unsigned char ct=1;
	while(cnt--)
	{
		//    	while(ct--);
		//	asm("nop");
		//	ct=1;
	}
}

#else

#define   WritePinCE() Rtc_CE=1
#define   ClearPinCE() Rtc_CE=0

#define   WritePinSCLK() Rtc_SCLK=1
#define   ClearPinSCLK() Rtc_SCLK=0

#define    setIOin() /##/
#define    setIOout()/##/

#define   WritePinIO() Rtc_IO=1
#define   ClearPinIO() Rtc_IO=0

#define   ReadPinIO()  Rtc_IO

//Delay
//BYTE delay;
//	#define Delay_0_5us(cnt)	//{/*delay =cnt ;while(delay--);*/} /##/
void Delay_0_5us(unsigned char cnt)
{
	//  unsigned char ct=1;
	while(cnt--)
	{
		//   	while(ct--);
		//	asm("nop");
		//	ct=1;
	}
}

#endif


void Rtc_Init()
{
	ClearPinSCLK();
	ClearPinCE();  /*发送结束条件的数据信号*/
}

/*******************************************************************
起动总线函数
函数原型: void  Rtc_Start();
功能:    启动总线,即发送起始条件.

********************************************************************/
static void Rtc_Start()
{
	ClearPinSCLK();
	Delay_0_5us(1);
	WritePinCE();
	Delay_0_5us(2);
}


/*******************************************************************
结束总线函数
函数原型: void  Rtc_Stop();
功能:       结束总线,即发送结束条件.

********************************************************************/
static void Rtc_Stop()
{
	WritePinSCLK();
	Delay_0_5us(1);
	ClearPinCE();  /*发送结束条件的数据信号*/
}


/*******************************************************************
字节数据传送函数
函数原型: void  Rtc_SendByte(BYTE c);
功能:  将数据c发送出去
********************************************************************/
static void Rtc_SendByte(BYTE c)
{
	BYTE mask;

	setIOout();
	for(mask=0x01;mask!=0;mask=mask<<1)  /*要传送的数据长度为8位*/
	{
		if(c&mask)
		WritePinIO();   /*判断发送位*/
		else
		ClearPinIO();

		Delay_0_5us(1);
		Delay_0_5us(1);

		WritePinSCLK();/*置时钟线为高,通知被控器开始接收数据位*/

		Delay_0_5us(1);
		Delay_0_5us(1);
		ClearPinSCLK();
	}
}


/*******************************************************************
字节数据传送函数
函数原型: BYTE  Rtc_RcvByte();
功能:  用来接收从器件传来的数据
********************************************************************/
static BYTE  Rtc_RcvByte()
{
	BYTE retc;
	BYTE mask;
	BOOL t;

	retc=0;

	setIOin();
	for(mask=0;mask<8;mask++)
	{
		Delay_0_5us(1);

		t=ReadPinIO();
		retc=retc>>1;

		if(t==1)
		retc=retc+0x80; /*读数据位,接收的数据位放入retc中 */

		Delay_0_5us(1);


		WritePinSCLK();       /*置时钟线为高使数据线上数据有效*/
		setIOout();
		WritePinIO();

		Delay_0_5us(1);
		Delay_0_5us(1);

		ClearPinSCLK();       /*置时钟线为低,准备接收数据位*/

	}
	return(retc);
}


/*******************************************************************
发送多字节数据函数
函数原型: BOOL  Rtc_SendStr(BYTE cmd,BYTE *s,WORD no);
功能:     从启动总线到命令,数据,结束总线的全过程,从器件
命令cmd,发送内容是s指向的内容,发送len个字节。
注意:    使用前必须已结束总线。
********************************************************************/
void Rtc_SendStr(BYTE cmd,BYTE *s,BYTE len)
{
	BYTE i;

	Rtc_Start();               /*启动总线*/
	Rtc_SendByte(cmd);            /*发送器件命令*/

	for(i=0;i<len;i++)
	{
		Rtc_SendByte(*s);               /*发送数据*/
		s++;
	}
	Rtc_Stop();                 /*结束总线*/
	return;
}


/*******************************************************************
有子地址器件读取多字节数据函数
函数原型: BOOL  Rtc_RcvStr(BYTE cmd,BYTE *s,WORD Len);
功能:     从启动总线到发送地址,子地址,读数据,结束总线的全过程,从器件
命令cmd,读出的内容放入s指向的存储区,读Len个字节。
如果返回1表示操作成功,否则操作有误。
注意:    使用前必须已结束总线。
********************************************************************/
void Rtc_RcvStr(BYTE cmd,BYTE *s,BYTE len)
{
	BYTE i;

	Rtc_Start();               /*启动总线*/
	Rtc_SendByte(cmd);


	for(i=0;i<len;i++)
	{
		*s=Rtc_RcvByte();               /*读取数据*/
		s++;
	}
	Rtc_Stop();                    /*结束总线*/

	return;
}

#define Burst_r  0xBF
#define Burst_w  0xBE

#define WP_r  0x8F
#define WP_w  0x8E
#define bmWp  0x80

#define Year_r  0x8D
#define Year_w  0x8C

#define Weekday_r  0x8B
#define Weekday_w  0x8A

#define Month_r  0x89
#define Month_w  0x88

#define day_r  0x87
#define day_w  0x86

#define Hour_r  0x85
#define Hour_w  0x84

#define Minu_r  0x83
#define Minu_w  0x82

#define Sec_r  0x81
#define Sec_w  0x80
#define bmCH 0x80        //clock halt

void RD_RTC(struct tagtime * time)
{
	BYTE wp=(~bmWp);
	Rtc_SendStr(WP_w,&wp,1);
	Rtc_RcvStr(WP_r,&wp,1);

	Rtc_RcvStr(Burst_r,(BYTE *)time,8);
	time->b12hours= (time->hour&0x80)?1:0;
	if(time->b12hours)
	{
		time->bAfterNoon= (time->hour&0x20)?1:0;
		time->hour=time->hour&0x3f;
	}
	else
	{
		time->hour=time->hour&0x1f;
		time->bAfterNoon= time->hour>0x12;
	}

	wp=bmWp;
	Rtc_SendStr(WP_w,&wp,1);
	Rtc_RcvStr(WP_r,&wp,1);
}


void WR_RTC(struct tagtime * time)
{
	BYTE wp=~bmWp;
	Rtc_SendStr(WP_w,&wp,1);
	Rtc_RcvStr(WP_r,&wp,1);

	time->sec&=(~bmCH);

	if(time->b12hours)
	{
		time->hour=time->hour&0x3f;
		time->hour|=0x80;
		if(time->bAfterNoon)
		time->hour|=0x20;
	}
	else
	{
		time->hour&=0x1f;
	}

	Rtc_SendStr(Burst_w,(BYTE *)time,8);

	wp=bmWp;
	Rtc_SendStr(WP_w,&wp,1);
	Rtc_RcvStr(WP_r,&wp,1);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -