SlideShare a Scribd company logo
Lecture 6 – Introduction to the
ATmega328 and Ardunio
C S E P567
Outline
⦁ Lecture 6
⦁ ATmega architecture and instruction set
⦁ I/O pins
⦁ Arduino C++ language
⦁ Lecture 7
⦁ ControllingTime
⦁ Interrupts andTimers
⦁ Lecture 8
⦁ Guest lecture – Radio communication
⦁ Lecture 9
⦁ Designing PID Controllers
AVR Architecture
AVR Architecture
⦁ Clocks and Power
⦁ Beyond scope of this
course
AVR Architecture
⦁ CPU
⦁ Details coming
AVR Architecture
⦁ Harvard architecture
⦁ Flash – program memory
⦁ 32K
⦁ SRAM – data memory
⦁ 2K
⦁ EEPROM
⦁ For long-term data
⦁ O n I/O data bus
Memory
⦁ Flash (32K) (15-bit addresses)
⦁ Program memory – read only
⦁ Non-volatile
⦁ Allocate data to Flash using PROGMEM keyword
⦁ see documentation
⦁ SRAM (2K)
⦁ T
emporary values,stack,etc.
⦁ Volatile
⦁ Limited space!
⦁ EEPROM (1K)
⦁ Long-term data
⦁ see documentation on EEPROM library
AVR CPU
⦁ Instruction Fetch
and Decode
AVR CPU
⦁ ALU Instructions
AVR CPU
⦁ I/O and special
functions
AVR Register File
⦁ 32 8-bit GP registers
⦁ Part of SRAM memory space
Special Addressing Registers
⦁ X,Y and Z registers
⦁ 16-bit registers made using registers 26 – 31
⦁ Support indirect addressing
AVR Memory
⦁ Program memory – Flash
⦁ Data memory - SRAM
Addressing Modes
⦁ Direct register
addressing
Addressing Modes
⦁ Direct I/O addressing
Addressing Modes
⦁ Direct data memory addressing
Addressing Modes
⦁ Direct data memory with displacement addressing
Addressing Modes
⦁ Indirect data memory addressing
Addressing Modes
⦁ Indirect data memory addressing with pre-decrement
Addressing Modes
⦁ Indirect data memory addressing with post-increment
Addressing Modes
⦁ Program memory addressing (constant data)
SRAM Read/Write Timing
Stack Pointer Register
⦁ Special register in I/O space [3E,3D]
⦁ Enough bits to address data space
⦁ Initialized to RAMEND (address of highest memory address)
⦁ Instructions that use the stack pointer
Program Status Register (PSR)
⦁ 
⦁ Status bits set by instructions/Checked by Branch/Skip
instructions
⦁ I – Global interrupt enable
⦁ T – Flag bit
⦁ H – Half carry (BCD arithmetic)
⦁ S – Sign
⦁ V – Overflow
⦁ N – Negative
⦁ Z – Zero
⦁ C – Carry
Simple 2-Stage Pipeline
⦁ Branch/Skip??
Single-Cycle ALU Instructions
⦁ Most instructions execute in one cycle
⦁ Makes program timing calculations (relatively) easy
⦁ No cache misses
⦁ 1 clock/instruction
Addressing Modes
⦁ JMP
,CALL – Direct Program MemoryAddressing
Addressing Modes
⦁ IJMP
,ICALL – Indirect program memory addressing
Addressing Modes
⦁ RJMP
,RCALL – Relative program memory addressing
Arithmetic Instructions
Logical Instructions
J um p and Call Instructions
Skip and Branch Instructions
Skip and Branch (cont)
Move, Load
Store
Load/Store Program Memory
Move, I/O, Push/Pop
Shift and Bit Instructions
Bit Instructions (cont)
AVR Architecture
⦁ Three timers
⦁ Very flexible
⦁ Choose clock rate
⦁ Choose“roll-over” value
⦁ Generate interrupts
⦁ Generate PWM signals
⦁ (represent 8-bit value with
using a clock signal)
⦁ More in next lecture…
Arduino Timing Functions
⦁ delay(ms)
⦁ wait for ms milliseconds before continuing
⦁ delayMicroseconds(us)
⦁ wait for us microseconds before continuing
⦁ unsigned long millis( )
⦁ return number of milliseconds since program started
⦁ unsigned long micros( )
⦁ return number of microseconds since program started
⦁ resolution of 4 microseconds
AVR Architecture
⦁ Interface to pins
⦁ Each pin directly
programmable
⦁ Program direction
⦁ Program value
⦁ Program pull-ups
⦁ Some pins are special
⦁ Analog vs.Digital
⦁ Clocks
⦁ Reset
I/O Ports
⦁ 3 8-bit Ports (B,C,D)
⦁ Each port controlled by 3 8-bit registers
⦁ Each bit controls one I/O pin
⦁ DDRx – Direction register
⦁ Defines whether a pin is an input (0) or and output (1)
⦁ PINx – Pin input value
⦁ Reading this “register” returns value of pin
⦁ PO RTx – Pin output value
⦁ Writing this register sets value of pin
Pin Circuitry
Pin Input
DDRx = 0
off
PO RTx
PINx
Synchronization Timing
⦁ Note:Takes a clock cycle for data output to be reflected
on the input
Pin Output
DDRx = 1
on
PO RTx
PINx
Pin Input – PORT controls pullup
DDRx = 0
off
PO RTx
PINx
I/O Ports
⦁ Pullups
⦁ If a pin is an input (DDRxi = 0):
⦁ PO RTxi = 0 – pin is floating
⦁ PO RTxi = 1 – connects a pullup to the pin
 Keeps pin from floating if noone driving
 Allows wired-OR bus
