Question: / * * EX: 7 . 0 When configuring hardware peripherals to generate interrupts, it is important to ensure that interrupts cannot be raised by

/** EX: 7.0
When configuring hardware peripherals to generate interrupts, it is
important to ensure that interrupts cannot be raised by other
peripherals while this configuration is taking place.
TASK: Disable interrupts globally before configuring the TCB0 peripheral
and re-enable interrupts globally afterwards.
HINT: Use the cli() and sei() functions provided by avr/interrupt.h.
*/
void clock_init(void)
{
/** CODE: Write your code for Ex 7.0 within this function. */
// TCB0 is a 16-bit timer that has been configured to generate
// an interrupt every 64th of a second. This was done to increase
// the resolution of the timer.
TCB0.CCMP =52083;
TCB0.CTRLB = TCB_CNTMODE_INT_gc;
TCB0.INTCTRL = TCB_CAPT_bm;
TCB0.CTRLA = TCB_ENABLE_bm;
}
/** EX: 7.1
The stopwatch will be controlled by pushbuttons S1, S2, and S4.
TASK: Write code to configure the pins connected to these pushbuttons
to generate an interrupt on falling edges.
See ATtiny1626 Datasheet 17.4 Register Summary - PORTx on p.153 and
17.5.12 Pin n Control on p.165.
*/
void buttons_init(void)
{
/** CODE: Write your code for Ex 7.1 within this function. */
}
/** EX: 7.2
To display the elapsed time on the 7-segment display, we need to count
the number of times the TCB0 CAPT interrupt is invoked.
TASK: Declare a global variable called "count_64" that will be used
to store a count of 1/64 seconds that have elapsed.
Ensure this variable is sufficiently large so that it can represent the
largest value that can be displayed on the 7-segment display.
The stopwatch should initially display 0x00.
*/
/** CODE: Write your code for Ex 7.2 above this line. */
/** EX: 7.3
To allow pushbuttons S1 and S2 to control when the stopwatch is
counting, we need to keep track of the state of the stopwatch.
TASK: Declare a global variable called "is_counting" that keeps track
of the stopwatch's state.
The stopwatch should initially be paused.
*/
/** CODE: Write your code for Ex 7.3 above this line. */
/** EX: 7.4
TASK: Declare an interrupt service routine (ISR) for the TCB0 peripheral
that was configured in Ex 7.0.
1. Find the vector number and name that correspond to the TCB0 CAPT
interrupt.
Refer to datasheet Section 8.2 Interrupt Vector Mapping on p.63.
2. Given that vectors are of the form "*_vect", find the appropriate
macro corresponding to the vector number identified in step 1.
Type "_vect" to invoke VSCode suggestions.
3. Declare an ISR using the ISR() macro syntax, where
is the macro identified in step 2.
4. If the stopwatch is currently counting, increment "count_64" by 1.
Ensure that this variable does not exceed the value which represents
15.9375 s (0xFF *1/16 s).
5. As the value displayed on the 7-segment display is a count of 1/16 s,
define a local variable called "count_16" and convert "count_64" to
a count of 1/16 s.
6. Update the 7-segment display to reflect the value of "count_16".
7. Acknowledge that the interrupt has been handled by clearing the CAPT
bit in the TCB0.INTFLAGS register.
See ATtiny1626 Datasheet 22.5.5 Interrupt Flags on p.260 for
information on how to clear this bit.
NOTE: You may use display_hex() to display a number in hexadecimal on
the 7-segment display. This function accepts an unsigned 8-bit integer.
To find the maximum value of "count_64" in step 4 and to perform the
conversion in step 5, you will need to use the following relationship to
convert between counts of 1/64 s and counts of 1/16 s:
1/16 second =4*(1/64 second)
*/
/** CODE: Write your code for Ex 7.4 above this line. */
/** EX: 7.5
To allow the variables "count_64" and "is_counting" to be shared between
compilation units (i.e., src/timer.c and src/buttons.c), we must declare
them as external variables.
TASK: Declare "count_64" and "is_counting" as external variables.
NOTE: An external declaration must not provide an initialisation.
*/
/** CODE: Write your code for Ex 7.5 above this line. */
/** EX: 7.6
TASK: Write an interrupt service routine for PORTA, that will be invoked
when a falling edge is detected on any pins connected to pushbuttons S1,
S2, or S4.
This ISR should check which pin generated the interrupt, and perform
one of the following actions:
- If S1 is pressed, start the stopwatch.
- If S2 is pressed, pause the stopwatch.
- If S4 is pressed, reset the stopwatch to 0x00.
See ATtiny1626 Datasheet 17.5.10 Interrupt Flags on p.163 for
information on how to clear these interrupt flags.
NOTE: Each pin has its own interrupt flag, and you must only clear the
interrupt flag for the pin that generated the interrupt.
*/
/** CODE: Write your code for Ex 7.6 above this line. */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!