- #include "stm32f10x.h"
- #include <io.h>
- #include <delay.h>
- #include <sys.h>
- #include <usart.h>
- #include <pcf8563.h>
- #define LED1 PBout(14)// PB14
- #define LED2 PBout(15)// PE15
- u8 Time_send[5];
- void Structure_time_Array()
- {
- Datetime_Structure DateTime;
- Time_send[0]=DateTime.year;
- Time_send[0]=DateTime.month;
- Time_send[0]=DateTime.day;
- Time_send[0]=DateTime.hour;
- Time_send[0]=DateTime.min;
- Time_send[0]=DateTime.second;
- }
- u8 x;
- int main()
- {
- delay_init(); //delay 函数初始化
- uart_init(9600); //串口初始化为115200波特率
- io_init();
- IIC_Init();
- delay_ms(10);
- PCF8563_set_time(0x13,0x0b,0x0a,0x0b,0x1d,0x1e);
- while(1)
- {
-
- // Datetime_Structure rtc_get_time();
- // Structure_time_Array();
- // delay_ms(100);
- // Usart_Send_Array_tx(Time_send,5);
-
- Usart_Send(PCF8563_Read_Byte(0x02));
- delay_ms(1000);
-
- }
- }
复制代码- #include <sys.h>
- #include <pcf8563.h>
- #include <delay.h>
- #define SDA_IN() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(u32)8<<28;}
- #define SDA_OUT() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(u32)3<<28;}
- //IO操作函数
- #define IIC_SCL PBout(6) //SCL
- #define IIC_SDA PBout(7) //SDA
- #define READ_SDA PBin(7) //输入SDA
- //初始化IIC
- void IIC_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE ); //使能GPIOB时钟
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7); //PB6,PB7 输出高
- }
- //产生IIC起始信号
- void IIC_Start(void)
- {
- SDA_OUT(); //sda线输出
- IIC_SDA=1;
- IIC_SCL=1;
- delay_us(4);
- IIC_SDA=0;//START:when CLK is high,DATA change form high to low
- delay_us(4);
- IIC_SCL=0;//钳住I2C总线,准备发送或接收数据
- }
- //产生IIC停止信号
- void IIC_Stop(void)
- {
- SDA_OUT();//sda线输出
- IIC_SCL=0;
- IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
- delay_us(4);
- IIC_SCL=1;
- IIC_SDA=1;//发送I2C总线结束信号
- delay_us(4);
- }
- //等待应答信号到来
- //返回值:1,接收应答失败
- // 0,接收应答成功
- u8 IIC_Wait_Ack(void)
- {
- u8 ucErrTime=0;
- SDA_IN(); //SDA设置为输入
- IIC_SDA=1;delay_us(1);
- IIC_SCL=1;delay_us(1);
- while(READ_SDA)
- {
- ucErrTime++;
- if(ucErrTime>250)
- {
- IIC_Stop();
- return 1;
- }
- }
- IIC_SCL=0;//时钟输出0
- return 0;
- }
- //产生ACK应答
- void IIC_Ack(void)
- {
- IIC_SCL=0;
- SDA_OUT();
- IIC_SDA=0;
- delay_us(2);
- IIC_SCL=1;
- delay_us(2);
- IIC_SCL=0;
- }
- //不产生ACK应答
- void IIC_NAck(void)
- {
- IIC_SCL=0;
- SDA_OUT();
- IIC_SDA=1;
- delay_us(2);
- IIC_SCL=1;
- delay_us(2);
- IIC_SCL=0;
- }
- //IIC发送一个字节
- //返回从机有无应答
- //1,有应答
- //0,无应答
- void IIC_Send_Byte(u8 txd)
- {
- u8 t;
- SDA_OUT();
- IIC_SCL=0;//拉低时钟开始数据传输
- for(t=0;t<8;t++)
- {
-
- if((txd&0x80)>>7)
- IIC_SDA=1;
- else
- IIC_SDA=0;
- txd<<=1;
- delay_us(2); //对TEA5767这三个延时都是必须的
- IIC_SCL=1;
- delay_us(2);
- IIC_SCL=0;
- delay_us(2);
- }
- }
- //读1个字节,ack=1时,发送ACK,ack=0,发送nACK
- u8 IIC_Read_Byte(unsigned char ack)
- {
- unsigned char i,receive=0;
- SDA_IN();//SDA设置为输入
- for(i=0;i<8;i++ )
- {
- IIC_SCL=0;
- delay_us(2);
- IIC_SCL=1;
- receive<<=1;
- if(READ_SDA)receive++;
- delay_us(1);
- }
- if (!ack)
- IIC_NAck();//发送nACK
- else
- IIC_Ack(); //发送ACK
- return receive;
- }
- void PCF8563_Write_Byte(unsigned char REG_ADD, unsigned char dat)
- {
-
- IIC_Start();
- IIC_Send_Byte(PCF8563_Write);//发送写命令并检查应答位
- while(IIC_Wait_Ack());
- IIC_Send_Byte(REG_ADD);
- IIC_Wait_Ack();
- IIC_Send_Byte(dat); //发送数据
- IIC_Wait_Ack();
- IIC_Stop();
-
- }
- //读取地数据
- // reg——add 要读取的寄存器
- unsigned char PCF8563_Read_Byte(unsigned char REG_ADD)
- {
- u8 ReData;
- IIC_Start( );
- IIC_Send_Byte(PCF8563_Write);//发送写命令并检查应答位
- while(IIC_Wait_Ack( ));
- IIC_Send_Byte(REG_ADD); //确定要操作的寄存器
- IIC_Wait_Ack();
- IIC_Start(); //启动总线
- IIC_Send_Byte(PCF8563_Read); //发送读取命令
- IIC_Wait_Ack();
- ReData = IIC_Read_Byte(0); //读取数据,加发送非应答
- IIC_Stop();
- return ReData;
- }
- //函数名称:void rtc_set_time(u8 year,u8 month,u8 day,u8 hour,u8 min,u8 second)
- //功 能:设置RTC芯片PCF8563的时间
- //输 入:year month day hour min second:年月日时分秒
- //输 出:无
- void PCF8563_set_time(u8 year,u8 month,u8 day,u8 hour,u8 min,u8 second)
- {
- PCF8563_Write_Byte(TIME_SECOND,second);
- delay_us(20);
- PCF8563_Write_Byte(TIME_MINUTE,min);
- delay_us(20);
- PCF8563_Write_Byte(TIME_HOUR,hour);
- delay_us(20);
- PCF8563_Write_Byte(TIME_DAY,day);
- delay_us(20);
- PCF8563_Write_Byte(TIME_MONTH,month);
- delay_us(20);
- PCF8563_Write_Byte(TIME_YEAR,year);
- delay_us(20);
-
- }
- //函数名称:Datetime_Structure rtc_get_time(void)
- //功 能:读取RTC芯片PCF8563的时间
- //输 入:无
- //输 出:Datetime_Structure结构体变量
- Datetime_Structure rtc_get_time(void)
- {
- u32 m_unTempTime;
- Datetime_Structure getTime_Struct;
- /*get time---second****************/
- m_unTempTime = PCF8563_Read_Byte(TIME_SECOND)&0x7f;
- m_unTempTime = (m_unTempTime/16)*10+m_unTempTime%16;
- getTime_Struct.second = m_unTempTime;
- /*get time---minute****************/
- m_unTempTime = PCF8563_Read_Byte(TIME_MINUTE)&0x7f;
- m_unTempTime = (m_unTempTime/16)*10+m_unTempTime%16;
- getTime_Struct.min = m_unTempTime;
- /*get time---hour****************/
- m_unTempTime = PCF8563_Read_Byte(TIME_HOUR)&0x3f;
- m_unTempTime = (m_unTempTime/16)*10+m_unTempTime%16;
- getTime_Struct.hour = m_unTempTime;
- /*get time---day****************/
- m_unTempTime = PCF8563_Read_Byte(TIME_DAY)&0x3f;
- m_unTempTime = (m_unTempTime/16)*10+m_unTempTime%16;
- getTime_Struct.day = m_unTempTime;
- /*get time---month****************/
- m_unTempTime = PCF8563_Read_Byte(TIME_MONTH)&0x1f;
- m_unTempTime = (m_unTempTime/16)*10+m_unTempTime%16;
- getTime_Struct.month = m_unTempTime;
- /*get time---year****************/
- m_unTempTime = PCF8563_Read_Byte(TIME_YEAR)&0xff;
- m_unTempTime = (m_unTempTime/16)*10+m_unTempTime%16;
- getTime_Struct.year = m_unTempTime;
- return getTime_Struct;
- }
复制代码 全部资料51hei下载地址:
库函数i2c_pcf8563.7z
(186.05 KB, 下载次数: 74)
|