Discussion in "8051 Discussion Forum" started by    Gastonio    Sep 9, 2010.
Tue Feb 15 2011, 05:45 pm
#81
i am referring to this code

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


declare temp1 and 2 as global variable and calling the get_temp() function temp1 and temp2 has some temperature values now u has to send it using 4 bie encoder and decoder
do it as
let say encoder 4 bits is connected to port1 and be remember ur sending 4 bit numeric data so data will not more than 9 in 4 bits so above 9 to 15 u can use any no for starting of ur message of temperature.

p1=temp1;    // LSB 4 bit of temp1
TE=1;  // enable transmission
delayms(700);  // can be tune better keep 500 or more

p1=temp1>
>
4;    // MSB 4 bit of temp1
TE=1;  // enable transmission
delayms(700);  // can be tune better keep 500 or more

now all temp1 is transmitted
in the same way transmit temp2

p1=temp2;    // LSB 4 bit of temp1
TE=1;  // enable transmission
delayms(700);  // can be tune better keep 500 or more

p1=temp2>
>
4;    // MSB 4 bit of temp1
TE=1;  // enable transmission
delayms(700);  // can be tune better keep 500 or more



now come to receiving side
unsigned char byte1,byte2,res1,res2;
while(VT==1);   //waiting to incoming data
byte1=(p1 & 0x0f);
while(VT==0); 
while(VT==1); // for next 4 bits
byte2=(p1 & 0x0f);
res1=(byte2<<4) | (byte1);
while(VT==0); 
// now res1 has value of temp1 that was in tx side

while(VT==1);   //waiting to incoming data
byte1=(p1 & 0x0f);
while(VT==0); 
while(VT==1); // for next 4 bits
byte2=(p1 & 0x0f);
res2=(byte2<<4) | (byte1);
Wed Feb 16 2011, 05:21 pm
#82
Well, I've modified program again. The temp value is seen, however on the start up, value of 80.00 is seen when it should be 85.00. It seems that only MSB of 16bits are used and no LSB bits. Both programs works the same: mine and the program which Majoka provided.

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(700); // can be tune better keep 500 or more
 
 P1=tmp1>
>
4; // MSB 4 bit of temp1
 TE=0; // enable transmission
 DelayMs(700); // can be tune better keep 500 or more
 
 P1=tmp2; // LSB 4 bit of temp1
 TE=0; // enable transmission
 DelayMs(700); // can be tune better keep 500 or more
 
 P1=tmp2>
>
4; // MSB 4 bit of temp1
 TE=0; // enable transmission
 DelayMs(700); // can be tune better keep 500 or more
	}
}

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>

void DelayMs(unsigned int); 
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==1); //waiting to incoming data
 byte1=(P1 & 0x0f);
 while(VT==0);
 while(VT==1); // for next 4 bits
 byte2=(P1 & 0x0f);
 while (VT==0);
 res1=(byte2<<4) | (byte1);
 // now res1 has value of temp1 that was in tx side
 
 while(VT==1); //waiting to incoming data
 byte1=(P1 & 0x0f);
 while(VT==0);
 while(VT==1); // for next 4 bits
 byte2=(P1 & 0x0f);
 while (VT==0);
 res2=(byte2<<4) | (byte1);
 
 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 char buffer[10];
 unsigned int j;
 init();
 while(1)
 {
  temp1 = Receiver();
  sprintf(buffer,"%.2f",temp1);
  writeCmd(0x80);
  sendstring(buffer); 
  for(j=0; j<10000; j++); 
}}
Wed Feb 16 2011, 11:02 pm
#83
the algo ur using for floating in rx site do it on tx side and do it in simple way to trouble shoot as ur using built in libraries so can't guess where should be error because communication is wireless
let say in tx site simple consider that temp1='A' and temp2='B'
do not call temperature functions
simple assign as
#include<reg52.h>
void DelayMs(unsigned int count);
sbit TE=P3^7;  

unsigned char tmp1, tmp2;

void main()
{  
temp1='A';
temp2='B';            
        while(1)
        {

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

}
}

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


in RX side
#include <reg52.h>
void DelayMs(unsigned int);
sbit VT =P3^7; // VT is connected to p3.7

