include <reg52.h>
#include <absacc.h>
#include <intrins.h>
#include "iic.h"
unsigned char iic_send_byte(unsigned char wdata)
{
unsigned char bit_cnt;
for(bit_cnt = 0;bit_cnt < 8;bit_cnt ++)
{
if(wdata & 0x80) SDA = 1;
else SDA = 0;
wdata <<= 1;
_delay_10uS();
SCL = 1;
_delay_10uS();
SCL = 0;
}
_delay_10uS();
SDA = 1;
SCL = 1;
_delay_10uS();
if(SDA == 1) bit_cnt = 0;//返回0,失败的写入
else bit_cnt = 1;//返回1,成功的写入
SCL = 0;
return bit_cnt;
}
unsigned char iic_recv_byte(void)
{
unsigned char bit_cnt;
unsigned char rdata;
SDA = 1;
for(bit_cnt = 0;bit_cnt < 8;bit_cnt ++)
{
SCL = 0;
_delay_10uS();
SCL = 1;
_delay_10uS();
rdata <<= 1;
if(SDA == 1) rdata |= 1;
_delay_10uS();
}
SCL = 0;
return rdata;
}
unsigned char iic_page_write(unsigned char sla,\
unsigned char suba,unsigned char *s,unsigned char len)
{
unsigned char i;
iic_start();//启动总线
if(iic_send_byte(sla) == 0) return 0xff;//发送器件地址,失败返回0xff
if(iic_send_byte(suba) == 0) return 0xff;//发送数据地址,失败返回0xff
for(i = 0;i < len;)
{
if(iic_send_byte(*s ++) == 0) return 0xff;
i ++;
suba ++;
if((suba & 0x07) == 0) break;//页越界,跳出
}
iic_stop();//结束总线
return(len - i);//写入i字节成功
}
unsigned char iic_send_str(unsigned char sla,\
unsigned char suba,unsigned char *s,unsigned char len)
{
unsigned char rm_cnt = len;
unsigned int i;
do
{
rm_cnt = iic_page_write(sla,suba + (len - rm_cnt),s + (len - rm_cnt),rm_cnt);
if(rm_cnt == 0xff) return 0;
for(i = 0;i < 1000;i ++) _delay_10uS();//10mS延时
}
while(rm_cnt);
return 1;//写入多字节成功
}
unsigned char iic_recv_str(unsigned char sla,\
unsigned char suba,unsigned char *s,unsigned char len)
{
unsigned char i;
iic_start();//启动总线
if(iic_send_byte(sla) == 0) return 0;//发送器件地址,失败返回0
if(iic_send_byte(suba) == 0) return 0;//发送数据地址,失败返回0
iic_start();//重新启动总线
if(iic_send_byte(sla | 1) == 0) return 0;//发送器件地址,失败返回0
for(i = 0;i < len - 1;i ++)
{
*s ++ = iic_recv_byte();//接收一字节数据
iic_ack(0);//发送有效应答位
}
*s = iic_recv_byte();//接收最后一字节数据
iic_ack(1);//发送非应答位
iic_stop();
return(1);
}
unsigned char iic_fill_data(unsigned char sla,\
unsigned char suba,unsigned char f_data,unsigned char len)
{
unsigned char fill_data[8];
unsigned char rm_cnt = len;
unsigned int i;
for(i = 0;i < 8;i ++)
{
fill_data[i] = f_data;
}
do
{
rm_cnt = iic_page_write(sla,suba + (len - rm_cnt),fill_data,rm_cnt);
if(rm_cnt == 0xff) return 0;
for(i = 0;i < 1000;i ++) _delay_10uS();//10mS延时
}
while(rm_cnt);
return 1;//写入多字节成功
}
这个是我写的最底层的部分,我做的密码箱的时候是可以的啊
但是在今天我写的时钟就不行了啊
应该是时钟报警不行
if(last_key_val==16)
{
cur_time1[2]++;
iic_send_str(CSI24WC02,0xff,cur_time1+2,1);
if(cur_time1[2]==0x24)
{
cur_time1[2]=0x00;
}
iic_recv_str(CSI24WC02,0xff,cur_time1+2,1);
alarm_buf[2]=cur_time1[2];
lcd12864_buf3[6] = (alarm_buf[2] >> 4) + '0';
lcd12864_buf3[7] = (alarm_buf[2] & 0x0f) + '0';
lcd12864_disp_str(0,3,lcd12864_buf3);
while(last_key_val==16);
}
这样不行,没掉下电就不行了!没有保存好时间 |