PDA

View Full Version : Problem making rf remote control



dheeraj91
02-03-2011, 06:35 PM
I tried to make a rf remote control for my bot using 434Mhz rf module and 2 atmega32.
In the transmitter end , I put switches to the first 4 bits of porta using internal pull up resistors.I also put up led's on portb so that each of the led turns on when each of the switch is pressed the same led's are also present in the reciever end to show whether the data is recieved or no.The first four bits of porta are connected to 12V dc motors via a relay control circuit.On the reciever end , i have connected pin 2 of the rf module to the usart reception pin.I have used usart to transmit and recieve data as I had read about that in a tutorial.The controller is clocked at 16Mhz using an external crystal.

I use codevision avr ide to write the programs and the transmitter and reciever programs are attached to this post.

The problem is that the signals are not recieved continuously.Sometimes the reciever shows the led after 5min , sometimes immediately and most of the time it doesnt even show the led.Please help me , I am new to using controllers.

The Transmitter Code:


#include <mega32.h>

#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif

#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif

#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// Write a character to the USART Transmitter
#ifndef _DEBUG_TERMINAL_IO_
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while ((UCSRA & DATA_REGISTER_EMPTY)==0);
UDR=c;
}
#pragma used-
#endif

void USART_Init( unsigned int baud )
{
/* Set baud rate */
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}

// Standard Input/Output functions
#include <stdio.h>

#include <delay.h>

// Declare your global variables here

void main(void)
{
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=P State6=P State5=P State4=P State3=P State2=P State1=P State0=P
PORTA=0xFF;
DDRA=0x00;

// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=0 State1=0 State0=T
PORTD=0x00;
DDRD=0x06;

// USART initialization
// Communication Parameters: 9 Data, 2 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 56000
UCSRA=0x00;
UCSRB=0x0C;
UCSRC=0x8E;
UBRRH=0x00;
UBRRL=0x11;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

delay_ms(100);
USART_Init(56000);

while (1)
{
// Place your code here
PORTB=0xFF;
if(PINA.0==0 && PINA.2==0)
{
putchar(0);
PORTB=0x00;
PORTB.0=1;
PORTB.2=1;
}
else if(PINA.1==0 && PINA.3==0)
{
putchar(1);
PORTB=0x00;
PORTB.1=1;
PORTB.3=1;
}
else if(PINA.0==0 && PINA.3==0)
{
putchar(2);
PORTB=0x00;
PORTB.0=1;
PORTB.3=1;
}
else if(PINA.1==0 && PINA.2==0)
{
putchar(3);
PORTB=0x00;
PORTB.1=1;
PORTB.2=1;
}
else if(PINA.0==0)
{
putchar(4);
PORTB=0x00;
PORTB.0=0;
}
else if(PINA.1==0)
{
putchar(5);
PORTB=0x00;
PORTB.1=0;
}
else if(PINA.2==0)
{
putchar(6);
PORTB=0x00;
PORTB.2=0;
}
else if(PINA.3==0)
{
putchar(7);
PORTB=0x00;
PORTB.3=0;
}
else if(PINA==0xFF)
{
putchar(9);
PORTB=0xFF;
}
};
}


The Reciever Code:


#include <mega32.h>

#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif

#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif

#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// Get a character from the USART Receiver
#ifndef _DEBUG_TERMINAL_IO_
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char status,data;
while (1)
{
while (((status=UCSRA) & RX_COMPLETE)==0);
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
return data;
};
}
#pragma used-
#endif


void USART_Init( unsigned int baud )
{
/* Set baud rate */
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}

// Standard Input/Output functions
#include <stdio.h>

#include <delay.h>

// Declare your global variables here

unsigned char data;
unsigned char check;

void main(void)
{

// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;

// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// USART initialization
// Communication Parameters: 9 Data, 2 Stop, No Parity
// USART Receiver: On
// USART Transmitter: Off
// USART Mode: Asynchronous
// USART Baud Rate: 56000
UCSRA=0x00;
UCSRB=0x14;
UCSRC=0x8E;
UBRRH=0x00;
UBRRL=0x11;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

check=0;
delay_ms(150);
USART_Init(56000);

while (1)
{
// Place your code here
data=getchar();
do
{
data=getchar();
}while(data==check);

if(data==0)
{
PORTB=0x05;
PORTA=0x05;
}
else if(data==1)
{
PORTB=0x0A;
PORTA=0x0A;
}
else if(data==2)
{
PORTB=0x09;
PORTA=0x09;
}
else if(data==3)
{
PORTB=0x06;
PORTA=0x06;
}
else if(data==4)
{
PORTB=0x01;
PORTA=0x01;
}
else if(data==5)
{
PORTB=0x02;
PORTA=0x02;
}
else if(data==6)
{
PORTB=0x04;
PORTA=0x04;
}
else if(data==7)
{
PORTB=0x08;
PORTA=0x08;
}
else if(data==9)
{
PORTB=0xFF;
PORTA=0x00;
}
check=data;
};
}

