- •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
Projects
Grab your joystick – and test your interrupts
Create a new directory Joystick and copy the PC_comm. and Demonstrator .c and
.h files from the PC_comm. directory.
In Programmers Notepad change Demonstrator.h to:
// Demonstrator.h Joystick version
#include <avr/signal.h> #include <inttypes.h>
#define KEY_UP |
0 |
|
#define KEY_DOWN |
1 |
|
#define KEY_LEFT |
2 |
|
#define KEY_RIGHT |
3 |
|
#define KEY_PUSH |
4 |
|
#define KEY_INVALID 5 |
|
|
#define BUTTON_A |
6 |
// UP |
#define BUTTON_B |
7 |
// DOWN |
#define BUTTON_C |
2 |
// LEFT |
#define BUTTON_D |
3 |
// RIGHT |
#define BUTTON_O |
4 |
// PUSH |
#define PINB_MASK ((1<<PINB4)|(1<<PINB6)|(1<<PINB7)) #define PINE_MASK ((1<<PINE2)|(1<<PINE3))
#define TRUE 1 #define FALSE 0
// declare functions
void PinChangeInterrupt(void); char getkey(void);
void initializer(void); void parseInput(char *); void joystick(void);
Open the Demonstrator.c and change it to:
114
Chapter 7: Microcontroller Interrupts and Timers
// Demonstrator.c Joystick version
#include "PC_Comm.h" #include "Demonstrator.h"
// declare global variables volatile char KEY = 0; volatile char KEY_VALID = 0; volatile char ENABLED = 0;
void initializer()
{
//Calibrate the oscillator: OSCCAL_calibration();
//Initialize the USART USARTinit();
//Init port pins DDRB |= 0xD8; PORTB |= PINB_MASK; DDRE = 0x00;
PORTE |= PINE_MASK;
//Enable pin change interrupt on PORTB and PORTE PCMSK0 = PINE_MASK;
PCMSK1 = PINB_MASK; EIFR = (1<<6)|(1<<7); EIMSK = (1<<6)|(1<<7);
DDRD = 0xFF; // set PORTD for output
DDRB = 0X00; // set PORTB for input
PORTB = 0xFF; // enable pullup on for input
PORTD = 0XFF; // set LEDs off
// say hello
sendString("\rPC_Comm.c ready to communicate.\r"); // identify yourself specifically
sendString("You are talking to the JoyStick demo.\r");
}
void parseInput(char s[])
{
115
Chapter 7: Microcontroller Interrupts and Timers
// parse first character switch (s[0])
{
case 'j':
if( (s[1] == 'o') && (s[2] == 'y')) joystick();
break; case 'd':
if((s[1]=='e')&&(s[2]=='m')&&(s[3]=='o')&&(s[4]=='?') ) sendString("You are talking to the JoyStick
demo.\r");
break;
default:
sendString("\rYou sent: '"); sendChar(s[0]);
sendString("' - I don't understand.\r"); break;
}
s[0] = '\0';
}
void joystick()
{
if(ENABLED == 0) ENABLED = 1; else ENABLED = 0;
}
SIGNAL(SIG_PIN_CHANGE0)
{
PinChangeInterrupt();
}
SIGNAL(SIG_PIN_CHANGE1)
{
PinChangeInterrupt();
}
void PinChangeInterrupt(void)
{
char buttons; char key;
buttons = (~PINB) & PINB_MASK; buttons |= (~PINE) & PINE_MASK;
116
Chapter 7: Microcontroller Interrupts and Timers
// Output virtual keys
if (buttons & (1<<BUTTON_A))
key |
= KEY_UP; |
|
else if |
(buttons & (1<<BUTTON_B)) |
|
key |
= KEY_DOWN; |
|
else if |
(buttons & (1<<BUTTON_C)) |
|
key |
= KEY_LEFT; |
|
else if |
(buttons & (1<<BUTTON_D)) |
|
key |
= KEY_RIGHT; |
|
else if |
(buttons & (1<<BUTTON_O)) |
|
key = KEY_PUSH; |
|
|
else |
|
|
key = KEY_INVALID; |
|
|
if(key != KEY_INVALID) |
|
|
{ |
|
|
if (!KEY_VALID) |
|
|
{ |
KEY = key; |
// Store key in global key buffer |
|
||
} |
KEY_VALID = TRUE; |
|
|
|
|
} |
|
|
//Delete pin change interrupt flags EIFR = (1<<PCIF1) | (1<<PCIF0);
if(ENABLED)
{
getkey();
}
}
char getkey(void)
{
char k;
cli(); // disable interrrupts so 'KEY' won't change while in
use
if (KEY_VALID) // Check for unread key in buffer
{
k = KEY; KEY_VALID = FALSE;
}
else
117