First program with avrlib : serial output

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

Hello,

I'm trying to make my first program work on an atmega128, in order to get some things sent over RS232. I do get things but not what I expect. I tried several baudrates, even changed FCPU to see what happens, but I'm not able to say hello to the world properly :?

Here is the code, based on avrlib examples (makefile from avrlib also):

#include         // include I/O definitions (port names, pin names, etc)
#include     // include interrupt support

#include "global.h"        // include our global settings
#include "uart2.h"        // include uart function library
#include "rprintf.h"    // include printf function library
#define STATUS_LED_PIN     PB7
#define STATUS_LED_PORT    PORTB

/* red status led */
#define STATUS_LED_on     (STATUS_LED_PORT   |=  (1 << STATUS_LED_PIN)) 
#define STATUS_LED_off    (STATUS_LED_PORT   &= ~(1 << STATUS_LED_PIN))



int main(void)
{
  /* hello world led pattern */
  DDRB = 0xFF;
  PORTB = 0xFF;
    STATUS_LED_on;
    // initialize our libraries
    // initialize the UART (serial port)
    uart0Init();
    uartSetBaudRate(0,9600);
    // make all rprintf statements use uart for output
    rprintfInit(uart0SendByte);
     // print a little intro message so we know things are working
    rprintf("Welcome to this test!");
    
    return 0;
}

I use winavr WinAVR-20071221, with an atmega128
fuses are like this: lfuse 0xe0, hfuse = 0xdd and efuse 0xff.

Is there something special to configure somewhere ? In general, does avrlib needs tweaking for recent winavr releases (I changed the signedness of some vars in order to remove some warnings in uart2.c)

Once this will work I'll then try LCD, then I2C and then put everything together for my first project....

Thanks for this great site !

Vincent.

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

The most likely culpright as usual would be your clock.
What have you told winAvr to be and what is it really?
Have you set up the fuses correctly?
Is the mega103 compatibilty off?
Are you using a crystal? (internal oscillator not really good for comms)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

What is F_CPU by the way? Your lfuse has the bottom 4 CKSEL bits set to 0000 which is "external clock" so presumably you have an external square wave generator pumping a signal into XTAL1 but at what frequency? The fact is that 9600 baud is not available at (for example) 1MHz because there's a -7.0% inaccuracy

Cliff

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

I'm at 16Mhz. I also tried 115200 and some other speeds, but still some garbage (at least, something goest out...). I remember seing error charts FCPU/baud somewhere. I'll check for the optimal combination.
And yes, 103 compatibility is off

V.

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

Have you done a flashy LED test to verify that it really is 16MHz?

Also if you have a 'scope it's a GREAT help for diagnosing this stuff. Just set up and infinite loop transmitting 'U' at 8N1 and this will generate a constant 01010101010 bit stream on TXD. For 9600 baud the pulse width should be 104us

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

make sure you change the frequency(f_cpu) at the global.h header file and also the make file so that they are same(here 16 mhz)....also try using uartsendbyte() function.

change without any change is no change ;-)

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

I just got UART0 working to RealTerm on the PC as my second beginner project. I works great. I did not use library, it seemed simpler to do directly? The code is also running A/D with interrupts. I send A/D data to UART0 every 200mS.
RealTerm is free and works great!

This is on a Atmeg128 at 16mHz crystal clock.
You might want to try to set CKOPT fuse active (clear it). The documentation says this sets full amplitude clock for the higher frequencies. It takes more power.
NONE of the other CKSEL bits are set low (activated).
I had to print out all the documentation on the 5 UART Registers to finally get this going.
Use the Simulator and see if the 5 UART registers have the values below after Init UART...They work @9600 @16mHz. Try setting them directly if they do not match.

What is "rprintf", I looked in STDIO.h docs and did not see it?? I have not connected UART0 to printf fuctions yet, I don't know how to direct them to UART0?
After that I want to use an LCD.

Good luck,
Mike

// USART0 initialization mega128 - 2 UARTS available
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600

UCSR0A=0x00;
UCSR0B=0x18;
UCSR0C=0x86;
UBRR0H=0x00;
UBRR0L=0x67;

I used this to send data:

while ( !(UCSR0A & (1 << UDRE0) ) ) {};

// Wait here

// UDRE0 = 1 when empty

UDR0 = Adata; //Send it

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

Quote:
I'm at 16Mhz.

You haven't really answered this question.
Quote:
Your lfuse has the bottom 4 CKSEL bits set to 0000 which is "external clock" so presumably you have an external square wave generator pumping a signal into XTAL1 but at what frequency?

Quote:
I also tried 115200 and some other speeds, but still some garbage

You do not seem to be looking at the data sheet either.

115.2k 8 -3.5% 16 2.1%

That's either 3.5% or 2.1% error, so you will get garbage.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

MotoDog wrote:
What is "rprintf", I looked in STDIO.h docs and did not see it?? I have not connected UART0 to printf fuctions yet, I don't know how to direct them to UART0?
After that I want to use an LCD.

Good luck,
Mike

This function is included in the avrlib, which also includes I2C, serial,LCD and some others.
I'll start from scratch a basic program with serial, LCD and I2C, to get all pre-requisites done. I'll let you know the result.

Thanks.

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

For the serial and LCD you may find the example in the user manual helpful:

