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 Breadboard 1 Potentiometer 1 Jumper wires 3
Analogue Input
November 29, 2021
This lab introduces you to acquiring analogue signals with an embedded platform. At the end of the lab, you should be able to:
• Configure the ADC to sample from a chosen input.
• Capture a single measurement from the chosen input channel.
• Configure the ADC to capture multiple samples at a specified sampling rate.
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 analogue-to-digital conversion. It is not necessary to understand all the details, but it is important that you familiarise yourself with the functions and configuration parameters available. Answer the following questions with reference to the Arduino:
Dr Matthew Howard 1 Deparment of Engineering © 2020 King’s College London
4CCE1PHC
1. What is the resolution of the ADC?
2. How many input channels are available?
3. Write a C statement that starts a single conversion.
4. Write a C statement that waits until a conversion is complete.
3 Laboratory Work 3.1 Measuring Voltages
In this part of the lab, you will create a program to take simple voltage measurements using the microcon- troller’s ADC interface. This is central to measuring signals from many analogue devices such as sensors. To complete this exercise, take the following steps:
1.
2.
3.
4 4.1
Wire up a simple circuit in which the Arduino’s 5V and GND pins are connected across the poten- tiometer, and the potentiometer wiper (middle pin) is connected to bit 0 of PORTC (i.e., PC0). Hint: Use the Arduino data sheet to find the pin on the development board that corresponds to PC0. A simple way to test the basic functionality of the ADC is to implement a program that sets the internal LED of the Arduino when a prespecified voltage is measured. Complete the skeleton code in Listing 1 to implement this functionality. Verify that your program compiles without errors. Download your program to the Arduino and verify that it works as expected by moving the poten- tiometer so that the voltage on the wiper pin moves above and below the threshold defined by a_max. Experiment with changing the value of a_max and verify that this part of the program is working correctly.
Optional Additional Work Logging Data
Modify your program to capture a new measurement on channel zero every second and write the result to the UART so it can be read in the terminal window. Write out both the raw 10-bit value and the corresponding voltage measured.
Run the program and record what range of values you are able to measure when varying the potentiometer.
4.2 Multichannel Measurements
Modify your program from §4.1 to read data from two (or more) separate channels of the ADC. In your lab kits, you will find several components suitable for testing this, such as the multiaxis joystick, potentiometers or light dependent resistors. You will need to dynamically switch between ADC channels to implement this.
Dr Matthew Howard 2 Deparment of Engineering © 2020 King’s College London
4CCE1PHC
7 8} 9
1 /* Read an ADC */
2 #include
3
4 #define SUCCESS 0 5
6 void
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
void led_on(void){
// Switch the Arduino’s inbuilt LED on.
}
void led_off(void){
// Switch the Arduino’s inbuilt LED off.
}
void adc_init(void){
// Configure ADC conversion. //
// This function should:
// – Configure the ADC to be left-justified,
// – Set AV_CC as the voltage reference.
// – Select the correct ADC pin on the Arduino as the input.
// – Set the prescaler to the maximum value.
// – Enable the ADC.
}
int adc_read(void){
// Read the ADC and return the measured value.
return 0; }
int main(void) {
led_init ();
adc_init ();
int a_max=128; /* Threshold value. If the ADC reading is
led_init(void){
// Set up I/O on Arduino’s inbuilt LED.
while (1)
{
if(adc_read() >
led_on (); /* else
led_off (); /*
return SUCCESS; }
a_max)
Turn LED on */ Turn LED off */
Listing 1: read_adc.c. 3
}
above this value, the LED should turn on. */
Dr Matthew Howard © 2020
Deparment of Engineering King’s College London