Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include #include #include #include #include #include lcd_model.h void setup(void) { set_clock_speed(CPU_8MHz); lcd_init(LCD_DEFAULT_CONTRAST); lcd_clear(); // (a) Initialise Timer 0 in normal mode so

image text in transcribed

image text in transcribed

image text in transcribed

#include

#include

#include

#include

#include

#include

#include

#include

#include "lcd_model.h"

void setup(void) {

set_clock_speed(CPU_8MHz);

lcd_init(LCD_DEFAULT_CONTRAST);

lcd_clear();

// (a) Initialise Timer 0 in normal mode so that it overflows

// with a period of approximately 0.008 seconds.

// Hint: use the table you completed in a previous exercise.

// (b) Enable timer overflow interrupt for Timer 0.

// (c) Turn on interrupts.

// (d) Enable the joystick left switch for digital input.

// (e) Display your student number, "z83734946", with nominal

// top-left corner at screen location (22,22).

// Keep the next instruction intact.

show_screen();

}

// (f) Create a volatile global variable called state_count.

// The variable should be an 8-bit unsigned integer.

// Initialise the variable to 0.

// (g) Define a volatile global 8-bit unsigned global variable

// called is_pressed which will store the current state of the switch.

// (h) Define an interrupt service routine to process timer overflow

// interrupts for Timer 0. Every time the interrupt service

// routine is called, state_count should:

// (h.a) Left-shift state_count one place;

// (h.b) Bitwise AND with a mask in which the 5 bits on the right

// are 1 and the others are 0.

// (h.c) Use bitwise OR to add the current open/closed value of the

// joystick left switch to the history.

// (h.d) If state_count is equal to the bit mask, then the switch has been

// observed 5 times in a row to be closed. Assign the value 1 to

// is_pressed, indicating that the switch should now be considered to be

// officially "closed".

// (h.e) If state_count is equal to 0, then the switch has been observed

// to be open at least 5 in a row, so store 0 in is_pressed,

// indicating that the switch should now be considered to be officially "closed".

// -------------------------------------------------

// Test driver.

// -------------------------------------------------

void process(void) {

static uint8_t prevState = 0;

if ( is_pressed != prevState ) {

prevState = is_pressed;

draw_string( 30, 40, prevState ? "closed" : "open ", FG_COLOUR);

show_screen();

}

}

int main(void) {

setup();

for ( ;; ) {

process();

}

}

The test driver keeps track of the previous state of the designated switch. If the switch state changes, the new state is saved (becoming the previous state) and the state is displayed on the LCD.

Do not use the static qualifier for global variables.

Implement and test non-blocking switch de-bouncing. The program will use the Teensy's 8-bit timer in normal mode, with overflow interrupt handling. You will implement a timer overflow interrupt handler which will sample the state of a designated switch at uniform intervals, and from these measurements determine if the switch has settled into an open or losed state exercise is similar in concept to the algorithm described in Lecture 9: Non-blocking de-bouncing. However the algorithm you will implement in this exercise is superior in two vays. Firstly, the use of an interrupt means that de-bouncing is carried out behind the scenes without holding up the main event loop of a program. Secondly, the interrupt handler will be to triggered at a pre-defined uniform frequency, so that computations in the main event loop do not affect the de-bouncing procedure. The central idea of this algorithm is as follows: The state of the switch is read at regular intervals, giving a time series of (0|1) values: 0 when the switch is open, 1 when the switch is closed. When the switch goes from open to closed or vice-versa, transient high-speed on-off transitions may be observed, causing spurious clicks to be detected The transient effects can be removed by smoothing the time series Instead of considering a single on-off value at any given time, we consider a sum of recent values, taken over an interval leading up to that time o This is similar to a moving average as used in statistics or stock analysis The example in Lecture 9 maintained separate sums for on and off observations, counting upwards with one or the other as appropriate switch states were detected, and swapping to the other when a transition was observed It is possible to simplify the state machine: o For each switch that we want to read, we keep an unsigned integer in which we store the recent history of the switch state. The mechanism is similar to a Lecture 9: Appendix 1 : Bit-packed boolean arrays, but we will not access the individual bits once they are stored in the history. o Each time we read the state of the switch, we delete the oldest historical state, and add the new state. o If the history fits into a single byte, then extremely fast bitwise operations can be used to update the history. Every time we get a new value, we push it into the "right-hand" end (i.e. least significant bit) of the history To make room for the new bit, we shift the existing bits one place to the left (using the left-shift operator If we want to keep fewer than 8 historical states, we can use a bit mask to expunge items that are too old. Example: a switch is initially open, then it closes and opens again, with ephemeral on-off spikes at both transitions, so that we observe the sequence of states to,1,0,1,1,0,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,If we use a sliding window of 4 bits, the history will be implemented as follows: Implement and test non-blocking switch de-bouncing. The program will use the Teensy's 8-bit timer in normal mode, with overflow interrupt handling. You will implement a timer overflow interrupt handler which will sample the state of a designated switch at uniform intervals, and from these measurements determine if the switch has settled into an open or losed state exercise is similar in concept to the algorithm described in Lecture 9: Non-blocking de-bouncing. However the algorithm you will implement in this exercise is superior in two vays. Firstly, the use of an interrupt means that de-bouncing is carried out behind the scenes without holding up the main event loop of a program. Secondly, the interrupt handler will be to triggered at a pre-defined uniform frequency, so that computations in the main event loop do not affect the de-bouncing procedure. The central idea of this algorithm is as follows: The state of the switch is read at regular intervals, giving a time series of (0|1) values: 0 when the switch is open, 1 when the switch is closed. When the switch goes from open to closed or vice-versa, transient high-speed on-off transitions may be observed, causing spurious clicks to be detected The transient effects can be removed by smoothing the time series Instead of considering a single on-off value at any given time, we consider a sum of recent values, taken over an interval leading up to that time o This is similar to a moving average as used in statistics or stock analysis The example in Lecture 9 maintained separate sums for on and off observations, counting upwards with one or the other as appropriate switch states were detected, and swapping to the other when a transition was observed It is possible to simplify the state machine: o For each switch that we want to read, we keep an unsigned integer in which we store the recent history of the switch state. The mechanism is similar to a Lecture 9: Appendix 1 : Bit-packed boolean arrays, but we will not access the individual bits once they are stored in the history. o Each time we read the state of the switch, we delete the oldest historical state, and add the new state. o If the history fits into a single byte, then extremely fast bitwise operations can be used to update the history. Every time we get a new value, we push it into the "right-hand" end (i.e. least significant bit) of the history To make room for the new bit, we shift the existing bits one place to the left (using the left-shift operator If we want to keep fewer than 8 historical states, we can use a bit mask to expunge items that are too old. Example: a switch is initially open, then it closes and opens again, with ephemeral on-off spikes at both transitions, so that we observe the sequence of states to,1,0,1,1,0,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,If we use a sliding window of 4 bits, the history will be implemented as follows

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2016 Riva Del Garda Italy September 19 23 2016 Proceedings Part 1 Lnai 9851

Authors: Paolo Frasconi ,Niels Landwehr ,Giuseppe Manco ,Jilles Vreeken

1st Edition

3319461273, 978-3319461274

More Books

Students also viewed these Databases questions