SlideShare a Scribd company logo
1 of 64
Download to read offline
Lecture 6 – Introduction to the 
ATmega328 and Ardunio 
CSE P567
Outline 
 Lecture 6 
 ATmega architecture and instruction set 
 I/O pins 
 Arduino C++ language 
 Lecture 7 
 Controlling Time 
 Interrupts and Timers 
 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 
 On 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) 
 Temporary 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 Memory Addressing
Addressing Modes 
 IJMP, ICALL – Indirect program memory addressing
Addressing Modes 
 RJMP, RCALL – Relative program memory addressing
Arithmetic Instructions
Logical Instructions
Jump 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 
 PORTx – Pin output value 
 Writing this register sets value of pin
Pin Circuitry
Pin Input 
off 
DDRx = 0 
PORTx 
PINx
Synchronization Timing 
 Note: Takes a clock cycle for data output to be reflected 
on the input
Pin Output 
on 
DDRx = 1 
PORTx 
PINx
Pin Input – PORT controls pullup 
off 
DDRx = 0 
PORTx 
PINx
I/O Ports 
 Pullups 
 If a pin is an input (DDRxi = 0): 
 PORTxi = 0 – pin is floating 
 PORTxi = 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
AVR arduino dasar
Arduino Digital and Analog I/O Pins 
 Digital pins: 
 Pins 0 – 7: PORT D [0:7] 
 Pins 8 – 13: PORT B [0:5] 
 Pins 14 – 19: PORT C [0:5] (Arduino analog pins 0 – 5) 
 digital pins 0 and 1 are RX and TX 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 OUTPUT 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 PORTx 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/Os 
 USART 
 PCI
Reading and Writing Pins Directly 
 Only one pin can be changed using the Arduino 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 like Verilog initial 
 executes once when program starts 
 loop() is like Verilog 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); // set the LED on 
delay(1000); // wait for a second 
digitalWrite(ledPin, LOW); // set the LED off 
delay(1000); // 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

What's hot

Arduino Workshop Day 2 - Advance Arduino & DIY
Arduino Workshop Day 2 - Advance Arduino & DIYArduino Workshop Day 2 - Advance Arduino & DIY
Arduino Workshop Day 2 - Advance Arduino & DIYVishnu
 
Arduino Workshop Day 1 - Basic Arduino
Arduino Workshop Day 1 - Basic ArduinoArduino Workshop Day 1 - Basic Arduino
Arduino Workshop Day 1 - Basic ArduinoVishnu
 
IOTC08 The Arduino Platform
IOTC08 The Arduino PlatformIOTC08 The Arduino Platform
IOTC08 The Arduino PlatformEoin Brazil
 
Intro to Arduino
Intro to ArduinoIntro to Arduino
Intro to ArduinoQtechknow
 
Introduction to arduino!
Introduction to arduino!Introduction to arduino!
Introduction to arduino!Makers of India
 
Programming with arduino
Programming with arduinoProgramming with arduino
Programming with arduinoMakers of India
 
Arduino اردوينو
Arduino اردوينوArduino اردوينو
Arduino اردوينوsalih mahmod
 
Arduino slides
Arduino slidesArduino slides
Arduino slidessdcharle
 
ARDUINO AND ITS PIN CONFIGURATION
 ARDUINO AND ITS PIN  CONFIGURATION ARDUINO AND ITS PIN  CONFIGURATION
ARDUINO AND ITS PIN CONFIGURATIONsoma saikiran
 
Arduino Introduction Guide 1
Arduino Introduction Guide 1Arduino Introduction Guide 1
Arduino Introduction Guide 1elketeaches
 
Arduino Workshop
Arduino WorkshopArduino Workshop
Arduino Workshopatuline
 
Arduino learning
Arduino   learningArduino   learning
Arduino learningAnil Yadav
 
Arduino for beginners- Introduction to Arduino (presentation) - codewithgauri
Arduino for beginners- Introduction to Arduino (presentation) - codewithgauriArduino for beginners- Introduction to Arduino (presentation) - codewithgauri
Arduino for beginners- Introduction to Arduino (presentation) - codewithgauriGaurav Pandey
 