allbits
02-03-2011, 07:20 PM
I have not read the code, but did you mean that you have connected the first four bits to the switches as well as the motors via relays? Please clarify.

dheeraj91
02-04-2011, 06:39 AM
The first four bits of PORTA in the transmitter are connected to the switches and on the reciever side connected to the dc motors via relay.The transmitter has switches
and the reciever has relays

avinash_g
02-04-2011, 09:09 AM
Hi,

I just Googled

rf communication with atmega32

http://www.google.co.in/search?q=rf+communication+with+atmega32&ie=utf-8&oe=utf-8&aq=t

And I got this page on 1st rank
http://extremeelectronics.co.in/avr-tutorials/rf-communication-between-microcontrollers-%E2%80%93-part-iii/

It describes the whole process in 3 detailed steps. May be it helps!

Also programming problems are solved via a Technique called debugging. Even the most experienced programmer cannot predict bug by just looking at the programmer. You need a JTAG hardware debugger connected with the target board. Then you run the programmer, use single stepping to see what impact each line of your source code has, then you need to use the "Watch" to see the effects on CPU registers and your programmer varriable. In this way you can get close to the bug. Change the program as required.

That was the generic debugging method used in any "computing" platform. In embedded system you need oscilloscopes, logic analyzer to see the signals too. Also another technique is to use a virtual simulator for debugging. Use VMLAB for AVR. It can help a large portion (if not full) to be developed and debugged then only move to real hardware platform for debugging.

Even people here can't just predict the problem by just looking at the code, they too need to debug it!

If you have been to doctors they first "diagnose" the disease then only they medicate or operate !!!! Their are all sort of things like XRAYS, MRI, Ultrasonic, Camera, Blood pressure, Stethoscope etc to first "see" whats going inside the body. Similarly we have Debugger (JTAG), Trace, Logs, Oscilloscopes, Logic analyzer etc.

Hope you and many other got the point

Ulhas
02-07-2011, 11:22 AM
This modules are not reliale, at least my experiance taught me this.
Look here

http://www.roboticsindia.com/showthread.php/3703-Best-reliable-wireless-module-CC2500-based-Sunrom-Model-1215

vigyanabikshu
02-09-2011, 09:51 AM
Hi,
RF Modules will not work well with USART. Use Manchester encoding and decoding method(Not easy method) or use HT12E/HT12D ics or use XBee RF modules(XB24-AWIT-004, www.aqtronics.com).
Regards...






I tried to make a rf remote control for my bot using 434Mhz rf module and 2 atmega32.
In the transmitter end , I put switches to the first 4 bits of porta using internal pull up resistors.I also put up led's on portb so that each of the led turns on when each of the switch is pressed the same led's are also present in the reciever end to show whether the data is recieved or no.The first four bits of porta are connected to 12V dc motors via a relay control circuit.On the reciever end , i have connected pin 2 of the rf module to the usart reception pin.I have used usart to transmit and recieve data as I had read about that in a tutorial.The controller is clocked at 16Mhz using an external crystal.

I use codevision avr ide to write the programs and the transmitter and reciever programs are attached to this post.

The problem is that the signals are not recieved continuously.Sometimes the reciever shows the led after 5min , sometimes immediately and most of the time it doesnt even show the led.Please help me , I am new to using controllers.

The Transmitter Code:


#include <mega32.h>

#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif

#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif

#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// Write a character to the USART Transmitter
#ifndef _DEBUG_TERMINAL_IO_
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while ((UCSRA & DATA_REGISTER_EMPTY)==0);
UDR=c;
}
#pragma used-
#endif

void USART_Init( unsigned int baud )
{
/* Set baud rate */
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}

// Standard Input/Output functions
#include <stdio.h>

#include <delay.h>

// Declare your global variables here

