0% found this document useful (0 votes)
3 views16 pages

Lab4 Reading Switches P1

Uploaded by

Nguyễn Nam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Lab4 Reading Switches P1

Uploaded by

Nguyễn Nam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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: [email protected]

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

You might also like