Menu

Custom Search

Microcontroller Program-eprom...

#include<8051.h>

#define scl P2_0 /*clock*/
#define sda P2_1 /*data*/

//#define blink P2_2 /*Port for blinking*/

//unsigned char data1;

void write_data(unsigned char);

void delay()
{
unsigned int i;
for(i=0;i<6500;i++);
}

void blink30()
{ int i;
for(i=0;i<3;i++)
{
P3_0=1;
delay();
P3_0=0;
delay();
}
}

void blink31()
{ int i;
for(i=0;i<3;i++)
{
P3_1=1;
delay();
P3_1=0;
delay();
}
}


void start()
{

scl=1;
sda=1;
while(sda!=0);
/*scl = 1;
delay(); add delay

sda = 0; sda line go LOW first
delay();
scl = 0; then followed by scl line with time delay
*/
}


//unsigned char stop()
void stop()
{
scl=1 ;
sda=0;
while(sda!=1);
//return (0);
}

void ack()
{
sda=1;
scl=0;
while(sda!=0);

}

/*void byte_write()
{
write_data(0xA0); //device address
P3_1=1;
delay();

//ack();
write_data(0x00); //word address
P3_2=1;
delay();
//ack();


}
*/

void byte_read()
{
write_data(0xA1); //device address
ack(); //acknowledge
}

/*void write_data(unsigned char data1)
{
unsigned char i;

for(i=0;i<8;i++)
{
if((data1 & 0x80)==0x80)
sda=1;

else
sda=0;

data1=data1<<1; //shift right for the next bit
delay();

scl = 1; //clock is high
delay ();
scl = 0; //clock is low
}

data1 = sda; //read acknowledge
delay();

scl = 1; //generate 9th clock pulse
delay();
scl = 0; //clock is low
delay(); //to avoid short pulse transition on sda line
return (data1);

}
ack();
}
*/

void write_data(unsigned char input_byte)
{
unsigned char i;
unsigned int mask;

mask = 0x80;

for (i=0; i<8; i++) /* send one byte of data */
{
if (mask & input_byte==1) /* send bit according to data */
sda = 1;


else
sda = 0;

input_byte = input_byte << 1; /* shift right for the next bit */

delay();
scl = 1; /* clock is high */
delay ();
scl = 0; /* clock is low */
}

input_byte= sda; /* read acknowledge */
delay();
scl = 1; /* generate 9th clock pulse */
delay();
scl = 0; /* clock is low */
delay(); /* to avoid short pulse transition on sda line */

ack();


}

unsigned char read_data()
{
unsigned char i;
unsigned int mask, rec_data;

rec_data = 0;
mask = 0x80;
for (i=0; i<8; i++)
{
if (sda)
rec_data |= 0x80;

else
rec_data|=0x00;

mask = mask >> 1;
scl = 1; /* clock is high */
delay();
scl = 0; /* clock is low */
}


delay();
scl = 1; /* clock is high */
delay();
scl = 0; /* clock is low */
sda = 1;
delay(); /* to avoid short pulse transition on sda line */
return (rec_data);


}

void main()
{

unsigned char x=0;

P2=0x00;
P3=0x00;
while(1)
{
scl=1;
sda=1;

// write
start();
//byte_write(); //device address and word address

write_data(0xA0);
delay();
delay();
P3_1=1;


write_data(0x00);
delay();
delay();
P3_2=1;


P3='A';
delay();
delay();
delay();
delay();
delay();

P3=0x00;
write_data('A');
P3_3=1;
delay();
delay();

stop();

//read

start(); //dummy write
//byte_write();

//write_data('A');

start();
byte_read();
x=read_data();

P3=x;

if(x=='A')
{
blink30=1;
}
else
{
blink31=1;
}

stop();

}


}

No comments:

Post a Comment