Atmega328 Features: Atmega48A-48Pa-88A-88Pa-168A-168Pa-328-328P - Datasheet PDF
Atmega328 Features: Atmega48A-48Pa-88A-88Pa-168A-168Pa-328-328P - Datasheet PDF
Atmega328 Features: Atmega48A-48Pa-88A-88Pa-168A-168Pa-328-328P - Datasheet PDF
http://www.atmel.com/Images/Atmel-8271-8-bit-AVR-MicrocontrollerATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet.pdf
Ex. PORTB
Pins PB0 PB7
May not be contiguous
See next slides!
Often bi-directional
Input
Output
ATmega328
Block Diagram
Input
Output
Arduino
pinMode(pin_no., dir)
pinMode(____, ____);
digitalWrite(0,HIGH);
digitalWrite(0,LOW);
ATmega328
Arduino
pin 0
(PD0)
Pin Voltages
TTL
5 V (for HIGH)
0 V (for LOW)
3.3 V CMOS
3.3 V (for HIGH)
0 V (for LOW)
Indeterminate!
ATmega328
Arduino
pin 3
(PD3)
digitalWrite(3,HIGH); turns
on the pull-up resistor
VTG
ATmega328
VTG= +5V
PD3
0
digitalWrite(3,LOW); turns
the pull-up resistor off
ATmega328
VTG= +5V
PD3
0
Remember this!
ATmega328
VTG= +5V
iweak
1
PD3
0
RGB LED
Red-RGB jumper
Tact switches
Red LEDs
Piezo
speaker
Pwr-gnd header
Reset button
Temperature sensor
Photoresistor
Analog pins header
Potentiometer
Often 5V or 0V
Bit No.
Upper nibble
(4 bits)
Lower nibble
(4 bits)
MSB
LSB
1 1 3 8
(Base 10)
1 23 1 2 2 0 21 1 20
8
4
0
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 details
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
Jump to bits
DDRx
PORTxn
PINxn
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 next lecture!
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:
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
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
IF key is in ignition THEN
ATmega328
PD3
PD2
VTG= +5V
PD0, PD1
Turn on lamp
Turn on buzzer
ELSE
Summary
Data direction
Input is default, but okay to set explictly
Output
Pull-up resistors
Summary, cont.