Complete the implementation of the setupLed0, turnOnLed0, and turnOffLed0 functi
ID: 3748357 • Letter: C
Question
Complete the implementation of the setupLed0, turnOnLed0, and turnOffLed0 functions. The hash-tags for this exercise are: #cab202, and #cab202blinkLed0.
By completing this exercise you will develop the ability to control one of the LEDs mounted on the Teensy circuit board, including:
Controlling the CPU clock speed to enable accurate timing.
Setting up a data direction register to enable digital output to the LED.
Writing data to an output register to control the LED.
To complete the program, follow the instructions detailed in the in-line comments in the skeleton code below.
Notes
Use this test driver to implement and test your function prior to submission.
#include <avr/io.h>
#include <util/delay.h>
#include <cpu_speed.h>
#include <macros.h>
void setupLed0( void ) {
// (a) Set the CPU speed to 8MHz (you must also be compiling at 8MHz).
// (b) Configure the data direction register for Port B to use LED0 for
// output. The data direction for LED0 is controlled by Pin 2. No other
// pins should be affected.
// (c) Turn off LED0 (and all other outputs connected to Port B) by
// clearing all bits in the Port B output register.
}
void turnOnLed0( void ) {
// (d) Set pin 2 of the Port B output register. No other pins should be
// affected.
}
void turnOffLed0( void ) {
// (e) Clear pin 2 of the Port B output register. No other pins should be
// affected.
}
int main(void) {
setupLed0();
while ( 1 ) {
turnOnLed0();
_delay_ms(500);
turnOffLed0();
_delay_ms(500);
}
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, you should see LED0 alternately flash on and off with a period of 1 second.
Submit your source code in the same manner as usual; do not submit your .hex file.
Explanation / Answer
//connecting buttons to led
#include <avr/io.h>
#include <util/delay.h>
#include <cpu_speed.h>
#include <macros.h>
void setupLed0( void ) {
// (a) Set the CPU speed to 8MHz (you must also be compiling at 8MHz).
set_clock_speed(CPU_8MHz);
// (b) Configure the data direction register for Port B to use LED0
// to LED0. The data direction for LED0 is controlled by Pin 2
// affected.
SET_BIT(DDRB,2);
// (c) Turn off LED0, and all other outputs connected to Port B, by
// clearing all bits in the Port B output register.
PORTB=0b00000000;
// (d) Set pin 2 of the Port B output register. No other pins should be
// affected.
SET_BIT(DDRB,2);
}
void turnOffLed0( void ) {
// (e) Clear pin 2 of the Port B output register. No other pins should be
// affected.
CLEAR_BIT(PORTB,2);
}
int main(void) {
setupLed0();
while ( 1 ) {
turnOnLed0();
_delay_ms(500);
}
return 0;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.