Question
Complete the implementation of the setup_led_buttons and process_led_buttons functions. The hash-tags for this exercise are: #cab202, and #cab202LedButtons. After completing this exercise you will be
Complete the implementation of the setup_led_buttons and process_led_buttons functions. The hash-tags for this exercise are: #cab202, and #cab202LedButtons.
After completing this exercise you will be able to detect when the user has depressed the two buttons, and control the LEDs mounted on the Teensy circuit board. You will:
Control the CPU clock speed to enable accurate timing.
Set up a data direction register to enable digital output to the LEDs.
Write data to an output register to control the LEDs.
Set up a data direction register to enable digital input from the buttons.
Test the pins in an input register to determine if buttons are pressed.
To complete the program, follow the instructions detailed in the in-line comments in the skeleton code below.
Refer to the Teensy Pin-out diagram (a subset of the QUT Teensy Schematic diagram) to identify the Port and Pin associated with each LED and switch. Note that the relevant switches are labelled SW1 and SW2 on the pin-out diagram, but are labelled SW2 and SW3 on the Teensy itself. Use the labels from the pin-out diagram rather than those printed on the Teensy.
#include
void setup_led_buttons( void ) { // (a) Set the CPU speed to 9MHz (you must also be compiling at 9MHz).
// (b) Configure the data direction register for Port B to enable output // to LED0 and LED1. The data direction for LED0 is controlled by Pin 2, // while that for LED1 is controlled by Pin 3. No other pins should be // affected.
// (c) Turn off LED0, LED1, and all other outputs connected to Port B, by // clearing all bits in the Port B output register.
// (d) Configure the data direction register for Port F to enable input // from the left button (SW1 on the pin-out diagram) and right button // (SW2 on the pin-out diagram). SW1 is activated by clearing Pin 6, and // SW2 is activated by clearing Pin 5. No other pins should be affected by // this operation. }
void process_led_buttons( void ) { // (e) Test the relevant pins in the Port F input register. If the // bits corresponding to _both_ SW1 and SW2 are set, turn off _both_ LEDs. No // pins other than those controlling LED0 and LED1 should be affected by // this action.
// (f) Otherwise, if the bit corresponding to SW1 is set, turn on LED0 // while ensuring that no other pins are affected.
// (g) Otherwise, if the bit corresponding to SW2 is set, turn on LED1 // while ensuring that no other pins are affected. }
int main(void) { setup_led_buttons();
while ( 1 ) { process_led_buttons(); _delay_ms(100); }
return 0; }
Use avr-gcc to compile the test driver, convert it to a .hex file, and download the .hex file to your Teensy. If the program is implemented correctly:
When the program starts, both LEDs will be off.
Pressing the left button will cause LED0 to light up and remain on after the button is released.
Pressing the right button will cause LED1 to light up and remain on after the button is released.
Pressing both buttons at the same time will cause LED0 and LED1 to turn off.
Submit your source code in the same manner as usual; do not submit your .hex file.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started