You are on page 1of 27

VietNam National University

University of Engineering and Technology


Universi
ty

EMBEDDED SYSTEM FUNDAMENTALS


(ELT3240, NHẬP MÔN HỆ THỐNG NHÚNG)

Dr. Nguyễn Kiêm Hùng


Email: kiemhung@vnu.edu.vn

Laboratory for Smart Integrated Systems


Objectives

In this lecture you will be introduced to:

– Developing embedded applications with FRDM-KL46Z


Platform
– Master Keil MDK-ARM IDE tool

2
Lab 5: Reading Switches

3
Exercise 1: Control LED by polling SW1
• Write an program to control LEDs by buttons
– Read the status of SW1, if SW1 is pressed -> turn on
GREEN LED, if SW1 is released -> turn off GREEN
LED

• Reading Material
– Chapter 11, 12 & 42 of /Docs/KL46P121M48SF4RM.pdf

4
Exercise 1: Control LED by polling SW1
• SW1 and SW2 are connected as follows.[1]

SW1  PortC3 SW2


SW2  PortC12

SW1

Green LED

5
Exercise 1: Control LED by polling SW1
Issue: When one pin is configured as an input and nothing is
connected to the pin ->program cannot read the pin state (floating or
unknown state)

Pull up and pull down resistor:

Pull up resistor: no input Pull down resistor: no


Short circuit when switch is
signal, bring input to input signal, bring input to
closed
high logic level low logic level
6
Exercise 1: Control LED by polling SW1
Pull-up/down Resistor
• Issue: When one pin is configured as an input and nothing is
connected to the pin ->program cannot read the pin state (floating or
unknown state)
• Pull-up/down Resistor can is selected and enabled from inside KL46Z
– PS (Pull Select): 1: Pull-up; 0: Pull-down
– PE (Pull Enable): 1: Enable; 0: Disable

7
Exercise 1: Control LED by polling SW1

Contact bounce is a serious problem we must address when inputs are


