serial wireles communication with atmega16 problem

Go To Last Post
22 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I using serial wireless communication for my new bot...i decided to test a sample program to switch a led on and off using serial communication...iam using an ask 433 mhz wireless transmitter for that and an bafo 81 usb t serial converter...

i connected pin 3 of db9 to the pin 13 of maxx 232

pin 12 of max 232 to the transmitter data pin

at the receiver side iam using the data pin to connect to the rxd pin of mcu....

at 1st before any serial command is issued i programmed the mcu to blnk the led...after i issue any commands then the blinking will stop and the led will be switched on or off according to my will...

if i transmit 'a' then the led will be switched on...if i transmit 'd' then the led will be switched off...iam using the atmega16 mcu at the receiver side and i have switched on global serial receive nterrupt on...but no matter wat i send from the pc the led kees on blinking as if the mcu is receiving no serial commands...the max 232 is fully operational and iam transmitting my data from a vb program
my serial port settings are
port no-com9
data bits-8
parity-none
stop bits-1

my vb programis tis...written in vb 2008 express edition

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MsgBox("hi how are u")
        SerialPort1.Open()



    End Sub

    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        MsgBox("u clicked")

    End Sub

  

    Private Sub switchon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles switchon.Click
        SerialPort1.Write(Chr(97))




    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SerialPort1.Write(Chr(100))

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        SerialPort1.Close()


    End Sub
End Class

and my mcu program is this

#include  
#include  
#include 

#define USART_BAUDRATE 38400 
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) 

int f=0;

void portinitialize();
void uartinitialize();
void handleleft();
void handleright();
void bytedecision(char);


void portinitialize()
 {
  DDRD|=(1<<4);
  TCCR1B|=(1<<CS12);
  }

