新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > pcf8563外部RTC驱动程序基于STM32F10x

pcf8563外部RTC驱动程序基于STM32F10x

作者:时间:2016-11-26来源:网络收藏
#ifndef uchar

#define uchar unsigned char
#endif

本文引用地址:http://www.eepw.com.cn/article/201611/321913.htm

#define SEC0x02 //秒寄存器
#define MIN0x03 //分寄存器
#define HOUR0x04 //时寄存器
#define DAY0x05 //日寄存器
#define WEEK0x06 //周寄存器
#define MONTH0x07 //月寄存器
#define YEAR0x08 //年寄存器
#define read_ADD 0xA3 //写器件地址
#define write_ADD 0xA2 //读器件地址


tm timer_8563;


// char ResetTime[ResetTime_Len];//定时复位时间RAM全局(星期-日-小时-分)80-80-12-00 设置为80相应报警无效


#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();//初始化

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); //分报警无效
writeData(0x0a,0x80);//小时报警无效
writeData(0x0b,0x80);//日报警无效
writeData(0x0c,0x80);//星期报警无效
writeData(0x01,0x00);

}



评论


技术专区

关闭