derived from switches (it’s produced by the mechanical nature of the
switch design)
8
Exercise 1: Control LED by polling SW1
• Initialization process for SW1: InitSW1()
– Programming the clock control circuitry
– System Integration Module (SIM) - This gives control of which peripherals are
clocked. (see [3] 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
SW1 on pin PTC3 we must first enable the clock to PortC in SCGC5.

SIM->SCGC5 |= ; /* Enable clock for port C */

9
Exercise 1: Control LED by polling SW1

• Initialization process for SW1: InitSW1()


– Configuring the operation mode of the I/O pins
– Port Control and Interrupts (PORTx->PCR[n]) - This register
determines which pins are used for which peripheral. ([3], chapter 11)
– Pin Control Register (PORTC_PCR3) - We will be using the SW1
on the FRDM-KL46, so we will need PORTC (the port we're on),
PCR3 (the pin we're on) and configure it to GPIO and enable its
pull-up resistor.
PORTC->PCR[3] |= | | ;
//This sets the Mux control of PTC3 to 001, or GPIO mode
//Enable pull-up resistor

10
Exercise 1: Control LED by polling SW1

• Initialization process for SW1: InitSW1()


– 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. ([3], 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 PTC3
as an input we would set bit 3 of PDDR to 0.

PTC->PDDR &= ;
//This sets PTC3 as an input. There are no masks defined for
each pin so we have to do it by hand

11
Exercise 1: Control LED by polling SW1
Project Flowchart with polling

12
Exercise 1: Control LED by polling SW1

• Reading a pin:
– Port Data Input Register (GPIO_PDIR) - it will return the value of an
input port in a 32 bit format. The value of a single pin can be obtained
by taking that bit from this register.

if ((PTC -> PDIR & (1<<3))==0) /* Pressed SW1 */


{
/* do something */

}
else /* unPressed SW1 */
{
/* do something */
}

13
Exercise 1: Control LED by polling SW1
#include "MKL46Z4.h"

void InitLED();
void InitSW();

int main()
{
InitLED(); //Initialization process for green LED
InitSW(); //Initialization process for SW1

while (1)
{
if ((PTC -> PDIR & (1<<3))==0) //If SW1 is pressed
{
//Do something
}
else
{
//Do something
}

return 0;
}

14
Exercise 1: Control LED by polling SW1

void InitLED()
{
//This enables clock to port
// This sets the Mux control of PTD5 to 001, or GPIO
//This sets PTD5 as an output.
}

void InitSW()
{
//This enables clock to port C
// This sets the Mux control of PTC3 to GPIO & enable pull up resistor & pull select
//This sets PTC3 as an input.

15
Exercise 1:
2: Control LED by polling SW1
SW
• Write an program to control LEDs by buttons
– Read the status of SW1, if SW1 is pressed -> turn on
GREEN LED, if SW1 is released -> turn off GREEN
LED
– Read the status of SW2, if SW2 is pressed -> turn off
RED LED, if SW1 is released -> turn on RED LED

• Reading Material
– Chapter 11, 12 & 42 of /Docs/KL46P121M48SF4RM.pdf

16
Exercise
Polling with
1: Control
Interrupt
LED by polling SW1
Polling Interrupt

Button is pressed?

Action when Button


is pressed?

17
Exercise 3:
2: Control LED by SW1 interrupt
• Write an program to control LEDs by button
interrupt
– If having interrupt signal -> Read the status of SW1,
if SW1 is pressed toggle GREEN LED

• Reading Material
– Chapter 11, 12 & 42 of /Docs/KL46P121M48SF4RM.pdf

18
Exercise 3:
2: Control LED by SW1 interrupt

// This enables clock to port D


//This sets the Mux control of PTD5 Init_LED()
//This sets PTD5 as an output.

//This enables clock to port C


// This sets the Mux control of PTC3 to GPIO &
enable pull up resistor & pull select
//This sets PTC3 as an input.
//This select falling edge interrupts for PORTC[3] Init_SW()
// Clear NVIC any pending interrupts on PORTC_D
// Enable NVIC interrupts source for PORTC_D
module

// Check if SW1 is pressed or not


//If SW1 is pressed, toggle Green LED
//Clear interrupt service flag Interrupt_handler()

19
Exercise 3:
2: Control LED by SW1 interrupt

• Initialization process of switch:


– Configuring the External Interrupt ([3], chapter 11)
– Port Control and Interrupts (PORTx->PCR[n]) - This register
determines which pins are used for which peripheral. Pin Control
Register (PORTC_PCR3) - We will be using the SW1 on the FRDM-
KL46, so we will need to configure external interrupt on PORTC (the
port we're on) and PCR3 (the pin we're on).

PORTC->PCR[3] |= PORT_PCR_IRQC(0xA);
/* Set value for IRQC bit field in PCR register to select falling edge
interrupts for PORTC[3].
20
Exercise 3:
2: Control LED by SW1 interrupt

• Initialization process of switch:


– Configuring the External Interrupt ( chapter 11)
- Why selecting Interrupt on falling edge?

+ Microcontroller is looking for a specific change in the state of a signal pin


(SW1 on PTC3)
+ In this case, the interrupt will be triggered when SW1 is pressed -> the
signal on the pin transitions from a high voltage level (logic 1) to a low voltage
level (logic 0) -> falling edge

PORTC->PCR[3] |= (0xA) << 16;


PORTC->PCR[3] |= PORT_PCR_IRQC(0xA);
/* Set value for IRQC bit field in PCR register to select falling edge
interrupts for PORTC[3]. 21
Exercise 3:
2: Control LED by SW1 interrupt

• Initialization process:
– Configuring the External Interrupt
– ARM's Nested Vector Interrupt Controller configuration. Note that
interrupt (IRQ) number for both PORTC and PORTD are 31 (Check it
in the Definition of IRQn_Type in MKL46Z4.h and see Core M0+ pdf
file).

NVIC_ClearPendingIRQ(IRQn_Type );
/* Clear NVIC any pending interrupts on PORTC_D */
NVIC_EnableIRQ(IRQn_Type );
/* Enable NVIC interrupts source for PORTC_D module */

22
Exercise 3:
2: Control LED by SW1 interrupt

23
Exercise 3:
2: Control LED by SW1 interrupt

• Initialization process:
– Configuring the External Interrupt
– ARM's Nested Vector Interrupt Controller configuration. Note that
interrupt (IRQ) number for both PORTC and PORTD are 31 (Check it
in the Definition of IRQn_Type in MKL46Z4.h and see Core M0+ pdf
file).

NVIC_ClearPendingIRQ (31);
/* Clear NVIC any pending interrupts on PORTC_D */
NVIC_EnableIRQ (31);
/* Enable NVIC interrupts source for PORTC_D module */

24
Exercise 3:
2: Control LED by SW1 interrupt

• Interrupt Handler:
void PORTC_PORTD_IRQHandler(void) {

/* Put a proper name of PORTC_PORTD Interrupt service routine ISR. See


startup_MKL46Z4.s file for function name */

/*do anything you want here*/

PORTC->PCR[3] |= 1 << 24;


// or PORTC->PCR[3] |= PORT_PCR_ISF_MASK;
/* Clear interrupt service flag in port control register otherwise int. remains active
*/
}

25
Exercise 3:
Exercise 1: Control
Control LED
LED by
by SW1
polling SW1
interrupt
#include "MKL46Z4.h"

void InitLED();
void InitSW();
void PORTC_PORTD_IRQHandler();

int main()
{
InitLED(); //Initialization process for green LED
InitSW(); //Initialization process for SW1

while (1);

return 0;
}

void InitLED()
{
}
void InitSW()
{
}
void PORTC_PORTD_IRQHandler()
{
}

26
Exercise 4:3: Control LEDs by SWs’ Interrupt
Interrupt
• Write an program to control LEDs by
monitoring switches using
interrupt
– Read the status of SW1, if SW1 is pressed toggle RED
LED
– Read the status of SW2, if SW1 is pressed Green RED
LED

• Reading Material
– Chapter 11, 12 & 42 of
/Docs/KL46P121M48SF4RM.pdf

27

You might also like