Hello everyone,
I have a Kinetis MK10DX128 VLH7 and Im trying to configure the UART0 module for a baud rate of 115200. I tryed several clock rates but my host receive only garbage. Every other baud rates until 115200 works fine. I tested it with two of my MCUs and they have the same problems. Here is my source code:
#include "derivative.h" /* include peripheral declarations */
#include "driver.h"
int main(void)
{
int n=0;
main_clock_init();
UART_init(115200,72000);
for(;;)
{
for(n=0;n<=100000;n++);
while(!(UART0_S1 & UART_S1_TDRE_MASK)){};
UART0_D = 'X';
}
return 0;
}
and the funktions in driver.c:
void main_clock_init() // Set clockrate to 72MHz
{
MCG_C4 |= MCG_C4_DMX32_MASK
| MCG_C4_DRST_DRS(2);
}
void UART_init(unsigned short baud,unsigned int uartclk_khz) // Initialise UART0
{
unsigned short sbr,brfa;
//Initialisation
SIM_SCGC4 |= SIM_SCGC4_UART0_MASK; // Activate clock for UART
SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK; // Activate clock for PortC
PORTB_PCR16 = PORT_PCR_MUX(3); // Set PortB 16 for UARC
PORTB_PCR17 = PORT_PCR_MUX(3); // Set PortB 17 for UARC
sbr = (unsigned short)((uartclk_khz*1000)/(baud*16));
UART0_BDH = (unsigned char)((sbr & 0x1F00)>>8); // Write high word of baud rate into register
UART0_BDL = (unsigned char)(sbr & 0x00FF); // Write low word of baud rate into register
brfa = (((uartclk_khz*32000)/(baud*16))-(sbr*32));
UART0_C4 = (unsigned char)(brfa & 0x001F); // Write divider into register
UART0_C1 = 0; // 8-Bit-Mode,No parity
//UART0_BDH |= UART_BDH_RXEDGIE_MASK; // Enable interrupts from an edge on RxD
//enable_irq(45); // Enable interrupts from UART module
//NVICIP45 = 0x10; // Set Priority 1 to the UART module
UART0_C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK ); // Enable receiver and transmitter
while(!(UART0_S1 & UART_S1_TDRE_MASK)){};
UART0_D = 'a';
}
It would be nice when you can help me.
regards,
Eric
已解决! 转到解答。
Eric
The slow clock is not very accurate and you look to already have an inaccuracy of -6% a 9600 Baud.This is not adequate for reliable UART operation so you either need to trim the value for each board (it may still be limited over temperature) or use a crystal source to guaranty reliable operation.
Regards
Mark
Eric
If you have a 72MHz system clock a fraction value of 0x02 and a divide value of 0x27 gives a Baud rate of 115200 exactly.
Should this not work, verify that you really have 72MHz clock. [Usually after seing the FLL factor MCG_C1 = 0 is used to move to FEE state and enable the oscillator - and wait for the source to become valid]
Also measure the Baud rate that is actually being generated to see whether you can work out what the error factor is because it may help identify any problem.
Regards
Mark
P.S. Is it possible that your clock source is not accurate? Once the baud-rate error becomes > 1.5% errors may start, The error increases as the baud-rate increases so at some point the communication starts failing.
Hello Mark,
thanks for your answer. I use the slow internal reference clock to generate the system clock. Could that be a problem? There is no restrictions in the manual thats say that this dont work.
Regards
Eric
Hi Eric,
as Mark indicates, I think your problem is the inaccurate clock. The internal clock is less accurate, there is drift, and it is affected by temperature and trimming. I suggest you measure the actual baud produced so you know how far you are off.
Erich
Ok, I configured the baud rate now to 9600 and measured the width of one Bit and its 110µs wide. So I have a baud rate of 9090 instead of 9600. Is that right? Or do I have to consider more things to measure the baud rate?
Eric
The slow clock is not very accurate and you look to already have an inaccuracy of -6% a 9600 Baud.This is not adequate for reliable UART operation so you either need to trim the value for each board (it may still be limited over temperature) or use a crystal source to guaranty reliable operation.
Regards
Mark