logo

C8051F应用笔记 C8051F单片机笑傲江湖 使用C8051F驱动PCF8563时钟芯片

使用C8051F驱动PCF8563时钟芯片

以下资料由微雪电子整理并发布,未经许可不得转载,否则追究相应责任!

系统功能

   利用C8051F驱动PCF8563时钟芯片,显示时钟。


硬件设计


LCD12864液晶电路原理图

使用C8051F驱动PCF8563时钟芯片电路原理图


软件设计

下面部分从TXT拷出,拷到网页,代码部分缺省了很多空格,比较凌乱,请谅解!
I2C(SMBus)详细程序请查看C8051F I2C(SMBus)程序

LCD12864液晶详细程序请查看C8051F LCD12864程序

//目标系统: 基于C8051F单片机
//应用软件: Silicon IDE / Keil

/*01010101010101010101010101010101010101010101010101010101010101010101
----------------------------------------------------------------------
实验内容:
使用C8051F驱动PCF8563时钟芯片,把读取到的时间在LCD12864上显示
----------------------------------------------------------------------
硬件连接:
DVK501(PCF8563PrlPort)    F320 EX
                           VCC    ---------   VCC
                           GND   ---------   GND
                           SDA    ---------   P1.0  
                           SCL    ---------   P1.1

DVK501(LCD12864PrlPort)  F320 EX
                           VCC    ---------   VCC
                           GND   ---------   GND
                           CS      ---------    P0.7  
                           SID     ---------    P0.6  
                           SCK   ---------    P0.0
----------------------------------------------------------------------
注意事项:
(1)若有加载库程序,请将光盘根目录下的“library”下的“C8051F”文件夹拷到D盘
(2)请详细阅读:光盘根目录下的“说明书首页.htm”里的相应产品说明
----------------------------------------------------------------------
10101010101010101010101010101010101010101010101010101010101010101010*/

#define _DVK501_F320_EX_

#include <c8051f320.h>
#include <../../../../library/C8051F/ws_clock.h>
#include <../../../../library/C8051F/ws_delay.h>
#include <../../../../library/C8051F/ws_iic_port.h>
#include <../../../../library/C8051F/ws_pcf8563_port.h>
#include <../../../../library/C8051F/ws_lcd_st7920_port.h>

void main(void)
{
uchar time[3],tmp=0;
PCA0MD &= ~0x40; // Clear watchdog timer enable
clock_external(); // Set oscillator in external,the clock is 12M
delay_ms(100);
Check_SDA();
XBR1 = 0x40; // Enable crossbar and weak pull-ups
SPI_Init();
st7920LcdInit();
showLine(0,0,lcd_buffer,"PCF8563 Example");
showLine(0,1,lcd_buffer,"Set Time: OK");
showLine(0,2,lcd_buffer,"Read Time:");
refreshLCD(lcd_buffer);

SMBus_Init();
Enb_Interrupt();
PCF8563_init();
PCF8563_setTime(12,0,0);
delay_ms(10);

while(1)
{
PCF8563_getTime(time);
if(time[0]!=tmp)
{
showLine(8,3,lcd_buffer,"%d2:%d2:%d2",time[2],time[1],time[0]);
refreshLCD(lcd_buffer);
}
tmp=time[0];
}
}

/*--------------------------------------------------------------------
函数全称:十进制强制转换为十六进制
函数功能:
注意事项:
提示说明:调用changeIntToHex(33),return 0x33
输 入:
返 回:
--------------------------------------------------------------------*/
#define changeIntToHex(dec) ( (((dec)/10) <<4) + ((dec)%10) )
/*--------------------------------------------------------------------
函数全称:十进制化为十六进制,并以十进制格式返回
函数功能:
注意事项:传参必须为 unsigned 类型,否则移位结果可能吓你一跳
提示说明:调用converseIntToHex(33),return 21
输 入:
返 回:
--------------------------------------------------------------------*/
#define converseIntToHex(dec) ( (((dec)>>4) *10) + ((dec)%16) )
/*--------------------------------------------------------------------
函数全称:十六进制强制转换为十进制
函数功能:
注意事项:传参必须为 unsigned 类型,否则移位结果可能吓你一跳
提示说明:调用changeHexToInt(0x33),return 33
输 入:
返 回:
--------------------------------------------------------------------*/
#define changeHexToInt(hex) ( (((hex)>>4) *10 ) + ((hex)%16) )
/*--------------------------------------------------------------------
函数全称:十六进制化为十进制,,并以十六进制格式返回
函数功能:
注意事项:
提示说明:调用converseHexToInt(0x33),return 0x51
输 入:
返 回:
--------------------------------------------------------------------*/
#define converseHexToInt(hex) ( (((hex)/10) <<4 ) + ((hex)%10) )

void WriteAByte(uchar wordAdr,uchar dat);
void ReadNByte(uchar wordAdr,uchar *pRdDat);
void PCF8563_getTime(uchar *buf);
void PCF8563_setTime(uchar hour,uchar minute,uchar second);
void PCF8563_init(void);

#define CTRL_BUF1 0x00
#define CTRL_BUF2 0x01

#define SECOND_DATA_BUF 0x02
#define MINUTE_DATA_BUF 0x03
#define HOUR_DATA_BUF 0x04

#define DAY_DATA_BUF 0x05
#define WEEK_DATA_BUF 0x06
#define MONTH_DATA_BUF 0x07
#define YEAR_DATA_BUF 0x08

#define MINUTE_AE_BUF 0x09
#define HOUR_AE_BUF 0x0A
#define DAY_AE_BUF 0x0B
#define WEEK_AE_BUF 0x0C

#define CLK_FRQ_BUF 0x0D
#define TIMER_CTRL_BUF 0x0E
#define COUNT_VAL_BUF 0x0F

void WriteAByte(uchar wordAdr,uchar dat)
{
SMBus_Write(0xA2,wordAdr,dat,2);
}

void ReadNByte(uchar wordAdr,uchar *pRdDat)
{
SMBus_Write(0xA2,wordAdr,0,1);
SMBus_Read(0xA3,pRdDat,3);
}

void PCF8563_getTime(uchar *buf)
{
ReadNByte(SECOND_DATA_BUF,buf);
buf[0] = buf[0] & 0x7f; //get second data
buf[1] = buf[1] & 0x7f; //get minute data
buf[2] = buf[2] & 0x3f; //get hour data

buf[0] = changeHexToInt(buf[0]);
buf[1] = changeHexToInt(buf[1]);
buf[2] = changeHexToInt(buf[2]);
}

void PCF8563_setTime(uchar hour,uchar minute,uchar second)
{
hour = changeIntToHex(hour); //将数据的Dex格式转换为Hex格式
minute = changeIntToHex(minute);
second = changeIntToHex(second);

WriteAByte(HOUR_DATA_BUF,hour);
WriteAByte(MINUTE_DATA_BUF,minute);
WriteAByte(SECOND_DATA_BUF,second);
}

void PCF8563_init(void)
{
WriteAByte(CTRL_BUF1,0x00); //basic setting
WriteAByte(CTRL_BUF2,0x12); //alarm enable
}


系统调试

改变初始化时间,将语句“PCF8563_setTime(12,0,0);”改成 “PCF8563_setTime(1,2,3);”,断电重启后查看现象。


使用C8051F驱动PCF8563时钟芯片

——