您的当前位置:首页正文

PCF8563程序

来源:独旅网
#include

#ifndef _PCF8653_DRIVER #define _pcf8653_DRIVER

#define SCL_SETB P1_1=1 #define SCL_CLR P1_1=0 #define SDA_SETB P1_0=1 #define SDA_CLR P1_0=0 #define PCF8563_SLA 0xA2

unsigned char init_time[7]={0,0,10,11,3,4,7};

//*名称: unsigned char bcd_convert_hex(unsigned char bcd_data)

//*功能: BCD码转16进制 //*参数: bcd_data 要转换的BCD码数据(0-100) //*返回: 转换后的16进制数据 unsigned char bcd_convert_hex(unsigned char bcd_data) { unsigned char temp; temp=((bcd_data>>4)*10)+(bcd_data&0x0F); return temp; }

//*名称: unsigned char hex_convert_bcd(unsigned char hex_dataa) //*功能: 16进制转BCD码 //*参数: hex_data 要转换的16进制数据(0-100) //*返回: 转换后的BCD码数据 unsigned char hex_convert_bcd(unsigned char hex_data) { unsigned char temp; unsigned char bcd_data; temp=hex_data%100; bcd_data=(temp/10<<4)|(temp%10); return bcd_data;

}

//*名称: void pcf8563_star(void) //*功能: 启动PCF8563芯片运行 void pcf8563_star(void) { i2c_write_byte(PCF8563_SLA,0x00,0x00); }

//*名称: void pcf8563_get_time(unsigned char *ptr) //*功能: 从RTC芯片获取时钟数据 并存放到指定数组 void pcf8563_get_time(unsigned char *ptr) { i2c_read_nbyte(PCF8563_SLA,0x02,ptr,7); ptr[0]=bcd_convert_hex(ptr[0]&0x7F); ptr[1]=bcd_convert_hex(ptr[1]&0x7F); ptr[2]=bcd_convert_hex(ptr[2]&0x3F); ptr[3]=bcd_convert_hex(ptr[3]&0x3F); ptr[4]=ptr[4]; ptr[5]=bcd_convert_hex(ptr[5]&0x1F); ptr[6]=bcd_convert_hex(ptr[6]); // }

//**名称: pcf8563_rewirte_time(unsigned char *ptr) 重写入PCF8563片 //*参数: *ptr 时钟数据数组 void pcf8563_rewirte_time(unsigned char *ptr) { unsigned char i; for(i=0;i<7;i++) { ptr[i]=hex_convert_bcd(ptr[i]); } i2c_write_byte(PCF8563_SLA,0x00,0x20); 钟芯停止运行 i2c_write_nbyte(PCF8563_SLA,0x02,ptr,7);

// 秒

// 分 // 小时 // 日

// 月 年

/*功能: 将时钟数据

// PCF8563时

//

将设置时间写入

PCF8563 i2c_write_byte(PCF8563_SLA,0x00,0x00); // PCF8563时钟芯开始运行 }

//***名称: void pcf8563_init(void) 功能: 如果时钟数据不对则写入初始值void pcf8563_init(void) { unsigned char time_temp[7]; pcf8563_get_time(time_temp); if(time_temp[0]>=60||time_temp[1]>=60||time_temp[2]>=24||time_temp[3]>=32 ||time_temp[5]>=13||time_temp[6]>=100) { pcf8563_rewirte_time(init_time); } } #endif

示例2

sbit SDA=P3^4; sbit SCL=P3^5; sbit P10=P1^0;

extern uchar g8563_Store[4]; /*时间交换区,全局变量声明*/

uchar code c8563_Store[4]={0x00,0x59,0x07,0x01}; /*写入时间初值:星期一07:59:00*/

/*内部函数,延时1 void Delay() {

// {P10=1;_nop_();P10=0;} _nop_();

_nop_(); /*根据晶振频率制定延时时间*/ }

/*内部函数,I2C开始 void Start() { EA=0; SDA=1; SCL=1; Delay(); SDA=0; Delay(); SCL=0; }

/*内部函数,I2C结束 void Stop() {

SDA=0; SCL=0; Delay(); SCL=1; Delay(); SDA=1; Delay(); EA=1; }

/*内部函数,输出ACK=0 void WriteACK() {

SDA=0; Delay(); SCL=1; Delay(); SCL=0; }

/*内部函数,输出ACK=1 void writenoack() {

SDA=1; Delay(); SCL=1; Delay(); SCL=0; }

/*内部函数,等待ACK void WaitACK()

{ uchar errtime=20; SDA=1;

Delay(); /*读ACK*/ SCL=1; Delay(); while(SDA) { errtime--;

if(!errtime) Stop(); }

SCL=0; Delay(); }

/**内部函数.输出数据字节 入口:B=数据void writebyte(uchar wdata) {

uchar i;

for(i=0;i<8;i++) {

if(wdata&0x80) SDA=1; else SDA=0; wdata<<=1; SCL=1; Delay(); SCL=0; }

WaitACK(); }

/*内部函数.输入数据 出口:B uchar Readbyte() {

uchar i,bytedata; SDA=1;

for(i=0;i<8;i++) {

SCL=1;

bytedata<<=1; bytedata|=SDA; SCL=0; Delay(); }

return(bytedata); }

/**输出数据->pcf8563

void writeData(uchar address,uchar mdata) {

Start();

writebyte(0xa2); /*写命令*/ writebyte(address); /*写地址*/ writebyte(mdata); /*写数据*/ Stop(); }

/*输入数据<-pcf8563

uchar ReadData(uchar address) { uchar rdata; Start();

writebyte(0xa2); /*写命令*/ writebyte(address); /*写地址*/

/*单字节*/ Start();

writebyte(0xa3); /*读命令*/ rdata=Readbyte(); writenoack(); Stop();

return(rdata); }

void ReadData1(uchar address,uchar count,uchar * buff) /*多字节*/ P8563_Read();

if(g8563_Store[0]==0)

P8563_Read(); /*如果为秒=0,为防止时间变化,再读一次*/ }

/**写时间修改值

void P8563_settime() {

uchar i;

{ uchar i; Start();

writebyte(0xa2); /*写命令*/ writebyte(address); /*写地址*/ Start();

writebyte(0xa3); /*读命令*/ for(i=0;ibuff[i]=Readbyte();

if(iwritenoack(); Stop(); }

/*内部函数,读入时间到内部缓冲区 void P8563_Read() { uchar time[7];

ReadData1(0x02,0x07,time);

g8563_Store[0]=time[0]&0x7f; /*秒*/ g8563_Store[1]=time[1]&0x7f; /*分*/ g8563_Store[2]=time[2]&0x3f; /*小时*/ g8563_Store[3]=time[4]&0x07; /*星期*/ }

/*读入时间到内部缓冲区----外部调用 void P8563_gettime() {

for(i=2;i<=4;i++) { writeData(i,g8563_Store[i-2]); } writeData(6,g8563_Store[3]); }

/**P8563的初始化-----外部调用 void P8563_init() {

uchar i;

if((ReadData(0xa)&0x3f)!=0x8) /*检查是否第一次启动,是则初始化时间*/ {

for(i=0;i<=3;i++) g8563_Store[i]=c8563_Store[i]; /*初始化时间*/ P8563_settime(); writeData(0x0,0x00);

writeData(0xa,0x8); /*8:00报警*/ writeData(0x1,0x12); /*报警有效*/ writeData(0xd,0xf0); } }

因篇幅问题不能全部显示,请点此查看更多更全内容