//PIC16FXX PICC
#include
#define i2c_sda RB3 //定义SD2068的数据口
#define i2c_scl RB5 //定义SD2068的时钟口
#define sd2068_INT RB4
#define true 1
#define false 0
#define OpenLampTimeHourAddr 0x14
#define OpenLampTimeMinuteAddr 0x15
#define CloseLampTimeHourAddr 0x16
#define CloseLampTimeMinuteAddr 0x17
#define TestSd2068Addr 0x18
#define Sd2068_R_Addr 0x65 //sd2068's 器件地址
#define Sd2068_W_Addr 0x64
unsigned char table1[7];
unsigned char ddata;
/********SD2068函数名********/
void sd2068_init (void);
void I2CWait();
void I2CStart();
void I2CStop();
void I2CAck();
void I2CNoAck();
bit I2CWaitAck();
void I2CSendByte(unsigned char demand);
char I2CReceiveByte();
void I2CWriteDate(unsigned char reg_minute,unsigned char reg_hour);
void SetTimeAdject(unsigned char dat);
//void SetWarmOut(); //秒报警
//***********************************************
void sd2068_init (void)
{
TRISB3 = 0; //i2c_sda
TRISB4 = 1;
TRISB5 = 0; //i2c_SCL
}
//************向SD2068发送一个字节*************/
void I2CSendByte(unsigned char demand)
{
unsigned char j=8; //从HI-->LO发送8bit数据
TRISB3 =0; //------------------------wait change
while(j--)
{
i2c_scl=0;
I2CWait();
if((demand&0x80)==0) i2c_sda=0;
else i2c_sda=1;
demand<<=1;
I2CWait();
i2c_scl=1;
I2CWait();
}
i2c_scl=0;
}
/*********MCU从SD2068读入一字节*********/
char I2CReceiveByte()
{
unsigned char a,i=8;
ddata=0;
i2c_sda=1;
while(i--)
{
ddata<<=1; //数据从高位开始读取
i2c_scl=0;
I2CWait();
i2c_scl=1;
TRISB3 =1; //----------------------------wait change
I2CWait(); //从高位开始 ddata|=SDA;ddata>>=1
if(i2c_sda)
{
ddata|=0x01;
}
}
i2c_scl=0;
TRISB3 = 0; //-------------------------------wait change
return ddata;
}
/******读SD2068实时数据寄存器******/
//
//in : none
// out : second,minute,hour,week,day,month,day == table1[0~6]
//
//----------------------------------
void I2CReadDate()
{
unsigned char n,a;
I2CStart();
I2CSendByte(Sd2068_R_Addr);
I2CWaitAck();
for(a=0;a<7;a++)
{
table1[a]=I2CReceiveByte();
if (a!=6) //最后一个数据不应答
{
I2CAck();
}
}
I2CNoAck();
I2CStop();
}
/******写SD2068允许程序******/
void WriteTimeOn()
{
I2CStart();
I2CSendByte(Sd2068_W_Addr);
I2CWaitAck();
I2CSendByte(0x10); //设置写地址10H
I2CWaitAck();
I2CSendByte(0x80); //置WRTC1=1
I2CWaitAck();
I2CStop();
I2CStart();
I2CSendByte(Sd2068_W_Addr);
I2CWaitAck();
I2CSendByte(0x0F); //设置写地址0FH
I2CWaitAck();
I2CSendByte(0x84); //置WRTC2,WRTC3=1
I2CWaitAck();
I2CStop();
}
/******写SD2068禁止程序******/
void WriteTimeOff()
{
I2CStart();
I2CSendByte(Sd2068_W_Addr);
I2CWaitAck();
I2CSendByte(0x0F); //设置写地址0FH
I2CWaitAck();
I2CSendByte(0x0) ; //置WRTC2,WRTC3=0
I2CWaitAck();
I2CSendByte(0x0) ; //置WRTC1=0(10H地址)
I2CWaitAck();
I2CStop();
}
/******写SD2068实时数据寄存器******/
void I2CWriteDate(unsigned char reg_minute,unsigned char reg_hour)
{
WriteTimeOn();
I2CStart();
I2CSendByte(Sd2068_W_Addr);
I2CWaitAck();
I2CSendByte(0x00); //设置写起始地址
I2CWaitAck();
I2CSendByte(0); // second
I2CWaitAck();
I2CSendByte(reg_minute); //minute
I2CWaitAck();
I2CSendByte(reg_hour); //hour ,二十四小时制
I2CWaitAck();
I2CSendByte(5); //week
I2CWaitAck();
I2CSendByte(4); //day
I2CWaitAck();
I2CSendByte(0x12); //month
I2CWaitAck();
I2CSendByte(0x12); //year
I2CWaitAck();
I2CStop();
WriteTimeOff();
}
/********** 调整时间误差 *********************/
void SetTimeAdject(unsigned char dat)
{
WriteTimeOn();
I2CStart();
I2CSendByte(Sd2068_W_Addr);
I2CWaitAck();
I2CSendByte(0x12); //设置写起始地址
I2CWaitAck();
I2CSendByte(dat); //清零数字调整寄存器
I2CWaitAck();
I2CStop();
WriteTimeOff();
}
//*********************************************
//单独写:
// 输入地址 要写入的数据
// 无输出
//*********************************************
void Write_a_Byte(unsigned char addr,unsigned char dat1)
{
WriteTimeOn();
I2CStart();
I2CSendByte(Sd2068_W_Addr);
I2CWaitAck();
I2CSendByte(addr); //设置写起始地址
I2CWaitAck();
I2CSendByte(dat1); //数
I2CWaitAck();
I2CStop();
WriteTimeOff();
}
//*********************************************
//单字节读
// 输入:要读出字节的地址
// 输出: 数据
//*********************************************
unsigned char Read_a_Byte(unsigned char addr)
{
unsigned char temp_reg;
I2CStart();
I2CSendByte(Sd2068_W_Addr);
I2CWaitAck();
I2CSendByte(addr); //设置写起始地址
I2CWaitAck();
I2CStart();
I2CSendByte(Sd2068_R_Addr);
I2CWaitAck();
temp_reg=I2CReceiveByte();
I2CNoAck();
I2CStop();
return(temp_reg);
}
文章评论(0条评论)
登录后参与讨论