⦁ Individual bits can be set cleared using bit-ops
⦁ A bit can be toggled by writing 1 to PINxi
⦁ SBI instruction e.g.
I/O Protection
Arduino Digital and Analog I/O Pins
⦁ Digital pins:
⦁ Pins 0 – 7: PORT D [0:7]
⦁ Pins 8 – 13:PO RT B [0:5]
⦁ Pins 14 – 19:PORT C [0:5] (Arduino analog pins 0 – 5)
⦁ digital pins 0 and 1 are RX andTX for serial communication
⦁ digital pin 13 connected to the base board LED
⦁ Digital Pin I/O Functions
⦁ pinMode(pin,mode)
⦁ Sets pin to INPUT or O UTPUT mode
⦁ Writes 1 bit in the DDRx register
⦁ digitalWrite(pin,value)
⦁ Sets pin value to LOW or HIGH (0 or 1)
⦁ Writes 1 bit in the PO RTx register
⦁ int value = digitalRead(pin)
⦁ Reads back pin value (0 or 1)
⦁ Read 1 bit in the PINx register
Arduino Analog I/O
⦁ Analog input pins:0 – 5
⦁ Analog output pins:3,5,6,9,10,11 (digital pins)
⦁ Analog input functions
⦁ int val = analogRead(pin)
⦁ Converts 0 – 5v
.voltage to a 10-bit number (0 – 1023)
⦁ Don’t use pinMode
⦁ analogReference(type)
⦁ Used to change how voltage is converted (advanced)
⦁ Analog output
⦁ analogWrite(pin,value)
⦁ value is 0 – 255
⦁ Generates a PWM output on digital pin (3,5,6,9,10,11)
⦁ @490Hz frequency
AVR Architecture
⦁ Analog inputs
⦁ Convert voltage to a
10-bit digital value
⦁ Can provide reference
voltages
PWM – Pulse Width Modulation
⦁ Use one wire to represent a multi-bit value
⦁ A clock with a variable duty cycle
⦁ Duty cycle used to represent value
⦁ We can turn it into a analog voltage using an integrating filter
Port Special Functions
⦁ Lots of special uses for pins
⦁ Clock connections
⦁ Timer connections
⦁ e.g.comparator output for PWM
⦁ Interrupts
⦁ Analog references
⦁ Serial bus I/O s
⦁ USART
⦁ PCI
Reading and Writing Pins Directly
⦁ O nly one pin can be changed using theArduino I/O
functions
⦁ Setting multiple pins takes time and instructions
⦁ To change multiple pins simultaneously,directly read/write
the pin registers
⦁ DDR{A/B/C}
⦁ PORT{A/B/C}
⦁ PIN{A/B/C}
⦁ e.g.to set all digital pins 0 – 7 to a value:
⦁ PORTD = B01100101;
AVR Architecture
⦁ Special I/O support
⦁ Serial protocols
⦁ Uses special pins
⦁ Uses timers
⦁ Beyond scope of this
course
Arduino C Programs
⦁ Arduino calls these“sketches”
⦁ Basically C with libraries
⦁ Program structure
⦁ Header:declarations,includes,etc.
⦁ setup()
⦁ loop()
⦁ Setup is likeVerilog initial
⦁ executes once when program starts
⦁ loop() is likeVerilog always
⦁ continuously re-executed when the end is reached
Blink Program
int ledPin = 13; // LED connected to digital pin 13
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
// set the LED on
// wait for a second
// set the LED off
// wait for a second
The Arduino C++ Main Program
int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}
Arduino Serial I/O
⦁ Communication with PC via USB serial line
⦁ Use the Serial Monitor in the IDE
⦁ Or set up a C or Java(or you-name-it) interface
⦁ Example Serial library calls
⦁ Serial.begin(baud-rate)
⦁ 9600 default
⦁ Serial.println(string)
⦁ int foo = Serial.read()
⦁ Read one byte (input data is buffered)
⦁ See documentation for more
Example Program