Arduino Foundations
Arduino FoundationsArduino Foundations
Arduino FoundationsJohn Breslin
 
Embedded system introduction - Arduino Course
Embedded system introduction - Arduino CourseEmbedded system introduction - Arduino Course
Embedded system introduction - Arduino CourseElaf A.Saeed
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduinoyeokm1
 

What's hot (20)

Fun with arduino
Fun with arduinoFun with arduino
Fun with arduino
 
Arduino Workshop Day 2 - Advance Arduino & DIY
Arduino Workshop Day 2 - Advance Arduino & DIYArduino Workshop Day 2 - Advance Arduino & DIY
Arduino Workshop Day 2 - Advance Arduino & DIY
 
Arduino Workshop Day 1 - Basic Arduino
Arduino Workshop Day 1 - Basic ArduinoArduino Workshop Day 1 - Basic Arduino
Arduino Workshop Day 1 - Basic Arduino
 
IOTC08 The Arduino Platform
IOTC08 The Arduino PlatformIOTC08 The Arduino Platform
IOTC08 The Arduino Platform
 
Intro to Arduino
Intro to ArduinoIntro to Arduino
Intro to Arduino
 
Introduction to arduino!
Introduction to arduino!Introduction to arduino!
Introduction to arduino!
 
Programming with arduino
Programming with arduinoProgramming with arduino
Programming with arduino
 
Arduino اردوينو
Arduino اردوينوArduino اردوينو
Arduino اردوينو
 
Arduino slides
Arduino slidesArduino slides
Arduino slides
 
ARDUINO AND ITS PIN CONFIGURATION
 ARDUINO AND ITS PIN  CONFIGURATION ARDUINO AND ITS PIN  CONFIGURATION
ARDUINO AND ITS PIN CONFIGURATION
 
Arduino course
Arduino courseArduino course
Arduino course
 
Arduino Introduction Guide 1
Arduino Introduction Guide 1Arduino Introduction Guide 1
Arduino Introduction Guide 1
 
Arduino Workshop
Arduino WorkshopArduino Workshop
Arduino Workshop
 
Arduino uno
Arduino unoArduino uno
Arduino uno
 
Arduino learning
Arduino   learningArduino   learning
Arduino learning
 
Arduino for beginners- Introduction to Arduino (presentation) - codewithgauri
Arduino for beginners- Introduction to Arduino (presentation) - codewithgauriArduino for beginners- Introduction to Arduino (presentation) - codewithgauri
Arduino for beginners- Introduction to Arduino (presentation) - codewithgauri
 
Presentation
PresentationPresentation
Presentation
 
Arduino Foundations
Arduino FoundationsArduino Foundations
Arduino Foundations
 
Embedded system introduction - Arduino Course
Embedded system introduction - Arduino CourseEmbedded system introduction - Arduino Course
Embedded system introduction - Arduino Course
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduino
 

Similar to AVR arduino dasar (20)

Lecture6
Lecture6Lecture6
Lecture6
 
Lecture6.pptx
Lecture6.pptxLecture6.pptx
Lecture6.pptx
 
Presentation
PresentationPresentation
Presentation
 
Microcontroller
MicrocontrollerMicrocontroller
Microcontroller
 
Arduino Programming Basic
Arduino Programming BasicArduino Programming Basic
Arduino Programming Basic
 
Arduino Day 1 Presentation
Arduino Day 1 PresentationArduino Day 1 Presentation
Arduino Day 1 Presentation
 
Arduino - Classes and functions
Arduino - Classes and functionsArduino - Classes and functions
Arduino - Classes and functions
 
Arduino cic3
Arduino cic3Arduino cic3
Arduino cic3
 
How to use an Arduino
How to use an ArduinoHow to use an Arduino
How to use an Arduino
 
MICROCONTROLLER.pptx
MICROCONTROLLER.pptxMICROCONTROLLER.pptx
MICROCONTROLLER.pptx
 
Arduino Programming
Arduino ProgrammingArduino Programming
Arduino Programming
 
arduinocourse-180308074529 (1).pdf
arduinocourse-180308074529 (1).pdfarduinocourse-180308074529 (1).pdf
arduinocourse-180308074529 (1).pdf
 
