UART on KL46Z Board

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

UART on KL46Z Board

3,510 Views
rohananand
Contributor II

Hello,

I am new to UART. I have written my first code to send a character to pc and then switch on and off the led. I am unable to find any character coming to my teraterm application. I am unable to find any data when i make a connection through teraterm to my com port.

The code is as follows:

#include "MKL46Z4.h"

//UART0,PTA1=RX and PTA1=TX

void UART_Init(void){

  SIM_SOPT2=SIM_SOPT2| 0x04000000;//Clock to UART0 MCGFLLCLK 48MHz

  SIM_SOPT5=SIM_SOPT5 & 0xFFFEFFF8;

  //open-drain mode disabled(Clear bit 16) plus

  //connection to UART_TX and UART_RX(Module to module interconnect) Clear bit 2,1 and 0

  SIM_SCGC4=SIM_SCGC4| 0x00000400;//set bit 10 for giving clock to UART0

  SIM_SCGC5=SIM_SCGC5| 0x00000200;//set bit 9 for giving clock to PORTA

  PORTA_PCR1=PORTA_PCR1|0x00000200;//PTA1 as UART1_RX (set bit  9 for alternative 2 which is UART0_RX)

  PORTA_PCR2=PORTA_PCR2|0x00000200;//PTA2 as UART1_RX (set bit  9 for alternative 2 which is UART0_TX)

  UART0_C2=0x00;//TX and RX both disabled

    UART0_BDH=0x01;//Baud rate =9600,48Mhz clock

    UART0_BDL=0x38;//BR=132.5=132=100111000

    UART0_C1=0x00;

    UART0_S2=UART0_S1 & 0xC1;

    UART0_C3=0x00;

    UART0_C4=0x0F;//OSR=16

    UART0_C5=UART0_C5 & 0x5C;

    UART0_C2=UART0_C2 | 0x0C;//TX and RX enable

}

void InitLEDG(void)

{

  SIM_SCGC5=SIM_SCGC5|SIM_SCGC5_PORTD_MASK;//Enable clock to PORTD

  PORTD_PCR5=256;//declare pins as GPIO

  GPIOD_PDDR=GPIOD_PDDR | 0x00000020;//set pin as output

  GPIOD_PDOR=GPIOD_PDOR | 0x00000020;//LED initially off

}

void InitSYSTICK(void)

{

  //this is a 24 bit down counter

  SysTick->CTRL=0;//disable timer

  SysTick->LOAD=0x00FFFFFF;//load to full value 0x00FFFFFF

  SysTick->VAL=0;//write anything to reset register

  SysTick->CTRL=0x00000004;//1=bus clock, 0=disable interrupt, 0=disable systick

}

void delay(void)

{

  SysTick->VAL=0;//reset counter

  SysTick->CTRL=0x00000005;//enable counter

  while((SysTick->CTRL & 0x00010000) == 0)//check flag

  {

  }

}

unsigned char UART_InChar(void){

  while(UART0_S1 & 0x20 !=0){

  return(UART0_D);

  }

}

void UART_OutChar(unsigned char data){

  while(UART0_S1 & 0x80 != 0){

  UART0_D=data;

  }

}

int main(){

  UART_Init();

  InitLEDG();

  InitSYSTICK();

  unsigned char x='a';

  while(1){

  UART_OutChar(x);

  GPIOD_PDOR= 0u<<5;

  delay();

  GPIOD_PDOR= 1u<<5;

  delay();

  }

  return 0;

}

The led is blinking but i cannot see any data coming on my teraterm.

Please help me.

Thanking you

Also if possible please send me some reference links where i can learn more about some terms that i cannot understand from the reference manual like Loop mode,receiver source select etc.

0 Kudos
1 Reply

1,940 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Rohan,

NXP has provided the sample code of the FRDM-KL46 board which includes the kind of demo about the UART, and please refer to it for details.

The link of the sample code of the FRDM-KL46 board is below.

cache.nxp.com/files/32bit/software/KL46_SC.exe
Have a great day,
Ping

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos