CS代考 4CCE1PHC

4CCE1PHC
1 Introduction
2 Preparation Materials
Make sure you have everything you need to com- plete this lab.
The hardware items should be included in your lab kit.
Ensure that you have the Atmel ATmega328P Datasheet, and the Arduino Schematic Circuit Di- agram to hand.
Item Qty
1 USB Type A – B cable 1 ADALM2000 1 USB Type A – Micro-USB cable 1 Jumper wires 8 Potentiometer 1 LED 1
Interrupts
December 6, 2021
This lab introduces you to working with interrupts in order to be able to write programs that can respond to internal and external events.
At the end of the lab, you should be able to:
• Install an interrupt handler routine.
• Use external interrupts to respond to external events.
• Use timer interrupts to improve the implementation of repetitive tasks.
Before starting work you should read through this document and complete the preparatory tasks detailed in §2.
Ensure that you have the following software in-
stalled on your computer: (i) a terminal program
(e.g., xterm or similar on Linux/Mac OS, cygwin
on Windows), (ii) a text editor (e.g., gedit or similar on Linux/Mac OS, notepad++ on Windows), (iii) avr-libc (including , avr-gccavr-objcopy and avrdude) (iv) the arduino IDE(optional), and (v) scopy..
Self-study
Read §3 below, read up on any new material (in textbooks and/or online) that you need to, to accomplish the tasks. Remember to make notes of where you got the information, so that you can quickly go there again if something does not work as expected.
Study and familiarise yourself with the data sheet for the ATmega328P and the avr-libc documentation related to interrupts. It is not necessary to understand all the details, but it is important that you familiarise
Dr Matthew Howard 1 Deparment of Engineering © 2020 King’s College London

4CCE1PHC
yourself with the functions and configuration parameters available. Answer the following questions with reference to the Arduino:
1.
2. 3.
3 3.1
How do you install an interrupt handler in C to be triggered when a specific value is reached in Timer 1? This is done by writing an interrupt service routine tied to the interrupt vector TIMER1_COMPA or TIMER1_COMPA.
List the different interrupt sources available on the Arduino. Each of the interrupt vectors in Table 12-6 of the ATmega328P data sheet is associated with a different interrupt source.
How are interrupts enabled and disabled globally? What methods are provided by avr-libc for this? This is done by clearing the interrupt flag in the status register SREG. The methods provided by avr-libc this are cli() which disables all interupts, and sei() which enables all interrupts.
Laboratory Work External Interrupts
In this part of the lab, you will create a program to trigger an event detected by a change in state of the digital I/O pins of the Arduino. This is the simplest example of event-driven computing. To complete this exercise, take the following steps:
1. Write a program that toggles the state of the internal LED of the Arduino when the PD3 digital I/O pin transitions between high and low or vice versa. Where possible, reuse the functions that you wrote for manipulating the LED state from previous labs. Verify that your program compiles without errors and download it to the Arduino. Test it works as expected by toggling the input to PD3 between 0 V and 5 V and observing the behaviour of the LED.
Hint: A simple way to toggle the input to PD3 is to connect/disconnect a jumper cable from the Arduino’s 5 V pin or create a circuit with a push-switch to control the voltage. Alternatively, you can use the power supply function of the ADALM2000 to do this.
Think: You may find that at some point toggling PD3 from low to high causes the LED state to toggle in the opposite direction. Why should this be? This is because the voltage may ‘bounce’ between high and low as you connect/disconnect the pin. If this transition happens between reads of the pin, it may be missed, causing the state to reverse.
Dr Matthew Howard 2 Deparment of Engineering © 2020 King’s College London

4CCE1PHC
1
2
3
4
5 6} 7
8
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
main()
led_init ();
DDRD = (1< /* Include IO library. */
void led_init(void){
DDRB = (1< /* Include IO library. */
#include /* Include interrupts library. */
on changes to PD3. */
9 void led_on(void){
10 /* Switch on LED (set PORTB pin 5). */
11 }
12
13 void led_off(void){
14 /* Switch off LED (unset PORTB pin 5). */
15 }
16
17 void input_init(void){
18 /* Set PORTD pin 3 as input. */
19 }
20
21 void isr_init(void){
1
2
3
4
5
6 7} 8
void led_init(void){
/* Set up internal LED to toggle
22 /* 23
24
25
Set up interrupts on INT1. This should:
1) Disable interrupts while setting up.
2) Configure INT1 to trigger on state change and enable INT1. 3) Re-enable global interrupts. */
26 } 27
28 ISR (INT1_vect)
29 {
30
31
32
33 } 34
35 int
36 {
37
38
39
40
41
42
43 }
Dr Matthew Howard © 2020
/* Interrupt Service Routine for INT1.
This should toggle the LED on/off when the INT1 interrupt is triggered. */
main() /* Main function – DO NOT MODIFY! */
led_init (); input_init (); isr_init ();
while(1) ;
Listing 2: Detecting digital input with an interrupt.
5
Deparment of Engineering King’s College London

4CCE1PHC
#include /* Include IO library. */
#include /* Include interrupts library. */
void led_init(void){
DDRB = (1<