void main()
 {
  unsigned char byte1,byte2,res1,res2;
 while(1)
 {

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

it will verify that A and B is coming after that it will sure all is ok just problem is in using ur algo with libraries


[ Edited Wed Feb 16 2011, 11:03 pm ]
Thu Feb 17 2011, 02:22 am
#84
You know, Majoka, I tried to find the problem the whole day. I tried your solution by sending 85 degrees temperature value by sending 0x05 and 0x50. And it isn't reveived right. I just always get 80.00 and when I send 125 degree code - 2000. As you say, it's a problem with libraries.
What can you suggest in place of those libraries? The algorythm of bit manipulations is really good.
Thu Feb 17 2011, 12:13 pm
#85
try ones the above code i write
and tell is the lcd is displaying A and B then we come to libraries
please try ones
Thu Feb 17 2011, 09:04 pm
#86
Hi, Majoka. I am receiving in the LCD the following things: $BA$ AB $BA$ AB $BA$ and so on...

Used these codes:

Tx:
#include<intrins.h>

#include<reg52.h>

#include<math.h>

#include<stdio.h>

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

unsigned char tmp1, tmp2;

void get_temp(void)
{	
	tmp1='A';
	tmp2='B';
}

void main()
{		
	while(1)
	{
	get_temp();

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

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 res1, res2;
sbit VT =P3^7; // VT is connected to p3.7 
 
 void Receiver (void)
 { 
 unsigned char byte1,byte2;
 
 while(VT==1); //waiting to incoming data
 byte1=(P1 & 0x0f);
 while(VT==0);
 while(VT==1); // for next 4 bits
 byte2=(P1 & 0x0f);
 res1=(byte2<<4) | (byte1); // now res1 has value of temp1 that was in tx side
 while (VT==0);
 
 while(VT==1); //waiting to incoming data
 byte1=(P1 & 0x0f);
 while(VT==0);
 while(VT==1); // for next 4 bits
 byte2=(P1 & 0x0f);
 res2=(byte2<<4) | (byte1);
 }

void main()
 {
 init();
 while(1)
 {
  Receiver();
  writeCmd(0xC4);
  writedat(res1);
  writedat(res2);
}}


[ Edited Thu Feb 17 2011, 09:53 pm ]
Fri Feb 18 2011, 01:02 am
#87
hi Gastonio
reduce delay from 500
as encoder data sheet say

Operation:
The 212 series of encoders begin a 4-word transmission cycle upon receipt of a transmission enable
(TE for the HT12E or D8~D11 for the HT12A, active low). This cycle will repeat itself as long as the
transmission enable (TE or D8~D11) is held low. Once the transmission enable returns high the encoder
output completes its final cycle and then stops



Information word:
If L/MB=1 the device is in the latch mode (for use with the latch type of data decoders). When the transmission
enable is removed during a transmission, the DOUT pin outputs a complete word and then
stops. On the other hand, if L/MB=0 the device is in the momentary mode (for use with the momentary
type of data decoders). When the transmission enable is removed during a transmission, the DOUT
outputs a complete word and then adds 7 words all with the 1 data code.


so be sure L/MB=1
in tx site just tune delay a
in rx side do this
unsigned char res1, res2;
sbit VT =P3^7; // VT is connected to p3.7
 
 void Receiver (void)
 {
 unsigned char byte1,byte2;
 
 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
 }

void main()
 {
 init();
 while(1)
 {
  Receiver();
  writeCmd(0xC4);
  writedat(res1);
  writedat(res2);
res1=res2=0x00;
}
}

now update about ur results with code
if results are not favorable then connect the DOUT pin of encoder to DIN pin of decoder with wire remove wireless modules or a while to be insure that every thing is ok problem is just with wireless communication
hope now problem will solve


[ Edited Fri Feb 18 2011, 01:03 am ]
Fri Feb 18 2011, 01:25 am
#88
Thanks, Majoka, but it's the same... I've also removed remote control modules and connceted DOUT and DIN with wires as you said. The same symbols are being received when sending AB. There is no problem with wireless communication, but with algo.
What is more, I am using HT-12E encoder not an HT-12A.


[ Edited Fri Feb 18 2011, 02:06 am ]
Fri Feb 18 2011, 10:39 am
#89
ok now try these codes the symbol $ is equal to swaping of A
now try these tx and rx and tell about results
use these codes with wire connections if it work then use with rf link
tx:
#include<reg52.h>
void DelayMs(unsigned int count);
sbit TE=P3^7;  

unsigned char tmp1, tmp2;

void get_temp(void)
{      
tmp1='A';
tmp2='B';
}

void main()
{              
        while(1)
        {
        get_temp();

 P1=tmp1; // LSB 4 bit of temp1      
 TE=0;    // enable transmission
 DelayMs(500); // can be tune better keep 500 or more
  TE=1;    // disable transmission
 DelayMs(500); // can be tune better keep 500 or more

 P1=tmp1>
>
4; // MSB 4 bit of temp1
 TE=0; // enable transmission
 DelayMs(500); // can be tune better keep 500 or more
   TE=1;    // disable transmission
 DelayMs(500); // can be tune better keep 500 or more

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

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

}
}

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


on rx side use:
unsigned char res1, res2;
sbit VT =P3^7; // VT is connected to p3.7
 
 void Receiver (void)
 {
 unsigned char byte1,byte2;
 
 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
 }

void main()
 {
 init();
 while(1)
 {
  Receiver();
  writeCmd(0xC4);
  writedat(res1);
  writedat(res2);
res1=res2=0x00;
}
}

be sure connection r ok p1.0 is with AD0
P1.1=AD1
P1.2=AD2
P1.3=AD3
use wired connection
 Gastonio like this.
Fri Feb 18 2011, 08:02 pm
#90
Thank you Majoka!!! It is working! A bit slow, but it works great.

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