Atmel's AVR microcontrollers are the chips that power Arduino, and are the go-to chip for many hobbyist and hardware hacking projects. In this book you'll set aside the layers of abstraction provided by the Arduino environment and learn how to program AVR microcontrollers directly. In doing so, you'll get closer to the chip and you'll be able to squeeze more power and features out of it. Each chapter of this book is centered around projects that incorporate that particular microcontroller topic. Each project includes schematics, code, and illustrations of a working project. - Program a range of AVR chips
- Extend and re-use other people's code and circuits
- Interface with USB, I2C, and SPI peripheral devices
- Learn to access the full range of power and speed of the microcontroller
- Build projects including Cylon Eyes, a Square-Wave Organ, an AM Radio, a Passive Light-Sensor Alarm, Temperature Logger, and more
- Understand what's happening behind the scenes even when using the Arduino IDE
Table of Contents Part I. The Basics 1. Introduction What Is a Microcontroller? The Big Picture A Computer on a Chip… …But a Very Small Computer What Can Microcontrollers Do? Hardware: The Big Picture The Core: Processor, Memory, and I/O Peripherals: Making Your Life Easier 2. Programming AVRs Programming the AVR Toolchain The Software Toolchain Linux Setup Windows Setup Mac Setup Arduino Setup Make and Makefiles AVR and the Arduino Arduino Pros Arduino Cons The Arduino: Hardware or Software? Both! The Arduino Is an AVR The Arduino Is an AVR Programmer Other Hardware Programmers Flash Programmers I Have Known and Loved Getting Started: Blinking LEDs Hookup ISP Headers AVRDUDE Configuring Your Makefile Flash Troubleshooting 3. Digital Output blinkLED Redux The Structure of AVR C Code Hardware Registers blinkLED Summary POV Toy Building the Circuit Pretty Patterns: The POV Toy Code Experiment! 4. Bit Twiddling Working Through the Code: Cylon Eyes Bit Twiddling and Cylon Eyes Bit Shifting Advanced Bit Twiddling: Above and Beyond Cylon Eyes Setting Bits with OR Toggling Bits with XOR Clearing a Bit with AND and NOT Showing Off Summary 5. Serial I/O Serial Communication Implementing Serial Communication on the AVR: Loopback Project Setup: Configuring the AVR Setup: Your Computer Setup: USB-Serial Adapter Putting It All Together: Test Out Your Loopback Troubleshooting Serial Connections Configuring USART: The Nitty-Gritty Details AVR Square-Wave Organ Making Music with Your Micro The Organ Library The Code Extra Goodies Summary 6. Digital Input Pushbuttons, Switches, Etc. Configuring Input: DDRs, PORTs, and PINs Interpreting Button Presses Changing State Debouncing Debounce Example AVR Music Box The Code Boss Button Desktop-side Scripting Extensions 7. Analog-to-Digital Conversion I ADC Hardware Overview Light Meter The Circuit The Code ADC Initialization Extensions Slowscope The AVR Code The Desktop Code Synergies AVR Night Light and the Multiplexer Multiplexing Setting the Mux Bits The Circuit The Code Summary Part II. Intermediate AVR 8. Hardware Interrupts External Interrupts 101: Real-time Button Pressing Examples External Interrupt 0 Example Pin-Change Interrupt Example Capacitive Sensor The Sensor The Code Global, Volatile Variables Debugging the Circuit 9. Introduction to the Timer/Counter Hardware Timer/Counters: Why and How? Test Your Reaction Time Using Timer 0 for a Better 8-Bit Organ AM Radio The Circuit CPU Speed AM Radio: The Code Summary 10. Pulse-Width Modulation Bright and Dim LEDs: PWM Brute-Force PWM Demo Timers PWM Demo Initializing Timers for PWM Mode PWM on Any Pin PWM on Any Pin Demo Closing: Alternatives to PWM and a Timer Checklist 11. Driving Servo Motors Servos The Secret Life of Servos The Circuit The Code Servo Sundial The Build Ready the Lasers! The Code Servo Sundial Calibration 12. Analog-to-Digital Conversion II Voltage Meter The Circuit The Code The Footstep Detector The Circuit The Theory Exponentially Weighted Moving Averages The Code Summary Part III. Advanced AVR Topics 13. Advanced PWM Tricks Direct-Digital Synthesis Making a Sine Wave Next Steps: Mixing and Volume Mixing Dynamic Volume Control Polling USART ADSR Envelope Auxiliary Files 14. Switches Controlling Big Loads: Switches Bipolar-Junction Transistors MOSFETs Power MOSFETs Relays Triacs and SSRs Switches: Summary DC Motors 15. Advanced Motors Going in Reverse: H-Bridges Code: Taking Your H-Bridge Out for a Spin Experts-Only H-Bridge PWM and the H-Bridge Drive Modes: Sign-Magnitude Drive Modes: Locked Anti-phase Drive Modes: Comparison Stepper Motors Kinds of Stepper Motors Full Stepping and Half Stepping Identification of Stepper Motor Wires Too Many Wires! Dual H-Bridge Chips: The SN754410 The Code Acceleration Control Microstepping 16. SPI How SPI Works Bit Trading Example Shift Registers EEPROM External Memory External Memory SPI Demo Hookup SPI Demo Code SPI EEPROM Library Header SPI EEPROM Library C Code initSPI SPI_tradeByte Convenience Functions Summary 17. I2C How I2C Works I2C Demo Hookup I2C Demo Library I2C Thermometer Demo SPI and I2C Data Logger Pointers in EEPROM The UART Serial Menu The Logger’s Event Loop 18. Using Flash Program Memory Using Flash Program Memory Memory Addresses The Address-Of Operator: & Pointers Pointers in Brief Pointers as Arguments to Functions Summary Optional: Dereferencing Pointers Talking Voltmeter PROGMEM Data Structures and the Header File Sound Playback and Voltage Reading: The .c File Generating the Audio Data Differential Pulse-Code Modulation Encoding Two-bit DPCM Encoding DPCM: wave2DPCM.py 19. EEPROM Using EEPROM Storing in Memory Reading from Memory Saving and Loading EEPROM Organizing Data in EEPROM Project: Vigenère Cipher Encoder/Decoder 20. Conclusion, Parting Words, and Encouragement Learning AVR: The Missing Chapters The Watchdog Timer Power Savings Crystals and Alternate Clock Sources Bootloaders Analog Comparator Debugging Put This Book Down and Build! About the Author Elliot Williams is a Ph.D. in Economics, a former government statistician, and a life-long electronics hacker. He was among the founding members of HacDC, Washington DC's hackerspace, and served as president and vice president for three years. He now lives in Munich, Germany, where he works for an embedded hardware development firm that has, to date, exactly one employee (and CEO). This book came out of his experiences teaching AVR programming workshops at HacDC. Paperback; 472 pages
|