Microcontroller › 8051 › one wire communication › Hi, void main() { unsigned
August 3, 2011 at 6:30 am
#6561
lucky
Participant
Hi,
void main()
{
unsigned char i,dat[5];
reset();
write_byte(0xCC); //skip rom command
write_byte(0x0F); //write scratchpad
write_byte(0x00); //TA1 – data
write_byte(0x00); //TA2 – data
write_byte(10); //data to memory location 0x10
write_byte(14);
reset();
write_byte(0xCC); //skip rom command
write_byte(0xAA); //read scratchpad command
for(i=0;i<5;i++)
{
dat=read_byte();
}
reset();
write_byte(0xCC); //skip rom command
write_byte(0x55); //copy scratchpad
write_byte(0x00); //TA1 – data
write_byte(0x00); //TA2 – data
write_byte(0x01); //ending off set
ms_delay(5);
reset();
// while(1);
}
void reset(void)
{
unsigned char presence;
write0();
ms_delay(500); //480
write1();
ms_delay(80); //70
presence = read_bit();
ms_delay(420); //410
write1(); //– not required
}
void write_byte(unsigned char w_buff)
{
unsigned char w_cnt = 0;
// P3=w_buff;
while(w_cnt<
{
// P2=w_buff;
if(w_buff & 0x01) //to send LSB first
{
write1();
}
else
{
write0();
}
w_buff = w_buff >> 1;
w_cnt++;
}
}
unsigned char read_byte(void)
{
unsigned char temp=0,r_cnt=0,r_buff;
while(r_cnt<
{
temp = read_bit();
if(temp)
{
r_buff |= 1;
}
r_buff=r_buff << 1;
r_cnt++;
}
P3=r_buff;
return(r_buff);
}
void write0(void)
{
ds=0;
ms_delay(70); //actual delay 60us
ds=1;
ms_delay(10);
}
void write1(void)
{
ds=0;
ms_delay(10); //actual delay 6us
ds=1;
ms_delay(65); //actual delay 54us
}
unsigned char read_bit(void)
{
unsigned char result;
ds=0;
ms_delay(10); //actual delay 6us
ds=1;
ms_delay(19); //actual dealay 11us
result=ds;
ms_delay(60); //sctual delay 45us
return(result);
}
this is my code for writing data into memory, coming to circuit iam using proteus..in that i was connected data pin of eeprom to port pin and i was connected a pullup to that wire…..