Help with AVR USART?
Hello...
Im trying to make a set of RF remote switches using an atmega16.
I want to push a momentary button on the Tx board and effectively have a relay connected to one of the output pins TRIGGERED on the Rx board.
I have attached the code I did for this, I send the entire data=PINC; through uart and catch it on the other end and do PORTC=data; but with this approach I would not be able to TRIGGER the output pin. Please help.....
TX side code....
#include <avr/io.h>
#include <util/delay.h>
#include "usart.h"
void main()
{
//Initialize the USART with Baud rate = 2400bps
USARTInit(416);
//Enable Internal Pullups on PORTC
PORTC=0xFF;
/*
Keep transmitting the Value of Local PORTC
to the Remote Station.
On Remote RX station the Value of PORTC
sent on AIR will be latched on its local PORTC
*/
uint8_t data;
while(1)
{
data=PINC;
/*
Now send a Packet
Packet Format is AA<data><data inverse>Z
total Packet size if 5 bytes.
*/
//Stabilize the Tx Module By Sending JUNK data
UWriteData('J'); //J for junk
//Send 'A'
UWriteData('A');
//Send Another 'A'
UWriteData('A');
//Send the data;
UWriteData(data);
//Send inverse of data for error detection purpose
UWriteData(~data);
//End the packet by writing 'Z'
UWriteData('Z');
//Wait for some time
_delay_loop_2(0);
_delay_loop_2(0);
_delay_loop_2(0);
_delay_loop_2(0);
}
}
RX Side code
#include <avr/io.h>
#include "usart.h"
void main()
{
uint8_t i; //Clasical loop varriable
uint8_t packet[5],data=0;
DDRC|=0xFF; //All Output
//Initialize the USART with Baud rate = 2400bps
USARTInit(416);
/*
Get data from the remote Tx Station
The data is the value of PORTC on Remote Tx Board
So we will copy it to the PORTC of this board.
*/
while(1)
{
//Wait for a packet
while(!UDataAvailable());
if(UReadData()!='A') continue;
while(!UDataAvailable());
if(UReadData()!='A') continue;
while(UDataAvailable()!=3);
//Get the packet
for(i=2;i<5;i++)
{
packet[i]=UReadData();
}
//Is it ok?
if(packet[2]!=((uint8_t)~packet[3])) continue;
if(packet[4]!='Z') continue;
//The packet is ok
data=packet[2];
//Now we have data put it to PORTC
PORTC=data;
}
}
Comments