- •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 7: Microcontroller Interrupts and Timers
You are talking to the Joystick demo.
Type in: joy
Move the joystick to the left and you should receive:
The joystick position is: LEFT
Move the joystick to the right and you should receive:
The joystick position is: RIGHT
Move the joystick up and you should receive:
The joystick position is: UP
Move the joystick down and you should receive:
The joystick position is: DOWN
Push the joystick while centered t and you should receive:
The joystick position is: PUSH
I’m tired and going to bed. Tomorrow we’ll look at timers. I may get so excited that I won’t be able to sleeeeeppp ummm errr zzzzzz….
Timers/Counters
Good morning! In Blinky.c we set the timing of the blinks using the _delay_loop_2(delaycount). The delay function uses a 16-bit count that takes 4 cycles/loop. This loop runs in the CPU, which can do nothing else while it is running. You set the period of the delay by sending a parameter for the number of ‘4 cycles’ you want to waste. Knowing the time per cycle allows you to set the time of the delay. Cycle wasting delays are a simple way to control some types of periodic events, but the simplicity comes at the cost of totally occupying the CPU while wasting the specified time. That’s a good idea if you don’t need to do anything else while the delay is running, but it makes a lousy way to mark time if you have anything else going on. Timers are peripheral devices that run independent of the CPU and only bother the CPU when set up to do so. The bothering can take the form of setting a flag that the CPU can poll, or throwing an interrupt to break into normal operations.
119
Chapter 7: Microcontroller Interrupts and Timers
The reason we usually see Timer/Counter hooked together is that the Timer/Counter peripheral keeps time by counting pulses. The pulses can come from a synchronous periodic source providing an accurate time count, or the pulses can come from an asynchronous non-periodic source providing an accurate count of the input pulses. In the first case we could be counting pulses from the 32.768 kHz watch crystal and keep accurate time. In the second case we could be counting pulses from a light beam interrupter circuit and keep an accurate count of the number of people entering a door and breaking the light beam.
The ATmega169 has three Timer/Counters, two 8 bit and one 16 bit. A timer overflows when it counts up to its maximum value (255 for the 8 bit and 65535 for the 16 bit devices) and resets to 0. We can get the Timer to overflow at lower values by putting a value in the OCR, Output Compare Register, for the specified timer and that timer will compare the value with the count and when they match it will set a flag or throw an interrupt. It can also be set to overflow to 0 on a match.
The timers can be configured for input capture events, where a change on a pin will cause the timer to save the count when the event occurred. This input capture count can be used to measure the width of external pulse. If the external pulses are periodic, we have a frequency counter.
The Timer/Counter runs independent of program execution and there are three ways for the program to monitor and react to Timer/Counter events.
1.Poll the overflow flags.
2.Break program execution with an interrupt.
3.Let the timer automatically change the level of output pins.
The clock of the Timer/Counters uses a prescaler connected to a multiplexer The prescaler is used to divide the input clock and the multiplexer selects which of the divided signals is used as the input clock. The clock source for the prescaler can be an external clock such as the 32.768 kHz crystal or it can use the system clock.
120