void main(void)
{
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=P State6=P State5=P State4=P State3=P State2=P State1=P State0=P
PORTA=0xFF;
DDRA=0x00;

// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=0 State1=0 State0=T
PORTD=0x00;
DDRD=0x06;

// USART initialization
// Communication Parameters: 9 Data, 2 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 56000
UCSRA=0x00;
UCSRB=0x0C;
UCSRC=0x8E;
UBRRH=0x00;
UBRRL=0x11;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

delay_ms(100);
USART_Init(56000);

while (1)
{
// Place your code here
PORTB=0xFF;
if(PINA.0==0 && PINA.2==0)
{
putchar(0);
PORTB=0x00;
PORTB.0=1;
PORTB.2=1;
}
else if(PINA.1==0 && PINA.3==0)
{
putchar(1);
PORTB=0x00;
PORTB.1=1;
PORTB.3=1;
}
else if(PINA.0==0 && PINA.3==0)
{
putchar(2);
PORTB=0x00;
PORTB.0=1;
PORTB.3=1;
}
else if(PINA.1==0 && PINA.2==0)
{
putchar(3);
PORTB=0x00;
PORTB.1=1;
PORTB.2=1;
}
else if(PINA.0==0)
{
putchar(4);
PORTB=0x00;
PORTB.0=0;
}
else if(PINA.1==0)
{
putchar(5);
PORTB=0x00;
PORTB.1=0;
}
else if(PINA.2==0)
{
putchar(6);
PORTB=0x00;
PORTB.2=0;
}
else if(PINA.3==0)
{
putchar(7);
PORTB=0x00;
PORTB.3=0;
}
else if(PINA==0xFF)
{
putchar(9);
PORTB=0xFF;
}
};
}


The Reciever Code:


#include <mega32.h>

#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif

#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif

#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// Get a character from the USART Receiver
#ifndef _DEBUG_TERMINAL_IO_
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char status,data;
while (1)
{
while (((status=UCSRA) & RX_COMPLETE)==0);
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
return data;
};
}
#pragma used-
#endif


void USART_Init( unsigned int baud )
{
/* Set baud rate */
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}

// Standard Input/Output functions
#include <stdio.h>

#include <delay.h>

// Declare your global variables here

unsigned char data;
unsigned char check;

void main(void)
{

// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;

// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// USART initialization
// Communication Parameters: 9 Data, 2 Stop, No Parity
// USART Receiver: On
// USART Transmitter: Off
// USART Mode: Asynchronous
// USART Baud Rate: 56000
UCSRA=0x00;
UCSRB=0x14;
UCSRC=0x8E;
UBRRH=0x00;
UBRRL=0x11;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

check=0;
delay_ms(150);
USART_Init(56000);

while (1)
{
// Place your code here
data=getchar();
do
{
data=getchar();
}while(data==check);

if(data==0)
{
PORTB=0x05;
PORTA=0x05;
}
else if(data==1)
{
PORTB=0x0A;
PORTA=0x0A;
}
else if(data==2)
{
PORTB=0x09;
PORTA=0x09;
}
else if(data==3)
{
PORTB=0x06;
PORTA=0x06;
}
else if(data==4)
{
PORTB=0x01;
PORTA=0x01;
}
else if(data==5)
{
PORTB=0x02;
PORTA=0x02;
}
else if(data==6)
{
PORTB=0x04;
PORTA=0x04;
}
else if(data==7)
{
PORTB=0x08;
PORTA=0x08;
}
else if(data==9)
{
PORTB=0xFF;
PORTA=0x00;
}
check=data;
};
}

dheeraj91
02-12-2011, 01:07 PM
How should I make manchester encoding and decoding ?Give me some link or so where i can learn how to do it.

Do zigbee modules work fine on the usart transmission.

allbits
02-12-2011, 06:01 PM
the point is, these modules are "AC coupled." If you connect a logic 'high' to the input of the transmitter, you will not get that logic high at the receiver. These modules require a change in the input signal, hence the term 'ac coupled'

So if you connect a sequence of pulses, you will receive the same pulse train at the receiver end. you can verify this with the help of an oscilloscope. but then, there is a limit to thedata rate, and normally these modules work best at a baud of 2400 bits per second. When you do a continuous serial transmission, there is still chance for a continuous steadystate - like, when you transmit a '0', or an FF.

With no parity, one stop bit, 8 data bits, the transmitted signal for FF will be :

0 11111111 1. With so many 'ones' or logichigh together, error the receiver fails as there is no change in the signal.

With manchester coding, you need not use a hardware usart. you have to generate the manchester code. The good thing about manchester code is that, it is a return-to-zero coding scheme, which means the data will return to zero on each bit. google can find you the link easily. think and try to code it, itis not very difficult.

Or else zigbee always work, you dont need to configure anything - just use in transparent mode. You can pump up the baud rate to 9600. Take care of the vcc. you have to give 3.3 V and not the 5V.

good luck.

vigyanabikshu
02-12-2011, 06:22 PM
Manchester encoding library is available on mikroC PIC compiler.