Arduino Software Internals - Norman Dunbar

Arduino Software Internals (eBook)

A Complete Guide to How Your Arduino Language and Hardware Work Together

(Autor)

eBook Download: PDF
2020 | First Edition
XIX, 617 Seiten
Apress (Verlag)
978-1-4842-5790-6 (ISBN)
Systemvoraussetzungen
56,99 inkl. MwSt
  • Download sofort lieferbar
  • Zahlungsarten anzeigen
It's not enough to just build your Arduino projects; it's time to actually learn how things work! This book will take you through not only how to use the Arduino software and hardware, but more importantly show you how it all works and how the software relates to the hardware.

Arduino Software Internals takes a detailed dive into the Arduino environment. We'll cover the Arduino language, hardware features, and how makers can finally ease themselves away from the hand holding of the Arduino environment and move towards coding in plain AVR C++ and talk to the microcontroller in its native language. 

What You'll Learn:
  • How the Arduino Language interfaces with the hardware, as well as how it actually works in C++;
  • How the compilation system works, and how kit can be altered to suit personal requirements;
  • A small amount of AVR Assembly Language;
  • Exactly how to set up and use the various hardware features of the AVR without needing to try and decode the data sheets - which are often bug ridden and unclear;
  • Alternatives to the Arduino IDE which might give them a better workflow;
  • How to build their own Arduino clone from scratch.

Who This Book Is For: 
No expertise is required for this book! All you need is an interest in learning about what you're making with Arduinos and how they work. This book is also useful for those looking to understand the AVR microcontroller used in the Arduino boards. In other words, all Makers are welcome!


Norman Dunbar is an Oracle Database Administrator. Norman has had a long running relationship with Electronics since childhood and computers since the late 1970s, and the Arduino was a perfect marriage of the two interests. With a love of learning new things, examining and explaining the Arduino Language and the hardware became a bit of a hobby, and as piles of notes expanded, Norman has now decided to publish his work.
It's not enough to just build your Arduino projects; it's time to actually learn how things work! This book will take you through not only how to use the Arduino software and hardware, but more importantly show you how it all works and how the software relates to the hardware.Arduino Software Internals takes a detailed dive into the Arduino environment. We'll cover the Arduino language, hardware features, and how makers can finally ease themselves away from the hand holding of the Arduino environment and move towards coding in plain AVR C++ and talk to the microcontroller in its native language. What You'll Learn:How the Arduino Language interfaces with the hardware, as well as how it actually works in C++;How the compilation system works, and how kit can be altered to suit personal requirements;A small amount of AVR Assembly Language;Exactly how to set up and use the various hardware features of the AVR without needing to try and decode the data sheets - which are often bug ridden and unclear;Alternatives to the Arduino IDE which might give them a better workflow;How to build their own Arduino clone from scratch.Who This Book Is For: No expertise is required for this book! All you need is an interest in learning about what you're making with Arduinos and how they work. This book is also useful for those looking to understand the AVR microcontroller used in the Arduino boards. In other words, all Makers are welcome!

