- •Chapter 1: Introduction
- •Goals
- •Chapter 2: Quick Start Guide
- •Software
- •WinAVR – Oh, Whenever…
- •Programmers Notepad
- •AVRStudio – FREE and darn well worth it.
- •Br@y++ Terminal:
- •Hardware
- •Constructing Your Development Platform
- •Blinking LEDs – Your First C Program
- •Write it in Programmers Notepad
- •Download to the Butterfly with AVRStudio
- •Blinky Goes Live
- •Simulation with AVRStudio
- •GOOD GRIEF!
- •Comments
- •Include Files
- •Expressions, Statements, and Blocks
- •Operators
- •Flow Control
- •Functions
- •The Main() Thing
- •Chapter 4: C Types, Operators, and Expressions
- •Data Types and Sizes
- •Seen on a shirt at a Robothon event:
- •Bits
- •Bytes
- •The long and short of it
- •Variable Names
- •Constants
- •Declarations
- •Arithmetic Operators
- •Relational and Logical Operators
- •Bitwise Operators
- •Testing Bits
- •Assignment Operators and Expressions
- •Conditional Expressions
- •Precedence and Order of Evaluation
- •Projects
- •Port Input and Output
- •Cylon Eye Speed and Polarity Control
- •Chapter 5: C Control Flow
- •Statements and Blocks
- •If-Else and Else-If
- •Switch
- •Loops – While, For and Do-while
- •Break and Continue
- •Goto and Labels
- •A few practical examples: strlen, atoi, itoa, reverse
- •Chapter 6: C Functions and Program Structures
- •Function Basics
- •Returns
- •Variables External, Static, and Register
- •Scope
- •Headers
- •Blocks
- •Initialization
- •Recursion
- •Preprocessor
- •Macro Substitution
- •Conditional Inclusion
- •Projects
- •Is anybody out there? Communicating with a PC
- •Demonstrator
- •PC_Comm
- •Using CommDemo:
- •Chapter 7: Microcontroller Interrupts and Timers
- •Interrupts
- •Projects
- •Grab your joystick – and test your interrupts
- •Using joystick
- •Timers/Counters
- •Calibrating the Butterfly oscillator:
- •OSCCAL_calibration() function – detailed explanation
- •ALL THIS AND WE HAVEN’T EVEN STARTED CALIBRATING YET!
- •Projects
- •Precision Blinking
- •Using Precision Blinking:
- •Pulse Width Modulation – LED Brightness Control
- •Pulse Width Modulation - Motor Speed Control
- •Speedometer
- •Chapter 8: C Pointers and Arrays
- •Addresses of variables
- •Function Arguments
- •Arrays
- •FIFOs and LIFOs: Stacks and Queues (Circular Buffers)
- •Stacks
- •Queues (Circular Buffers)
- •Function Pointers
- •Complex Pointer and Array Algorithms
- •Projects
- •Messenger
- •Arrays in RAM and ROM
- •Does anybody know what time it is? A Real Time Clock.
- •A one second interrupt
- •Converting Computer Time to Human Readable Time
- •The Real Timer Clock Software
- •Music to my ears. “Play it again Sam.”
- •More on pointers to arrays
- •Setting the frequency
- •Setting the duration
- •An example song array – Fur Elise
- •Using the Piezo-element to make sound
- •Initializing the Timer1 for PWM to the piezo-element.
- •Generating the tone using PWM from Timer1
- •Using the Timer0 interrupt to play a tune
- •Chapter 9 – Digital Meets Analog – ADC and DAC
- •But First - A Debugging Tale
- •Analog to Digital Conversion
- •What is Analog to Digital Conversion?
- •Analog to Digital Conversion by Successive Approximation
- •Analog to Digital Conversion with the ATMEGA169
- •Starting a Conversion
- •Conversion Timing
- •Changing Channels
- •Digital Noise Reduction
- •Conditioning the Analog Input Signal
- •Accuracy
- •Projects
- •Initializing the ADC
- •Reading the ADC
- •Light Meter
- •Temperature Meter
- •The @#%#&*#!!!! Volt Meter
- •Using ADC
- •DAC and ADC - Function Generator / Digital Oscilloscope
- •Chapter 10: C Structures
- •Structure Basics
- •Structures and Functions
- •Structure Arrays
- •Typedef
- •Unions
- •Bit-fields
- •Bit-Fields the C-way
- •Bit-fields the masking-way
- •Projects
- •Finite State Machine
- •Chapter 11 The Butterfly LCD
- •PC to LCD test program
- •Conclusion
- •Appendix 1: Project Kits
- •Data I/O
- •PWM Motor Control
- •Appendix 2: Soldering Tutorial
- •Appendix 3: Debugging Tale
- •Appendix 4: ASCII Table
- •Appendix 5: Decimal, Hexadecimal, and Binary
- •Appendix 6: Motor Speed Control Wheel
- •Appendix 7: HyperTerminal
- •Index
Chapter 2: Quick Start Guide
# Target file name (without extension). TARGET = main
•To:
MCU = atmega169
#Output format. (can be srec, ihex, binary) FORMAT = ihex
#Target file name (without extension). TARGET = Blinky
•Close and save changes to makefile to the Blinky directory.
•Open Tools and click [WinAVR] Make All to make your Blinky.hex file
•Open Tools and click [WinAVR] Make Extcoff to make your Blinky_coff file.
Download to the Butterfly with AVRStudio
• Find AVR Studio (you should have an icon for it on your desktop) and open it.
•In the File menu Open ‘…\Blinky\Blinky.cof
•Select the AVR Simulator and the ATMEGA169 as:
31
Chapter 2: Quick Start Guide
•Select Finish
•DO NOT try to run the simulation; the delay loop will take forever to run. We’ll use the simulator later.
•Turn the Butterfly off.
•Press and hold down the joystick button.
•Back to the AVR Studio, open the Tools menu and WHILE HOLDING DOWN THE JOYSTICK BUTTON click the AVR Prog menu item. Then wait until you see:
32
Chapter 2: Quick Start Guide
•Release the joystick button. Your finger hurts doesn’t it? Enter Blinky.hex in the ‘Hex file’ box. Press the program button and the program should magically flow from your PC into the AVR Butterfly Flash memory.
•AVR Prog will say: Erasing Programming Verifying OK.
•WHEN YOU WANT TO DOWNLOAD A DIFFERENT HEX FILE, DON’T FORGET TO CHANGE THE HEX FILE NAME. DON’T SAY I DIDN’T WARN YOU AFTER YOU WASTE TIME SCRATCHING YOUR HEAD OVER WHY YOUR NEW PROGRAM SEEMS TO RUN EXACTLY LIKE THE LAST ONE YOU DOWNLOADED. I make this mistake a lot.
•If instead of the above window you get:
•Go back a few steps and try again. You probably left Bray’s Terminal running so it has locked the port. Then maybe not.
Blinky Goes Live
•Turn the power supply off and then back on, the LCD will be blank, click the joystick up (maybe a couple of times) and:
33
Chapter 2: Quick Start Guide
•Your LEDs should be making like a Cylon with the light bouncing back and forth. If you don’t know what a Cylon is, try Googling Battlestar Galactica, not that I’m recommending the series, but the bad guys had great eyes:
Figure 11: From the cover of the Battlestar Galactica comic Red Cylon.
When you compiled Blinky.c you may have suspected that a lot of stuff was going on in the background, and you would have been right. The compiler does a lot of things, and fortunately for us, we don’t really need to know how it does what it
34