Discussion in "8051 Discussion Forum" started by    Gastonio    Sep 9, 2010.
Sun Feb 13 2011, 08:25 pm
#71
Hi, I've returned to my project. And now I am trying to send symbols using wirless communicaiton with encoder and decoder and I am having problems. I want to send A (0x41) and revceive it on the receiver end. First I am sending LSB of 0x41 and then shift it to the right and send MSB. And in the reveiver side I shift MSB four places to the left and add LSB, however on the LCD i just see D. Could you please take a look at those programs and say what's wrong with them?

Edit: I've added delay, so now I can receive A. The problem is that this A blinks on the LCD. Maybe I shoud make seperate function for receiving and in the main() function just write the whole symbol?

Tx:
#include<reg51.h>

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

 void main()
 {
 while(1)
 {
 A=0x41;
 TE=0;
 P1=A;
 DelayMs(500);
 TE=1;
 A=A>
>
4;
 DelayMs(500);
 TE=0;
 P1=A;
 DelayMs(500);
 TE=1;
 }}

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

Rx:
#include<reg51.h>

#include<1602.h>

void DelayMs(unsigned int);
unsigned char temp;
unsigned char temp2;
unsigned char temp3;
unsigned char mybyte; 
sbit led1=P2^0;
sbit VT =P3^7; // VT is connected to p3.7

void main()
 {
 init();
 
 while(1)
 {
 while (VT==1);
 mybyte=P1;
 temp=mybyte;
 
 DelayMs(500);

 while (VT==1);
 mybyte=P1;
 temp2=mybyte;

 temp2 = temp2<<4;
 temp = temp2 + temp;
 writeCmd(0x80);
 writedat(temp);
 led1=~led1; 
}}

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


[ Edited Sun Feb 13 2011, 09:17 pm ]
Sun Feb 13 2011, 10:18 pm
#72
I've just realised now, how much problems will I face using encoder and decoder... I can't even imagine how to send and then show float value of temperature on the receiver side.
Sun Feb 13 2011, 11:38 pm
#73
hi Gastonio
ru check it in Proteus
in proteus it will be verified that ur logic is ok or not and also tell about ur connection of controller port to encoder and decoder
Mon Feb 14 2011, 12:28 am
#74
Connecetions are ok. I'll use this code which I prepared to use without encoder and decoder. I'll have to modify it.
I was thinking of sending 16bits: tmp1 and tmp2. And then use the rest of the code in the receiver side. Will it be a good option? I believe that doing so will solve the problem of displaying float value of temperature.

#define uchar unsigned char
#define uint unsigned int
#include<intrins.h>

#include<reg52.h>

#include<math.h>

#include<stdio.h>


sbit DQ=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;
}// Delay provides 16us per loop, plus 24us. Therefore delay(5) = 104us	  

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); 
} 

float get_temp()
{
	uchar tmp1,tmp2;
	float temp;
	uint tmp;
	bit flag;
	ow_reset();
	write_byte(0xCC);
	write_byte(0x44);
	ow_reset();
	write_byte(0xCC);
    write_byte(0xBE);
	tmp1=read_byte();
	tmp2=read_byte();
	tmp=tmp2*256;
	tmp=tmp+tmp1;
	flag=tmp&&0xf800;
	if(flag==0)
	{
		temp=(~tmp+1)*0.0625;
		temp=temp*-1;	
	}
	else
	temp=tmp*0.0625;
	return temp;	
}

void sercon(void)
{
	SCON   = 0x50;		
	TMOD  |= 0x20;		
	TH1     = 0xF3;             
	TR1     = 1;                  	
	TI	   = 1;               
}

void main()
{
    float temp;
    uint cnt = 0;

	sercon();

	while(1)
	{
		temp = get_temp();

		printf("%.2f\r\n",temp);
		cnt = 10000;
		for(;cnt >
 0;cnt--)
		{};		
	}
} 


[ Edited Mon Feb 14 2011, 12:29 am ]
Mon Feb 14 2011, 06:30 pm
#75
but uart can send 8 bit at a time so when u send 16 bit no it can't be sent directly it also has ti break into two characters
Mon Feb 14 2011, 10:58 pm
#76
I am using 4bit Encoder and Decoder. I want to modify that code to use it with encoder and decoder.
Is it even possible to use encoder and decoder with the DS18B20?


[ Edited Mon Feb 14 2011, 11:03 pm ]
Tue Feb 15 2011, 03:10 am
#77
First success! I am able to see values on the LCD using encoder and decoder. The values aren't correct. But I guess I need some more software tuning. Maybe there are mistakes in the sequence of bit manipulations or whatever. I am adding those programs here. Could you look at them?

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;
}// Delay provides 16us per loop, plus 24us. Therefore delay(5) = 104us

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 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;	
}

void main()
{
unsigned char tmp1,tmp2; 

	while(1)
	{
	tmp1=get_temp();
	tmp2=get_temp();
	
	TE=0;
 	
	P1=tmp1;
 	DelayMs(500);

 	tmp1=tmp1>
>
4;
 	P1=tmp1;
 	DelayMs(500);
	
 	P1=tmp2;
 	DelayMs(500);

 	tmp2=tmp2>
>
4;
  	P1=tmp2;
 	DelayMs(500);
	}
}

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);
unsigned char temp;
unsigned char temp1;
unsigned char temp2;
unsigned char temp3;
unsigned char temp4;
unsigned char mybyte; 
sbit led1=P2^0;
sbit VT =P3^7; // VT is connected to p3.7

 float Receiver ()
 { 
 float temp5;
 bit flag;
 
 while (VT==1);
 mybyte=P1;
 temp=mybyte;
 
 DelayMs(500);

 while (VT==1);
 mybyte=P1;
 temp1=mybyte;

 DelayMs(500);
 
 while (VT==1);
 mybyte=P1;
 temp2=mybyte;
 
 DelayMs(500);
 
 while (VT==1);
 mybyte=P1;
 temp3=mybyte;

 temp1 = temp1<<4;
 temp = temp1 + temp;

 temp3=temp3<<4;
 temp2=temp3+temp2;
 
 temp4=temp2<<8;
 temp4=temp4+temp; 

 flag=temp4&&0xf800;

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

 led1=~led1; 
 return(temp5);
 }

void main()
 {
 float temp5;
 unsigned char buffer[10];
 init();
 while(1)
 {
  temp5 = Receiver();
  sprintf(buffer,"%.2f",temp5);
  writeCmd(0x80);
  sendstring(buffer); 
}}

 void DelayMs(unsigned int count)
 { 
 unsigned int i; 
 while(count) {
 i = 115;
 while(i>
0) i--;
 count--;
 }}
Tue Feb 15 2011, 04:01 am
#78
I guess problem is in returning multiple values in Tx side.

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;
}

Is there a way to return both values?
Tue Feb 15 2011, 06:44 am
#79


Is there a way to return both values?

Gastonio


You can only "return" one item,
but you could store your values in global variables before returning.
Tue Feb 15 2011, 03:43 pm
#80
I'll try to put the whole content of that function to the main function. I think that will be the same as making those vaules as global variables.

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