?? rtc.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 + -