Here we look at the digital input and output from the device.
The typical hardware devices that handle the physical aspects of digital signals are LEDs and Switches.
- LED output
- Push Button input
- Application Shield IO
- exercises
- extension
LED output
Clone the repository s1.1
this contains a basic blinky program.
Build the program and upload it into the FRDM-K64F. Once done you will need to press the reset button.
resources
- Refer to the LED diagram in the FRDM K64F Userguide section 10, page 14 for pin connections.
- Summary of important pin names
- An overview of the board and the mbed library
The start point is (s1.1):
#include <mbed.h>
int main() {
DigitalOut red(PTB22);
while(1) {
red.write(0);
wait(0.5);
red.write(1);
wait(0.5);
}
}
- Using the FRDM K64F Userguide and/or the FRDM K64F Schematic, identify the pins that drive the Green and Blue LEDs on the board.
Green LED pin PTE26 (aka LED_GREEN), Blue LED pin PTB21 (aka LED_BLUE)
- Modify the program to manage the Green and Blue LEDs in addition to the Red LED. solution 1.1
- Modify the flashing pattern to make use of all three LEDs.
solution 1.1
- What value needs to be written to the pin in order to
- turn the LED on? 0
- turn the LED off? 1
- From the Schematic can you identify why the values are what they are?
What is pin-2 of the LED package connected to?
Pin-2 of the LED package is connected to 3.3V (logic high/1).
If a 1 is output from the pin it is at 3.3V, there is no voltage difference across the LED so it does not light.
Output a 0 there is a voltage difference and so the LED turns on.
- Write a wrapper that provides functions for
on()
, off()
, and/or set()
that takes into account the way the LEDs are wired.
solution 1.1
Clone the repository s1.2.
This is the base level LED driver from s1.1 with the addition of a single push button (One of the two on the base board.)
#include <mbed.h>
#include <stdbool.h>
int main() {
bool flashing = false;
DigitalOut red(PTB22);
DigitalIn btn(PTC6);
while(1) {
if( btn.read() ) flashing = true;
else flashing = false;
if(flashing){
red.write(0);
wait(0.5);
red.write(1);
wait(0.5);
}else{
red.write(1);
}
}
}
- Locate the buttons (switches SW2 and SW3) on the FRDM K64F Schematic.
- Using the schematic and observing the behaviour of the program what can be deduced about:
- What value is read when the button is
- pressed? 0
- not pressed? 1
- Why is this the case?
The Pins are connected to 3.3V via a pull-up resistor, so the pin reads a logic 1 when the button in not pressed. Pressing the button creates a direct connection to ground (0V) so the pin reads a logic 0
- Add a wrapper function that evaluates as
bool ispressed( /*button*/ )
that returns the correct state of the button.
solution 1.2
Application Shield IO
The Application Shield has a second RGB-LED and a 5-way joystick like switch.
- Refering to the Application Shield Schematic,
- Identify the pins that drive the shield RGB-LED
- Extend the capabilities of the LED wrapper functions to include the Shield’s LEDs. solution 1.3
- Find the 5-way switch on the schematic
- Is it wired the same way with respect to the ground and 3.3V signals?
No. The switches are wired the opposite way round.
- What will the impact be on the implementation of the
ispressed()
function?
Not nice, I need to invert some pin readings and not others. Solution look at the truth table for (a xor true)
- extend the capability of the
ispressed()
function to include the application shield.
solution 1.4
exercises
- Flash an LED at given rate
- 1Hz
- 10Hz
- 45 flashes per minute
- Traffic light sequence: Using Blue instead of Amber:
- create a sequence of led on and offs that replicate a traffic light sequence. What timings do you use for each phase?
- using the RGB LED on the base and the shield create a junction with two traffic lights:
- what are the allowed combinations of lights?
- how would you ensure correct operation?
- Maritime navigation lights have characteristic sequences of flashes,
(see https://en.wikipedia.org/wiki/Light_characteristic),
How would you create the following patterns
- a flashing light has rate of 37.5 flashes per minute, the light is off for three times longer than it is on
- A light flashes (as above) showing 3 flashes, every 6 seconds.
- safety switch (two hand start) is a common feature of machinery. To start flashing both switches (buttons) must be pressed together.
- flash at variable rates: a countdown timer on a camera flashes initially at 1 flash per second, it flashes faster over 10 seconds, showing a continuous light just before the camera and flash fires.
extension
explore plain C bare-metal code.
- gate clock signals
- configure pin
- set gpio state
- set,clear,toggle
© 2017 Dr Alun Moon
alun.moon@northumbria.ac.uk