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

📄 pcf8563._h

📁 pcf8563开发
💻 _H
字号:
/*******************************************
*       PCF8563的I2C(TWI)操作函数		   *
*  文 件 名:pcf8563.h					   *
*  函数功能:利用CPU的硬件TWI接口操作	   *
*  设    计:张子凤						       *
*  修改日期:2008年11月11日				   *
*  版    本:V1.0						   *
*  主控芯片:Mega16						   *
*******************************************/
//全局变量定义
unsigned char timer[8];		  				 //时钟数据

//写入时间函数
unsigned char write_tim(unsigned char a)
{   
  sendbyte(a);				  //发送
  waitack();						  //等待写完成
  if(chkack()!=MT_SLA_ACK)return I2C_ERR;  //检查是否正确写入
  else return I2C_CRR;
}

//读出时间函数
unsigned char read_tim(void)
{
 rcvackbyte();					  //接收一字节并返回应答
 waitack();						  //等待接收完成
 if(chkack()!=MR_DATA_ACK)return I2C_ERR; 		  //检查是否接收成功
 else
 return(TWDR);				  //读出时钟数据
}

/**********************************
*          读出数据函数			  *
**********************************/
unsigned char read_timer(void)
{
 start();		 	 	          //启动总线
 waitack();						  //等待启动完成
 if(chkack()!=START)return I2C_ERR;		  //检查是否启动成功
 write_tim(PCF8563_SLA_W);				  //发送写地址
 write_tim(0x02);				  //写数据地址
	
 start();
 waitack();						  //等待启动完成
 if(chkack()!=RESTART)return I2C_ERR;	  //检查是否启动成功
 write_tim(PCF8563_SLA_R);				  //发送读地址
	
 timer[0]=read_tim()&0x7F;		  //读出秒数据
 timer[1]=read_tim()&0x7F;		  //读出分数据
 timer[2]=read_tim()&0x3F;		  //读出时数据
 timer[3]=read_tim()&0x3F;		  //读出日数据
 timer[4]=read_tim()&0x07;		  //读出周数据
 if(timer[4]==0)timer[4]=7;
 timer[5]=read_tim();	  		  //读出月数据
 if((timer[5]&0x80)==0)
  timer[7]=0x20;	 		  //世纪位为0,是21世纪
 else 
  timer[7]=0x19;			  //世纪位不为0,是20世纪
 timer[5]=timer[5]&0x1F;
 timer[6]=read_tim();			  //读出年数据
 stop();
 return I2C_CRR;
}

/**********************************
*          写入时钟函数			  *
**********************************/
unsigned char write_timer(unsigned char *dat)
{
 start();		 	 	          //启动总线
 waitack();						  //等待启动完成
 if(chkack()!=START)return I2C_ERR;		  //检查是否启动成功
 write_tim(PCF8563_SLA_W);				  //发送写地址
 write_tim(0x00);				  //写数据首地址
 write_tim(0x20);				  //写控制/状态寄存器1,暂停计时
 write_tim(0x00);				  //写控制/状态寄存器2
 write_tim(dat[0]);			      //写秒数据0
 write_tim(dat[1]);			  //写分数据
 write_tim(dat[2]);			  //写时数据
 write_tim(dat[3]);			  //写日数据
 write_tim(dat[4]);			  //写周数据
 write_tim(dat[5]);			  //写月数据
 write_tim(dat[6]);			  //写年数据
 stop();
 return I2C_CRR;
}

/**********************************
*          启动时钟函数			  *
**********************************/
unsigned char start_timer(void)
{
  start();		 	 	          //启动总线
  waitack();						  //等待启动完成
  if(chkack()!=START)return I2C_ERR;		  //检查是否启动成功
  write_tim(PCF8563_SLA_W);				  //发送写地址
  write_tim(0x00);				  //写数据首地址
  write_tim(0x00);				  //写控制/状态寄存器1,暂停计时
  stop();
  return I2C_CRR;
}

/**********************************
*   将时钟数据转换后在LCD上显示	  *
**********************************/ 
void timer_lcd(void)
{
 LCD_set_xy(4,0);			 	 	 //写指令:第1行第4列地址

 LCD_write_char(0,(timer[7]>>4)+0x30);
 LCD_write_char(0,(timer[7]&0x0f)+0x30);
 LCD_write_char(0,(timer[6]>>4)+0x30);		 //显示年
 LCD_write_char(0,(timer[6]&0x0f)+0x30);
 LCD_write_char(0,'/');
 LCD_write_char(0,(timer[5]>>4)+0x30);		 //显示月
 LCD_write_char(0,(timer[5]&0x0f)+0x30);
 LCD_write_char(0,'/');
 LCD_write_char(0,(timer[3]>>4)+0x30);		 //显示日
 LCD_write_char(0,(timer[3]&0x0f)+0x30);

 LCD_set_xy(2,1);			 		 //写指令:第2行地址
   
 LCD_write_char(0,(timer[2]>>4)+0x30);		 //时
 LCD_write_char(0,(timer[2]&0x0f)+0x30);
 LCD_write_char(0,':');
 LCD_write_char(0,(timer[1]>>4)+0x30);		 //分
 LCD_write_char(0,(timer[1]&0x0f)+0x30);
 LCD_write_char(0,':');   
 LCD_write_char(0,(timer[0]>>4)+0x30);		 //秒
 LCD_write_char(0,(timer[0]&0x0f)+0x30);
 LCD_write_char(0,' ');
 LCD_write_char(0,'W');	LCD_write_char(0,'e');	//星期的前导字
 LCD_write_char(0,'e');  LCD_write_char(0,'k');
 LCD_write_char(0,(timer[4]&0x0f)+0x30);	 //星期数据
}

//定时器1:每秒从8563中读取4次数据,更新显示
void timer1_init(void)	       //定时器1初始化:250毫秒定时,预分频256
{
 TCCR1B = 0x00; 		   	   //停止定时器
 TCNT1H = 0x8F;			   //初值高字节
 TCNT1L = 0x81;			   //定时初值低字节
 TCCR1A = 0x00;
 TCCR1B = 0x03; 	 	   	   //启动定时器
}

#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void) 	   //定时器1中断入口:250MS中断一次
{
 TCNT1H = 0x8F;			   //重装初值
 TCNT1L = 0x81;   
 read_timer();	   	   	   //读出当前时钟
 timer_lcd();		   	   //显示数据转换
}

⌨️ 快捷键说明

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