⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcf8563.c~rf104dce2.tmp

📁 MSP430F1611+LCD12864_st7920+Key刚完成调试.请有需要的朋下载!
💻 TMP
字号:
/********************************************************************
文件名:pcf8563l.c/pcf8563l.h
名称:PCF8563实时时钟芯片应用程序 
********************************************************************/
#include "PCF8563.h"
#include "IIC_PCF8563.h"
#define uint unsigned int
#define uchar unsigned char
//时钟取得的数

__interrupt void TimerB_ISR(void);
void Init_PCF8563_Port(void);
void WR_PCF8563_Data(void);
void Delay_PCF8563(uchar s);

unsigned char SendStr(unsigned char  sladdr,  unsigned char  subaddr,  unsigned char  *s, unsigned char  len);
unsigned char RecvStr(unsigned char sladdr, unsigned char subaddr ,unsigned char *s, unsigned char  len);

/*
unsigned char PCF_time[6]={ 
0x30,//秒(vl、00~59BCD码)
0x59,//分(-、00~59BCD码)
0x23,//时(-、-、00~23BCD码)
0x31,//日(-、-、01~31BCD码)
0x12,//世纪月(c、-、-、01~12BCD码)
0x99,//年(00~99BCD码)
};
*/

//对于PCF8563的程序出现的问题.
// 1 在PCF8563的英文资料里有: 秒,分,时,月,年:的最大值分别为:89 89 63 63 31 
//所以在输出端要对BCD码进行校正  对于:秒,分,时,月,年:的最大值超出59 59 24 12 99 的分别按最大值取
//要采用C语言中的年月日的算法.
//要先将十六进抽转化为十进:
//如: 0x2E 十进制里是 46  否则转化为0x32 与0x3e 将出现特殊字母.
unsigned char PCF_time[6]={ 
0x00,//秒(vl、00~59BCD码)
0x40,//分(-、00~59BCD码)
0x18,//时(-、-、00~23BCD码)
0x19,//日(-、-、01~31BCD码)
0x06,//世纪月(c、-、-、01~12BCD码)
0x08,//年(00~99BCD码)
};



unsigned char time[12];//设定时钟(要求全局声明unsigned char time[8])
unsigned char RealTime[12];

uint IntTime[6]={0};

void Delay_PCF8563(uchar s)
{
    while(s--) 
  {
   for(uint j=0;j<100;j++); 
  }
}

void WR_PCF8563_Data()
{
  SetTime();
  //Delay_PCF8563(1);
  GetTime();
}

void SetTime()
{
  SendStr(PCF8563W,0x02,PCF_time,4);
  SendStr(PCF8563W,0x07,PCF_time+4,2);
}


//读取时钟(要求全局声明unsigned char time[8])
void GetTime()
{
  RecvStr(PCF8563R,0x02,PCF_time,4);
  RecvStr(PCF8563R,0x07,PCF_time+4,2);
}

//组合BCD码到非组合BCD码的转换
//分别为 秒 分 时 日  月  年 
//格式化读取的时间值(入口time数组[6])
//输出 l[12] 高位在高 低位在低

void _FormatTime2BCD(uchar *t,uchar *l)   // *t 是PCF_time   *l是 time
{	//uchar dat,dat_H,dat_L;
  uchar i,temp0,temp1;  
  
  //以下分别去掉时间值中的无效位,年位不用换
  *t&=0x7f;
  *(t+1)&=0x7f;
  *(t+2)&=0x3f;
  *(t+3)&=0x3f;
  *(t+4)&=0x1f;
  *(t+5)&=0xff;	    //将BCD码转换为十进制
  for(i=0;i<6;i++)
  {
   IntTime[i]=*(t+i);   //将十进制显示温度值 
  }
  
  
  for(i=0;i<6;i++)
  {
    temp0=t[i];
    temp1=temp0;
    l[2*i]=temp1&0x0f;//低位
    l[2*i+1]=temp0>>4;	//高位
  }
}
//读8563时间
//为得到的时间 数组 格式为非组合BCD每位占二字节 
// l 长度大于16

void PCF8563_get_BCDtime(uchar *l){
  
  GetTime();
  _FormatTime2BCD(PCF_time,l);
  
}

//读8563时间
//为得到的时间 数组 格式为实际数 每位占一字节 
// l 长度大于8
void PCF8563_get_RELtime(uchar *l){
  
  uchar i,temp0,temp1;
  GetTime();
  //以下分别去掉时间值中的无效位,年位不用换
  *PCF_time&=0x7f;
  *(PCF_time+1)&=0x7f;
  *(PCF_time+2)&=0x3f;
  *(PCF_time+3)&=0x3f;
  *(PCF_time+4)&=0x1f;
  *(PCF_time+5)&=0xff;
  
  for(i=0;i<8;i++){
    temp0=PCF_time[i];
    temp1=temp0;
    temp1=temp1&0x0f;
    temp0=temp0>>4;
    temp0=temp0*10;//十位数
    temp0=temp0+temp1;
    
    l[i]=temp0;
  }
}
//得到从00:00:00以来的秒数
//
//
uint  PCF8563_get_RELATtime(void)
{
  uchar i,temp0,temp1,l[3];
  
  GetTime();
  //以下分别去掉时间值中的无效位,年位不用换
  *PCF_time&=0x7f;
  *(PCF_time+1)&=0x7f;
  *(PCF_time+2)&=0x3f;
  *(PCF_time+3)&=0x3f;
  *(PCF_time+4)&=0x1f;
  *(PCF_time+5)&=0xff;
  
  for(i=0;i<3;i++){
    temp0=PCF_time[i];
    temp1=temp0;
    temp1=temp1&0x0f;//得到个位
    temp0=temp0>>4;
    temp0=temp0*10;//十位数
    temp0=temp0+temp1;
    
    l[i]=temp0;
  }
  return l[0]+60*l[1]+3600*l[2];
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -