您的位置 首页 传感器

pcf8563外部RTC驱动程序根据STM32F10x

ifndefuchardefineucharunsignedcharendifdefineSEC0x02秒寄存器defineMIN0x03分寄存器defineHOUR0x04时

#ifndef uchar

#define uchar unsigned char
#endif

#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);

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/chuanganqi/258418.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部