UART baud rate issues

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

UART baud rate issues

Jump to solution
2,063 Views
vinkar
Contributor III

Hello Friends,

I have been trying to ping my PC (hyper terminal) from the UART. I want to ping it at 9600 baud. I have given the BDH:BDL = 0x222 = 506 (in decimal). The clock is from MCGFLLCLK and I have no pre scalers associated with it. So its at 20.97MH (default value). So, i deally must configure the hyper terminal at 9600 baud. When done it shows garbage. Then i lowered the hyperterminal baud upto 2400 or so and got all the data perfectly. So , it seems I am configuring the UART to 2400 baud by some wrong method. Where am I making.The code is pasted below. Kindly assist me out

//UART - configured - 9600, hyperterminal - configure for 2400 !!!!!

/*

* main implementation: use this 'C' sample to create your own application

*

*/

// selected baud - 9600 //

// hyper terminal - 2400 baud for FRDM board !!!!!!!! //

#include "derivative.h" /* include peripheral declarations */

int main(void)

{

        int tempCount,tempCount2 = 0;

        short int c;

                // clocking //

        SIM_SOPT2 |= 0x01000000;      // clock sel for the timer0 //

        SIM_SCGC5 |= 0x00000400;          // gating of clock //

        SIM_SCGC6 |= 0x01000000;      // gating of timer //

        SIM_SOPT2 |= SIM_SOPT2_UART0SRC(1);

                SIM_SCGC4 = SIM_SCGC4_UART0_MASK;

                SIM_SCGC5 = SIM_SCGC5_PORTB_MASK;

                PORTB_PCR2 = PORT_PCR_ISF_MASK|PORT_PCR_MUX(2);

                PORTB_PCR1 = PORT_PCR_ISF_MASK|PORT_PCR_MUX(2);

                UART0_C2 &= ~ (UART0_C2_TE_MASK| UART0_C2_RE_MASK);

        PORTB_PCR8 |= 0x00000100;    // alternative pin mux selection //

        GPIOB_PDDR |= 0x00000100;    // pin direction //

                UART0_BDH = 0x02;

                UART0_BDL = 0x22;

                UART0_C4 = 0x0F;

                UART0_C1 = 0x00;

                UART0_C3 = 0x00;

                UART0_MA1 = 0x00;

                UART0_MA1 = 0x00;

                UART0_S1 |= 0x1F;

                UART0_S2 |= 0xC0;

                UART0_C2 |= UART0_C2_TE_MASK| UART0_C2_RE_MASK;

                while (1)

                {

                        //#if UART_MODE == POLLING_MODE

                                while(!(UART0_S1&UART0_S1_RDRF_MASK));

                                        c = UART0_D;

                                while(!(UART0_S1&UART0_S1_TDRE_MASK) && !(UART0_S1&UART0_S1_TC_MASK));

                                        UART0_D = c;

                        //#endif

                }

        }

Vinod

Tags (2)
0 Kudos
1 Solution
801 Views
mjbcswitzerland
Specialist V

Hi

Assuming you have MCGFLLCLK at 20971520Hz and UART0 is clocked at the same speed a value of 0x0222 (546 decimal) gives a baud rate of 20971520/16/546 = 2'300.58 Baud.

If you want 9600 Baud the divider needs to be 20971520/16/9600 = 0x0089 (137 decimal) [rounded up]

The optimal divider value can be calculated by

#define UART_CLOCK     20971520

Divider = (unsigned short)((((UART_CLOCK/16/9600)+1)*2)/2);

The formular gives 137 rather than 136 (as simply doing  20971520/16/9600), which is slightly more accurate since it rounds up and not down.

Regards

Mark

View solution in original post

0 Kudos
2 Replies
802 Views
mjbcswitzerland
Specialist V

Hi

Assuming you have MCGFLLCLK at 20971520Hz and UART0 is clocked at the same speed a value of 0x0222 (546 decimal) gives a baud rate of 20971520/16/546 = 2'300.58 Baud.

If you want 9600 Baud the divider needs to be 20971520/16/9600 = 0x0089 (137 decimal) [rounded up]

The optimal divider value can be calculated by

#define UART_CLOCK     20971520

Divider = (unsigned short)((((UART_CLOCK/16/9600)+1)*2)/2);

The formular gives 137 rather than 136 (as simply doing  20971520/16/9600), which is slightly more accurate since it rounds up and not down.

Regards

Mark

0 Kudos
801 Views
vinkar
Contributor III

Thank u Mark.

0 Kudos