void uartinitialize()
 {
   UCSRB |= (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and reception circuitry 
   UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes 

   UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register 
   UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register 

   UCSRB |= (1 << RXCIE); // Enable the USART Recieve Complete interrupt (USART_RXC) 
   sei(); // Enable the Global Interrupt Enable flag so that interrupts can be processed 
 }
void handleleft()
 {
  int m=0;
  PORTD&=~(1<<4);
  
  for(m=0;m<=100;m++)
  _delay_ms(10);
 }

void handleright()
 {
  int m=0;
  PORTD|=(1<<4);

  for(m=0;m<=100;m++)
  _delay_ms(10);
  }

void bytedecision(char a)
 { 
  if(a=='a' || a=='A')
   {
    handleleft();
    }
  if(a=='d' || a=='D')
   {
    handleright();
   }
 }

int main (void) 
{ 
   portinitialize();
   uartinitialize();
   
   for(;;) // Loop forever 
   { 
     if(f==0)
	   
	    
     {
  
    if(TCNT1>=31250)
	 
	 {
	  PORTD^=(1<<4);  // run test program blinking- echoing is handled by the ISR instead of in the main loop 
	  TCNT1=0;
	 }
	 }
 }
       
      
} 

ISR(USART_RXC_vect) 
{ 
   f=1;
   char ReceivedByte; 
   ReceivedByte = UDR; // Fetch the recieved byte value into the variable "ByteReceived" 
   //UDR = ReceivedByte; // Echo back the received byte back to the computer 
   bytedecision(ReceivedByte);
} 

plz this is long but do help me

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Where is the problem? Is it with the PC program? Or the AVR? or the wireless link? Or there could be a number of problems. Narrow down the possibilities then you can start to solve the problem.

First up, sending serial data via the cheap ask tx/rx is bound to have problems (as many have found). Some people say they do get it to work, but I've yet to see a commercial product that sends uart format data - usually it is encoded similar to a IR remote control format or manchester encoded.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i really dont know where the problem lies....i ahev done all the connections properly....connections to max 232 is right the mcu program is right....so i really dont knw where the problem lies...

wat do u mean by encoding ...wat kinda encoder do u mean to use....and if not ask rx/tx then which module/...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What about Grounding? Rf singnal needs carefull grouding... It needs a seprate ground plane to minimize the noise... For Rf signal the PCB should be atleast two layer board, where one layer is completly dedicated to ground plane....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the transmitter circuit is on a breadboard and the receiver is in the controller board...i dont think noise is a factor coz the receiver side is receiving nothing...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i checked all circuits.... pin1 and 3 shorted with 10 uf , pin 4 and 5 with 10 uf...pin 2 and vc shorted with 10 uf....pin 6 and gnd shorted with 10 uf.....

txd pinof db9(pin 3)....connected to pin13 of max 232
rxd pin of atmega16(pin pd0)...connected to pin 12 of max232

all circuits are ok..i there sumthing wrong in the code...coz even via hyperterminal and directly connecting the mcu to the max232 i cant transfer the databits... :(

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Unless the RF transmitter and receiver you are using have a data encoder/decoder on them, you cannot simply connect it to a serial port and expect it to work. RF is noisy, and reliable transmission is complicated.

Perhaps look into a ZigBee module, as it will have the underlying protocol to maintain reliable communications.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

as i said bfore i cant communicate even with direct connection...between max 232 and mcu...plzz see if theer is sumthing wrong in the code or the pin config i described above...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

do you only have the 1 wire (TX) connected between the PC and the MAX232? If so... you also need to connect the ground (pin 5)

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

no ground of seril port is connected with ground of max 232...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what is FCPU being defined as, and are you sure that the fuses are set correctly that you are actually using that clock/speed?

If you have a scope, write a simple program for the AVR that, initializes the UART the way you have abive, and then simply stuffs UDR constantly with a capital U.

Load it onto the AVR, and then put the scope on the TX pin of the AVR... the frequency you measure is 1/2 the baud rate. So for 38400 baud, you should be measuring 19200Hz. If it is more than 2% off from that, you have a problem. (the internal oscillator is only guaranteed to be 10% accurate from the factory, though it can be tuned to within 1% of a desired frequency) If drastically off, then your FCPU define is wrong, or your fuses are.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also note that you should be writing to UBRRH before UBRRL. Writing of UBRRL is what triggers the update of the baud rate prescaler.

Quote:
This is a 12-bit register which contains the USART baud rate. The UBRRH contains the four
most significant bits, and the UBRRL contains the 8 least significant bits of the USART baud
rate. Ongoing transmissions by the transmitter and receiver will be corrupted if the baud rate is
changed. Writing UBRRL will trigger an immediate update of the baud rate prescaler.

It's not clear, and I'm not sure when, if ever, the baud rate prescaler is updated when you reverse the write order. (it may reload every prescaler period, or it may not)

As a general rule, I follow the 16bit register access guidelines for any multi-byte register on the AVR.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hmm..actually i am testing a sample code of blinking exactly with 1 sec gap..and its working correctly so i guess the cpu frequency is correct...abt the UBRRh bfore the UBRRl...this is according to the tutorials given in the forum

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It may not make a difference, as usually the datasheet calls it out when it does... but given the paragraph I quoted, I'd be inclined to write UBRRH first... it can't hurt.

Either way... try the U thing... it will tell you exactly what baud rate the AVR is running at... and then we can move on from there.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the mcu was woking correctly with the same connection with serial communicatio when i was sending data from the mcu to the pc some months ago ...now i have changed that by sending data from the pc to the mcu...and it aint working ...iam so frustrated ...lol

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't care about what WAS at some ancient point in history... only about what IS. Run the test, and then we have a baseline to work forward from. Otherwise you're wasting our time in trying to help you.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i dont have an oscilloscope thats the prob....is there any other way???

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok, you could have told me that earlier!

If you have a frequency counter, you can use that instead.

Barring that... simply connect your PC and see if it receives the U's correctly. Then we can step forward to a simple echo program. at that point we know we have a working link, and can get back to your code here.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

should i check the max 232 1st ??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i checked the maax 232 ..by doing a lo0p back...connected pin 13 to tx pin 12 to pin 11 and rx of db9 to pin 14....and it worked flawlessly...

whatever i was typing in the terminal was receiving back ..hence i think there is no probbs with the max232 ...so i guess prob lies wither in my code or my atmega16...plzz check and tell...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i changed my code to volatile int f=0; but still iam not able to control the serial signals i give out...suppose i type a on the hyperterminal then the led should switch on but it doesnt happen neother does typing d in hyperterminal switch the led off...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

plzz have a recheck ....i changed my code....

#include  
#include  
#include 


#define USART_BAUDRATE 38400 
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) 

volatile int f=0;

void portinitialize();
void uartinitialize();
void handleleft();
void handleright();
void bytedecision(volatile unsigned char);


void portinitialize()
 {
  DDRD|=(1<<4);
  TCCR1B|=(1<<CS12);
  }

void uartinitialize()
 {
   UCSRB |= (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and reception circuitry 
   UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes 

   UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register 
   UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register 

   UCSRB |= (1 << RXCIE); // Enable the USART Recieve Complete interrupt (USART_RXC) 
   sei(); // Enable the Global Interrupt Enable flag so that interrupts can be processed 
 }
void handleleft()
 {
  int m=0;
  PORTD&=~(1<<4);
  
  for(m=0;m<=100;m++)
  _delay_ms(10);
 }

void handleright()
 {
  int m=0;
  PORTD|=(1<<4);

  for(m=0;m<=100;m++)
  _delay_ms(10);
  }

void bytedecision(volatile unsigned char a)
 { 
  if(a=='a' || a=='A')
   {
    handleleft();
    }
  if(a=='d' || a=='D')
   {
    handleright();
   }
 }

int main (void) 
{ 
   portinitialize();
   uartinitialize();
   
   for(;;) // Loop forever 
   { 
     if(f==0)
	   
	    
     {
  
    if(TCNT1>=31250)
	 
	 {
	  PORTD^=(1<<4);  // run test program blinking- echoing is handled by the ISR instead of in the main loop 
	  TCNT1=0;
	 }
	 }
 }
       
      
} 

ISR(USART_RXC_vect) 
{ 
   f=1;
   volatile unsigned char ReceivedByte; 
   ReceivedByte = UDR; // Fetch the recieved byte value into the variable "ByteReceived" 
   //UDR = ReceivedByte; // Echo back the received byte back to the computer 
   bytedecision(ReceivedByte);
} 

is it the baud rate coz i dont have a prob whil transmitting adata from mcu to pc