In this part I want to show You how to use usart in kl46z with FTDI converter. In this programme we will send password via usart. If the password will be correct - green led will indicate this, otherside we'll see red led.

Step 1: Pinout & FTDI

We have to check pinouts for Rx and Tx. We can see it in datashet on 172 page. I decided to use pins of USART1. Then connect converter with kl46z board.GND -> GND, VCC -> 3,3V and remember that Rx of microcontroler must be connected to Tx of FTDI and Tx to Rx.

Download FTDI drivers from http://www.ftdichip.com/FTDrivers.htm and install. Don't forget to plug FTDI to USB port of your computer :) At the end go to the control panel of your computer by clicking 'windows key' + 'pause' then 'device manager' -> 'PORT( COM and LPT)' and check your FTDI COM number. We will use it to connect via terminal.

Step 2: Connecting With Terminal

Firstly download terminal. I use putty but there are lots of other terminals.

puTTy download link http://www.putty.org/

Open puTTy and click Serial on radio button. Then you have to write speed, and Serial line. Serial line is COMx, when 'x' is your FTDI COM, and speed must be the same as in our programme. After that click Open.

Step 3: Writing a Programme

We have to include Serial library.

#include "mbed.h"
#include <Serial.h><br>
#define BAUD 9600		// speed of transmission
#define BUFFER_SIZE 20		// buffer for password
#define ENTER 0x0d		// Enter button in HEX
Serial uart1(PTE0,PTE1,"uart1");
DigitalOut redLed(LED2);	// set red led pin as output
DigitalOut greenLed(LED1);	// set green led pin as output
void usart_Init(size_t baud);                           // uart init                       
char *get_string(void);
char *command = "password";
int main()
    char *c;						  
    usart_Init(BAUD);					 // initialize usart
    redLed.write(1);                                     // turn off LEDs

    uart1.printf("\n\rProgram is starting now...\n\r");	 // print text in terminal
    uart1.printf("Baud rate - %d\n\r",BAUD);
    while (1) 
        c = get_string();				   // c = string from terminal
        if(strcmp(c,command) == 0)                         // if string is the same as "password"
                greenLed.write(0);                         // turn on LED
                uart1.printf("\n\rCorrect password !");    // print message
            redLed.write(0);				   // turn red LED on
            uart1.printf("\n\rWrong password");
         greenLed.write(1);				   // turn LEDs off
void usart_Init(size_t baud)                       
   uart1.baud(baud);				   // Set baud rate
   uart1.format();                                 // Defaut: 8 bit data, no parity, 1 stop bit 
char *get_string(void)				
    char buffer[BUFFER_SIZE];
    char *strBuf = buffer;			// put pointer on buffer
    int i = 0, cnt=0;
    char c;
    while((c = uart1.getc()) != ENTER)		// while char is not ENTER
        buffer[i] = c;				// put char to the buffer
        i++, cnt++;
    if(cnt <= BUFFER_SIZE)
        for(i = cnt; i < BUFFER_SIZE; i++)	// if data is shortest than buffer size
        {					// then put NULLs after string
           buffer[i] = '\0';			// ex. exampleString'\0''\0''\0'../
        uart1.printf("\n\rWrong string size !");
    return strBuf;

About This Instructable




More by kubavit:STM32F4 Discovery part2 - ADC Graph STM32F4 Discovery part1 - Touch Buttons Getting started with FRDM kl46z part 4 - ADC & DAC 
Add instructable to: