|
编的程序是这样的,但不能得到我想要的结果
#include
#include
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
sbit SDL=P2^1;
sbit SDA=P2^0;
delay()
{
_nop_();
_nop_();
_nop_();
_nop_();
}
IIC_start()
{
SDA=1;
delay();
SDL=1;
delay();
SDA=0;
delay();
SDL=0;
delay();
}
IIC_stop()
{
SDL=0;
delay();
SDA=0;
delay();
SDL=1;
delay();
SDA=1;
delay();
}
IIC_send(uint8 byte)
{
uint8 i,j=0x80;
for(i=0;i<8;++i)
{
SDL=0;
delay();
if(byte&j==1)
SDA=1;
else
SDA=0;
j>>=1;
delay();
SDL=1;
delay();
SDL=0;
delay();
}
SDL=0;
delay();
SDA=1;
delay();
SDL=1;
delay();
SDL=0;
delay();
}
uint8 IIC_read()
{
uint8 k,k1=0x00;
for(k=0;k<8;++k)
{
SDL=0;
delay();
SDA=1;
delay();
SDL=1;
delay();
if (SDA==1)
k1 |=0x01;
k1<<=1;
SDL=0;
delay();
}
SDL=0;
delay();
SDA=1;
delay();
SDL=1;
delay();
SDL=0;
delay();
return k1;
}
IIC_write_EEPROM(uint8 add,byte)
{
IIC_start();
IIC_send(0xa0);
IIC_send(add);
IIC_send(byte);
IIC_stop();
}
uint8 IIC_read_EEPROM (uint8 add)
{
uint8 n;
IIC_start();
IIC_send(0xa0);
IIC_send(add);
IIC_start();
IIC_send(0xa1);
n=IIC_read();
IIC_stop();
return (n);
}
uart_init()
{
SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
}
uart_send(uint8 m)
{
SBUF=m;
while(!TI);
TI=0;
}
main()
{
uint8 i1=0,j1=0x00,c;
uint16 k1=0;
SDL=1;
SDA=1;
uart_init();
while(1)
{
IIC_write_EEPROM(j1,i1);
for(k1=0;k1<1000;++k1)
delay();
c=IIC_read_EEPROM(j1);
uart_send(c);
j1++;
i1++;
if(j1 == 0xff) {j1 = 0;}
if(i1 == 0xff) {i1 = 0;}
for(k1=0;k1<1000;++k1)
delay();
}
} |
|