Discussion in "8051 Discussion Forum" started by    Gastonio    Sep 9, 2010.
Fri Feb 18 2011, 09:28 pm
#91
Schematics a bit later. Working codes:

Tx:
#include<intrins.h>

#include<reg52.h>

#include<math.h>

#include<stdio.h>

void DelayMs(unsigned int count);
sbit DQ=P2^7;
sbit TE=P3^7;	

void delay(int useconds)
{
int s;
for (s=0; s<useconds;s++);
}

unsigned char ow_reset(void)
{
unsigned char presence;
DQ = 0; //pull DQ line low
delay(29); // leave it low for 480us
DQ = 1; // allow line to return high
delay(3); // wait for presence
presence = DQ; // get presence signal
delay(25); // wait for end of timeslot
return(presence); // presence signal returned
} // 0=presence, 1 = no part   

unsigned char read_bit(void) 
{
unsigned char i;
DQ = 0; // pull DQ low to start timeslot
DQ = 1; // then return high
for (i=0; i<3; i++); // delay 15us from start of timeslot
return(DQ); // return value of DQ line
}

void write_bit(char bitval)
{
DQ = 0; // pull DQ low to start timeslot
if(bitval==1) DQ =1; // return DQ high if write 1
delay(5); // hold value for remainder of timeslot
DQ = 1;
}

unsigned char read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i=0;i<8;i++)
{
if(read_bit()) 
value|=0x01<<i; // reads byte in, one byte at a time and then shifts it left. If DQ=0, skip, if DQ=1 execute shifting of "1" then OR.
delay(6); // wait for rest of timeslot
}
return(value);
}// Delay provides 16us per loop, plus 24us. Therefore delay(5) = 104us

void write_byte(char val)
{
unsigned char i;
unsigned char temp2;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
temp2 = val>
>
i; // shifts val right 'i' spaces
temp2 &= 0x01; // copy that bit to temp
write_bit(temp2); // write bit in temp into
}
delay(5); 
}

unsigned char tmp1, tmp2;

void get_temp(void)
{	
	ow_reset();
	write_byte(0xCC);
	write_byte(0x44);
	ow_reset();
	write_byte(0xCC);
    write_byte(0xBE);
	tmp1=read_byte();
	tmp2=read_byte();
}

void main()
{		
	while(1)
	{
	get_temp();
 P1=tmp1; // LSB 4 bit of temp1  
 TE=0; // enable transmission   	  
 DelayMs(300); // can be tune better keep 500 or more
 TE=1;
 DelayMs(200);
 
 P1=tmp1>
>
4; // MSB 4 bit of temp1
 TE=0; // enable transmission
 DelayMs(300); // can be tune better keep 500 or more
 TE=1;
 DelayMs(200);

 P1=tmp2; // LSB 4 bit of temp1
 TE=0; // enable transmission
 DelayMs(300); // can be tune better keep 500 or more 
 TE=1;
 DelayMs(200);
 
 P1=tmp2>
>
4; // MSB 4 bit of temp1
 TE=0; // enable transmission
 DelayMs(300); // can be tune better keep 500 or more
 TE=1;
 DelayMs(200);
	}
}

void DelayMs(unsigned int count)
 { 
 unsigned int i; 
 while(count) {
 i = 115;
 while(i>
0) i--;
 count--;
 }}

Rx:
#include<reg51.h>

#include<1602.h>

#include<intrins.h>

#include<stdio.h>

unsigned char buffer[16];
unsigned char cdis1[16] = {"  TEMPERATURE:  "};
unsigned char cdis2[16] = {"           C    "}; 
sbit VT =P3^7; // VT is connected to p3.7
 
 float Receiver ()
 { 
 float temp1;
 bit flag;
 unsigned int temp;
 unsigned char byte1,byte2,res1,res2;

 while(VT==0); //waiting to incoming data
 byte1=(P1 & 0x0f);
 while(VT==1);
 while(VT==0); // for next 4 bits
 byte2=(P1 & 0x0f);
 res1=(byte2<<4) | (byte1); // now res1 has value of temp1 that was in tx side
 while (VT==1);
 
 while(VT==0); //waiting to incoming data
 byte1=(P1 & 0x0f);
 while(VT==1);
 while(VT==0); // for next 4 bits
 byte2=(P1 & 0x0f);
 res2=(byte2<<4) | (byte1);
 while(VT==1); // for next 4 bits
  
 temp=res2<<8;
 temp=temp+res1; 
 flag=temp&&0xf800;

 	if(flag==0)
	{
		temp1=(~temp+1)*0.0625;
		temp1=temp1*-1;	
	}
	else
	temp1=temp*0.0625;
	return temp1;	
    }

void main()
 {
 float temp1;
 unsigned int temp2;
 init();
 writeCmd(0x80);
 sendstring(cdis1);
 writeCmd(0xC0);
 sendstring(cdis2);
 writeCmd(0xCA);	
 writedat(0xDF);
 
 while(1)
 {
  temp1 = Receiver(); 
  temp2 = temp1 * 100;
  sprintf(buffer, "%d.%02d", temp2/100, temp2%100);
  writeCmd(0xC4);
  sendstring(buffer);
  temp2=0x00;
}}


[ Edited Mon Feb 21 2011, 08:28 pm ]
Tags rf communication 8051wireless communication microcontrollermicrocontroller wireless data transferHM-R868 TM-R868
Fri Feb 18 2011, 09:49 pm
#92
reduce the delay and tell about schematic and final code so it helps others
Mon Feb 21 2011, 11:21 pm
#93
Schematics:



[ Edited Mon Feb 21 2011, 11:24 pm ]
Tue Feb 22 2011, 08:25 am
#94
good work and share
Sat Mar 26 2011, 04:06 pm
#95
Hi Gastonio,

I m doing almost same kind of project.
I saw your schematic. How did you got HT12E chips in proteus.
Did you created a library.
Sat Mar 26 2011, 04:43 pm
#96
@ kirangowle
encoders r not in proteus
he make it custom ic package not a library
it will not work in proteus
Sat Mar 26 2011, 04:59 pm
#97
good work Gastonio.really good for beginner like me.
Sat Apr 23 2011, 03:00 pm
#98
Yes, Majoka is right, I did a custom IC package in Proteus. And there is one mistake. Resistor from decoder should be placed to encoder IC and vice versa.
I've already made PCB's for both transmitter and receiver.
There is also mistake in showing negative temperature which I solved too:
flag=temp&0xF800;

if(flag == 0)
{
temp1=temp*0.0625;
}
if (flag == 0xF800)
{
temp1=(~temp+1)*0.0625;
temp1=temp1*-1;
}
return temp1;

And one more thing that should be noted is that when transimtter and receiver are in seperate boards and are powered by seperate power sources it is esential to add some kind of synchro character. For example, send number 0x09 first and at the receiver side check if this number is received. And if it is received, then start receiving temperature bits.
I am thinking of making more sophisticated synchro thing, because it is not working as good as I would want. But it's not bad either. It is working well, when you turn off the receiver and turn it on again. You will see the reight temp value again. Without syncro character, you will lose synchronzation in case if receiver is turned off.


[ Edited Sat Apr 23 2011, 03:01 pm ]
Mon Apr 25 2011, 10:46 pm
#99
is it RF nice.. i should tag this post.
Mon Apr 25 2011, 11:26 pm
yes ajay its RF

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm
Bernardwarge
Tue Mar 26 2024, 11:15 am