This tutorial covers the details of Blinking an LED on the Kinetis K40 using the TWR-K40X256-KIT evaluation board. It will introduce the evaluation board, and some basic CodeWarrior features.
Overview
In this exercise students will explore a Freescale Cup Car application which targets a Freescale K40 board attached to the Tower System.
Students will:
- Configure the Software Development Environment
- Configure the evaluation board hardware
- Learn how to import example files into a CodeWarrior project
- Build a project
- Download and run the code on a Kinetis K40 Tower System board
- Learn how to utilize the GPIO Peripheral to blink a LED
To successfully complete this exercise, students will need the following board and development environment.
- The K40 Tower card, TWR-K40x256
- CodeWarrior for Microcontrollers
- USB Cord
1. Hardware
Read the Blink LED overview article for general information on LED circuits, GPIO pins and reference manuals.
2. Set up the Software Development Environment
There are several steps necessary to prepare the evaluation board and PC for microcontroller programming and development. Interfacing the evaluation board with a PC requires downloading and Install the CodeWarrior IDE, as well as the device drivers for programming the microcontroller via USB.
A. Download and Install Codewarrior
Before completing this example project, download-and-install-codewarrior-10-1 or the latest version compatible with the twr-k40x256-kit.
B. Download and Install Drivers
In addition to CodeWarrior, it may(needs verified) be necessary to install one or both of the following tools:
- RAppID initialization tool- RAppID comes on the DVD provided with your evaluation board. In the main directory of the DVD, click on the "TRK_MPC5604B.html" file to open the DVD interface which provides user manuals, software, schematics and documentation for the evaluation board.
- P&E Microcomputer Systems, Inc drivers- P&E is a a computer driver for the TRK-MPC5604B and Kinetis Tower system device, enabling evaluation board programming via USB through the CodeWarrior debug OSJTAG interface. This driver can be downloaded here in case this isn't found on the disk.
3. Set up the Hardware: Twr K40x256 Hardware Setup
There are several Twr K40x256 hardware configuration steps. Follow the twr-k40x256-hardware-setup instructions before importing the LED Project.
4. Import the LED Project
After the software is successfully downloaded and installed, the next step is to import an existing project into your Workspace. in this case, the LED_BLINK_96MHZ Project. Follow the instructions on the codewarrior-project-import page to import the LED_BLINK_96MHZ project into CodeWarrior.
If errors are encountered, look in the Problems view and resolve them. Ignore any warnings.
5. Build the Code
If there is more than one project in your project view, make sure the proper project is the focus. The most reliable way to do this is to right click the project and choose Build Project as shown below. You can also go to the Project menu and choose the same command.
By default, the application is set to link to RAM. If you want your program in FLASH, make sure that you have that build configuration enabled:

