代码:
PCF8563.C
#include <xc.h>
#include"PCF8563.h"
uchar ack;
uchar get_time[9];
uchar send[2]={0xAA,0x55};
uchar year[4],mon[2],day[2],hour[2],minute[2],sec[2];
void start_i2c()
{
SDA=1;
nop();
SCL=1;
nop();nop();nop();nop();nop();
SDA=0;//发送起始信号
nop();nop();nop();nop();nop();
SCL=0;//钳住I2C总线,准备发送数据或接收数据
nop();nop();
}
void stop_i2c()
{
SDA=0;
nop();
SCL=1;
nop();nop();nop();nop();nop();
SDA=1;
nop();nop();nop();nop();nop();
}
void i2c_ack()
{
SDA=0;
nop();
SCL=1;
nop();nop();
SCL=0;
nop();nop();
}
void i2c_noack()
{
SCL=0;
nop();nop();
SDA=1;
nop();
SCL=1;
nop();nop();
SCL=0;
nop();nop();
}
void send_i2c_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
if(dat&0x80)
{
SDA=1;
}
else
SDA=0;
nop();nop();
dat<<=1;
SCL=1;
nop();nop();nop();nop();nop();nop();
SCL=0;
}
nop();
TRISC4=1;
SDA=1;
nop();nop();
SCL=1;
nop();nop();
if(LATCbits.LATC4==1)
ack=1;
else
ack=0;
SCL=0;
nop();nop();
TRISC4=0;
}
read_i2c_byte()
{
uchar i;
uchar read_data;
for(i=0;i<8;i++)
{
TRISC4=1;
nop();nop();
SCL=1;
read_data<<=1;
if(LATCbits.LATC4==1)
read_data=read_data+1;
nop();
SCL=0;
nop();
}
TRISC4=0;
return(read_data);
}
void write_i2c_data(uchar address,uchar data)
{
uchar i;
start_i2c();//启动IIC
send_i2c_byte(0XA2);//写操作指令
if(ack==1)
return;
send_i2c_byte(address);//写寄存器地址
if(ack==1)
return;
send_i2c_byte(data);
stop_i2c();//结束IIC
ms_delay(1);
}
int read_i2c_data(uchar address)
{
uchar i;
uchar receive;
start_i2c();//启动IIC
send_i2c_byte(0XA2);//写操作指令
if(ack==1)
return 1;
send_i2c_byte(address);//写寄存器地址
if(ack==1)
return 1;
start_i2c();//重新启动IIC
send_i2c_byte(0XA3);//读操作指令
if(ack==1)
return 1;
receive=read_i2c_byte();
stop_i2c();//结束IIC
ms_delay(1);
return(receive);
}
void set_time()
{
write_i2c_data(0x00,0x00);
write_i2c_data(0x02,0x00);
write_i2c_data(0x03,0x00);
write_i2c_data(0x04,0x09);
write_i2c_data(0x05,0x18);
write_i2c_data(0x06,0x02);
write_i2c_data(0x07,0x10);
write_i2c_data(0x08,0x16);
ms_delay(1);
}
void ms_delay(uint xms)
{
int i,j;
for(i=0;i<xms;i++)
{for(j=0;j<50;j++);}
}
PCF8563.H#ifndef _PCF8563_H_
#define _PCF8563_H_
#define nop() asm("nop")
#define uchar unsigned char
#define uint unsigned int
#define SCL LATCbits.LATC3
#define SDA LATCbits.LATC4
void us_delay(uint i);//
void start_i2c();//启动IIC
void stop_i2c();//停止信号
void i2c_ack();//返回应答信号
void i2c_noack();//无应答信号
void send_i2c_byte(uchar dat);//IIC写8位字节数据
int read_i2c_byte();//IIC读8位字节数据
void write_i2c_data(uchar address,uchar data);
void set_time();
void ms_delay(uint xms);
uchar read_i2c_data(uchar address);
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* NEWFILE_H */
主程序
void main(void)
{
u8 shi,ge;
ANCON0bits.ANSEL6=0; //RE1(AN6)配置为数字IO
ANCON1=0x00; //将部分带模拟输入功能的引脚设置为数字IO
//PADCFG1=0x80;
//输出使用LAT寄存器,输入使用PORT寄存器
TRISDbits.TRISD4=0; //PORD4配置为输出
TRISEbits.TRISE1=0; //PORE1配置为输出
//ODCON=0x80;
TRISC=0X00;
LCD_Init();
LCD_CLS();
//LCD_P8x16Str(45,5,(u8*)"EASEAR");
//LCD_P6x8Str(0,7,(u8*)"www.holteksupport.com");
//Draw_BMP(0,0,100,3,(u8*)Dot);
LCD_P8x16Char(13,3,'2');
LCD_P8x16Char(21,3,'0');
LCD_P8x16Char(45,3,'-');
LCD_P8x16Char(69,3,'-');
LCD_P8x16Char(45,5,':');
LCD_P8x16Char(69,5,':');
set_time();
ms_delay(1);
while(1)
{
get_time[0]=read_i2c_data(0x02);
get_time[1]=read_i2c_data(0x03);
get_time[2]=read_i2c_data(0x04);
get_time[3]=read_i2c_data(0x05);
get_time[4]=read_i2c_data(0x06);
get_time[5]=read_i2c_data(0x07);
get_time[6]=read_i2c_data(0x08);
ms_delay(1);
MS_delay(500);
shi=get_time[0]>>4;
ge=get_time[0]&0x0f;
LCD_P8x16Char(77,5,shi+0x30);
LCD_P8x16Char(85,5,ge+0x30);
shi=get_time[1]>>4;
ge=get_time[1]&0x0f;
LCD_P8x16Char(53,5,shi+0x30);
LCD_P8x16Char(61,5,ge+0x30);
shi=get_time[2]>>4;
ge=get_time[2]&0x0f;
LCD_P8x16Char(29,5,shi+0x30);
LCD_P8x16Char(37,5,ge+0x30);
shi=get_time[3]>>4;
ge=get_time[3]&0x0f;
LCD_P8x16Char(77,3,shi+0x30);
LCD_P8x16Char(85,3,ge+0x30);
shi=get_time[5]>>4;
ge=get_time[5]&0x0f;
LCD_P8x16Char(53,3,shi+0x30);
LCD_P8x16Char(61,3,ge+0x30);
shi=get_time[6]>>4;
ge=get_time[6]&0x0f;
LCD_P8x16Char(29,3,shi+0x30);
LCD_P8x16Char(37,3,ge+0x30);
}
}
读出来全是1.
请问高手,哪里错了?谢谢
|
|