2 Input Output Ports
2 Input Output Ports
2 Input Output Ports
1
Chapter 2 EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 – Input / Output Ports
2.1 Pin configurations
2.2 Block diagram
2.3 AVR CPU Core
2.4 Memories
2.5 I/O-ports
2.6 Lab: Write I/O-ports
2
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 - Input / Output Ports
2.1 Pin configurations
3
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 - Input / Output Ports
2.1 Pin configurations
4
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 - Input / Output Ports
2.1 Pin configurations
1. VCC and GND: digital supply voltage and ground, respectively.
2. Port B (PB7:0) XTAL1/XTAL2/TOSC1/TOSC2: Port B is an 8-bit bi-directional I/O port with internal pull-up
resistors (selected for each bit). The Port B output buffers have symmetrical drive characteristics with both high
sink and source capability. As inputs, port B pins that are externally pulled low will source current if the pull-up
resistors are activated. The Port B pins are tri-stated when a reset condition becomes active, even if the clock is
not running.
3. Port C (PC5:0): Port C is a 7-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The
PC5:0 output buffers have symmetrical drive characteristics with both high sink and source capability. As
inputs, Port C pins that are externally pulled low will source current if the pull-up resistors are activated. The
port C pins are tri-stated when a reset condition becomes active, even if the clock is not running.
4. Port D (PD7:0): Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit).
The port D output buffers have symmetrical drive characteristics with both high sink and source capability. As
inputs, port D pins that are externally pulled low will source current if the pull-up resistors are activated. The
port D pins are tri-stated when a reset condition becomes active, even if the clock is not running.
5. PC6/RESET: If the RSTDISBL fuse is programmed, PC6 is used as an input pin. If the
RSTDISBL fuse is unprogrammed, PC6 is used as a reset input. A low level on
this pin for longer than the minimum pulse length will generate a reset, even
if the clock is not running.
Source: http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf
5
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 - Input / Output Ports
2.1 Pin configurations
6. AVCC: AVCC is the supply
voltage pin for the A/D
converter, PC3:0, and ADC7:6.
It should be externally
connected to VCC, even if the
ADC is not used. If the ADC is
used, it should be connected
to VCC through a low-pass
filter.
7. AREF: AREF is the analog
reference pin for the A/D
converter.
8. ADC (ADC7:6): In the TQFP and
QFN/MLF package, ADC7:6
serve as analog inputs to the
A/D converter. These pins are
powered from the analog
supply and serve as 10-bit ADC
channels.
6
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 - Input / Output Ports
2.1 Pin configurations
Absolute Maximum Ratings
7
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 - Input / Output Ports
2.1 Pin configurations
DC Characteristics
8
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 - Input / Output Ports
2.2 Block diagram
The Atmel® ATmega328P
is a low-power CMOS 8-
bit microcontroller based
on the AVR® enhanced
RISC architecture1. By
executing powerful
instructions in a single
clock cycle, the
ATmega328P achieves
throughputs approaching
1MIPS2 per MHz allowing
the system designer to
optimize power
consumption versus
processing speed.
1AVR is a family of microcontrollers developed since 1996 by Atmel, acquired by Microchip Technology in 2016 (acquisition price $3.56B). These are modified Harvard architecture 8-bit RISC (reduced instruction set
computer) single-chip microcontrollers. AVR was one of the first microcontroller families to use on-chip flash memory for program storage, as opposed to one-time programmable ROM, EPROM, or EEPROM used by
other microcontrollers at the time.
2Million instructions per second
9
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 - Input / Output Ports
2.3 AVR CPU Core
The main function of the CPU core is to ensure correct
program execution. The CPU must therefore be able to
access memories, perform calculations, control
peripherals, and handle interrupts.
10
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 - Input / Output Ports
2.3 AVR CPU Core
General Purpose Registers: The register file is optimized for the AVR® enhanced RISC instruction set1. ATmega-328
has 32 General Purpose registers. These all of the registers are the part of Static Random Access Memory (SRAM).
1A reduced instruction set computer, or RISC, is a computer instruction set that allows a computer's microprocessor to have fewer cycles per instruction (CPI) than a complex
instruction set computer (CISC).
11
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 - Input / Output Ports
2.4 Memories
The AVR® architecture has two main memory spaces, the data memory and Program Memory Map
the program memory space. In addition, the ATmega328P features an
EEPROM memory for data storage. All three memory spaces are linear and
regular.
12
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 - Input / Output Ports
2.4 Memories
SRAM Data Memory: The lower 2303 data memory locations address Data Memory Map
both the register file, the I/O memory, extended I/O memory, and
the internal data SRAM. The first 32 locations address the register
file, the next 64 location the standard I/O memory, then 160
locations of extended I/O memory, and the next 2048 locations
address the internal data SRAM.
13
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
Chapter 2 - Input / Output Ports
2.5 I/O-ports
All AVR® ports have true read-modify-write functionality
when used as general digital I/O ports. This means that the I/O Pin Equivalent Schematic
direction of one port pin can be changed without
unintentionally changing the direction of any other pin
with the SBI (set bin in I/O registers) and CBI (clear bin in
I/O registers) instructions.
14
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
1. PWM-controlled LED
Pulse width modulation (PWM) is a method of reducing the average power delivered by an electrical signal, by
effectively chopping it up into discrete parts. The average value of voltage (and current) fed to the load is controlled
by turning the switch between supply and load on and off at a fast rate. The longer the switch is on compared to the
off periods, the higher the total power supplied to the load.
It is noted that only Pins 3, 5, 6, 9, 10, 11 on Arduino UNO R3 board can be controlled by analogWrite to output
PWM signals, other pins are not able to output PWM signals.
15
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
1. PWM-controlled LED
Objectives:
Using PWM to control the brightness of an LED, try to demonstrate decreasing the LED’s lumen from maximum to
dimmed, and increasing to maximum.
Demo: https://www.youtube.com/watch?v=c5ApkEn6c-Q
Schematic:
Connect an LED to Pin6 of Arduino UNO R3 board,
as shown in the schematic diagram.
16
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
1. PWM-controlled LED
Codes: type in the code and upload to the board, be familiar with the idea of programming in this code. Ignore the
line numbers during inputting the code which are used for code interpretation in following slides.
17
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
1. PWM-controlled LED
Code interpretation:
https://www.youtube.com/watch?v=3BwQGZTdHkk
https://www.youtube.com/watch?v=YIk6FuR4TyY
Line 29 – Creating a new integer variable ‘LED’, and assigning ‘6’ to it. LED is then pointing to Pin6 (PD6).
Line 38 - analogWrite, can be used to output PWM signals from Arduino pins. The value in the bracket following
analogWrite determines the pulse width, or the output voltage. The value can be set from 0 to 255. If the value is 0,
output voltage is 0V. The value 64, duty cycle 25%, output voltage 1.25V. The value 255, output voltage is maximum
at 5V. Setting the value can specify the output voltage. We now see how to control the LED brightness using
analogWrite. The cathode of the LED is sunk by the Pin 6. OFF appearing at the Pin 6 will light the LED. If the Pin 6
outputs PWM signals, the longer the OFF state, the shorter the pulse width, the brighter the LED.
18
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
1. PWM-controlled LED
Code interpretation:
Line 35:46 - To continuously change the LED brightness, the value in the analogWrite instruction should be varied
continuously. In a For loop, the variable i will change with the loop. If the variable i is used as the value in
analogWrite, the LED brightness can be changed continuously.
Define a new variable fadeValue with the initial value of 0 in the For loop.
The variable fadeValue ranges from 0 to 255, which is in line with the analogWrite value.
Each execution, fadeValue will increase by 5 by this instruction. It takes longer if increasing by 1.
The other For loop will brighten the LED. Since both For loops are in the Loop function, the LED will be continuously
switched between dimmed and bright.
19
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
2. Playing a scale on speaker
Objectives:
A speaker connected to Arduino UNO R3 board is controlled to play a scale(C5-C6) while the LED is flashing.
20
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
2. Playing a scale on speaker
Schematic:
Connect a speaker to PinA3 of Arduino UNO R3
board, as shown in the schematic diagram. The
speaker has two terminals, one of which is
connected to ground and another is connected to the
Pin A3. The Pin A3 outputs a square wave with
specific frequency, which will be converted to a tone
by the speaker. If Arduino outputs 262 Hz square
wave from the Pin A3, which is close to Middle C on a
piano. The speaker will play a tone close to the note.
Tutorial:
https://www.youtube.com/watch?v=oMocA9R0IsE
21
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
2. Playing a scale on speaker
Codes: type in the code and upload to the board, study the study using the guides followed.
22
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Read and write I/O-ports
2. Playing a scale on
speaker
Codes: “pitches.h”, we will
create a file to store the 88
piano frequencies. Click the
triangle button in the
Arduino software, choose
New Tab, and name the file,
e.g. pitches.h. Type in the
below code and save in the
same folder as the main
code. *.h means this is a
header file.
23
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
2. Playing a scale on speaker
Code interpretation:
Tutorials:
https://www.youtube.com/watch?v=bnAY03EMwF0
https://www.youtube.com/watch?v=SkhQI_9scuc
https://www.youtube.com/watch?v=nV904uWowTk
https://www.youtube.com/watch?v=WH7BBiHI7Nc
24
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
2. Playing a scale on speaker
Code interpretation:
Line 51 - In the Loop function, this new instruction Tone generates a square wave of the specified frequency on a
pin. There are two syntaxes for Tone: two parameters and three parameters. Pin, the pin on which to generate the
tone; frequency, the frequency of the tone in hertz; duration, the duration of the tone in milliseconds. Command
Tone can drive the speaker to play a tone with specific frequencies. If each of the frequency of a piano scale plays on
the speaker, we can hear Do, Re, Mi, Fa, So, La, Ti, Do.
There are 88 keys on a piano, i.e. 88 frequencies. If the 88 frequencies are stored in individual variables in the main
sketch, it would occupy a lot of memory in Arduino in case not all the frequencies are used. We can alternatively
store the 88 frequencies in another file ‘pitches.h’, and refer to any of the frequencies in the main sketch.
In the pitches.h, #define is a useful C component that allows the programmer to give a name to a constant value
before the program is compiled. Defined constants in Arduino don't take up any program memory space on the chip.
The compiler will replace references to these constants with the defined value at compile time. Defines have the
syntax as #define constantName value, without the semicolon at the end. The first piano key A0 with the frequency
of 28 Hz, can be written as #define NOTE_A0 28. All the rest of the piano keys can be defined with specific
frequencies in the pitches.h file.
25
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
2. Playing a scale on speaker
Code interpretation:
Line 32 - To use the variables in the header file, command #include is used. #include is used to include outside
libraries in your sketch. This gives the programmer access to a large group of standard C libraries (groups of pre-
made functions), and also libraries written especially for Arduino.
For example, to use pitches.h file, at the beginning, key in #include "pitches.h".
#include tells the complier to access the pitches.h file when the main sketch involves unknown variables.
26
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
2. Playing a scale on speaker
Code interpretation:
Line 36:38 - An array is a collection of variables that are accessed with an index number. Arrays in the C
programming language, on which Arduino is based, can be complicated, but using simple arrays is relatively
straightforward. All the methods shown on the screen are valid ways to create an array.
int myInts[6];
int myPins[] = {2, 4, 8, 3, 6};
int mySensVals[5] = {2, 4, -8, 3, 2};
char message[6] = "hello";
You can declare an array without initializing it as in myInts. myInts has been declared as an integer array, 6 in the
square bracket sizes the array. In myPins we declare an array without explicitly choosing a size, so there isn’t any
number in the square bracket. The assignment stores 5 integer numbers in the array myPins, which are 2, 4, 8, 3 and
6. The compiler counts the elements and creates an array of the appropriate size. Finally you can both initialize and
size your array, as in mySensVals[5], where the 5 sizes the array and the {2, 4, -8, 3, 2} initialises the array. Finally you
can both initialize and size your array, as in mySensVals[5], where the 5 sizes the array and the {2, 4, -8, 3, 2} initialises the array.
27
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
2. Playing a scale on speaker
Code interpretation:
Note that when declaring an array of type char, one more element than your initialization is required, to hold the
required null character.
Arrays are zero indexed, that is, referring to the array initialization above, the first element of the array is at index 0,
hence mySensVals[0] is integer 2, mySensVals[1] is integer 4, and so forth. It also means that in an array with ten
elements, index nine is the last element. For example, int myArray[10]={9,3,2,4,3,2,7,8,9,11}; myArray[9] contains
11, the last element. For this reason you should be careful in accessing arrays. Index number should be no greater
than declared array size – 1.
Line 36:38 declare an integer array, scale, and initialize it with 8 variables which are identical to the 8 variables in the
header file pitches.h. After the assignment, the scale in fact stores the 8 integer numbers representing the 8 piano
keys. Why are the 8 specific variables selected? well, these variables provide the frequency values of a whole scale
on the piano.
28
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang
2.6 Lab: Write I/O-ports
2. Playing a scale on speaker
Code interpretation:
Line 48:55 - In the loop function, mainly there is a For loop. Since the variable thisNote increases from 0 to 7, this
section will be executed 8 times.
During each execution, the LED will be turned on, then, the tone command outputs square waves from the Pin A3 to
drive the speaker. The square wave frequency is determined by one of the variables in the array scale. thisNote is
the loop variable which also acts as the index for the array scale. When thisNote == 0, tone command will deliver
the first frequency value in the scale, i.e. NOTE_C5, or 523, to the Pin A3, the speaker will play a pitch of 523 Hz.
Next, thisNote becomes 1, which indexes NOTE_D5, or 587 in the array scale, so the speaker will play a pitch of 587
Hz, and so forth.
The tone command sets the duration to be 350 ms, then it delays for 350 ms and dims the LED, another delay for
200 ms is followed. These 2 delays cause 550-ms delay in total. But the square wave generated by the tone
command will last for 350 ms only, and stop for 200 ms. We can hear the speaker plays a pitch for 350 ms and stops
for 200 ms, and then it plays the next pitch for 350 ms. The digitalWrite command between the two delays is to light
the LED in phase with the speaker.
29
EN2081 Microcontroller Applications, Cardiff School of Engineering, Chris Yang