avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] uart problem with atmega 128


From: Theodore A. Roth
Subject: Re: [avr-gcc-list] uart problem with atmega 128
Date: Fri, 14 Nov 2003 10:06:18 -0800 (PST)


On Wed, 1 Jan 1997, Jean François BOUTILLON wrote:

>
>  Hi all. I have a problem with my uart on atmega 128. I try to make s
>  simple program that just echoes the character it receives. The soure
>  of the program is belox. The program works ok (the =charactes are
>  echoed) BUT thr program restarts each time a character is sent (the
>  green led blinks each time a character is sent). Does anyone knows
>  why the orogram restarts ?

I just ran your program on a mega128 in a stk500. It works fine for me.
I'm not seeing portb4 blink except at initial powerup. Also, running the
program in gdb with a breakpoint in main, I only hit the breakpoint once
indicating that the program never jumped back to reset.

I've seen spurious resets in my own code quite a few times. Most often,
it's caused by a stack overflow in a multitasking system (push a 'call'
return addr passed the end of the stack, switch tasks, write 0x0000 to
the return addr, then when returning from call in first task you get a
jump to reset).

You can also generate a spurious reset if you enable an interrupt that
you have defined a SIGNAL() or INTERRUPT() handler for. The default
behaviour with avr-libc 1.0 is to reset. This can also happen if you
misspell the name of the sig handler.

A third possible source of reset is if the WDTON fuse in the Extended
fuse byte is programmed. This locks the watchdog timer on.

One other note. You should probably change this:

  #define F_CPU            16000000 /* 16Mhz */
  #define UART_BAUD_RATE   9600     /* 9600 baud */

to this:

  #define F_CPU            16000000L /* 16Mhz */
  #define UART_BAUD_RATE   9600L     /* 9600 baud */

GCC gave me this warning if the 'L' isn't present:

  jfb-example.c:58: warning: integer overflow in expression

The warning is on the line where you use UART_BAUD_SELECT. I also got
garbage out the serial port until I made the change.

>
>  Thanks a lot
>
>  Jeff
>
>  Source of the code :
>  /************************************************************/
>  #include <avr/io.h>
>  #include <avr/interrupt.h>
>  #include <avr/signal.h>
>
>
>  #define ON(X) outp(1<<(X), PORTB)
>  #define OFF() outp(0, PORTB)
>  #define F_CPU            16000000      /* 16Mhz */
>  #define UART_BAUD_RATE      9600      /* 9600 baud */
>
>  #define PAUSE() {unsigned char i,j;for(i=0;i<255;i++)for(j=0;j<255;j++);}
>
>  #define UART_BAUD_SELECT (F_CPU/(16*UART_BAUD_RATE)-1)
>
>  typedef unsigned char  u08;
>  typedef          char  s08;
>
>  SIGNAL(SIG_UART0_RECV)
>  {
>   u08 recu=inp(UDR0);
>   outp(recu,UDR0);
>  }
>
>
>  int main(void)
>  {
>   // Port B as output
>   outp(0xff ,DDRB);
>
>   // initialize UART
>   outp((1<<UCSZ01)|(1<<UCSZ00),UCSR0C);
>      outp((u08)UART_BAUD_SELECT, UBRR0L);
>   outp((1<<RXEN0)|(1<<RXCIE)|(1<<TXEN0),UCSR0B);
>   sei();
>
>   // Make green led blink once
>   ON(4);PAUSE();OFF();
>
>   for (;;)
>   {
>    // make red led blink
>    PAUSE();PAUSE();ON(6);PAUSE();PAUSE();OFF();
>   }
>  }


reply via email to

[Prev in Thread] Current Thread [Next in Thread]