#include <reg51.h>
#include <intrins.h>
#define OP_READ 0xa1 // 器件地址以及读取操作
#define OP_WRITE 0xa0 // 器件地址以及写入操作
#define MAX_ADDR 0x7fff // AT24C256最大地址
unsigned char code dis_code[11]={0xc0,0xf9,0xa4,0xb0, // 0, 1, 2, 3
0x99,0x92,0x82,0xf8,0x80,0x90,0xff};// 4, 5, 6, 7, 8, 9, off
// 写入到AT24C01的数据串
unsigned char code dis_code2[11]={0xff,0x90,0x80,0xf8, // 0, 1, 2, 3
0x82,0x92,0x99,0xb0,0xa4,0xf9,0xc0};// 4, 5, 6, 7, 8, 9, off
// 写入到AT24C01的数据串
sbit SDA = P3^4;
sbit SCL = P3^3;
sbit LED = P2^0;
bit bdata ack;
#define nops(); {_nop_();_nop_();_nop_();_nop_();}
//------------------------------------------------------------------------------------------------------
void IIC_start(void); //起始条件
void IIC_stop(void); //停止条件
void IIC_ack_main(bit ack_main);
void IIC_sendbyte(unsigned char senddata);
unsigned char IIC_readbyte(void);
//unsigned char IIC_read_cur_addr(unsigned char ad_main);
void IIC_RdFromROM(unsigned char ad_main,unsigned char *rddata,unsigned char addh,unsigned char addl,unsigned char num);
void IIC_WrToROM(unsigned char ad_main,unsigned char *wrdata,unsigned char addh,unsigned char addl,unsigned char num);
void delayms(unsigned char ms);
main(void)
{
unsigned int i;
unsigned char rddata[11];
IIC_WrToROM(0xa0,dis_code,0x00,0x00,11);
i = 0;
while(1)
{
LED = 0;
IIC_RdFromROM(0xa0,rddata,0x00,0x00,11); // 循环读取24Cxxx内容,并输出到P0口
for(i=0;i<11;i++)
{
P0 = rddata[i];
delayms(250);
delayms(250);
delayms(250);
delayms(250);
}
}
}
//=====================================24LC256================================
/*****************************************************************************
****
****
****
****
****
******************************************************************************/
void IIC_start(void)
{
// SCL=0;
SDA=1;
_nop_();
SCL=1;
//_nop_();_nop_();_nop_();_nop_();_nop_();
nops();
SDA=0;
//_nop_();_nop_();_nop_();_nop_();_nop_();
nops();
SCL=0;
_nop_();
_nop_();
}
/*****************************************************************************
****
****
****
****
****
******************************************************************************/
void IIC_stop(void)
{
// SCL=0;
SDA=0;
_nop_();
SCL=1;
// _nop_();_nop_();_nop_();_nop_();_nop_();
nops();
nops();
SDA=1;
// _nop_();_nop_();_nop_();_nop_();_nop_();
nops();
nops();
}
/*****************************************************************************
****
****
****
****
****
******************************************************************************/
void IIC_ack_main(bit ack_main)//1 应答信号 0 非应答信号
{
if(ack_main)
SDA=0; //主器件为接收方,从器件为发送方,应答信号
else
SDA=1; //主器件为接收方,从器件为发送方,非应答信号
// _nop_();_nop_();_nop_();_nop_();_nop_();
nops();
SCL=1;
// _nop_();_nop_();_nop_();_nop_();_nop_();
nops();
SCL=0;
}
/*****************************************************************************
****
****
****
****
****
******************************************************************************/
void IIC_sendbyte(unsigned char senddata)
{
unsigned char idata count=8; //bit count control
unsigned char temp;
do
{
temp=senddata;
SCL=0;
nops();
if((temp&0x80)==0x80)
{
SDA=1;
}
else
{
SDA=0;
}
SCL=1;
senddata=senddata<<1;
count--;
}while(count);
SCL=0; //不考虑从应答位|但要控制好时序 */
}
/*****************************************************************************
****
****
****
****
****
******************************************************************************/
unsigned char IIC_readbyte(void)
{
unsigned char idata count,temp,temp1;
temp=0;
temp1=0;
count=8;
SDA=1;
do
{
_nop_();
SCL=0;
nops();
SCL=1;
nops();
if(SDA)
{
temp=temp|0x01;
}
else
{
temp=temp&0xfe;
}
if(count-1)
{
temp1=temp<<1;
temp=temp1;
}
count--;
}while(count);
SCL=0;
_nop_();
_nop_();
return temp;
}
void IIC_WrToROM(unsigned char ad_main,unsigned char *wrdata,unsigned char addh,unsigned char addl,unsigned char num)
{
unsigned char idata i;
IIC_start();
IIC_sendbyte(ad_main);
IIC_ack_main(1);
IIC_sendbyte(addh);
IIC_ack_main(1);
IIC_sendbyte(addl);
IIC_ack_main(1);
for(i=0;i<num;i++)
{
IIC_sendbyte(*(wrdata+i));
IIC_ack_main(1);
}
IIC_stop();
delayms(20);
}
void IIC_RdFromROM(unsigned char ad_main,unsigned char *rddata,unsigned char addh,unsigned char addl,unsigned char num)
{
unsigned char i,j;
i=0;
IIC_start();
IIC_sendbyte(ad_main);
IIC_ack_main(1);
IIC_sendbyte(addh);
IIC_ack_main(1);
IIC_sendbyte(addl);
IIC_ack_main(1);
IIC_start();
IIC_sendbyte(ad_main+1);
IIC_ack_main(1);
for(i=0;i<num-1;i++)
{
*(rddata+i)=IIC_readbyte();
IIC_ack_main(1);
}
*(rddata+num-1)=IIC_readbyte();
IIC_ack_main(0);
IIC_stop();
}
void delayms(unsigned char ms)
// 延时子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 125; i++);
}
}