c:
24c02记忆开机次数.zip
//------------------------------------------------------------
// 开机次数记忆
//------------------------------------------------------------
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit sda=P2^1;
sbit scl=P2^0;
//------------------------------------------------------------
// 函数申明
//------------------------------------------------------------
void iic_start();
void iic_stop();
void iic_ack();
void iic_noack();
void iic_writebyte(uchar dat);
uchar iic_readbyte();
void iic_at24c02_writebyte(uchar add,uchar dat);
uchar iic_at24c02_readbyte(uchar add);
//------------------------------------------------------------
void delay()
{
_nop_();_nop_();_nop_();_nop_();_nop_();
}
void delay1()
{
uint t=30000;
while(t--);
}
void init()
{
sda=1;
scl=1;
}
void iic_start()
{ sda=1;
scl=1;
delay();
sda=0;
delay();
scl=0;
}
void iic_stop()
{
sda=0;
scl=1;
delay();
sda=1;
delay();
}
void iic_ack() //从器件返回应答信号
{ uchar i; //多字节中如何给从器件应答信号
scl=1;
delay();
while((sda==1)&&(i<200))i++;
scl=0;
delay();
}
void iic_noack()
{
sda=1;
delay();
scl=1;
delay();
scl=0;
delay();
}
void iic_writebyte(uchar dat)
{
uchar i;
scl=0;
for(i=0;i<8;i++)
{
if(dat&0x80) sda=1;
else sda=0;
dat=dat<<1;
delay();
scl=1;
delay();
scl=0;
delay();
}
sda=1;
delay();
}
uchar iic_readbyte()
{
uchar i,dat;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
dat=dat<<1;
if(sda) dat++;
scl=0;
delay();
}
return dat;
}
void iic_at24c02_writebyte(uchar add,uchar dat)
{
init();
iic_start();
iic_writebyte(0xae); //写器件地址
iic_ack();
iic_writebyte(add); //写地址 10
iic_ack();
iic_writebyte(dat); //写数据 0x00
iic_ack();
iic_stop();
delay1();
}
uchar iic_at24c02_readbyte(uchar add)
{ uchar a;
init();
iic_start();
iic_writebyte(0xae); //写器件地址
iic_ack();
iic_writebyte(add); //写地址 10
iic_ack();
iic_start();
iic_writebyte(0xaf); //读数据
iic_ack();
a=iic_readbyte();
iic_noack();
iic_stop();
return a;
}
void main()
{ uchar a;
a=iic_at24c02_readbyte(10);
P1=iic_at24c02_readbyte(10);
a++;
iic_at24c02_writebyte(10,a);
while(1);
}
阅读(254) | 评论(0) | 转发(0) |