can I change UART baud rate in run time?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

I read through API doc, but I can not find it. Is there a way I can configure the baud rate by software?

   

Thanks,

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

When you set the clock of the UART to external, you have to provide your own (with a pwm for instance) then you have programatically full control over the frequency/Baud-rate.

   

 

   

Bob

View solution in original post

0 Likes
16 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

When you set the clock of the UART to external, you have to provide your own (with a pwm for instance) then you have programatically full control over the frequency/Baud-rate.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

 I see, I will use a Clock then set divider to adjust the frequency. Hope it will work.

   

Thanks,

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Don't worry, it will!

   

Bob

0 Likes
Anonymous
Not applicable

 I thought this should be easy, but I got stuck again

   

 

   

the external clock is 1.8432MHz and I have the following code:

   

 

   

        UART_CLOCK_Start();

   

switch (BaudRate) {

   

case BR_9600:

   

UART_CLOCK_SetDividerValue(11);

   

break;

   

case BR_19200:

   

UART_CLOCK_SetDividerValue(5);

   

break;

   

case BR_38400:

   

UART_CLOCK_SetDividerValue(2);

   

break;

   

case BR_57600:

   

UART_CLOCK_SetDividerValue(1);

   

break;

   

case BR_115200:

   

break;

   

}

   

 

   

work great with 115200, but as soon as I change to any other baud rate, for example 57600, then my UART ISR will be triggered twice if I press keyboard once. For example, I type "U", (0x55), and my ISR pick up 0xB8 the first time, then 0x0 the second time.

   

I tried replacing CLOCK_SetDividerValue() with Clock_SetDivider(), does not make any difference.

0 Likes
Anonymous
Not applicable

Hi Boulder,

   

 

   

Is the clock exactly 1.8432MHz? If it is derived from one of the internal clocks, then please look at the clock tree to know what the actual value of the clock is, based on the divider used.

   

Can you please upload the project, or atleast the project containing the UART part. That'll make it easier to find out the cause of the issue.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The clock will be 1.846 MHz when you remove the 5% checkbox, that is an error of about 0,2%. That will be quite sufficant for UART specs.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Obvously the UART has to be stopped and started again. Have a look at the attached (working) project. It did not work, when the UART was free running.

   

 

   

Happy coding

   

Bob

0 Likes
Anonymous
Not applicable

 I located the problem:

   

The problem is the clock itself. I routed the UartClock to a I/O pin and monitored the signal on a scope. When the baud rate is 115200, so no divide, I can see the 1.8432 MHz signal on the scope. However if I set baud rate to be 9600, and I set a break point on UART_CLOCK_SetDivider(11); the the break point trigger, I can still see the 1.8432MHz, and one single step after the setDivider (or SetDividerValue()) the wave on scope become 1.99MHz. If I use 19200, then after SetDivider(5), I am measuring 4MHz, so if the baud rate is 38400, then I got 8MHz.

   

It seems the API doc is backward. It is multiply, not divide. I am going to configure it as multiply and give it another try.

0 Likes
Anonymous
Not applicable

 purely by trial and error, I found the parameters to setup the baud rate:

   

the clock is set to 9600 x 16 = 153.6KHz

   

   

switch (BaudRate) {

   

case BR_9600:

   

// do nothing

   

break;

   

case BR_19200:

   

UART_CLOCK_SetDivider(78);

   

break;

   

case BR_38400:

   

UART_CLOCK_SetDivider(38);

   

break;

   

case BR_57600:

   

UART_CLOCK_SetDivider(25);

   

break;

   

case BR_115200:

   

UART_CLOCK_SetDivider(12);

   

break;

   

default:

   

break;

   

}

   

 

   

by setting to these numbers, I can measure the correct clock on scope and also talking to my computer with corrsponding baud rate. I guess I will need to talk to a FAE about this issue, the only thing I am worried about now is that a year from now, with a different patch of chip, all the constant number setting will still give me correct baud rate.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

It is not multiply, it is easier:

   

The clock is derived from the 24MHz Bus-clock. Divider set to 13 gives the reqiured 1.843 MHz

   

When the divider is set to 26 you get half the frequency, just as desired.

   

No multiplication...

   

 

   

Bob

Anonymous
Not applicable

 so the divider is based on 24MHz, and it has nothing to do with the my Uart_Clock...I thought the reference is the uart_clock. That's certainly confusing.

   

Well at least now these number make some sense now. Thanks for the help!

0 Likes
Anonymous
Not applicable

Hi boulder,

   

 

   

As Bob has correctly pointed out, you need to set the divider taking into account the source from which the clock is derived.

   

To know the source of the clock used, you must look into the "Clocks" section in the .cydwr and set the appropriate divider. You can also force the Source of the clock from the configuration window of the clock.

0 Likes
Anonymous
Not applicable

Hi Bob Marlowe,

   

I am using PSoC4-BLE,  Need to change baudrate of SCB-UART using PWM. Can u help me on this

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

SCB UART clock cannot be driven by a PWM. Use a UDB based UART or change the clock divider yourself.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

If i use UDB based UART Resource exceeds 100%, on run time stopbit, parity and baudrate has to be changed. I am successfull in psoc3 but PSoC4 BLE UDB resource availability is  less. can u help me with example of clock divider with SCB(from 2400 to 115200). 

   

Thank you.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Look into the "System Reference Guide", there are the APIs for clock divider changes listed.

   

Easiest: Set the UART to desired baud rate, then control (write down)in .cydwr view the clock divider used by the system. Use that list to switch the baud rate.

   

 

   

Bob

0 Likes