pcf8563 external RTC driver based on STM32F10x

Publisher:cxx7848653Latest update time:2015-10-08 Source: eefocusKeywords:pcf8563 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#ifndef fly

#define uchar unsigned char
#endif

#define SEC     0x02 //Second register
#define MIN     0x03 //Minute register
#define HOUR    0x04 //Hour register
#define DAY     0x05 //Day register
#define WEEK    0x06 //Week register
#define MONTH   0x07 //Month register
#define YEAR    0x08 //Year register #
define read_ADD 0xA3 //Write device address
#define write_ADD 0xA2 //Read device address


tm timer_8563;


// char ResetTime[ResetTime_Len];   //Timer reset time RAM global (week-day-hour-minute) 80-80-12-00 Set to 80 and the corresponding alarm will be invalid


#define PCF8563_SCK_High()  {   GPIO_InitTypeDef GPIO_InitStructure;
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD  ;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
                GPIO_Init(GPIOD, &GPIO_InitStructure);
                GPIO_SetBits(GPIOD, GPIO_Pin_11);}
#define PCF8563_SCK_Low()   {   GPIO_InitTypeDef GPIO_InitStructure;
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD  ;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
                GPIO_Init(GPIOD, &GPIO_InitStructure);
                GPIO_ResetBits(GPIOD, GPIO_Pin_11);}

#define PCF8563_DATA_High() {   GPIO_InitTypeDef GPIO_InitStructure;
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD  ;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
                GPIO_Init(GPIOD, &GPIO_InitStructure);
                GPIO_SetBits(GPIOD, GPIO_Pin_10);}
#define PCF8563_DATA_Low()  {   GPIO_InitTypeDef GPIO_InitStructure;
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD  ;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
                GPIO_Init(GPIOD, &GPIO_InitStructure);
                GPIO_ResetBits(GPIOD, GPIO_Pin_10);}
#define PCF8563_DATA_In()   {   GPIO_InitTypeDef GPIO_InitStructure;
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD ;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
                GPIO_Init(GPIOD, &GPIO_InitStructure);}
#define PCF8563_DATA_Sta()  (   GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_10))


void Delay_8563()
{
int i=100;
while(i--);
}

void Start()
{
PCF8563_DATA_High();
PCF8563_SCK_High();
Delay_8563();
PCF8563_DATA_Low();
Delay_8563();
PCF8563_SCK_Low();
}

void Stop()
{
PCF8563_DATA_Low();
PCF8563_SCK_Low();
Delay_8563();
PCF8563_SCK_High();
Delay_8563();
PCF8563_DATA_High();
Delay_8563();
}

void WriteACK(uchar ack)
{
if(ack)
{
PCF8563_SCK_High();
}
else
{
PCF8563_SCK_Low();
}
Delay_8563();
PCF8563_SCK_High();
Delay_8563();
PCF8563_SCK_Low();
}

void WaitACK()

uchar errtime=20;
PCF8563_DATA_High();
Delay_8563();
PCF8563_SCK_High();
Delay_8563();
PCF8563_DATA_In();
while(PCF8563_DATA_Sta())
{
errtime--;
if(!errtime) Stop();
}
PCF8563_SCK_Low();
Delay_8563();
}

void writebyte(uchar wdata)
{
uchar i;
for(i=0;i<8;i++)
{
if(wdata&0x80)
{
PCF8563_DATA_High();
}
else
{
PCF8563_DATA_Low();
}
wdata<<=1;
PCF8563_SCK_High();
Delay_8563();
PCF8563_SCK_Low();
}
WaitACK();     //I2C器件或通讯出错,将会退出I2C通讯
}

uchar Readbyte()
{
uchar i,bytedata;
PCF8563_DATA_High();
for(i=0;i<8;i++)
{
PCF8563_SCK_High();
bytedata<<=1;
PCF8563_DATA_In();
if(PCF8563_DATA_Sta())
bytedata|=0x01;
else
bytedata|=0x00;
PCF8563_SCK_Low();
Delay_8563();
}
return(bytedata);
}

void writeData(uchar address,uchar mdata)
{
Start();
writebyte(0xa2);
writebyte(address);
writebyte(mdata);
Stop();
}

uchar ReadData(uchar address)
{
uchar rdata;
Start();
writebyte(0xa2);
writebyte(address);
Start();
writebyte(0xa3);
rdata=Readbyte();
WriteACK(1);
Stop();
return(rdata);
}
void ReadData1(uchar address,uchar count,uchar * buff)
{
uchar i;
Start();
writebyte(0xa2);
writebyte(address);
Start();
writebyte(0xa3);
for(i=0;i
{
buff[i]=Readbyte();
if(i
}
WriteACK(1);
Stop();
}



void P8563_set_time(u8 year,u8 month,u8 day,u8 hour,u8 min,u8 sec,u8 week)
{
  Start();//Initialize
 
  writeData(YEAR,year);
  writeData(MONTH,month);
  writeData(DAY, day);
  writeData(HOUR,hour);
  writeData(MIN,min);
  writeData(SEC,sec);
  writeData(WEEK,week);
}

void P8563_get_time()
{
  timer_8563.w_year = ReadData(YEAR);
  timer_8563.w_month = ReadData(MONTH);
  timer_8563.w_date = ReadData(DAY);
  timer_8563.hour = ReadData(HOUR);
  timer_8563.min = ReadData(MIN);
  timer_8563.sec = ReadData(SEC);
  timer_8563.week = ReadData(WEEK);
}

void P8563_init(void)
{
  writeData(0x0,0x00);
  writeData(0x09,0x80); //Minute alarm is invalid
  writeData(0x0a,0x80); //Hour alarm is invalid
  writeData(0x0b,0x80); //Daily alarm is invalid
  writeData( 0x0c,0x80); //week alarm is invalid
  writeData(0x01,0x00);

}

Keywords:pcf8563 Reference address:pcf8563 external RTC driver based on STM32F10x

Previous article:SST25VF016B serial flash driver based on STM32F10x
Next article:Porting fatfs file system to STM32F10x

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号