More Related Content

Similar to Lecture6.pptx (20)

Arduino Programming Basic
Arduino Programming BasicArduino Programming Basic
Arduino Programming Basic
 
Chp4 introduction to the pic microcontroller copy
Chp4 introduction to the pic microcontroller   copyChp4 introduction to the pic microcontroller   copy
Chp4 introduction to the pic microcontroller copy
 
unit-2.pptx
unit-2.pptxunit-2.pptx
unit-2.pptx
 
Arduino Foundations
Arduino FoundationsArduino Foundations
Arduino Foundations
 
Pentium processor
Pentium processorPentium processor
Pentium processor
 
Microcontroller 8051 gs
Microcontroller 8051 gsMicrocontroller 8051 gs
Microcontroller 8051 gs
 
Chp 2 and 3.pptx
Chp 2 and 3.pptxChp 2 and 3.pptx
Chp 2 and 3.pptx
 
Arduino Programming
Arduino ProgrammingArduino Programming
Arduino Programming
 
Arduino intro.pptx
Arduino intro.pptxArduino intro.pptx
Arduino intro.pptx
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontroller
 
Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051
 
Embedded systems, 8051 microcontroller
Embedded systems, 8051 microcontrollerEmbedded systems, 8051 microcontroller
Embedded systems, 8051 microcontroller
 
Microcontroller 8051 By Mitesh kumar
Microcontroller 8051 By Mitesh kumarMicrocontroller 8051 By Mitesh kumar
Microcontroller 8051 By Mitesh kumar
 
Arduino by yogesh t s'
Arduino by yogesh t s'Arduino by yogesh t s'
Arduino by yogesh t s'
 
Rig nitc [autosaved] (copy)
Rig nitc [autosaved] (copy)Rig nitc [autosaved] (copy)
Rig nitc [autosaved] (copy)
 
8253,8254
8253,8254 8253,8254
8253,8254
 
Presentation On: "Micro-controller 8051 & Embedded System"
Presentation On: "Micro-controller 8051 & Embedded System"Presentation On: "Micro-controller 8051 & Embedded System"
Presentation On: "Micro-controller 8051 & Embedded System"
 
arduino.ppt
arduino.pptarduino.ppt
arduino.ppt
 
8051 Presentation
8051 Presentation8051 Presentation
8051 Presentation
 
Hardware interfacing basics using AVR
Hardware interfacing basics using AVRHardware interfacing basics using AVR
Hardware interfacing basics using AVR
 

More from KamalZeghdar

Chapitre 3 Modbus.pptx
Chapitre 3 Modbus.pptxChapitre 3 Modbus.pptx
Chapitre 3 Modbus.pptxKamalZeghdar
 
cour PIC16F877.pptx
cour PIC16F877.pptxcour PIC16F877.pptx
cour PIC16F877.pptxKamalZeghdar
 
cours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdf
cours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdfcours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdf
cours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdfKamalZeghdar
 