Table of Contents 5
About the Author 14
About the Technical Reviewer 15
Preface 16
Chapter 1: Introduction 17
1.1. Arduino Installation Paths 18
1.2. Coding Style 20
1.3. The Arduino Language 22
1.4. Coming Up 22
Chapter 2: Arduino Compilation 25
2.1. Preferences.txt 25
2.1.1. Using an ICSP for All Uploads 28
2.1.2. Change the Action of Home and End Keys 30
2.1.3. Setting Tab Stops 31
2.2. Globally Defined Paths 32
2.3. Boards.txt 34
2.3.1. Arduino Uno Example 34
2.3.1.1. Board Identifier 36
2.3.1.2. Identification Settings 36
2.3.1.3. Upload Settings 38
2.3.1.4. Bootloader Settings 39
2.3.1.4.1. Boards.local.txt 41
2.3.1.5. Build Settings 41
2.3.1.6. Configuring an ICSP 43
2.4. Platform.txt 44
2.4.1. Build Recipes 45
2.4.2. Pre- and Post-build Hooks 50
2.5. Programmers.txt 52
2.6. Compiling a Sketch 55
2.6.1. Arduino Sketch (*.ino) Preprocessing 55
2.6.2. Arduino Sketch (*.ino) Build 57
2.7. The Arduino main() Function 60
2.8. Header File Arduino.h 63
2.8.1. Header File avr/pgmspace.h 65
2.8.2. Header File avr/io.h 65
2.8.2.1. Header File avr/iom328p.h 66
2.8.2.2. Header File avr/portpins.h 66
2.8.2.3. Header File < avr/common.h>
2.8.2.4. Header File < avr/version.h>
2.8.2.5. Header File < avr/fuse.h>
2.8.2.6. Header File < avr/lock.h>
2.8.3. Header File avr/interrupt.h 68
2.8.4. Header File binary.h 69
2.8.5. Header File WCharacter.h 71
2.8.6. Header File WString.h 72
2.8.7. Header File HardwareSerial.h 72
2.8.8. Header File USBAPI.h 73
2.8.9. Header File pins_arduino.h 73
2.9. The init() Function 75
2.9.1. Enabling the Global Interrupt Flag 75
2.9.2. Enabling Timer/counter 0 76
2.9.3. Timer/counter 0 Overflow Interrupt 79
2.9.4. Configuring Timer/counter 1 and Timer/counter 2 83
2.9.5. Initializing the Analogue to Digital Converter 86
2.9.6. Disabling the USART 88
Chapter 3: Arduino Language Reference 89
3.1. Digital Input/Output 91
3.1.1. Function pinMode() 91
3.1.2. Function digitalRead() 100
3.1.3. Function digitalWrite() 103
3.2. Analogue Input/Output 106
3.2.1. Function analogReference() 106
3.2.2. Function analogRead() 109
3.2.3. Function analogWrite() 113
3.3. Advanced Input/Output 120
3.3.1. Function tone() 120
3.3.2. Function noTone() 135
3.3.3. Function pulseIn() 138
3.3.4. Function pulseInLong() 146
3.3.5. Function shiftIn() 150
3.3.6. Function shiftOut() 153
3.4. Time 156
3.4.1. Function delay() 157
3.4.2. Function delayMicroseconds() 163
3.4.3. Function micros() 167
3.4.4. Function millis() 170
3.5. Interrupts 173
3.5.1. Function interrupts() 173
3.5.2. Function noInterrupts() 174
3.5.3. Function attachInterrupt() 174
3.5.4. Function detachInterrupt() 185
3.6. Bits and Bobs 187
3.6.1. Macro bit() 187
3.6.2. Macro bitClear() 189
3.6.3. Macro bitRead() 189
3.6.4. Macro bitSet() 190
3.6.5. Macro bitWrite() 190
3.6.6. Macro highByte() 191
3.6.7. Macro lowByte() 191
3.6.8. Macro sbi() 192
3.6.9. Macro cbi() 193
Chapter 4: Arduino Classes 195
4.1. The Print Class 195
4.1.1. Class Members 198
4.1.2. Using the Print Class 200
4.2. The Printable Class 205
4.2.1. An Example Printable Class 207
4.3. The Stream Class 211
4.3.1. Class Members 213
4.4. The HardwareSerial Class 221
4.4.1. Interrupt Handlers 222
4.4.1.1. USART Receive Complete Interrupt 223
4.4.1.2. USART Data Register Empty Interrupt 225
4.4.2. Class Functions and Macros 228
4.4.2.1. Macro SERIAL_TX_BUFFER_SIZE 228
4.4.2.2. Macro SERIAL_RX_BUFFER_SIZE 229
4.4.2.3. Typedefs tx_buffer_index_t and rx_buffer_index_t 230
4.4.2.4. Serial Communications Parameters 230
4.4.2.5. Macro HAVE_HWSERIAL0 232
4.4.2.6. Constructor HardwareSerial() 233
4.4.2.7. Function begin(unsigned long baud) 234
4.4.2.8. Function begin(unsigned long, uint8_t) 235
4.4.2.8.1. Notes on Baud Rate Calculations 239
4.4.2.8.2. Notes on Baud Rate Errors 242
4.4.2.8.3. Notes on Low- and High-Speed Communications 243
4.4.2.9. Function end() 243
4.4.2.10. Operator bool() 244
4.4.2.11. Function available(void) 245
4.4.2.12. Function peek(void) 246
4.4.2.13. Function read(void) 247
4.4.2.14. Function availableForWrite(void) 248
4.4.2.15. Function flush(void) 250
4.4.2.16. Function write(uint8_t) 252
4.4.2.17. Function _rx_complete_irq(void) 258
4.4.2.18. Function _tx_udr_empty_irq(void) 258
4.5. The String Class 258
Chapter 5: Converting to the AVR Language 261
5.1. Introduction 262
5.2. Numbering Systems 263
5.2.1. Decimal Numbering 263
5.2.2. Binary Numbering 264
5.2.3. Hexadecimal Numbering 264
5.3. Binary Logical Operations 266
5.4. NOT 266
5.5. AND 267
5.6. OR 267
5.7. XOR 268
5.8. Replacing the Arduino Language 269
5.8.1. The ATmega328P Pins and Ports 269
5.9. Replacing pinMode() 273
5.10. Replacing digitalWrite() 276
5.10.1. Enabling Internal Pullup Resistors 279
5.10.2. Bit Twiddling 279
5.11. Replacing digitalRead() 281
5.11.1. Toggling Output Pins 283
5.11.2. Installing digitalToggle() 285
Chapter 6: Alternatives to the Arduino IDE 288
6.1. PlatformIO 289
6.1.1. Installing PlatformIO Core 289
6.1.2. Testing PlatformIO Core 291
6.1.2.1. Set Up Your Environment 291
6.1.2.2. Set Up a New Project 294
6.1.2.3. Initialize the Project 297
6.1.2.4. Arduino-Style Projects 301
6.1.2.4.1. Compiling Arduino Projects 302
6.1.2.4.2. Uploading Arduino Projects 305
6.1.2.5. AVR-Style Projects 308
6.1.2.5.1. Compiling AVR Projects 311
6.1.2.5.2. Uploading AVR Projects 313
6.1.3. Burning Bootloaders 314
6.1.4. PlatformIO in an IDE 314
6.1.4.1. Set Up a New Code::Blocks Arduino Project 316
6.1.4.2. Set Up a New Code::Blocks AVR Project 318
6.1.5. PlatformIO Home 320
6.1.5.1. Creating Projects 321
6.1.5.2. Opening Projects 322
6.1.5.3. Importing Arduino Sketches 322
6.1.6. PlatformIO IDE 323
6.1.6.1. Installation 324
6.1.6.2. PIO Home Tab 325
6.1.6.3. Creating a New Project 326
6.1.6.4. Opening Existing Projects 327
6.1.6.5. Editing the Project 327
6.1.6.6. Compiling a Project 328
6.1.6.7. Upload or Program a Project 329
6.2. Arduino Command Line 330
6.2.1. Obtaining the Arduino CLI 331
6.2.2. Installing 331
6.2.3. Configuring the CLI 333
6.2.4. Creating Sketches 335
6.2.5. Installing Platforms 338
6.2.6. Compiling Sketches 339
6.2.7. Uploading Sketches 342
6.2.8. Uploading Sketches with an ICSP 344
6.2.9. Burning Bootloaders 348
6.2.10. Serial Usage 353
Chapter 7: ATmega328P Configuration and Management 356
7.1. ATmega328P Fuses 357
7.1.1. Low Fuse Bits 358
7.1.1.1. SUT Fuse Bits 358
7.1.1.2. CKSEL Fuse Bits 359
7.1.2. Low Fuse Factory Default 360
7.1.3. Arduino Low Fuse Settings 361
7.1.4. High Fuse Bits 362
7.1.5. High Fuse Factory Default 364
7.1.6. Arduino High Fuse Settings 365
7.1.7. Extended Fuse Bits 366
7.1.8. Extended Fuse Factory Default 367
7.1.9. Arduino Extended Fuse Settings 367
7.2. Brown-Out Detection 367
7.3. The Watchdog Timer 370
7.3.1. Watchdog Timer Modes of Operation 370
7.3.2. Amended Sketch setup() Function 373
7.3.3. Watchdog Timer Reset 374
7.3.4. The Watchdog Timer Control Register 375
7.3.5. Enabling the Watchdog Timer 379
7.3.6. Setting the Watchdog Timer Timeout 380
7.3.7. Disabling the Watchdog Timer 385
7.3.8. Putting the AVR to Sleep 387
7.3.9. Sleep Modes 392
7.3.9.1. Idle Sleep Mode 394
7.3.9.2. ADC Noise Reduction Sleep Mode 397
7.3.9.3. Power Down Sleep Mode 400
7.3.9.4. Power Save Sleep Mode 402
7.3.9.5. Standby Sleep Mode 405
7.3.9.6. Extended Standby Sleep Mode 407
7.3.10. Analogue Comparator 409
7.4. Power Reduction 412
7.4.1. Power Consumption 412
7.4.1.1. Calculating Power Requirements 415
7.4.2. Power Reduction Register 416
7.4.3. Saving Arduino Power 418
7.4.4. The Power Functions 421
7.5. Bootloaders 423
7.5.1. Flash Memory 423
7.5.2. Lock Bits 424
7.5.2.1. Device Lock Bits 424
7.5.2.2. Bootloader Lock Bits 426
7.5.2.2. Bootloader Lock Bits 0 426
7.5.2.2. Bootloader Lock Bits 1 427
7.5.3. Installing the Uno (Optiboot) Bootloader 428
7.5.4. Optiboot Bootloader Operation 430
Chapter 8: ATmega328P Hardware: Timers and Counters 432
8.1. Timer/Counters 432
8.1.1. Timer/Counter 0 (8 Bits) 434
8.1.2. Timer/Counter 1 (8, 9, 10, and/or 16 Bits) 435
8.1.3. Timer/Counter 2 (8 Bits) 436
8.1.4. Timer/Counter Clock Sources 438
8.1.5. Timer/Counter Operating Modes 440
8.1.5.1. Timers Disabled 440
8.1.5.1.1. Disabling the Timers 441
8.1.5.2. Normal Mode 442
8.1.5.2.1. Setting Normal Mode 443
8.1.5.2.2. Example Sketch 444
8.1.6. Clear Timer on Compare Match Mode 451
8.1.6.1. Setting CTC Mode 454
8.1.6.2. Example Sketch 455
8.1.7. PWM Modes 459
8.1.7.1. Duty Cycle 459
8.1.7.2. PWM Frequencies 462
8.1.7.3. Fast PWM Mode 463
8.1.7.3.1. Setting Fast PWM Mode 471
8.1.7.3.2. Example Sketch 472
8.1.7.4. Phase Correct PWM Mode 476
8.1.7.4.1. Setting Phase Correct PWM Mode 483
8.1.7.4.2. Example Sketch 484
8.1.7.5. Phase and Frequency Correct PWM Mode 489
8.1.7.5.1. Setting Phase and Frequency Correct PWM Mode 493
8.1.8. Too Much to Remember? Try AVRAssist 494
8.2. Counting 495
8.2.1. Setting External Counting 496
8.2.2. Counter Example 497
8.3. Input Capture Unit 502
Chapter 9: ATmega328P Hardware: ADC and USART 508
9.1. The Analogue Comparator 508
9.1.1. Reference Voltage 510
9.1.2. Sampled Voltage 510
9.1.3. Digital Input 510
9.1.4. Enable the Analogue Comparator 511
9.1.5. Select Reference Voltage Source 511
9.1.5.1. External Reference 512
9.1.5.2. Internal Bandgap Reference 512
9.1.6. Select Sampled Voltage Source Pin 512
9.1.6.1. Sample Voltage on Pin AIN1 512
9.1.6.2. Sample Voltage on Pins A0–A7 513
9.1.7. Sampled Voltage Summary 513
9.1.8. Comparator Outputs 514
9.1.9. Comparator Example 515
9.2. Analogue to Digital Converter (ADC) 519
9.2.1. ADC Setup and Initiation 520
9.2.1.1. Powering the ADC 521
9.2.1.2. Selecting the Prescaler 521
9.2.1.3. Selecting the Reference Voltage Source 523
9.2.1.4. Left or Right Alignment? 524
9.2.1.5. Selecting an Input Source 526
9.2.1.6. Disable Digital Input 527
9.2.1.7. ADC Interrupt 528
9.2.1.8. Single-Shot or Auto Trigger? 529
9.2.1.9. Enabling the ADC and Initiating Conversions 531
9.2.1.10. ADC Conversions 532
9.2.2. Noise Reduction 533
9.2.3. Temperature Measurement 534
9.2.4. ADC Example 537
9.3. USART 543
9.3.1. Baud Rates 544
9.3.2. Double Speed 545
9.3.3. Baud Rate Calculations 545
9.3.4. Baud Rate Errors 547
9.3.5. What Is a Frame? 548
9.3.6. Parity 549
9.3.7. Interrupts 550
9.3.7.1. TX Complete Interrupt 550
9.3.7.2. TX Data Register Empty Interrupt 550
9.3.7.3. RX Complete 550
9.3.8. Initializing the USART 551
9.3.8.1. Powering the USART 552
9.3.8.2. Choosing the USART Mode 552
9.3.8.3. Baud Rate Setting 554
9.3.8.4. Frame Settings 554
9.3.8.5. Setting Parity 554
9.3.8.6. Setting Stop Bits 555
9.3.8.7. Setting Data Width 555
9.3.8.8. Enabling Double-Speed Mode 556
9.3.8.9. Enabling Interrupts 557
9.3.8.10. Enabling Data Transmission 558
9.3.8.11. Enabling Data Receipt 558
9.3.8.12. Transmitting or Receiving 9-Bit Data 559
9.3.9. USART Checks 559
9.3.9.1. USART Receive Complete 560
9.3.9.2. USART Transmit Complete 560
9.3.9.3. USART Data Register Empty 560
9.3.9.4. USART Frame Error 561
9.3.9.5. USART Data Overrun 561
9.3.9.6. USART Parity Error 562
9.3.10. USART Example 562
Appendix A:Arduino Paths 571
Appendix B:ATmega328P Pinout 574
Appendix C:ATmega328P Power Restrictions 576
C.1. Power in Total 577
C.2. Power per Port 577
C.3. Power per Pin 577
Appendix D:Predefined Settings 578
D.1. Global Interrupts 578
D.2. Timer/counter 0 578
D.3. Timer/counters 1 and 2 579
D.4. USART 580
D.5. Analogue to Digital Converter 581
Appendix E:ADC Temperature Measuring 582
Appendix F:Assembly Language: Briefly 591
Appendix G:Smallest Blink Sketch? 595
Appendix H:NormDuino 601
Appendix I:No ICSP? No Problem! 606
I.1. ArduinoISP Sketch 606
I.2. Connections 607
I.3. Choose Your Programmer 609
I.4. Burn the Bootloader 609
Appendix J:Breadboard 8 MHz Board Setup 610
Appendix K: AVRAssist 614
K.1. Components 614
K.2. In Use 614
Index 617