Make sure you do a "Clean" operation (under the project menu) after you make the configuration change.
If you encounter errors, look in the problems view and resolve them. You can ignore any warnings.
6. Download/Debug/Run
If the project builds correctly, it is time to download to the board and watch it work. Ensure that the USB cable that came with the board connects the board to the host computer’s USB port.
There are multiple ways to issue the Debug command. Right click the project in the projects view and choose Debug As->CodeWarrior Download. Alternatively, y go to the Run menu and choose Debug (F11).
Click the Resume button
and your should see the LED blinking!
Click the Pause button
to stop execution. Click the Terminate button
to end debugging.
7. Learning Step: LED Code Description
Read/Write
If the LED was on Port C, Bit 7 we might have code like:
#define LED_E1_Location(1<<7)
For example, to toggle a pin the following code might be used:
GPIOC_PSOR=LED_E1_LOC
"Sets" the LED located at "E1" high.
the syntax means the following GPIOC refers to Port C
"Set" is one of three commonly utilized commands for GPIO control. There are also commands for "Clear" and "Toggle."
- Command: "GPIOC_PSOR" literally means PORT SET OUTPUT REGISTER which SETS a pin high
- Command: "GPIOC_PCOR" clears a pin, PORT CLEAR OUTPUT REGISTER which CLEARS a pin to the "low" state
- Command: "GPIOC_PTOR" toggles a pin opposite of the current state
the « is a "shift" command which is discussed in the c-programming-for-embedded-systems. You can alter the raw register as well using a MASK but the dedicated set/clear/toggle registers are more straightforward.
**NEEDS UPDATED **
| Hardware | Chip Port/Pin | Comment |
|---|
| SW3 | PT | cell-content |
| SW4 | cell-content | cell-content |
Functions
The following functions can access the LED;
LED_En_TOGGLE;
LED_En_ON;
LED_En_OFF
Variables
Locations of the LED's
- LED_E1_LOC
- LED_E2_LOC
- LED_E3_LOC
- LED_E4_LOC
from k40_TOWER_BOARD_SUPPORT.h
#define LED_E1_LOC (1<<7)
#define LED_E2_LOC (1<<8)
#define LED_E3_LOC (1<<9)
#define LED_E4_LOC (1<<11)
#define LED_E1_OFF GPIOC_PSOR=LED_E1_LOC
#define LED_E1_ON GPIOC_PCOR=LED_E1_LOC
#define LED_E1_TOGGLE GPIOC_PTOR=LED_E1_LOC
#define LED_E2_OFF GPIOC_PSOR=LED_E2_LOC
#define LED_E2_ON GPIOC_PCOR=LED_E2_LOC
#define LED_E2_TOGGLE GPIOC_PTOR=LED_E2_LOC
#define LED_E3_OFF GPIOC_PSOR=LED_E3_LOC
#define LED_E3_ON GPIOC_PCOR=LED_E3_LOC
#define LED_E3_TOGGLE GPIOC_PTOR=LED_E3_LOC
#define LED_E4_OFF GPIOB_PSOR=LED_E4_LOC
#define LED_E4_ON GPIOB_PCOR=LED_E4_LOC
#define LED_E4_TOGGLE GPIOB_PTOR=LED_E4_LOC
Initialize the GPIO
From K40_TOWER_BOARD_SUPPORT.c
void InitK40GPIO()
{
SIM_SCGC5 = SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK;
PORTC_PCR7 = PORT_PCR_MUX(1) | PORT_PCR_DSE_MASK;
PORTC_PCR8 = PORT_PCR_MUX(1) | PORT_PCR_DSE_MASK;
PORTC_PCR9 = PORT_PCR_MUX(1) | PORT_PCR_DSE_MASK;
PORTB_PCR11 = PORT_PCR_MUX(1) | PORT_PCR_DSE_MASK;
PORTC_PCR18 = PORT_PCR_MUX(1) | PORT_PCR_DSE_MASK;
PORTE_PCR28 = PORT_PCR_MUX(1) | PORT_PCR_DSE_MASK;
GPIOC_PDDR |= LED_E1_LOC | LED_E2_LOC | LED_E3_LOC;
GPIOB_PDDR |= LED_E4_LOC;
LED_E1_OFF;
LED_E2_OFF;
LED_E3_OFF;
LED_E4_OFF;
}
Blink the LED:
Within Main.c or any other C file created, Blink the LED's using the following functions:
LED_E1_TOGGLE;
LED_E1_ON;
LED_E2_OFF;
Other K40 Tutorials:
K40 Related Pages
K40: Turning A Servo Tutorial
K40: Drive DC Motor Tutorial
Kinetis K40
TWR-K40X256
K40: Blinking LED
Credits / References
Some of the content from this tutorial originated from:
Shawn Moffit: Electrical Engineering, Penn State University
for - K40 Code
Processor Expert Hands-On Lab Rev. 1.0, 05/2011
by Jim Trudeau, Freescale Semiconductor, Inc.
for - some text descriptions of steps
Original Attachment has been moved to: LED_BLINK_96MHZ.zip