Uploaded by mikewithatrike

Ch 2 Microcontroller Hardware

advertisement
Chapter 2.
The Hardware of a Microcontroller
Ch2. The Hardware of a Microcontroller – I. Structure Overview
• Arduino Uno: a microcontroller development board
Reset button
Ease of use
Open-source nature
14 Digital I/O pins
USB connector
Microcontroller
(Atmel ATmega328)
External power jack
(7 – 12 V)
Power pins 6 Analog
Inputs (can also be used as DIOs)
- Analog output?
- Other types of Arduino board? (https://www.arduino.cc/)
Ch2. The Hardware of a Microcontroller – I. Structure Overview
• Microcontroller: a little computer on a chip
- ATmega328
- 8-bit AVR RISC-based microcontroller
- In a dual in line package (DIP or DIL)
- 28 pins
- Pinout
Ch2. The Hardware of a Microcontroller – I. Structure Overview
• ATmega328 (cont’d)
- ATmega328 Architecture (very simplified)
UART (serial data interface)
2 KB SRAM
(working
memory)
32 KB Flash
Memory
(program
instructions)
1 KB EEPROM
(non-volatile)
CPU
16 MHz
Input/Output ports
- types of memory
- datasheet:
https://ww1.microchip.com/downloads/en/Device
Doc/Atmel-7810-Automotive-MicrocontrollersATmega328P_Datasheet.pdf
Ch2. The Hardware of a Microcontroller – I. Structure Overview
• Types of Memory
Non-volatile:
- permanent storage of data
- does not require power to retain its data
• ROM (Read Only Memory):
• EPROM (Erasable-Programmable ROM):
• EEPROM (Electrically-Erasable PROM):
• Flash Memory:
Volatile :
- the data is lost when power is removed
• SRAM (static RAM):
• DRAM (dynamic RAM):
Ch2. The Hardware of a Microcontroller – I. Structure Overview
• Types of Memory
Non-volatile:
- permanent storage of data
- does not require power to retain its data
• ROM (Read Only Memory) – the CPU cannot write data to ROM
• EPROM (Erasable-Programmable ROM)
– can be erased w/ ultraviolet light applied through a transparent quartz window
• EEPROM (Electrically-Erasable PROM) – can be erased electrically and rewritten
• Flash Memory – a special type of EEPROM
Volatile :
- the data is lost when power is removed
• SRAM (static RAM) – retains its data as long as the memory is powered
• DRAM (dynamic RAM) – data must be refreshed (rewritten) periodically
• Digital or Analog?
- Digital signals: two states of 0 (off) and 1 (on)
in electrical terms, 0 or 5 volts.
- Digital input voltage, Vin:
Input low voltage range: VIL min < Vin < VIL max  0
Input high voltage range: VIH min < Vin < VIH max  1
check the datasheet! VIL and VIH ? (Ch. 28 Electrical Characteristics)
- Analog Input: the range is 0 to 5 V  converted to a number
between 0 to 1023 (Arduino)
through ADC (Analog to Digital Converter).
resolution? 0 V, 5/1024 ≅ 0.005 V, 0.01 V, 0.015 V, …, 5V
Ch2. The Hardware of a Microcontroller – I. Structure Overview
• Components of a typical full-featured microcontroller
Fig 7.2 (from the reference book, “Introduction to Mechatronics and Measurement Systems,” 5th ed.)
Ch2. The Hardware of a Microcontroller – I. Structure Overview (Other microcontrollers)
• PIC32 microcontrollers
- Manufactured by Microchip
(http://www.microchip.com/pagehandler/e
n-us/family/32bit/ )
- More powerful
- Development boards are also available
(picture on the left)
- Arduino is much easier to use
ATmega328
PIC32MX795F512L
CPU Speed
16 MHz
80 MHz
Flash Memory
32 KB
512 KB
2 KB
128 KB
RAM
Ch2. The Hardware of a Microcontroller – I. Structure Overview (Other microcontrollers)
• ESP32 microcontrollers
https://www.espressif.com/en/products/socs/esp32
- Development Boards: https://www.espressif.com/en/products/devkits
- Designed and made by Espressif.
- It has a quite different structure (more powerful) from that of Arduino.
- Arduino IDE (as well as almost all libraries) can be used.
- Wi-Fi, Bluetooth
- In general, cheaper than Arduino, but may not be for beginners (Arduino is
easier and simpler to use).
Arduino vs Raspberry Pi
- microcontroller board
- a simple computer that can run one program at
a time over and over again
- single board computer (SBC)
- general-purpose computer, usually w/ a Linux
OS and the ability to run multiple program
- very simple and easy to use
- quick setup. It can just be plugged in and start
executing its code right away. not much cables.
- more complicated to use than Arduino
- long set up and will need extra components.
- 8-bit microcontroller
- 2KB RAM, 16 MHz
- 20 I/O pins
- each pin can drive a max 40 mA
- consumes much less power
(Raspberry Pi 3 Model B)
- 64-bit microprocessor
- 1GB RAM, 1.2 GHz
- 40 GPIO pins
- 16 mA
- analog input pins
- variety of Shields.
- USB ports, audio output, graphic driver for
HDMI output, Bluetooth, camera port
Arduino vs Raspberry Pi
-Best used for simple repetitive tasks:
reading a temperature, driving a simple robot,
self-watering plant, etc.
-LEDs, motors, several different types of sensors
- a project that involves several functionalities at
the same time:
driving a more complicated robot, performing
multiple tasks, doing intense calculations,
connecting online, etc.
- retro gaming platform, a web server, mediacentered applications
When to use them together?
- To use the large number of libraries and sharable examples for the Arduino.
- To supplement an Arduino project with more processing power.
- When you're dealing with 5V logic levels with the Pi. (The Pi operates at 3.3 V)
- When you need exact control in real time, such as a controller for a 3D printer, with the Pi.
(Since Raspberry Pi OS (Raspbian) is not a real-time operating system, a microcontroller would be
more precise in timing, which is based on "instruction per clock cycles.“)
Ch2. The Hardware of a Microcontroller – II. Pin Functions
ATmega 328 Microcontroller
• Pin Functions
The ATmeg328 features
max clock frequency of 16 MHz
32 KB program memory (flash)
2 KB data memory (RAM)
many digital I/O channels
6 channel 10-bit analog (ADC) input lines
UART/SPI for serial communications
6 PWM outputs for analog(ish) output
3 counter/timers (one 16-bit, two 8-bit timers)
and more…
 check with the Datasheet
To cram these so much functionality into 28 pins, many of the pins
serve multiple functions!
Ch2. The Hardware of a Microcontroller – II. Pin Functions
• The ATmega 328 Pin Functions
• See the Datasheet
for more details:
“13.3 Alternate Port
Functions”
Pin Label
Function
PCINTx
Pin Change INTerrupt Source x. This pin serve as an external interrupt source
OCxy (x=0,1,2)
(y=A,B)
Output Compare match output. Usually used to generate pulse trains (PWM:
pulse-width modulation)
RESET
Setting this pin low (connecting it to 0 V) momentarily reset the microcontroller.
ADCx
Anlog-to-Digital (ADC) inputs
TXD
Transmit Data. Data output pin for the UART.
RXD
Receive Data. Data input pin for the UART.
Vcc
Digital Supply Voltage
GND
Ground
Ch2. The Hardware of a Microcontroller – II. Pin Functions
- For example, pin 1 (PC6) can serve as an interrupt source, a reset signal input, or
a digital input or output.
- Which function a particular pin actually serves is determined by Registers. Each
register is 8-bit and sits at some memory address.
- The value of the register bits, 0 or 1, control the functions of the pins.
Ch2. The Hardware of a Microcontroller – II. Pin Functions
Port Registers:
• ATmega328 has three ports: B, C, and D
- Port B maps to pins PB0 to PB7 (8 channels)
- Port C maps to pins PC0 to PC6 (7 channels)
- Port D maps to pins PD0 to PD7 (8 channels)
•
Each port is controlled by three registers: DDRX, PORTX, and PINX (X = B, C, or D)
- The DDR register determines whether the pin is an INPUT (0) or OUTPUT (1).
- The PORT register controls whether the pin is HIGH or LOW.
- The PIN register reads the state of INPUT.
• Each bit of these registers corresponds to a single pin. For example,
- The lowest bit of DDRB, PORTB, and PINB refers to pin PB0 (pin 14)
- DDRB = B11111110  Set pins PB1 to PB7 as outputs; pin PB0 as input
(See the datasheet “13.4 Register Description” for more details.)
Ch2. The Hardware of a Microcontroller – II. Pin Functions
• Port manipulation through these registers?
- Typically done with the PinMode() function in your code.
(http://arduino.cc/en/Reference/PinMode) also see in an example code.
- When to use port manipulation?
- to turn pins on and off very quickly (within fractions of a microsecond)
- to set multiple pins at the same time.
- running low on program memory (a lot fewer bytes of complied code)
 However, not very recommended! (Error-prone, more complicated)
Ch2. The Hardware of a Microcontroller – III. Other Important Components
Some Important Components of ATmega328 architecture
• CPU: The central processing unit. The
CPU can be clocked either by the
internal RC oscillator (8 MHz) or an
external oscillator/resonator at up to
16 MHz (meaning it can execute one
instruction every 62.5 nanoseconds).
16 MHz ceramic resonator
(for CPU clock)
16 MHz crystal oscillator
(for USB-serial communication)
Ch2. The Hardware of a Microcontroller – III. Other Important Components
Some Important Components of ATmega328 architecture (cont’d)
•
Counter/timers:
The ATmega328 has one 16-bit and two
8-bit counters. The 16-bit counter can
count from 0 up to 216 − 1 and the 8-bit
up to 28 − 1. Counters can be configured
to count external or internal events. In
the latter case, we refer to the counter
as a timer. The counter can be
configured to generate an interrupt
when it rolls over.
•
Output Compare:
Output compare pins are used to
generate a single pulse of specified
duration, or a continuous pulse train of
specified duty cycle and frequency. They
work with timers to generate the precise
timing. A common use of output
compare pins is to generate PWM
signals.
Ch2. The Hardware of a Microcontroller – III. Other Important Components
Some Important Components of ATmega328 architecture (cont’d)
Ch2. The Hardware of a Microcontroller – III. Other Important Components
Components of ATmega328 architecture (cont’d)
• SPI Serial Communication
The Serial Peripheral Interface bus provides a simple method for serial
communication between a master device (typically a microcontroller) and one or
more slave devices. Communication rates can be up to tens of megabits per
second. (see the book Exploring Arduino Ch. 11)
• TWI 2-wire Serial Interface:
This is compatible with I2C (Inter-Integrated Circuit) protocol. It is a somewhat
more complicated serial communication standard that allows several devices to
communicate over only two shared lines. Any of the devices can be the master at
any given time. The maximum data rate is less than for SPI.
(see the book Exploring Arduino Ch. 10)
• USART Serial Communication
The Universal Synchronous and Asynchronous serial Receiver and Transmitter
module provides a highly flexible method for serial communication between two
devices. Typical data rates are 9600 bits per second (9600 baud) up to hundreds
of thousands of bits per second.
(see the book Exploring Arduino Ch. 7)
Ch2. The Hardware of a Microcontroller – III. Other Important Components
• Voltage regulator of the Arduino Uno
- The Arduino has built-in 5 V and 3.3 V regulators.
- Regulate whatever voltage (between 7 V and 12 V) is supplied to the board to a
constant 5 V and 3.3 V
- 5 V is used for all the logic on the board (toggling a digital I/O pin between 5 V and 0 V)
& external circuitry.
- 3.3 V is useful for 3.3 V shields and external circuitry that uses 3.3 V.
-
The 3.3 V regulator can supply maximum current of 50 mA.
3.3 V voltage regulator
5 V voltage regulator
Ch2. The Hardware of a Microcontroller – III. Other Important Components
• LEDs
- The Uno board has 4 LEDs, labeled L, RX, TX, and ON.
- ON is green when the board is powered.
- RX and TX indicate that data is being received or transmitted by the board.
- L is a very special LED that is connected to digital pin 13. Great for debugging.
Ch2. The Hardware of a Microcontroller – III. Other Important Components
• Reading Assignments:
Monk (Programming Arduino) Ch1.
Blum (Exploring Arduino) Ch1.
Ch2. The Hardware of a Microcontroller – III. Other Important Components
Advanced Topics:
Differences between ceramic resonator (for CPU) and
crystal oscillator (for USB) in Arduino.
crystal
Accuracy
Temp. stability
Size
Sensitivity
Cost
more accurate
more stable
⋅
more sensitive
⋅
resonator
accurate enough
⋅
(often) smaller
⋅
cheaper
- Both are based on mechanical resonant (vibration) devices.
(RC oscillators are based on electrical phase-shift circuits and the least accurate.)
Further readings:
https://www.maximintegrated.com/en/app-notes/index.mvp/id/2154
http://arduinoalternatorregulator.blogspot.com/2014/07/a-choice-crystal-vs-ceramic-resonator.html
Download