Erscheint lt. Verlag 25.4.2020
Zusatzinfo XIX, 617 p. 194 illus., 162 illus. in color.
Sprache englisch
Themenwelt Mathematik / Informatik Informatik Betriebssysteme / Server
Mathematik / Informatik Informatik Programmiersprachen / -werkzeuge
Informatik Weitere Themen Hardware
Schlagworte Arduino • Arduino AVR • Arduino IDE • Arduino Language • Arduino Uno • Assembly language • ATmega328p • AVRAssist • C++ • PlatformIO
ISBN-10 1-4842-5790-1 / 1484257901
ISBN-13 978-1-4842-5790-6 / 9781484257906
Haben Sie eine Frage zum Produkt?
PDFPDF (Wasserzeichen)
Größe: 7,7 MB

DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasser­zeichen und ist damit für Sie persona­lisiert. Bei einer missbräuch­lichen Weiter­gabe des eBooks an Dritte ist eine Rück­ver­folgung an die Quelle möglich.

Dateiformat: PDF (Portable Document Format)
Mit einem festen Seiten­layout eignet sich die PDF besonders für Fach­bücher mit Spalten, Tabellen und Abbild­ungen. Eine PDF kann auf fast allen Geräten ange­zeigt werden, ist aber für kleine Displays (Smart­phone, eReader) nur einge­schränkt geeignet.

Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.

Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.

Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.

Mehr entdecken
aus dem Bereich