#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit sda=P2^1;
sbit scl=P2^2;
void delay(void)
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
void delay0()
{
uint i=30000;
while(i--)
;
}
void init(void)
{
sda=1;
scl=1;
}
void start(void)
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop(void)
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void ack(void)
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<200))
i++;
sda=0;
delay();
}
void noack(void)
{
scl=1;
delay();
sda=1;
delay();
scl=0;
delay();
}
void i2cwr_byte(uchar dat)
{
uchar i;
scl=0;
for(i=0;i<8;i++)
{
scl=1;
if(dat&0x80)
{
sda=1;
}
else
{
sda=0;
}
dat=dat<<1;
delay();
scl=1;
delay();
scl=0;
delay();
}
sda=1;
delay();
}
uchar i2cre_byte(void)
{
uchar i;
uchar value;
scl=0;
delay();
scl=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
value=value<<1;
if(sda)
{
value++;
}
scl=0;
delay();
}
return value;
}
void main()
{
init();
start();
i2cwr_byte(0xa0);
ack();
i2cwr_byte(10);
ack();
i2cwr_byte(0x55);
ack();
stop();
delay0();
init();
start();
i2cwr_byte(0xa0);
ack();
i2cwr_byte(10);
ack();
start();
i2cwr_byte(0xa1);
ack();
P1=i2cre_byte();
noack();
stop();
}
此帖出自
小平头技术问答
一周热门 更多>