http://www.nongnu.org/avr-libc/u...

(that's just using standard avr-libc functions and not relying on third party "libraries" like avrlib which is very confusingly named to be too similar to avr-libc IMHO!)

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

Read the FAQ#3 of Clawson.

also did you double check on the stop bits and parity on your Terminal ?!?

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

js wrote:

115.2k 8 -3.5% 16 2.1%

That's either 3.5% or 2.1% error, so you will get garbage.

Eh? A 2.1% error per bit will only build up a 19% error by the time the stop bit comes around (OK, maybe allow another 1/16th bit error for mis-sampling the "start" bit). It's sloppy, but it should still work.

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

Quote:
It's sloppy, but it should still work.
My comments were in reply to
Quote:
I also tried 115200 and some other speeds, but still some garbage

Thinking of this comment from the data sheet.
Quote:
UBRR values which yield an actual baud rate differing less than 0.5% from the target
baud rate, are bold in the table. Higher error ratings are acceptable, but the receiver will
have less noise resistance when the error ratings are high, especially for large serial
frames
Personally I don't use anything that gives me more than 0.2% error or only baud rates marked in bold.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

The boundary usually occurs at about +/- 2%

I don't think you can ever hope for a -3.5% error to work. A +2.1% might be OK.

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

Hello !

So here we are:

Welcome to UART LCD I2C test !
LCD tests!
Line 2
Back to serial display...
DS1631 detected and initialized!
TH is set to: 35
TL is set to: 30
Raw Temp: 7472 Real Temp is: + 29.1875 degrees C
Raw Temp: 7472 Real Temp is: + 29.1875 degrees C
Raw Temp: 7488 Real Temp is: + 29.2500 degrees C
Raw Temp: 7472 Real Temp is: + 29.1875 degrees C
Raw Temp: 7488 Real Temp is: + 29.2500 degrees C
Raw Temp: 7472 Real Temp is: + 29.1875 degrees C
Raw Temp: 7488 Real Temp is: + 29.2500 degrees C
Raw Temp: 7472 Real Temp is: + 29.1875 degrees C
Raw Temp: 7488 Real Temp is: + 29.2500 degrees C
Raw Temp: 7536 Real Temp is: + 29.4375 degrees C
Raw Temp: 7584 Real Temp is: + 29.6250 degrees C
Raw Temp: 7616 Real Temp is: + 29.7500 degrees C
Raw Temp: 7664 Real Temp is: + 29.9375 degrees C
Raw Temp: 7680 Real Temp is: + 30.0000 degrees C
Raw Temp: 7712 Real Temp is: + 30.1250 degrees C
Raw Temp: 7712 Real Temp is: + 30.1250 degrees C
Raw Temp: 7728 Real Temp is: + 30.1875 degrees C
Raw Temp: 7744 Real Temp is: + 30.2500 degrees C
Raw Temp: 7760 Real Temp is: + 30.3125 degrees C
Raw Temp: 7760 Real Temp is: + 30.3125 degrees C
Raw Temp: 7776 Real Temp is: + 30.3750 degrees C
Raw Temp: 7776 Real Temp is: + 30.3750 degrees C
Raw Temp: 7792 Real Temp is: + 30.4375 degrees C
Raw Temp: 7792 Real Temp is: + 30.4375 degrees C
Raw Temp: 7808 Real Temp is: + 30.5000 degrees C
Raw Temp: 7808 Real Temp is: + 30.5000 degrees C
Raw Temp: 7808 Real Temp is: + 30.5000 degrees C
Raw Temp: 7776 Real Temp is: + 30.3750 degrees C
Raw Temp: 7776 Real Temp is: + 30.3750 degrees C
Raw Temp: 7744 Real Temp is: + 30.2500 degrees C
Raw Temp: 7728 Real Temp is: + 30.1875 degrees C
Raw Temp: 7712 Real Temp is: + 30.1250 degrees C
Raw Temp: 7696 Real Temp is: + 30.0625 degrees C
Raw Temp: 7680 Real Temp is: + 30.0000 degrees C
Raw Temp: 7680 Real Temp is: + 30.0000 degrees C
Raw Temp: 7648 Real Temp is: + 29.8750 degrees C
Raw Temp: 7648 Real Temp is: + 29.8750 degrees C
Raw Temp: 7632 Real Temp is: + 29.8125 degrees C
Raw Temp: 7632 Real Temp is: + 29.8125 degrees C
Raw Temp: 7616 Real Temp is: + 29.7500 degrees C

As you can see, I finally got it working.... and the temp is quite high in here :)
I'm now using another board, running with a 16Mhz crystal and set to 9600bps. I tried 115200, but the first line wasn't displayed.
So now, I'll move onto the DS1307 functions.

Thanks for your help and comments.

Another post on LCD will appear soon.....

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

Quote:
but the first line wasn't displayed

What's your SUT setting? Probably taking longer for the crystal oscillation to stabilise.

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

so what the defect ?!

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

clawson wrote:
Quote:
but the first line wasn't displayed

What's your SUT setting? Probably taking longer for the crystal oscillation to stabilise.

Bingo !
My SUT was to 4ms. Switched to 64ms and now I have everything displayed.

I now understand that AVR development needs extra care on timing issues (UART, I2C, LCD,....). This is very new for me, but I'm very happy to progress.