Arduino by yogesh t s'
Arduino by yogesh t s'Arduino by yogesh t s'
Arduino by yogesh t s'
 
4 IOT 18ISDE712 MODULE 4 IoT Physical Devices and End Point-Aurdino Uno.pdf
4 IOT 18ISDE712  MODULE 4 IoT Physical Devices and End Point-Aurdino  Uno.pdf4 IOT 18ISDE712  MODULE 4 IoT Physical Devices and End Point-Aurdino  Uno.pdf
4 IOT 18ISDE712 MODULE 4 IoT Physical Devices and End Point-Aurdino Uno.pdf
 
Chp 2 and 3.pptx
Chp 2 and 3.pptxChp 2 and 3.pptx
Chp 2 and 3.pptx
 
Arduino intro.pptx
Arduino intro.pptxArduino intro.pptx
Arduino intro.pptx
 
Avr report
Avr reportAvr report
Avr report
 
amba.ppt
amba.pptamba.ppt
amba.ppt
 
amba.ppt
amba.pptamba.ppt
amba.ppt
 
amba (1).ppt
amba (1).pptamba (1).ppt
amba (1).ppt
 

Recently uploaded

Field Report on present condition of Ward 1 and Ward 2 of Pabna Municipality
Field Report on present condition of Ward 1 and Ward 2 of Pabna MunicipalityField Report on present condition of Ward 1 and Ward 2 of Pabna Municipality
Field Report on present condition of Ward 1 and Ward 2 of Pabna MunicipalityMorshed Ahmed Rahath
 
Caltrans District 8 Update for the CalAPA Spring Asphalt Conference 2024
Caltrans District 8 Update for the CalAPA Spring Asphalt Conference 2024Caltrans District 8 Update for the CalAPA Spring Asphalt Conference 2024
Caltrans District 8 Update for the CalAPA Spring Asphalt Conference 2024California Asphalt Pavement Association
 
12. Stairs by U Nyi Hla ngae from Myanmar.pdf
12. Stairs by U Nyi Hla ngae from Myanmar.pdf12. Stairs by U Nyi Hla ngae from Myanmar.pdf
12. Stairs by U Nyi Hla ngae from Myanmar.pdftpo482247
 
introduction to python, fundamentals and basics
introduction to python, fundamentals and basicsintroduction to python, fundamentals and basics
introduction to python, fundamentals and basicsKNaveenKumarECE
 
Injection Power Cycle - The most efficient power cycle
Injection Power Cycle - The most efficient power cycleInjection Power Cycle - The most efficient power cycle
Injection Power Cycle - The most efficient power cyclemarijomiljkovic1
 
presentation by faizan[1] [Read-Only].pptx
presentation by faizan[1] [Read-Only].pptxpresentation by faizan[1] [Read-Only].pptx
presentation by faizan[1] [Read-Only].pptxkhfaizan534
 
Flutter GDE session GDSC ZHCET AMU, aligarh
Flutter GDE session GDSC ZHCET AMU, aligarhFlutter GDE session GDSC ZHCET AMU, aligarh
Flutter GDE session GDSC ZHCET AMU, aligarhjamesbond00714
 
Evaluation Methods for Social XR Experiences
Evaluation Methods for Social XR ExperiencesEvaluation Methods for Social XR Experiences
Evaluation Methods for Social XR ExperiencesMark Billinghurst
 
A brief about Jeypore Sub-station Presentation
A brief about Jeypore Sub-station PresentationA brief about Jeypore Sub-station Presentation
A brief about Jeypore Sub-station PresentationJeyporess2021
 
Chapter 2 Canal Falls at Mnnit Allahabad .pptx
Chapter 2 Canal Falls at Mnnit Allahabad .pptxChapter 2 Canal Falls at Mnnit Allahabad .pptx
Chapter 2 Canal Falls at Mnnit Allahabad .pptxButcher771
 
Searching and Sorting Algorithms
Searching and Sorting AlgorithmsSearching and Sorting Algorithms
Searching and Sorting AlgorithmsAshutosh Satapathy
 
