4CCE1PHC
1 Introduction
Timers/Counters and Pulse-Width Modulation
November 22, 2021
This lab introduces you to the configuration and use of timers to produce accurately timed wave forms while keeping the CPU-core free for other processing.
At the end of the lab, you should be able to:
• Configure a timer for a desired mode of operation.
• Use a timer to produce a rectangular wave of a given frequency. • Use a timer to produce a pulse-width modulated wave.
• Use pulse-width modulation to control a servo motor.
Before starting work you should read through this document and complete the preparatory tasks detailed in §2.
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 Arduino documentation (e.g., ATmega328P Datasheet, Arduino Schematic Pinout Diagram) to hand.
Item Qty
1 USB Type A – Mini-USB cable 1 ADALM2000 1 USB Type A – Micro-USB cable 1 Breadboard 1 TowerPro SG90 Servomotor 2 TowerPro SG90 servo horns 2 LEDs 2 330 Ohm resistors 2 Jumper wires 12
Read §3 below and 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 get any information, so that you can quickly go there again if something does not work as expected.
Answer the following questions:
1. How does one use an oscilloscope to determine the frequency of a periodic signal?
Dr Matthew Howard 1 Deparment of Engineering © 2020 King’s College London
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-gcc, avr-objcopy and avrdude) (iv) the arduino IDE(optional), and (v) scopy.
Self-study
4CCE1PHC
2. How can one set or clear a specific bit in a register of the ATmega328P? 3. How does one configure a specific pin of the ATmega328P as output?
4. Use the data sheet to complete Table 3 on page 4.
3 Laboratory Work
3.1 Pulse-width Modulation with Timers
In this part of the lab, you will create a program that uses the Atmega328p’s Timer 1 to produce a PWM signal. To complete this exercise, take the following steps:
1. Wire up a simple circuit to display the state of bit 1 of PORTB (i.e., PB1) of the Arduino with an external LED. Ensure that the LED is properly protected by putting a 330 Ω resistor between it and the ground.
Hint: Use the Arduino data sheet to find the pin on the development board that corresponds to PB1.
2. Create a new file pwm_timer_fast.c and open it up in a text editor. Implement a program that sets PB1 as an output, and initialises it to low. Compile your program, upload it to the development board, and verify it works as expected.
3. Within your program, you may have included the following line to set up PB1 as an output:
1 DDRB = 0b00000010; /* set PB1 pin as output */
The constant 0b00000110 correctly sets bit 1 of the DDRB register, however, it is a little hard to read, since one must refer to the data sheet to understand the meaning of the bits being set/unset. A more readable way to write this is to include an explicit reference to PB1 when configuring the register. This can be done by using bitwise shift and or notation as follows:
1 DDRB = (1<