Lecture Microcontroller Overview
Lecture Microcontroller Overview
Fundamentals
B. Furman
15SEP2015
Learning Objectives
User
Interface
ME 106
ME 120
Controller
ME 106
Power
Interface
INTEGRATION
ME 106
ME 154
ME 157
ME 195
Signal
Conditioning
ME 106
ME 190
ME 187
ME 106
ME 120
Actuator
Sensor
System to
Control ME 110
ME 182
ME 136 ME 189
ME 154 ME 195
ME 157
ME 120
ME 297A
BJ Furman 22JAN11
What is a Microcontroller?
ANALOG
INPUTS
http://www.adafruit.com/index.php?main_page=popup_image&pID=50
ATmega328 Features
http://www.atmel.com/Images/Atmel-8271-8-bit-AVR-Microcontrol
ler-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet.pdf
Arduino Duemilanove
http://www.arduino.cc/en/Main/ArduinoBoardDuemilanove
See the handout: Arduino_ATmega328_pin_mapping_and_schematic
Pin 13
LED
USB
connect
or
Digital pins
header
Reset
button
ATmega328
MCU
Barrel
jack
Analog pins
header
Power-ground
header
http://arduino.cc/en/uploads/Main/ArduinoDuemilanove.jpg
Arduino Uno R3
ATmega16u2 replaces FT232RL for USB-serial communication
http://www.adafruit.com/index.php?main_page=popup_image&pID=50
See: http://learn.adafruit.com/arduino-tips-tricks-and-techniques/arduino-uno-faq
Arduino Due
Note: 3.3 V !!
http://www.adafruit.com/index.php?main_page=popup_image&pID=1076
See: http://arduino.cc/en/Main/ArduinoBoardDue
ATmega168/328
Operating Voltage
5V
7-12V
6-20V
40 mA
50 mA
Flash Memory
SRAM
1 KB (ATmega168) or 2 KB (ATmega328)
EEPROM
Clock Speed
16 MHz
http://www.arduino.cc/en/Main/ArduinoBoardDuemilanove
http://arduino.cc/en/uploads/Main/arduino-duemilanove-schematic.pdf
ATmega328 Microcontroller
Pin
name
Pin
number
Special
functio
n
Note the
limitations!
p. 316
Source:http://www.atmel.com/dyn/products/product_card.asp?PN=ATmega328P
Absolute Maximums
Ex. PORTB
Pins PB0 PB7
May not be contiguous
Often bi-directional
See next slides!
Input
Output
ATmega328
Block Diagram
Inpu
t
Outp
ut
M68HC11 microcontroller
Arduino
pinMode(pin_no., dir)
Pin Voltages
TTL
5 V (for HIGH)
0 V (for LOW)
3.3 V CMOS
3.3 V (for HIGH)
0 V (for LOW)
pinMode(____, ____);
digitalWrite(PIN_LED,HIGH);
digitalWrite(PIN_LED,LOW);
ATmega328
Ardui
no
pin 0
(PD0)
ATmega328
Ardui
no
pin 3
(PD3)
Indeterminate!
SPST
momentary
digitalWrite(PIN_SWITCH,HIGH);
pinMode(PIN_SWITCH,INPUT_PULLUP);
ATmega328
VTG= +5V
PD3
0
digitalWrite(PIN_SWITCH,LOW);
turns the pull-up resistor off
ATmega328
VTG= +5V
PD3
0
Remember this!
ATmega328
VTG= +5V
iweak
PD3
0
RC servo
header
RGB
LED
Red-RGB
jumper
Tact
switches
Red
LEDs
Pwr-gnd
Piezo
speake
r
header
Reset
button
Temperature
sensor
Photoresist
or
Analog pins
header
Potentiomet
er
11 - LED0 (red)
9 - LED1 (red) or RGB (green)
6 - LED2 (red) or RGB (blue)
3 - LED3 (red) or RGB (red)
13 - LED on Arduino
Assignments
13
12
11
10
SCK
MISO
MOSI
SS
OC1
ICP
AIN1
AIN0
T1
T0
INT1
INT0
TXD
RXD
LED
LED
LED
pwm
pwm
LED0
pwm
pwm
pwm
pwm
LED1
LED2
LED3
green
blue
red
piezo
servo
SW0
SW1
SW2
SW3
Assignments
7
photocell
POT
temp sensor
Often 5V or 0V
Bit No.
Upper nibble
(4 bits)
MSB
(Most Significant Bit)
Lower nibble
(4 bits)
LSB
(Least Significant Bit)
1 1 3 8
30
1138
(Base 10)
1 23 1 2 2 0 21 1 2 0
8
1 13
(Base 10)
0 to 15
Binary
0
Practice
0b10011001 in hex
0b10011001 as a base 10 number
0x5A in binary (use 8 bits)
0b11111111 in hex and as a base 10 number
(37)10 in binary and hex
the prefix '0x' is C notation that means that the digits which follow are hex digits
the prefix '0b' means that the digits which follow are binary digits
Back to PORT
Solution
1100 0111 in hex = 0xC7
1001 1001 in hex = 0x99
1001 1001 in base 10 = 153
0x5A in binary = 0b0101 1010
0b1111 1111 = 0xFF or 255
(37) = 0b0010 0101 or 0x25
So What?
Source:http://www.atmel.com/dyn/products/product_card.asp?PN=ATmega328P p. 93
DDRx
PORTx
PINx
Jump to
Example 1
pinMode(PIN_D3, OUTPUT);
pinMode(PIN_D5, OUTPUT);
pinMode(PIN_D7, OUTPUT);
DDRD = 0b10101000;
or
DDRD = 0xA8;
or
DDRD | = 1<<PD7 | 1<<PD5 | 1<<PD3;
More on this coming soon!
Example 2
Arduino approach
pinMode(0, INPUT);
pinMode(1, INPUT);
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
Or if me106.h is used:
pinMode(PIN_D0, INPUT);
pinMode(PIN_D1, INPUT);
digitalWrite(PIN_D0, HIGH);
digitalWrite(PIN_D1, HIGH);
Alternate approach
setup()
setup()
loop()
configures pin modes and
registers
loop()
like while(1) {}
Where is main() ?
Digital IO Practice 1
Reading a pin
ATmega328
PD3
Reading a pin
Pseudocode:
Set up PD3 as an input
Turn on PD3 pull-up resistor
Read voltage on Arduino pin 3 (PIN_D3)
IF PIN_D3 voltage is LOW (latched), THEN
call function ig_enable()
ELSE
call function ig_disable()
ATmega328
VTG= +5V
PD3
0
ATmega328
Reading a pin
VTG= +5V
Pseudocode:
Set up PD3 as an input
Turn on PD3 pull-up resistor
Read voltage on Arduino pin 3 (PIN_D3)
IF PIN_D3 voltage is LOW (latched), THEN
call function ig_enable()
ELSE
call function ig_disable()
One way
(snippet, not full program)
PD3
0
#define PIN_SWITCH 3
#define LATCHED LOW
pinMode(PIN_SWITCH,INPUT_PULLUP);
belt_state = digitalRead(PIN_SWITCH);
if (belt_state == LATCHED)
{ ig_enable(); }
else
{ ig_disabled(); }
Digital IO Practice 2
Pseudocode first
ATmega328
PD3
PD2
PD0, PD1
Pseudocode:
Set up data direction of pins
Make PD0 and PD1 inputs
Turn on pull up resistors for PD0 and PD1
Make PD2 and PD3 outputs
Loop forever
ATmega328
PD3
PD2
VTG= +5V
ELSE
Turn on lamp
Turn on buzzer
PD0, PD1
ELSE
ATmega328
PD3
PD2
VTG= +5V
PD0, PD1
0
ATmega328
PD3
PD2
VTG= +5V
PD0, PD1
0
PD1
ATmega328
PD3
PD2
VTG= +5V
PD0, PD1
0
Summary
Data direction
Input is default, but okay to set explictly
Output
Pull-up resistors
Summary, cont.
Digital Input