Redefine USART Baud Rate on the fly

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

Hi you all,

Sorry, this is probably a newbie question but i'm having a problem redefining the USART baud rate on ATMEGA16U2 on the fly.

I initially define my baud rate speed at 9600 bit/s but at some instances on my code i need to change the speed to 125000 bit/s.

This is what i do:

 

// Initial defines

    #if !defined F_CPU
       #define F_CPU 16000000UL  // Specifies a system clock of 16MHz
    #endif

    #if !defined USART_BAUDRATE
        #define USART_BAUDRATE 9600        // Specifies a USART Baud Rate
    #endif

    #if !defined BAUD_PRESCALE
        #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)   // Specifies a Baud Rate Prescale
    #endif

// Change the baud rate on my code:

(...)

        #ifdef USART_BAUDRATE
            if(USART_BAUDRATE == 9600)
            {
                UCSR1B = (0<RXEN1) | (0<<RXCIE1) | (0 << TXEN1);        // Turn off the rx and tx circuits
                #undef USART_BAUDRATE
                #undef BAUD_PRESCALE
                #define USART_BAUDRATE 125000
                #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)   // Specifies a Baud Rate Prescale
                USART_Init();
            }
            #else
                #define USART_BAUDRATE 125000
                #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)   // Specifies a Baud Rate Prescale
                USART_Init();
        #endif

(...)

 

// Configures the USART Module
void USART_Init(void)
{
    UCSR1C = (1<<UPM11) | (1<<UCSZ10) | (1<<UCSZ11);    // Use 8-bit character sizes - URSEL bit set to select the UCRSC register; Even Parity
    UBRR1H = (BAUD_PRESCALE >> 8);            // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
    UBRR1L = BAUD_PRESCALE;   // Specifies a Baud Rate Prescale; Load lower 8-bits of the baud rate value into the low byte of the UBRR register
}

 

My code doesn't change the USART Baud Rate and i don't get why.

Any help?

 

Thank you all.

This topic has a solution.

Last Edited: Thu. Aug 20, 2015 - 12:57 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You don't seem to understand the difference between the C preprocessor and C runtime code? try -save-temps and study the .i files !

 

You do realise that the pre-pro operates and changes the text of your source files long before the C compiler ever gets to see them don't you?

 

If you want to make a runtime change then you want to do something like:

  #define PRESCALE9600 (((F_CPU / (9600 * 16UL))) - 1)   // Specifies a Baud Rate Prescale
  #define PRESCALE125000 (((F_CPU / (125000 * 16UL))) - 1)   // Specifies a Baud Rate Prescale

...
  UBRR = PRESCALE9600;
...

  if (UBRR == PRESCALE9600) {
      UBRR = PRESCALE125000; // change speed on the fly.
  }

 

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

I'm not a preprocessor expert, and always have to work through things when using e.g. token pasting.

 

Below is an excerpt from my parameterized AVR8 USART "driver", that I use especially in apps with more than one USART on the AVR.  OP )or others) might find it interesting.  It was developed some years ago with help from the 'Freaks.  The base speed is CodeVision's internal preprocessor symbol, derived from the project settings.  An intermediate step to start to add the "L" to force 32-bit operations.  (looking at that now, should that be "UL"?)


//-----------------------------------------------------------------------------
//
//	Baud Rates
//	----------
//
//	Use the datasheet formula UBRR = (FCLK / (BAUD*16)) - 1
//	For magic crystal clock rates and common baud rates, UBRRnH is always 0
//
#ifndef _MCU_CLOCK_FREQUENCY_
#error System clock rate in Hertz must be globally defined--aborting.
#endif

#define ABL_FCLKZ(freq) freq##L
#define ABL_FCLKX(freq) ABL_FCLKZ(freq)
#define ABL_FCLK ABL_FCLKX(_MCU_CLOCK_FREQUENCY_)

#define	UBRRVAL(baud) 	((ABL_FCLK)/(baud*16))-1
#define UBRR_2400				UBRRVAL(2400)
#define UBRR_4800				UBRRVAL(4800)
#define UBRR_9600				UBRRVAL(9600)
#define UBRR_19200				UBRRVAL(19200)
#define UBRR_28800				UBRRVAL(28800)
#define UBRR_38400				UBRRVAL(38400)
#define UBRR_57600				UBRRVAL(57600)
#define UBRR_115200				UBRRVAL(115200)

...
//
//	System Polling Speed
//	====================
//

#define	OM_BAUD	UBRR_57600
...
//
// Start the UART after all of the startup delays
//
retval = uart_init (OM_BAUD);

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Oh... shame on me!
Sorry for this dumb question. I prefer to be ignorant for 5 minutes instead of being an idiot for the whole life!
I really appreciate your answers! Thank you ;)