Conventional vs Modern method (Philosophies) of Tunneling-re.pptx
Conventional vs Modern method (Philosophies) of Tunneling-re.pptxConventional vs Modern method (Philosophies) of Tunneling-re.pptx
Conventional vs Modern method (Philosophies) of Tunneling-re.pptxSAQIB KHURSHEED WANI
 
عناصر نباتية PDF.pdfbotanical elements..
عناصر نباتية PDF.pdfbotanical elements..عناصر نباتية PDF.pdfbotanical elements..
عناصر نباتية PDF.pdfbotanical elements..mennamohamed200y
 
Introduction to Data Structures .
Introduction to Data Structures        .Introduction to Data Structures        .
Introduction to Data Structures .Ashutosh Satapathy
 
Advanced Additive Manufacturing by Sumanth A.pptx
Advanced Additive Manufacturing by Sumanth A.pptxAdvanced Additive Manufacturing by Sumanth A.pptx
Advanced Additive Manufacturing by Sumanth A.pptxSumanth A
 
The Journey of Process Safety Management: Past, Present, and Future Trends
The Journey of Process Safety Management: Past, Present, and Future TrendsThe Journey of Process Safety Management: Past, Present, and Future Trends
The Journey of Process Safety Management: Past, Present, and Future Trendssoginsider
 
pulse modulation technique (Pulse code modulation).pptx
pulse modulation technique (Pulse code modulation).pptxpulse modulation technique (Pulse code modulation).pptx
pulse modulation technique (Pulse code modulation).pptxNishanth Asmi
 

Recently uploaded (20)

Field Report on present condition of Ward 1 and Ward 2 of Pabna Municipality
Field Report on present condition of Ward 1 and Ward 2 of Pabna MunicipalityField Report on present condition of Ward 1 and Ward 2 of Pabna Municipality
Field Report on present condition of Ward 1 and Ward 2 of Pabna Municipality
 
Caltrans District 8 Update for the CalAPA Spring Asphalt Conference 2024
Caltrans District 8 Update for the CalAPA Spring Asphalt Conference 2024Caltrans District 8 Update for the CalAPA Spring Asphalt Conference 2024
Caltrans District 8 Update for the CalAPA Spring Asphalt Conference 2024
 
12. Stairs by U Nyi Hla ngae from Myanmar.pdf
12. Stairs by U Nyi Hla ngae from Myanmar.pdf12. Stairs by U Nyi Hla ngae from Myanmar.pdf
12. Stairs by U Nyi Hla ngae from Myanmar.pdf
 
introduction to python, fundamentals and basics
introduction to python, fundamentals and basicsintroduction to python, fundamentals and basics
introduction to python, fundamentals and basics
 
Injection Power Cycle - The most efficient power cycle
Injection Power Cycle - The most efficient power cycleInjection Power Cycle - The most efficient power cycle
Injection Power Cycle - The most efficient power cycle
 
presentation by faizan[1] [Read-Only].pptx
presentation by faizan[1] [Read-Only].pptxpresentation by faizan[1] [Read-Only].pptx
presentation by faizan[1] [Read-Only].pptx
 
Flutter GDE session GDSC ZHCET AMU, aligarh
Flutter GDE session GDSC ZHCET AMU, aligarhFlutter GDE session GDSC ZHCET AMU, aligarh
Flutter GDE session GDSC ZHCET AMU, aligarh
 
Evaluation Methods for Social XR Experiences
Evaluation Methods for Social XR ExperiencesEvaluation Methods for Social XR Experiences
Evaluation Methods for Social XR Experiences
 
A brief about Jeypore Sub-station Presentation
A brief about Jeypore Sub-station PresentationA brief about Jeypore Sub-station Presentation
A brief about Jeypore Sub-station Presentation
 
Chapter 2 Canal Falls at Mnnit Allahabad .pptx
Chapter 2 Canal Falls at Mnnit Allahabad .pptxChapter 2 Canal Falls at Mnnit Allahabad .pptx
Chapter 2 Canal Falls at Mnnit Allahabad .pptx
 
Industry perspective on cold in-place recycling
Industry perspective on cold in-place recyclingIndustry perspective on cold in-place recycling
Industry perspective on cold in-place recycling
 
