Lab3 - Blinking LEDs
Lab3 - Blinking LEDs
2
Lab 4: Blinking LEDs
3
Exercise 1: Blinking green LED
• Create a application with the infinite loop design. The project contains the user code files:
– main.c: This file contains the main() function, start point of program, call to other function.
– LED.c: The file contains functions for initialization process + control I/O
+ Supply clock signal to port InitLED()
+ Configure operation mode of I/O pins (configured pin used in GPIO mode, data
direction of pin (input or output))
+ Function for I/O control (blink green led): BlinkLED1(), BlinkLED2(), BlinkLED3()
– LED.h: The header file contains the function prototypes created in LED.c and must be
included into the file main.c.
• Reading Material
– Chapter 11, 12, 42 of KL46 Sub-Family Reference Manual
4
Exercise 1: Blinking green LED
• Red LED and Green LED are connected as shown
in the below Table. (See [1])
Red LED
Green LED
Green PTD5
[FRDM-KL46Z User’s Manual]
5
Review: Programming I/O
• Initialization process:
+ Supply clock signal to port
+ Configure operation mode of I/O pins
(configured pin used in GPIO mode, data
direction of pin (input or output))
• I/O Control:
+ Control pin out 1 or 0 to turn led on or off.
6
Exercise 1: Blinking green LED
• Initialization process:
– Programming the clock control circuitry
– System Integration Module (SIM) - This gives control of which peripherals are
clocked. (chapter 12)
– System Clock Gating Control Register 5 (SIM_SCGC5) - This register holds the
clock enable for the various ports on the MCU. IN ORDER TO USE ANY PINS
ON A PORT THIS MUST BE ENABLED FOR THAT PORT. For example, to use the
red LED on pin PTD5 we must first enable the clock to Port D in SCGC5.
typedef struct { /**< System Clock Gating Control Register 4, offset: 0x1034 */
IO uint32_t SCGC4; /**< System Clock Gating Control Register 5, offset: 0x1038 */
IO uint32_t SCGC5; /**< System Clock Gating Control Register 6, offset: 0x103C
IO uint32_t SCGC6; */
IO uint32_t SCGC7; /**< System Clock Gating Control Register 7, offset: 0x1040 */
IO uint32_t /**< System
Access Clock
to SCGC5 Divider=>Register
register 1, offset: 0x1044 */
SIM->SCGC5
CLKDIV1; (...)
} SIM_Type; To enable clock to Port D -> Set bit 12 of SCGC5 to 1 7
How?
Exercise 1: Blinking green LED
• Initialization process:
– Programming the clock control circuitry
– System Integration Module (SIM) - This gives control of which peripherals are
clocked. (chapter 12)
– System Clock Gating Control Register 5 (SIM_SCGC5) - This register holds the
clock enable for the various ports on the MCU. IN ORDER TO USE ANY PINS
ON A PORT THIS MUST BE ENABLED FOR THAT PORT. For example, to use the
red LED on pin PTD5 we must first enable the clock to Port D in SCGC5.
SIM->SCGC5 |= 1<<12
Or SIM->SCGC5 |= SIM_SCGC5_PORTD_MASK; //0x1000 = 1<<12
//This enables the clock to PORTD
8
Exercise 1: Blinking green LED
• Initialization process:
– Configuring the operation mode of the I/O pins
– Port Control and Interrupts (PORT module, chapter 11): support for port control, and external interrupt functions.
- Pin Control Register n (PORTx_PCRn)
MUX bits
PORTx_PCRn D10
D9 D8
Analog Circuit
I0 S0 S1
GPIO Pn.x S2
Alternative Func2 I1
Alternative Func3 I2
I3 Pin of Chip
Alternative Func4
Alternative Func5 I4
Alternative Func6 I5
Alternative Func7 I6
I8
9
Exercise 1: Blinking green LED
• Initialization process:
- Configuring the operation mode of the I/O pins (PORTx->PCR[n]): This register
determines which pins are used for which peripheral (chapter 11).
– Pin Control Register (PORTD_PCR5): We will be using the green LED on the FRDM-
KL46, so we will need PORTD (the port we're on), PCR5 (the pin we're on) and
configure it to GPIO => MUX = 001
PORTD->PCR[5] |= 1 << 8;
//This sets the Mux control of PTD5 to 001, or GPIO
10
Exercise 1: Blinking green LED
• Initialization process:
– Configuring the operation mode of the I/O pins
– Once a pin is configured as GPIO this register determines whether it
is an input or an output => Module General-Purpose Input/Output
(GPIO) (chapter 42)
– Port Data Direction Register (GPIO_PDDR) - This register
configures GPIO as inputs or outputs. Pin number is the same
as bit number, 0 is defined as input, 1 as output. So to
define PTD5 as an output we would set bit 5 of PDDR to 1.
PTD->PDDR |= (1<<5)
//This sets PTD5 as an output.
11
Exercise 1: Blinking green LED
• Blinking LED:
– The following register is used to write to GPIO pin (Chapter 42).
– Port Data Output Register (GPIO_PDOR) - This register will change the
output of a pin to the value given. So an input of 0 will change the bit to
a 0, an input of 1 will change the bit to a 1.
– Port Data Set Register (GPIO_PSOR) - This register will set the output
of a pin. An input of 0 will not change the current value of the pin, while
a 1 will set the bit to a 1.
– Port Data Clear Register (GPIO_PCOR) - This register will clear the
output of a pin. An input of 0 will not change the current value of the pin,
while a 1 will clear the bit to a 0.
– Port Data Toggle Register (GPIO_PTOR) - This register will toggle the
output of a pin. An input of 0 will not change the current value of the pin,
while a 1 will change the bit to the inverse of its current state.
12
Exercise 1: Blinking green LED
• Blinking LED:
– Example:
//Turn green led off, set pin 5 of portD to 1 (cathode of led connected to pin
5)
PTD->PDOR |= (1u<<5); //Change PTD5 to 1, turns LED OFF
for (i; i < 3000000; i++){}; //Burn some time
// Turn green led on, set pin 5 of portD to 1
PTD->PDOR &= ~((uint32_t)(1u<<5));
for (i; i < 3000000; i++){}; //Burn some time
13
Exercise 1: Blinking green LED
Create new project on Keil
- Go to Project>New µVision Project... then choose a directory and a name for yourproject
- Choose target device
14
Exercise 1: Blinking green LED
• Create new project on Keil
– Add CORE & Startup from CMSIS as the following
figure.
15
Exercise 1: Blinking green LED
• Adding to the project the user code files as follows:
– main.c: This file contains the main() function
– LED.c: The file contains functions to initialize the GPIO port pin
and to set the port pin on or off. The Init LED() function
initializes the GPIO port pin. The functions BlinkLEDx()
blink green LED on the port pin.
– LED.h: The header file contains the function prototypes
created in LED.c and must be included into the file
main.c.
16
Exercise 1: Blinking green LED
17
Exercise 1: Blinking green LED
• Write code for LED.c
#include "MKL46Z4.h" // Device header
void InitLED(void)
{ //This function enables the green LED on the
FRDM-KL46Z development board
; //This enables the clock to PORTD
; //This sets the Mux control of PTD5 to 001, or
GPIO
; //This sets PTD5 as an output.
}
void BlinkLED1(void)
{ //This method turns the LED off, then back on using two separate commands.
uint32_t i = 0; //Create a loop variable
; //Set PTD5 = 1, turns LED OFF (Cathode connected to PTD5)
for(i=0; i < 5000000; i++){}; //Burn some time
; //Clear PTD5 = 0, turns LED ON
} for(i=0; i < 5000000; i++){}; //Burn some time
18
Exercise 1: Blinking green LED
• Write code for LED.h
void InitLED(void); // Initialize GPIO
void BlinkLED1(void); // Use GPIO_PSOR and GPIO_PCOR
void BlinkLED2(void); // Use GPIO_PDOR
void BlinkLED3(void); // Use GPIO_PTOR
19
Exercise 1: Blinking green LED
• Debug the Blinky application
– From the toolbar, choose Project -> Options for Target …, click the Debug
tab, enable Use, and select the applicable debug driver.
– Select <Setting> to configure the debugger
20
Exercise 1: Blinking green LED
• Debug the Blinky application
– setting the debug connection
21
Exercise 1: Blinking green LED
• Debug the Blinky application
– Switch to the dialog Utilities and enable Use Debug Driver
– The device selection already configures the Flash programming algorithm
for on-chip memory. Verify the configuration using the Settings button
22
Exercise 1: Blinking green LED
• Debug the Blinky application
– To compile our program click on Rebuild in the tool bar.
• The Build Output window shows messages about the
compiling
progress.
11
23
Exercise 1: Blinking green LED
• Debug the Blinky application
– Program the application into Flash memory.
• From the toolbar, choose Download. The Build Output window
shows messages about the download progress.
• The green status light on the FRDM-KL46Z should light up, then go
dark, and your LED should be blinking if every is OK.
1 2
24
Exercise 2: Blinking LEDs
• Create a application to blink RED and Green LEDs
oppositely:
– If the Red LED is on, then green LED is off.
– If the Red LED is off, then green LED is on.
• Reading Material
– Chapter 11, 12 & 42 of KL46 Sub-Family Reference Manual
25
FRDM-KL46Z Overview
References: (at : http://courses.uet.vnu.edu.vn/)
26