cours-gratuit.com--system1id048.pdf
cours-gratuit.com--system1id048.pdfcours-gratuit.com--system1id048.pdf
cours-gratuit.com--system1id048.pdfKamalZeghdar
 
cours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdf
cours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdfcours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdf
cours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdfKamalZeghdar
 

More from KamalZeghdar (7)

003404788(1).pptx
003404788(1).pptx003404788(1).pptx
003404788(1).pptx
 
Chapitre 3 Modbus.pptx
Chapitre 3 Modbus.pptxChapitre 3 Modbus.pptx
Chapitre 3 Modbus.pptx
 
cour PIC16F877.pptx
cour PIC16F877.pptxcour PIC16F877.pptx
cour PIC16F877.pptx
 
2_Unit1_1-1.ppt
2_Unit1_1-1.ppt2_Unit1_1-1.ppt
2_Unit1_1-1.ppt
 
cours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdf
cours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdfcours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdf
cours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdf
 
cours-gratuit.com--system1id048.pdf
cours-gratuit.com--system1id048.pdfcours-gratuit.com--system1id048.pdf
cours-gratuit.com--system1id048.pdf
 
cours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdf
cours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdfcours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdf
cours-gratuit.com--pas-a-pas-vers-l-assembleur-par-lord-noteworthyid045.pdf
 

Recently uploaded

How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?DOT TECH
 
Computer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sComputer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sMAQIB18
 
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...elinavihriala
 
tapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive datatapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive datatheahmadsaood
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单ewymefz
 
Using PDB Relocation to Move a Single PDB to Another Existing CDB
Using PDB Relocation to Move a Single PDB to Another Existing CDBUsing PDB Relocation to Move a Single PDB to Another Existing CDB
Using PDB Relocation to Move a Single PDB to Another Existing CDBAlireza Kamrani
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhArpitMalhotra16
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单ocavb
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJames Polillo
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单ewymefz
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESSubhajit Sahu
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单enxupq
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...John Andrews
 
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...Domenico Conte
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单yhkoc
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单ukgaet
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单ewymefz
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundOppotus
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Subhajit Sahu
 

Recently uploaded (20)

How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?
 
Computer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sComputer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage s
 
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
 
tapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive datatapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive data
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
Using PDB Relocation to Move a Single PDB to Another Existing CDB
Using PDB Relocation to Move a Single PDB to Another Existing CDBUsing PDB Relocation to Move a Single PDB to Another Existing CDB
Using PDB Relocation to Move a Single PDB to Another Existing CDB
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 

