Events and interrupts

Handling events, button interrupts and timer interrupts. We want to be able to respond to external events, as they occur. Or to create a precisely timed series of events.

  1. Button edge Events
  2. Timer interrupts
  3. Soft timer
  4. Morse Code  – – ⋅ ⋅ ⋅ ⋅ – ⋅ ⋅ ⋅ ⋅ ⋅

Signal Edge

rising edge

when the pin chages from a 0 to a 1

faling egde

when the pin chages from a 1 to a 0

Button edge Events

The repository s2.1 uses a falling and rising edge interrupt on each of the two buttons on the base board.

  1. Run the program and examine it’s behaviour. (You’ll need the serial-monitor to see the output from the print statements)
  2. Pay close attention to when the actions occur, when the button is pressed or released.
  1. Identify the input pins related to the buttons and the 5-way joystick on the shield.
  2. Each pin can have an interrupt for the rising-edge and falling-edge signal, a total of 14 possible interrupts.
  3. Investigate the actions of the interrupts for the base-board switches and the 5-way switch. What differences can you see?

Timer interrupts

The FRDM-K64F had a Periodic Interrupt Timer (PIT). This can generate an interrupt at a given rate. There are 4 individual timers, which can be used in isolation or chained together to give a more flexible (and longer period) interrupt pattern.

The Mbed library uses this to drive a soft-timer. This allows an arbitrary number of functions to be called at given periods, rather that depend on hardware limits.

Clone s2.2

Modify the program to use a callback every 1ms (1000µs). Use the callback function (flash()) to count in ms, and flash an led with the following pattern:

  1. On for 0.1s
  2. Off for 0.1s
  3. On for 0.3s
  4. Off for 0.7s

Try some of the flashing patterns from seminar 01

Soft timer

The MBED library uses a soft timer with a microsecond (μs) resolution.
We can write our own simpler version, to control an LED flashing. s2.3

  1. Modify the program to vary the period and duration of the flashes
  2. A Camera 10 second timer counts down to taking a picture. It flashes once per second initially, getting faster, until the final second has the light showing continuously for the last second.
    • modify the program to produce this sequence on one of the LEDs
    • simulate the camera flash by turning on all the channels (red,green,blue) of the other LED.

Morse Code  – – ⋅ ⋅ ⋅ ⋅ – ⋅ ⋅ ⋅ ⋅ ⋅

Morse is sent as a series of flashes, dots and dashes. There is a convention for the timing as follows:

The dot duration is the basic unit of time measurement in code transmission.
The duration of a dash is three times the duration of a dot.
Each dot or dash is followed by a short silence, equal to the dot duration.
The letters of a word are separated by a space equal to three dots (one dash),
and the words are separated by a space equal to seven dots.

  1. Write a program to flash an led in dots and dashes.
    Use the timer period to control the speed of a dot-time-unit.
  2. Devise an encoding for dots and dashes.
    (a suggestion: use 0 for off 1 for on, a dash is 111 )
  3. Use the callback function to walk along a string of 0s and 1s turning the LED on or off as required.

The word PARIS is often used to measure timing in Morse transmissions, it is 50 dot-units long. At 10 words per-minute, what is the length of a dot-unit? Use this as the period for the timer.

One suggestion of how the skeleton of the callback might look.
Assume the message is a string/array pointed to by message

char c = *message++; /* sets c to the next character,
                        and moves the pointer on by one */
if( c=='0' ) /* off */
if( c=='1' ) /* off */
if( c=='\0' ) /* NUL character at the end of a C string */

Now you have created one of the first digital network communications devices!
You can send messages to each other.
(assuming you can read Morse)


© 2017   Dr Alun Moon
alun.moon@northumbria.ac.uk