#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
}