Searching and Sorting Algorithms
Searching and Sorting AlgorithmsSearching and Sorting Algorithms
Searching and Sorting Algorithms
 
Caltrans view on recycling of in-place asphalt pavements
Caltrans view on recycling of in-place asphalt pavementsCaltrans view on recycling of in-place asphalt pavements
Caltrans view on recycling of in-place asphalt pavements
 
Hydraulic Loading System - Neometrix Defence
Hydraulic Loading System - Neometrix DefenceHydraulic Loading System - Neometrix Defence
Hydraulic Loading System - Neometrix Defence
 
Conventional vs Modern method (Philosophies) of Tunneling-re.pptx
Conventional vs Modern method (Philosophies) of Tunneling-re.pptxConventional vs Modern method (Philosophies) of Tunneling-re.pptx
Conventional vs Modern method (Philosophies) of Tunneling-re.pptx
 
عناصر نباتية PDF.pdfbotanical elements..
عناصر نباتية PDF.pdfbotanical elements..عناصر نباتية PDF.pdfbotanical elements..
عناصر نباتية PDF.pdfbotanical elements..
 
Introduction to Data Structures .
Introduction to Data Structures        .Introduction to Data Structures        .
Introduction to Data Structures .
 
Advanced Additive Manufacturing by Sumanth A.pptx
Advanced Additive Manufacturing by Sumanth A.pptxAdvanced Additive Manufacturing by Sumanth A.pptx
Advanced Additive Manufacturing by Sumanth A.pptx
 
The Journey of Process Safety Management: Past, Present, and Future Trends
The Journey of Process Safety Management: Past, Present, and Future TrendsThe Journey of Process Safety Management: Past, Present, and Future Trends
The Journey of Process Safety Management: Past, Present, and Future Trends
 
pulse modulation technique (Pulse code modulation).pptx
pulse modulation technique (Pulse code modulation).pptxpulse modulation technique (Pulse code modulation).pptx
pulse modulation technique (Pulse code modulation).pptx
 

AVR arduino dasar

  • 1. Lecture 6 – Introduction to the ATmega328 and Ardunio CSE P567
  • 2. Outline  Lecture 6  ATmega architecture and instruction set  I/O pins  Arduino C++ language  Lecture 7  Controlling Time  Interrupts and Timers  Lecture 8  Guest lecture – Radio communication  Lecture 9  Designing PID Controllers
  • 4. AVR Architecture  Clocks and Power  Beyond scope of this course
  • 5. AVR Architecture  CPU  Details coming
  • 6. AVR Architecture  Harvard architecture  Flash – program memory  32K  SRAM – data memory  2K  EEPROM  For long-term data  On 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)  Temporary 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
  • 25. Simple 2-Stage Pipeline  Branch/Skip??
  • 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 Memory Addressing
  • 28. Addressing Modes  IJMP, ICALL – Indirect program memory addressing
  • 29. Addressing Modes  RJMP, RCALL – Relative program memory addressing
  • 32. Jump 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  PORTx – Pin output value  Writing this register sets value of pin
  • 46. Pin Input off DDRx = 0 PORTx PINx
  • 47. Synchronization Timing  Note: Takes a clock cycle for data output to be reflected on the input
  • 48. Pin Output on DDRx = 1 PORTx PINx
  • 49. Pin Input – PORT controls pullup off DDRx = 0 PORTx PINx
  • 50. I/O Ports  Pullups  If a pin is an input (DDRxi = 0):  PORTxi = 0 – pin is floating  PORTxi = 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.
  • 53. Arduino Digital and Analog I/O Pins  Digital pins:  Pins 0 – 7: PORT D [0:7]  Pins 8 – 13: PORT B [0:5]  Pins 14 – 19: PORT C [0:5] (Arduino analog pins 0 – 5)  digital pins 0 and 1 are RX and TX 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 OUTPUT 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 PORTx 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/Os  USART  PCI
  • 58. Reading and Writing Pins Directly  Only one pin can be changed using the Arduino 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 like Verilog initial  executes once when program starts  loop() is like Verilog 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); // set the LED on delay(1000); // wait for a second digitalWrite(ledPin, LOW); // set the LED off delay(1000); // 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