您好,欢迎访问三七文档
当前位置:首页 > 高等教育 > 实验设计 > STM32对时钟芯片PCF8563的编程
这是用STM32对PCF8563编程的程序、模拟IIC,经验证可用~移植用到的文件:IIC.hIIC.cPCF8563.hPCF8563.c,我把它们全都复制到了这个Word文档里。使用要初始化的函数:voidPCF8563_Init(void).就唯一这个。全局变量:u8PCF8563_Time[7];程序初始的值是给PCF8563设置的时间,程序中读取返回的值是PCF8563当时的时间。我这里使用的IIC的SCL,SDA的引脚分别为PA.0PA.1,具体看IIC.h和IIC.C。只要修改好两个引脚的初始化配套你手上的板子这,就能成功移植~Delay_nus(20):这是一个延迟20us的函数。IIC.h:#ifndef_IIC_H_#define_IIC_H_#includestm32f10x.h/*Privatetypedef-----------------------------------------------------------*//*Privatedefine------------------------------------------------------------*//*#defineI2C_Speed100000#defineI2C1_SLAVE_ADDRESS70xA0#defineI2C_PageSize256*///SCLPA0//SDAPA1#defineSCL_H()GPIO_SetBits(GPIOA,GPIO_Pin_0)#defineSCL_L()GPIO_ResetBits(GPIOA,GPIO_Pin_0)#defineSDA_H()GPIO_SetBits(GPIOA,GPIO_Pin_1)#defineSDA_L()GPIO_ResetBits(GPIOA,GPIO_Pin_1)//返回0,和1#defineRead_SDA()GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)u8IIC_ReadByte(void);voidIIC_WriteByte(u8byte);voidIIC_WaitAck(void);voidIIC_Stop(void);voidIIC_Start(void);voidIIC_Init(void);voidI2C_Ack(void);voidI2C_NoAck(void);//取回八个字节的数据:秒,分,时,天,星期,月份,年份。#endifIIC.c:#includeIIC.h#includedelay.h/**/voidIIC_Init(void){GPIO_InitTypeDefGPIO_InitStructure;/*ConfigureI2C1pins:SCLandSDA*/RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;//开漏输出,可以在不用改变成输入的情况下读取IO的电平GPIO_Init(GPIOA,&GPIO_InitStructure);SCL_H();//拉高SDA_H();}voidIIC_Start(void){/*SDA_H();SCL_H();delay_nus(20);SDA_L();delay_nus(20);*/SDA_H();SCL_H();delay_nus(20);SDA_L();delay_nus(20);SDA_L();delay_nus(20);}voidIIC_Stop(void){SCL_L();//1delay_nus(20);//2SDA_L();//3.1,2,3这三行不可缺少delay_nus(20);SCL_H();delay_nus(20);SDA_H();delay_nus(20);}voidIIC_WaitAck(void){u16k;SCL_L();SDA_H();delay_nus(20);SCL_H();k=0;while((Read_SDA()!=0)&&(k60000))k++;delay_nus(20);SCL_L();delay_nus(20);}voidIIC_WriteByte(u8byte){u8i=0;for(i=0;i8;i++){SCL_L();delay_nus(20);if(byte&0x80){SDA_H();}else{SDA_L();}delay_nus(20);SCL_H();delay_nus(20);byte=1;}SCL_L();delay_nus(20);}u8IIC_ReadByte(void){u8i,ReadByte;SDA_H();for(i=0;i8;i++){ReadByte=1;SCL_L();delay_nus(20);SCL_H();delay_nus(20);if(Read_SDA()){ReadByte|=0x01;}else{ReadByte&=~(0x01);}}returnReadByte;}voidI2C_Ack(void){SCL_L();delay_nus(20);SDA_L();delay_nus(20);SCL_H();delay_nus(20);SCL_L();delay_nus(20);}voidI2C_NoAck(void){SCL_L();delay_nus(20);SDA_H();delay_nus(20);SCL_H();delay_nus(20);SCL_L();delay_nus(20);}PCF8563.h:#ifndef_PCF8563_H_#define_PCF8563_H_#includeIIC.h#defineReadCode0xa3#defineWriteCode0xa2voidPCF8563_Init(void);u8PCF8563_ReaDAdress(u8Adress);voidPCF8563_WriteAdress(u8Adress,u8DataTX);//取回7个字节的数据:秒,分,时,天,星期,月份,年份。//全局变量externu8PCF8563_Time[7];voidPCF8563_ReadTimes(void);//在CLKOUT上定时1S输出一个下降沿脉冲voidPCF8563_CLKOUT_1s(void);#endifPCF8563.c:#includePCF8563.h//全局变量,程序初始的值就是要初始化的时间,//用途:1:初始化时间。2:读取返回时间//秒,分,时,天,星期,月份,年份。u8PCF8563_Time[7]={50,59,23,31,6,12,12};voidPCF8563_Init(void){IIC_Init();//十进制码转换成BCD码PCF8563_Time[0]=((PCF8563_Time[0]/10)4)|(PCF8563_Time[0]%10);PCF8563_Time[1]=((PCF8563_Time[1]/10)4)|(PCF8563_Time[1]%10);PCF8563_Time[2]=((PCF8563_Time[2]/10)4)|(PCF8563_Time[2]%10);PCF8563_Time[3]=((PCF8563_Time[3]/10)4)|(PCF8563_Time[3]%10);//PCF8563_Time[4]=((PCF8563_Time[4]/104))|(PCF8563_Time[4]%10);//星期不用转换PCF8563_Time[5]=((PCF8563_Time[5]/104))|(PCF8563_Time[5]%10);PCF8563_Time[6]=((PCF8563_Time[6]/104))|(PCF8563_Time[6]%10);PCF8563_CLKOUT_1s();PCF8563_WriteAdress(0x00,0x20);//禁止RTCsourceclock//初始化PCF8563的时间PCF8563_WriteAdress(0x02,PCF8563_Time[0]);PCF8563_WriteAdress(0x03,PCF8563_Time[1]);PCF8563_WriteAdress(0x04,PCF8563_Time[2]);PCF8563_WriteAdress(0x05,PCF8563_Time[3]);PCF8563_WriteAdress(0x06,PCF8563_Time[4]);PCF8563_WriteAdress(0x07,PCF8563_Time[5]);PCF8563_WriteAdress(0x08,PCF8563_Time[6]);PCF8563_WriteAdress(0x00,0x00);//EnableRTCsorceclock}u8PCF8563_ReaDAdress(u8Adress){u8ReadData;IIC_Start();IIC_WriteByte(0xa2);IIC_WaitAck();IIC_WriteByte(Adress);IIC_WaitAck();IIC_Start();IIC_WriteByte(0xa3);IIC_WaitAck();ReadData=IIC_ReadByte();IIC_Stop();returnReadData;}voidPCF8563_WriteAdress(u8Adress,u8DataTX){IIC_Start();IIC_WriteByte(0xa2);IIC_WaitAck();IIC_WriteByte(Adress);IIC_WaitAck();IIC_WriteByte(DataTX);IIC_WaitAck();IIC_Stop();}//取回八个字节的数据:秒,分,时,天,星期,月份,年份。voidPCF8563_ReadTimes(void){IIC_Start();IIC_WriteByte(0xa2);IIC_WaitAck();IIC_WriteByte(0x02);IIC_WaitAck();IIC_Start();IIC_WriteByte(0xa3);IIC_WaitAck();PCF8563_Time[0]=IIC_ReadByte()&0x7f;I2C_Ack();PCF8563_Time[1]=IIC_ReadByte()&0x7f;I2C_Ack();PCF8563_Time[2]=IIC_ReadByte()&0x3f;I2C_Ack();PCF8563_Time[3]=IIC_ReadByte()&0x3f;I2C_Ack();PCF8563_Time[4]=IIC_ReadByte()&0x07;I2C_Ack();PCF8563_Time[5]=IIC_ReadByte()&0x1f;I2C_Ack();PCF8563_Time[6]=IIC_ReadByte();I2C_NoAck();IIC_Stop();PCF8563_Time[0]=((PCF8563_Time[0]&0xf0)4)*10+(PCF8563_Time[0]&0x0f);PCF8563_Time[1]=((PCF8563_Time[1]&0xf0)4)*10+(PCF8563_Time[1]&0x0f);PCF8563_Time[2]=((PCF8563_Time[2]&0xf0)4)*10+(PCF8563_Time[2]&0x0f);PCF8563_Time[3]=((PCF8563_Tim
本文标题:STM32对时钟芯片PCF8563的编程
链接地址:https://www.777doc.com/doc-6360112 .html