Professional Documents
Culture Documents
Lab4 - Reading Switches - P2
Lab4 - Reading Switches - P2
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
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)
7
Exercise 1: Control LED by polling SW1
9
Exercise 1: Control LED by polling SW1
10
Exercise 1: Control LED by polling SW1
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.
}
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?
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
19
Exercise 3:
2: Control LED by SW1 interrupt
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:
– 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) {
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