Lecture6.pptx

  • 1. Lecture 6 – Introduction to the ATmega328 and Ardunio C S E P567
  • 2. Outline ⦁ Lecture 6 ⦁ ATmega architecture and instruction set ⦁ I/O pins ⦁ Arduino C++ language ⦁ Lecture 7 ⦁ ControllingTime ⦁ Interrupts andTimers ⦁ Lecture 8 ⦁ Guest lecture – Radio communication ⦁ Lecture 9 ⦁ Designing PID Controllers
  • 4. AVR Architecture ⦁ Clocks and Power ⦁ Beyond scope of this course
  • 6. AVR Architecture ⦁ Harvard architecture ⦁ Flash – program memory ⦁ 32K ⦁ SRAM – data memory ⦁ 2K ⦁ EEPROM ⦁ For long-term data ⦁ O n I/O data bus
  • 7. Memory ⦁ Flash (32K) (15-bit addresses) ⦁ Program memory – read only ⦁ Non-volatile ⦁ Allocate data to Flash using PROGMEM keyword ⦁ see documentation ⦁ SRAM (2K) ⦁ T emporary values,stack,etc. ⦁ Volatile ⦁ Limited space! ⦁ EEPROM (1K) ⦁ Long-term data ⦁ see documentation on EEPROM library
  • 8. AVR CPU ⦁ Instruction Fetch and Decode
  • 9. AVR CPU ⦁ ALU Instructions
  • 10. AVR CPU ⦁ I/O and special functions
  • 11. AVR Register File ⦁ 32 8-bit GP registers ⦁ Part of SRAM memory space
  • 12. Special Addressing Registers ⦁ X,Y and Z registers ⦁ 16-bit registers made using registers 26 – 31 ⦁ Support indirect addressing
  • 13. AVR Memory ⦁ Program memory – Flash ⦁ Data memory - SRAM
  • 14. Addressing Modes ⦁ Direct register addressing
  • 15. Addressing Modes ⦁ Direct I/O addressing
  • 16. Addressing Modes ⦁ Direct data memory addressing
  • 17. Addressing Modes ⦁ Direct data memory with displacement addressing
  • 18. Addressing Modes ⦁ Indirect data memory addressing
  • 19. Addressing Modes ⦁ Indirect data memory addressing with pre-decrement
  • 20. Addressing Modes ⦁ Indirect data memory addressing with post-increment
  • 21. Addressing Modes ⦁ Program memory addressing (constant data)
  • 23. Stack Pointer Register ⦁ Special register in I/O space [3E,3D] ⦁ Enough bits to address data space ⦁ Initialized to RAMEND (address of highest memory address) ⦁ Instructions that use the stack pointer
  • 24. Program Status Register (PSR) ⦁ ⦁ Status bits set by instructions/Checked by Branch/Skip instructions ⦁ I – Global interrupt enable ⦁ T – Flag bit ⦁ H – Half carry (BCD arithmetic) ⦁ S – Sign ⦁ V – Overflow ⦁ N – Negative ⦁ Z – Zero ⦁ C – Carry
  • 26. Single-Cycle ALU Instructions ⦁ Most instructions execute in one cycle ⦁ Makes program timing calculations (relatively) easy ⦁ No cache misses ⦁ 1 clock/instruction
  • 27. Addressing Modes ⦁ JMP ,CALL – Direct Program MemoryAddressing
  • 28. Addressing Modes ⦁ IJMP ,ICALL – Indirect program memory addressing
  • 29. Addressing Modes ⦁ RJMP ,RCALL – Relative program memory addressing
  • 32. J um p and Call Instructions
  • 33. Skip and Branch Instructions
  • 34. Skip and Branch (cont)
  • 36. Store
  • 39. Shift and Bit Instructions
  • 41. AVR Architecture ⦁ Three timers ⦁ Very flexible ⦁ Choose clock rate ⦁ Choose“roll-over” value ⦁ Generate interrupts ⦁ Generate PWM signals ⦁ (represent 8-bit value with using a clock signal) ⦁ More in next lecture…
  • 42. Arduino Timing Functions ⦁ delay(ms) ⦁ wait for ms milliseconds before continuing ⦁ delayMicroseconds(us) ⦁ wait for us microseconds before continuing ⦁ unsigned long millis( ) ⦁ return number of milliseconds since program started ⦁ unsigned long micros( ) ⦁ return number of microseconds since program started ⦁ resolution of 4 microseconds
  • 43. AVR Architecture ⦁ Interface to pins ⦁ Each pin directly programmable ⦁ Program direction ⦁ Program value ⦁ Program pull-ups ⦁ Some pins are special ⦁ Analog vs.Digital ⦁ Clocks ⦁ Reset
  • 44. I/O Ports ⦁ 3 8-bit Ports (B,C,D) ⦁ Each port controlled by 3 8-bit registers ⦁ Each bit controls one I/O pin ⦁ DDRx – Direction register ⦁ Defines whether a pin is an input (0) or and output (1) ⦁ PINx – Pin input value ⦁ Reading this “register” returns value of pin ⦁ PO RTx – Pin output value ⦁ Writing this register sets value of pin
  • 46. Pin Input DDRx = 0 off PO RTx PINx
  • 47. Synchronization Timing ⦁ Note:Takes a clock cycle for data output to be reflected on the input
  • 48. Pin Output DDRx = 1 on PO RTx PINx
  • 49. Pin Input – PORT controls pullup DDRx = 0 off PO RTx PINx
  • 50. I/O Ports ⦁ Pullups ⦁ If a pin is an input (DDRxi = 0): ⦁ PO RTxi = 0 – pin is floating ⦁ PO RTxi = 1 – connects a pullup to the pin  Keeps pin from floating if noone driving  Allows wired-OR bus ⦁ Individual bits can be set cleared using bit-ops ⦁ A bit can be toggled by writing 1 to PINxi ⦁ SBI instruction e.g.
  • 52.
  • 53. Arduino Digital and Analog I/O Pins ⦁ Digital pins: ⦁ Pins 0 – 7: PORT D [0:7] ⦁ Pins 8 – 13:PO RT B [0:5] ⦁ Pins 14 – 19:PORT C [0:5] (Arduino analog pins 0 – 5) ⦁ digital pins 0 and 1 are RX andTX for serial communication ⦁ digital pin 13 connected to the base board LED ⦁ Digital Pin I/O Functions ⦁ pinMode(pin,mode) ⦁ Sets pin to INPUT or O UTPUT mode ⦁ Writes 1 bit in the DDRx register ⦁ digitalWrite(pin,value) ⦁ Sets pin value to LOW or HIGH (0 or 1) ⦁ Writes 1 bit in the PO RTx register ⦁ int value = digitalRead(pin) ⦁ Reads back pin value (0 or 1) ⦁ Read 1 bit in the PINx register
  • 54. Arduino Analog I/O ⦁ Analog input pins:0 – 5 ⦁ Analog output pins:3,5,6,9,10,11 (digital pins) ⦁ Analog input functions ⦁ int val = analogRead(pin) ⦁ Converts 0 – 5v .voltage to a 10-bit number (0 – 1023) ⦁ Don’t use pinMode ⦁ analogReference(type) ⦁ Used to change how voltage is converted (advanced) ⦁ Analog output ⦁ analogWrite(pin,value) ⦁ value is 0 – 255 ⦁ Generates a PWM output on digital pin (3,5,6,9,10,11) ⦁ @490Hz frequency
  • 55. AVR Architecture ⦁ Analog inputs ⦁ Convert voltage to a 10-bit digital value ⦁ Can provide reference voltages
  • 56. PWM – Pulse Width Modulation ⦁ Use one wire to represent a multi-bit value ⦁ A clock with a variable duty cycle ⦁ Duty cycle used to represent value ⦁ We can turn it into a analog voltage using an integrating filter
  • 57. Port Special Functions ⦁ Lots of special uses for pins ⦁ Clock connections ⦁ Timer connections ⦁ e.g.comparator output for PWM ⦁ Interrupts ⦁ Analog references ⦁ Serial bus I/O s ⦁ USART ⦁ PCI
  • 58. Reading and Writing Pins Directly ⦁ O nly one pin can be changed using theArduino I/O functions ⦁ Setting multiple pins takes time and instructions ⦁ To change multiple pins simultaneously,directly read/write the pin registers ⦁ DDR{A/B/C} ⦁ PORT{A/B/C} ⦁ PIN{A/B/C} ⦁ e.g.to set all digital pins 0 – 7 to a value: ⦁ PORTD = B01100101;
  • 59. AVR Architecture ⦁ Special I/O support ⦁ Serial protocols ⦁ Uses special pins ⦁ Uses timers ⦁ Beyond scope of this course
  • 60. Arduino C Programs ⦁ Arduino calls these“sketches” ⦁ Basically C with libraries ⦁ Program structure ⦁ Header:declarations,includes,etc. ⦁ setup() ⦁ loop() ⦁ Setup is likeVerilog initial ⦁ executes once when program starts ⦁ loop() is likeVerilog always ⦁ continuously re-executed when the end is reached
  • 61. Blink Program int ledPin = 13; // LED connected to digital pin 13 // The setup() method runs once, when the sketch starts void setup() { // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); } // the loop() method runs over and over again, // as long as the Arduino has power void loop() { digitalWrite(ledPin, HIGH); delay(1000); digitalWrite(ledPin, LOW); delay(1000); } // set the LED on // wait for a second // set the LED off // wait for a second
  • 62. The Arduino C++ Main Program int main(void) { init(); setup(); for (;;) loop(); return 0; }
  • 63. Arduino Serial I/O ⦁ Communication with PC via USB serial line ⦁ Use the Serial Monitor in the IDE ⦁ Or set up a C or Java(or you-name-it) interface ⦁ Example Serial library calls ⦁ Serial.begin(baud-rate) ⦁ 9600 default ⦁ Serial.println(string) ⦁ int foo = Serial.read() ⦁ Read one byte (input data is buffered) ⦁ See documentation for more