新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > pcf8563外部RTC驅(qū)動(dòng)程序基于STM32F10x

pcf8563外部RTC驅(qū)動(dòng)程序基于STM32F10x

作者: 時(shí)間:2016-11-26 來源:網(wǎng)絡(luò) 收藏
#ifndef uchar

#define uchar unsigned char
#endif

本文引用地址:http://m.butianyuan.cn/article/201611/321913.htm

#define SEC0x02 //秒寄存器
#define MIN0x03 //分寄存器
#define HOUR0x04 //時(shí)寄存器
#define DAY0x05 //日寄存器
#define WEEK0x06 //周寄存器
#define MONTH0x07 //月寄存器
#define YEAR0x08 //年寄存器
#define read_ADD 0xA3 //寫器件地址
#define write_ADD 0xA2 //讀器件地址


tm timer_8563;


// char ResetTime[ResetTime_Len];//定時(shí)復(fù)位時(shí)間RAM全局(星期-日-小時(shí)-分)80-80-12-00 設(shè)置為80相應(yīng)報(bào)警無效


#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器件或通訊出錯(cuò),將會(huì)退出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); //分報(bào)警無效
writeData(0x0a,0x80);//小時(shí)報(bào)警無效
writeData(0x0b,0x80);//日?qǐng)?bào)警無效
writeData(0x0c,0x80);//星期報(bào)警無效
writeData(0x01,0x00);

}



關(guān)鍵詞: pcf8563外部RTCSTM32F10

評(píng)論


技術(shù)專區(qū)

關(guān)閉