原创 Freescale MCF52211读写pcf8563的程序

2009-6-15 16:08 2762 5 5 分类: MCU/ 嵌入式

继续把mcf52211读写pcf8563这个rtc的代码贴上来。mcf52211是飞思卡尔公司coldfire V2核心系列处理器的一款,主要用在手持设备,支持USB OTG,飞思卡尔的片子的可靠性和稳定性是值得信任的,绝非一般的ARM能比。






/**************************************


** File: pcf8563.h


** pcf8563 RTC driver header file


************************************/



#ifndef _LZP_MCF52211_PCF8563_H_
#define _LZP_MCF52211_PCF8563_H_
#define WRPCF8563  0xA2
#define RDPCF8563  0xA3


unsigned char pcf8563_write_byte(unsigned char addr,unsigned char dat);
unsigned char pcf8563_read_byte(unsigned char addr, unsigned char *buf);
void pcf8563_init();
unsigned char pcf8563_read_time(unsigned char *time);
unsigned char get_cmos_time(unsigned char *time,unsigned char flag);
unsigned char set_cmos_time(unsigned char *time);
#endif



/*********************************************


** File: pcf8563.c


**  PCF8563 RTC driver code


****************************************/


#include "i2c_driver.h"
#include "pcf8563.h"


unsigned char pcf8563_write_byte(unsigned char addr,unsigned char dat)
{
 i2c_start();
 if(i2c_send_byte(WRPCF8563))
 {
  i2c_stop();
  return 1;
 }
 if(i2c_send_byte(addr))
 {
  i2c_stop();
  return 1;
 }
 if(i2c_send_byte(dat))
 {
  i2c_stop();
  return 1;
 }
 i2c_stop();
 return 0;//success
}


unsigned char pcf8563_read_byte(unsigned char addr, unsigned char *buf)
{
 i2c_start();
 if(i2c_send_byte(WRPCF8563))
 {
  i2c_stop();
  return 1;
 }
 if(i2c_send_byte(addr))
 {
  i2c_stop();
  return 1;
 }
 
 i2c_restart();
 if(i2c_send_byte(RDPCF8563))
 {
  i2c_stop();
  return 1;
 }
 
 *buf=i2c_recv_byte(1);
 i2c_stop();
 return 0;//success
}



void pcf8563_init()
{
 unsigned char i;
 
 pcf8563_write_byte(0x00,0);
 pcf8563_write_byte(0x01,0); 
 pcf8563_read_byte(0x07,&i);
 i&=0x7f;
 pcf8563_write_byte(0x07,i);
 pcf8563_write_byte(0x0d,0x03);
 pcf8563_write_byte(0x0e,0x03);
}


unsigned char pcf8563_read_time(unsigned char *time)
{
 unsigned char i,ret=0;
 for(i=0;i<7;i++)
 {
     ret=pcf8563_read_byte(0x08-i,&time);
  if(ret)
   return ret;
 }
     return 0;
}
/* get cmos time
* time format:
* time[0]--year,
* time[1]---month
* time[2]---date;
* time[3]---if flag="1", day of week, otherwise, hour
* time[4]--- if flag="1", hour;otherwise, minute
* time[5]---if flag="1", minute;otherwise second
* time[6]---if flag="1",second
*return:
* 1---failed
* 0--success
* Note: all date-time is in BCD format
*/
unsigned char get_cmos_time(unsigned char *time,unsigned char flag)
{
 unsigned char i;
    if(pcf8563_read_time(time))
  return 1;
/*如果为秒=0,为防止时间变化,再读一次*/
    if((time[6]&0x7f)==0)
 {
       if(pcf8563_read_time(time))
   return 1;
    }
 time[1]&=0x1f;
 time[2]&=0x07;
 time[3]&=0x3f;
 time[4]&=0x3f;
 time[5]&=0x7f;
 time[6]&=0x7f;
 i=time[2];
 time[2]=time[3];
 time[3]=i;
 time[3]&=0x7f;
 if(flag==0)
 {
  time[3]=time[4];
  time[4]=time[5];
  time[5]=time[6];
 }


 return 0;
}


/************************
* time[0]~time[6]:year-month-date-day of week-hour-minute-second
* return:
* 0---success
* 1---failed
* Note: all date-time is in BCD format
***********************/
unsigned char set_cmos_time(unsigned char *time)
{
 unsigned char i;
 unsigned char ret;
 i=time[2];
 time[2]=time[3];
 time[3]=i;
 for(i=0;i<7;i++)
 {
  ret=pcf8563_write_byte(0x08-i,time);
  if(ret)
   return 1;
 }
 return 0;
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
5
关闭 站长推荐上一条 /3 下一条