go_away

Author Topic: UART not working  (Read 2452 times)

0 Members and 1 Guest are viewing this topic.

Offline CornelamTopic starter

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 0
  • I can smell a robot from a mile away
UART not working
« on: October 13, 2008, 12:51:01 PM »
HI!
I struggle for hours to make my ATMEGA32 UART working but i cannot make it!
I've tested my converter 1000 of times and it hasn't any problem.
I've tried a loop-back test here is the code:

while(1){
           LED_off();
           rprintf("A");
           if (uartGetByte()==65){
              LED_on();
              delay_cycles(160000); //8 MHz internal oscillator
              }
}

When i run it the LED start's flashing after about 4 seconds!!!
Using printf() instead of rprintf() dosen't do anything (LED not flashing)
WHAT IS WRONG????

Thanks in Advance!!!

Offline CornelamTopic starter

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 0
  • I can smell a robot from a mile away
Re: UART not working
« Reply #1 on: October 13, 2008, 01:27:13 PM »
I managed to the do the loop-back test without a while and works so not my programming is incorrect!
But the hard question is what is not working ???
My Desktop (which i'm using) has 2 COM ports. In one i have the STK200 and in the other my converter.
After setting up Hyperterminal like in the Tutorial i get nothing but if i reset the microcontroller i get a random character!
Does anybody know what can happen????
Thanks in advance!!!

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: UART not working
« Reply #2 on: October 13, 2008, 01:42:23 PM »
Just a guess - but looking at your code you have a delay after LED_on but no delay between the LED_off and the LED_on
So the LED will stay in one state for most of the time (so it will appear to not be flashing).
The 4 second delay probably comes about because the transmit is happening under interrupts and rprintf uses a memory buffer. So to start with the buffer has space to store the next character to be sent and so you get: LED_off, LED_on, delay(160000) so it doesn't appear to flash (as mentioned above).
What probably then happens is that the transmit buffer fills up so the next call to 'rprintf("A")' doesnt have any available space in the buffer so it 'waits' for the background transmission to free up some space. This then introduces an apparent delay - so you now have LED_off, a delay caused by rprintf, LED_on, delay(160000). This new delay in the rprintf then makes the LED flash.
To test it out try changing your code to say:-

Code: [Select]
if (uartGetByte()==65){
         delay_cycles(160000); //8 MHz internal oscillator
         LED_on();
         delay_cycles(160000); //8 MHz internal oscillator
}

It may then start to flash immediately.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: UART not working
« Reply #3 on: October 14, 2008, 04:12:43 AM »
Can you tell us more about your code, for example, are you using modified $50 Robot code?

Quote
delay_cycles(160000);
delay_cycles uses a long int, so you can't get more than 2^16 = 65536 (for 0 to 65535)

Quote
After setting up Hyperterminal like in the Tutorial i get nothing but if i reset the microcontroller i get a random character!
Means you probably have it all wired properly but your code isn't right . . .

Offline CornelamTopic starter

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 0
  • I can smell a robot from a mile away
Re: UART not working
« Reply #4 on: October 14, 2008, 08:41:27 AM »
#include "Utils.h"

int main(void){

       uartInit();
       uartSetBaudRate (9600);
       rprintfInit (UartSendByte);

       LED_off(); //Just in case

       configure_ports(); //All are for Output

       while(1){
            Led_on();
            rprintf("Hello World!!!");
            LED_off();
            delay_cycles(300000); //8 MHz Internal Clock
            }
       return 0;
 }

The Baud rate is ok , i'm using the Serial port of my desktop computer not a adapter!
I will try now with the modified 50$ Robot code.
BTW i'm using a ATMEGA32.
I know about int but i'm using Long Int...
Thanks in Advance!
« Last Edit: October 14, 2008, 08:42:28 AM by Cornelam »

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: UART not working
« Reply #5 on: October 14, 2008, 10:28:30 AM »
Quote
i'm using the Serial port of my desktop computer not a adapter!
You have the UART of your ATmega attached directly to what? If you are using the serial port of your computer, you need to use a TTL to RS232 adapter. No exceptions! ;D

Quote
I know about int but i'm using Long Int...
if you use delay_cycles, you are using long int :P

Offline CornelamTopic starter

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 0
  • I can smell a robot from a mile away
Re: UART not working
« Reply #6 on: October 14, 2008, 11:42:58 AM »
I have i TTL to RS232 adapter! It's exactly the schematic on the uart tutorial page! It's connected to the same +5 and GND as my ATMEGA32.
Why it cannot work? How can i do a loop-back test to test if the adapter works???

Offline CornelamTopic starter

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 0
  • I can smell a robot from a mile away
Re: UART not working
« Reply #7 on: October 14, 2008, 12:27:49 PM »
I did a loop-back test on the adapter and it worked!
I think i know the problem: On the adapter schematic it writes TX and RX but what do they mean?
I must connect to TX and RX of the Atmega or the TX-->RX and RX-->TX???

Offline CornelamTopic starter

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 0
  • I can smell a robot from a mile away
Re: UART not working
« Reply #8 on: October 14, 2008, 12:52:40 PM »
Everything WORKS!!! :))
I connected the RX to the RX!
That was the problem!
Thanks for the help! :D

 


Get Your Ad Here