Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#include<8052.h>
#include<stdio.h>
__sbit __at 0x96 scl;
__sbit __at 0x97 sda;
void I2Cinit()
{
P1 = 0xc0;
}
void aknowledge() //acknowledge condition
{
sda=0;
scl=1;
scl=0;
sda=1;
}
void nack() // not acknowledge condition
{
sda=1;
scl=1;
scl=0;
scl=1;
}
void start() //start condition
{
sda=1;
scl=1;
sda=0;
scl=0;
}
void rstart() //re-start condition
{
scl=0;
sda=1;
scl=1;
sda=0;
}
void stop() //stop condition
{
scl=0;
sda=0;
scl=1;
sda=1;
}
unsigned char read_byte() //reading from EEPROM serially
{
unsigned int i;
unsigned char reead=0;
for(i=0;i<8;i++)
{
reead=reead<<1;
scl=0;
if(sda==1)
reead++;
scl=1;
}
scl=0;
sda=1;
return reead; //Returns 8 bit data here
}
void send_byte(unsigned char value) //send byte serially
{
unsigned int i;
unsigned char send;
send=value;
for(i=0;i<8;i++)
{
scl=0;
sda=send/128; //extracting MSB
send=send<<1; //shiftng left
scl=1;
}
scl=0;
sda=1;
}
void Write(unsigned char ch, int addr)
{
start();
send_byte(0xA0);
aknowledge();
send_byte((unsigned char)(addr>>8));
aknowledge();
send_byte((unsigned char)addr);
aknowledge();
send_byte(ch); //device address
aknowledge();
stop();
}
int Read(int addr)
{
unsigned char ret;
start();
send_byte(0xA0);
aknowledge();
send_byte((unsigned char)(addr>>8)); // High addr
aknowledge();
send_byte((unsigned char)addr); // low addr
aknowledge();
rstart();
send_byte(0xA1); //device read address
aknowledge();
ret=read_byte();
nack(); // stop reading
stop();
return ret;
}
void delayus(int x)
{
while(x--);
}
void delayms(int x)
{
while(x--)
delayus(97);
}
void main()
{
int x,j;
I2Cinit();
for(x=0;x<256;x++)
{
Write(x,x);
delayms(5);
}
for(x=255;x>0;x--)
j = Read(x);
}