Hello i've been trying to send some informations throught UART for some time but i can't get it to work, i'm probably missing something. The problem is that if i check with logic analyzer my TX line i can clearly see that data is correct but in realterm or any other software connected through USB-TTL only what i get is null(00).
Here is my simple code:
#include <stdio.h>
#include "board.h"
#include "peripherals.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "MK82F25615.h"
uint16_t sbr;int main(void) {
/* Init board hardware. */
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
/* Init FSL debug console. */
BOARD_InitDebugConsole();
SIM -> CLKDIV1 &= ~SIM_CLKDIV1_OUTDIV1_MASK;
SIM -> CLKDIV1 &= ~SIM_CLKDIV1_OUTDIV2_MASK;SIM -> SCGC5 |= SIM_SCGC5_PORTB_MASK;
SIM -> SCGC2 |= SIM_SCGC2_LPUART0_MASK;
SIM -> SOPT2 |= SIM_SOPT2_LPUARTSRC(0x01);MCG -> C6 &= ~ MCG_C6_PLLS_MASK;
MCG -> C4 |= MCG_C4_DMX32_MASK;
MCG -> C4 |= MCG_C4_DRST_DRS(0);PORTB-> PCR[16] = PORT_PCR_MUX(0x03);
PORTB-> PCR[17] = PORT_PCR_MUX(0x03);LPUART0 -> CTRL &= ~LPUART_CTRL_TE_MASK;
LPUART0 -> CTRL &= ~LPUART_CTRL_RE_MASK;
LPUART0 -> BAUD |=LPUART_BAUD_OSR(15);sbr = (23986176/(16*9600));
LPUART0 -> BAUD |= LPUART_BAUD_SBR(sbr);LPUART0 -> CTRL &= ~LPUART_CTRL_M_MASK;
LPUART0 -> CTRL &= ~LPUART_CTRL_PE_MASK;
LPUART0 -> BAUD &= ~LPUART_BAUD_SBNS_MASK;LPUART0 -> CTRL |= LPUART_CTRL_TE_MASK;
LPUART0 -> CTRL |= LPUART_CTRL_RE_MASK;while(1)
{LPUART0 -> CTRL |= LPUART_CTRL_TE_MASK;
LPUART0 -> DATA = 0x53;
while(!(LPUART0 -> STAT & LPUART_STAT_TC_MASK) );LPUART0 -> CTRL &= ~LPUART_CTRL_TE_MASK;
}
return 0 ;
}
Any ideas? i'm suspecting baudrate but i have no idea what could be wrong in calculations.
Changing oversampling ratio solved the baudrate problem. How oversampling affects TX side?
Hi Rufus
What baud rate do you measure?
Is 23986176 the PLL/FLL speed that you really have?
For more exact 9600 Baud calculation use:
(((LPUART_CLOCK/8/9600) + 1)/2)
Regards
Mark
uTasker developer and supporter (+5'000 hours experience on +60 Kinetis derivatives in +80 product developments)
Kinetis: http://www.utasker.com/kinetis.html
According to MCG_C4 with 32.768kHz reference mutiplied by FLL Factor 732 i receive 23986176.
I switched to internal fast clock 4Mhz and transmission seems to be working but the data i put do the register it's not what i receive on my PC so it seems like the baudrate is still off with new formula.
I used this to switch for internal fast clock:
MCG -> SC &= ~MCG_SC_FCRDIV_MASK;
MCG -> C1 |=MCG_C1_IRCLKEN_MASK |MCG_C1_IREFSTEN_MASK;
MCG -> C2 |=MCG_C2_IRCS_MASK;SIM -> SCGC5 |= SIM_SCGC5_PORTB_MASK;
SIM -> SCGC2 |= SIM_SCGC2_LPUART0_MASK;
SIM -> SOPT2 |= SIM_SOPT2_LPUARTSRC(3);
Grzegorz
You really need to "measure" the Baud rate - if it is off by a certain factor knowing it will help you identify why.
Regards
Mark