#include<REGX52.h>
#include<INTRINS.H>
#define uchar unsigned char
#define uint unsigned int
sbit sck=P0^3;
sbit sda=P0^2;
void Delay()
{
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
void Delay1ms(unsigned int count)
{
unsigned int i,j;
for(i=0;i<count;i++)
for(j=0;j<250;j++);
}
void initial()
{
sck=1;
sda=1; Delay();
}
void start()
{
sda=1;
Delay();sck=1; Delay();
sda=0; Delay(); //sck=0;
}
void stop()
{
sda=0;Delay();sck=1;
Delay();
sda=1; Delay();
}
void ack()
{
uchar i;
sck=1; Delay();
while(sda==1&&i<200) i++;
sck=0;
Delay();
}
void uack()
{
sck=1;
sda=1;
Delay();
sck=0; Delay();
}
void word_wr(uchar value)
{
uchar i;
sck=0;Delay();
for(i=0;i<8;i++)
{
if(value&0x80) sda=1;
else sda=0;
value<<=1;Delay();
sck=1;Delay(); sck=0;Delay();
}
sda=1;Delay();
}
uchar word_rd()
{
uchar i,value;
sck=0;sda=1;Delay();
for(i=0;i<8;i++)
{
sck=1;Delay();
value<<=1;
if(sda==1) value++;
sck=0;Delay();
}
return value ;
}
void write_byte(uchar add,uchar value)
{
start(); word_wr(0xa0);ack();
word_wr(add);ack();
word_wr(value);ack(); stop(); Delay1ms(3);
}
uchar read_byte(uchar add)
{
uchar unite;
start(); word_wr(0xa0);ack();
word_wr(add);ack(); start();
word_wr(0xa1);ack();unite=word_rd();
uack();stop(); Delay1ms(3);
return unite;
}
main()
{
initial();
write_byte(10,0x55);
Delay1ms(3);
initial(); P3=read_byte(10);
while(1);
}
本来是可以看见p3口出现0x55,但是就是没结果,看了几遍也没啥岀误,仿真完全能通过。
此帖出自
小平头技术问答
一周热门 更多>