- CO3009 -
- Microcontroller Basics -
CO3009 – Microcontroller Basics 2
Microprocessors vs. Microcontrollers
§ Microprocessors (μPs)
▫ General-purpose compute “engine”
▫ External memory and I/O devices
▫ Often requires an operating system (OS)
§ Microcontrollers (MCUs)
▫ Usually chosen for a specific purpose
▫ Small packages
▫ On-chip memory and peripherals
▫ Fast “on time,” no BIOS or OS needed
§ https://www.youtube.com/watch?v=dcNk0urQsQM
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 3
Where Did the MCU Come From?
§ Intel introduced the 8051 MCU in 1980
▫ Small amount of read-only memory (ROM)
▫ External memory expansion if needed
▫ Four 8-bit I/O ports
▫ Not much different from today’s MCUs
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 4
8051 Architecture (1980)
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 5
Texas Instruments MSP430 MCU
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 6
ESP8266
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 7
PIC18F8722
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 8
STM32
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 9
Why Use an MCU?
§ Everything in one small package
§ Mix and match peripherals and I/O types
§ Lots of memory, flash for code, SRAM for data
§ Readily available hardware and software tools
§ Helpful support communities and forums
§ Reference designs
§ Code libraries and examples
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 10
What Peripherals Do MCUs Offer?
§ Digital I/O -- On or Off
▫ Parallel signals
▫ Pulse-width-modulated logic signals
▫ Counters and timers
§ Analog I/O -- Voltages
▫ Comparators
▫ Analog-to-digital converters (ADCs)
▫ Digital-to-analog converters (DACs)
§ Communication Devices
▫ UART, USART, SPI, I2C
▫ CAN, USB, Ethernet
§ Interrupts
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 11
Peripherals Devices in MCUs
§ Parallel I/O Ports
▫ Usually 8 or 16 bits for simultaneous control
▫ Toggle individual bits
▫ Require setup of registers
§ Parallel I/O-Port Examples
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 12
How Do I Set Up I/O Ports?
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 13
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 14
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 15
Pulse Width Modulation (PWM)
§ Pulse Width Modulation (PWM) is a technique that conforms a signal
width, generally pulses based on modulator signal information.
§ The general purpose of PWM is to control power delivery, especially to
inertial electrical devices.
§ The on-off behavior changes the average power of signal.
§ Output signal alternates between on and off within a specified period.
§ If signal toggles between on and off quicker than the load, then the load
is not affected by the toggling.
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 16
Pulse Width Modulation (PWM)
§ Duty Cycle
▫ D = on-time/period
§ VLOW is often zero.
𝐕𝐀𝐕𝐆 = 𝐃𝐕𝐇𝐈 + (𝟏 − 𝐃)𝐕𝐋𝐎𝐖
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 17
PWM with 555 Timer
§ Potentiometer is used to adjust the duty cycle
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 18
Counters and Timers
§ Operate for a specific period or create a delay
§ Count external events, count up or down
§ Count clock “ticks” between the same or different events
§ Choose from various clock sources
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 19
Analog Comparator
§ Compare two voltages and then ...
▫ Cause a bit to change state
▫ Generate an interrupt
▫ Wake an MCU from a sleep state
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 20
Analog-To-Digital Conversion (ADC)
§ Embedded system applications are often required to interface with
analog signals.
§ They must be able to convert input analog signals, for example from
microphone or temperature sensor, to digital data.
§ They must also be able to convert digital signals to analog form, for
example if driving a loudspeaker or dc motor
§ We will first consider conversion from analog-to-digital, before later
looking at digital-to-analog conversion
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 21
Concepts of Analog-To-Digital Conversion
§ An analog-to-digital convertor (ADC) is an electronic circuit whose
digital output is proportional to its analog input.
§ Effectively it "measures" the input voltage, and gives a binary output
number proportional to its size.
§ The input range of the ADC is usually
determined by the value of a voltage reference.
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 22
Analog-To-Digital Conversion
§ Example
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 23
Data Acquisition System
§ Example
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 24
Digital-to-Analog Converter (DAC)
§ Unipolar output, might require external offset
§ High-impedance output, could require buffering
§ 10- and 12-bit DACs common on MCUs
§ Filter a PWM output to get an analog voltage
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 25
UART Communications
§ Universal Asynchronous Receiver-Transmitter (UART)
▫ Serial communications
▫ Self-timing operations
▫ Usually 8-bit transmissions at standard rates
▫ Common on most MCUs
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 26
UART Communications Timing
Communications at 9600 bits/sec
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 27
What's Wrong with Serial Ports?
§ A common serial port, the kind with TX and RX lines, is called
“asynchronous” (not synchronous) because there is no control over
when data is sent or any guarantee that both sides are running at
precisely the same rate.
▫ Since computers normally rely on everything being synchronized to a single
“clock”, this can be a problem when two systems with slightly different clocks try
to communicate with each other.
UART
anhpham@hcmut.edu.vn
® Synchronous Serial Communication
CO3009 – Microcontroller Basics 28
SPI vs. I2C
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 29
Serial Peripheral Interface (SPI)
§ Developed by Motorola
▫ Also known as MicroWire (National
Semiconductor), QSPI (Queued),
MicrowirePlus.
▫ Synchronous Serial Communication.
§ Primarily used for serial
communication between a host
processor and peripherals.
§ Can also connect 2 processors via SPI
§ SPI works in a master-slave
configuration with the master being the
host microcontroller for example and
the slave being the peripheral
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 30
SPI Operation
§ For SPI, there are Serial Clocks (SCLK), Chip Select lines (CS), Serial
Data In (SDI) and Serial Data Out( SDO).
§ There is only one master, there number of slaves depends on the
number of chip select lines of the master.
§ Synchronous operation, latch on rising or falling edge of clock, SDI on
rising edge, SDO on falling edge.
§ Master sends out clocks and chip selects. Activates the slaves it wants
to communicate with.
§ Operates in 1 to 2 MHz range.
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 31
SPI – Master Slave Setup
§ In this setup, there are 3 slave devices. The SDO lines are tied together
to the SDI line of the master.
§ The master determines which chip it is talking to by the CS lines. For
the slaves that are not being talked to, the data output goes to a Hi Z
state
Multiple Independent Slave
Configuration
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 32
SPI – Master Slave Setup
§ In this example, each slave is cascaded so that the output of one slave
is the input of another. When cascading, they are treated as one slave
and connecting to the same chip select.
Multiple slave cascaded
configuration
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 33
SPI Peripherals Types
§ Types
▫ Converters (ADC, DAC)
▫ Memories (EEPROM, RAM’s,Flash)
▫ Sensors (Temperature, Humidity, Pressure)
▫ Real Time Clocks
▫ Misc-Potentiometers, LCD controllers, UART’s, USB controller, CAN controller,
amplifiers.
§ Vendors that make these peripherals
▫ Atmel –EEPROM, Dig. POT’s
▫ Infineon- Pressure Sensors, Humidity Sensors
▫ Maxim- ADC, DAC, UART,
▫ TI- DSP’s, ADC, DAC
▫ National Semiconductor-Temperature Sensors, LCD/USB controllers
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 34
What’s Wrong with SPI?
§ The most obvious drawback of SPI is the number of pins required. Connecting a
single master to a single slave with an SPI bus requires four lines; each additional
slave requires one additional chip select I/O pin on the master.
§ SPI only allows one master on the bus, but it does support an arbitrary number of
slaves (subject only to the drive capability of the devices connected to the bus and
the number of chip select pins available).
SPI
anhpham@hcmut.edu.vn I2C
CO3009 – Microcontroller Basics 35
Inter-Integrated Circuit (I2C)
§ I2C requires a mere two wires, like asynchronous serial, but those two wires can
support up to 1008 slave devices. Also, unlike SPI, I2C can support a multi-master
system, allowing more than one master to communicate with all devices on the bus.
§ Data rates fall between asynchronous serial and SPI.
▫ Most I2C devices can communicate at 100kHz or 400kHz.
§ There is some overhead with I2C.
▫ For every 8 bits of data to be sent, one extra bit of meta data (the “ACK/NACK” bit, which we’ll
discuss later) must be transmitted.
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 36
Serial Communications
§ Further Reading: “Serial Port Complete,” 2nd ed., by Jan Axelson,
Lakeview Research, 2007. ISBN: 978-1-931448-06-2.
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 37
Controller-Area Network (CAN)
§ A standard for vehicle equipment
§ Uses an ISO-type “stack”
§ 2-wire differential bus, no common ground needed
§ Uses standardized packets of information
“Controller Area Network,” by Konrad Etschberger,” IXXAT Automation, 2001. ISB: 978-3-00-007376-0.
anhpham@hcmut.edu.vn “A Comprehensive Guide to Controller Area Network,” by Wilfried Voss, Copper Hill Media, 2008. ISBN: 978-0976511601.
CO3009 – Microcontroller Basics 38
Ethernet and USB
§ Governed by standards
§ Require a software “stack”
▫ Purchase, license, or create one yourself
▫ MCU vendors might have stacks
§ Some MCUs include everything except the physical interface (PHY)
§ Can demand considerable memory
§ Start with a development kit or reference design
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 39
Interrupts
§ Cause immediate action
§ Internal and external hardware and software sources
§ Two types of action -- one or many vectors
§ Can present debug challenges
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 40
An ADC Interrupt
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 41
An ADC Interrupt
16/08/2021
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 42
How Do I Get a Quick Start?
anhpham@hcmut.edu.vn
CO3009 – Microcontroller Basics 43
How Do I Get Started?
anhpham@hcmut.edu.vn