- •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 6: C Functions and Program Structures
Projects
Is anybody out there? Communicating with a PC
Most microcontrollers are buried deep in some device where they run in merry isolation from the rest of the world. Their programs are burned into them and never change. But there are many instances when we might want to communicate with a microcontroller. The Butterfly uses a joystick and an LCD, which is fine for its built-in applications. For anything more complex, like changing the microcontroller software, nothing beats using the PC’s RS232 serial communications port to communicate with the microcontroller through its Universal Synchronous Asynchronous Receiver Transmitter, USART, peripheral. The microcontroller and the PC must agree on the transmission speed in data bits per second, Baud rate, the number of bits per data unit, Data Bits, the parity of the data, Parity, the number of stop bits, Stop Bits, and Flow Control. (Refer to Constructing Your Development System section of Chapter 2 for the required settings) All this information is somewhat arcane and is legacy from even before Teletype machines. Fortunately the USART takes care of most of this stuff for you, so you don’t need to understand it. If you are really interested, get Jan Axelson’s Serial Port Complete (www.lvr.com).
What we need is a method to send commands and data from the PC and receive responses from the Butterfly. In this section we will develop a generic command interpreter skeleton that we will reuse in later programs. In this project we will use this skeleton to build a demonstration that let’s the PC send a command name and a number to the Butterfly. The Butterfly will respond with text.
We will put this software in four files:
PC_Comm.h
PC_Comm.c
Demonstrator.h
Demonstrator.C
The PC_Comm files have many things in them that are well beyond our C training at this point, so just copy them and don’t think too hard about it yet. We will revisit each function later as we increase our knowledge. You should have no
98
Chapter 6: C Functions and Program Structures
trouble understanding anything in the Demonstrator files. If you do, review. In future projects we will only need to make changes to Demonstrator.h and Demonstrator.c.
Demonstrator
Create a new PC Comm directory and in Programmer’s Notepad open a new C/C++ file and write:
// Demonstrator.h CommDemo version
void initializer(void);
void parseInput(char *);
void Comm1(char *); void Comm2(char *); void Comm3(char *); void Comm4(char *);
void responder(char *, char );
Save this file as Demonstrator.h.
In Programmer’s Notepad open a new C/C++ file and write:
// Demonstrator.c PC Comm version
#include "PC_Comm.h"
void initializer()
{
//Calibrate the oscillator: OSCCAL_calibration();
//Initialize the USART USARTinit();
//say hello
sendString("\rPC_Comm.c ready to communicate.\r"); // identify yourself specifically
sendString("\rYou are talking to the PC_Comm demo.\r");
}
void parseInput(char s[])
99
Chapter 6: C Functions and Program Structures
{
// parse first character switch (s[0])
{
case 'c':
if( (s[1] == 'o') && (s[2] == 'm') && (s[3] == 'm') ) switch (s[4]) // parse the fifth character
{
case 'a': Comm1(s); break;
case 'b': Comm2(s); break; case 'c': Comm3(s); break;
case 'd': Comm4(s); break; default:
sendString("\rYou sent: '"); sendChar(s[0]);
sendString("' - I don't understand.\r");
}
break; case 'd':
if( (s[1] == 'e') && (s[2] == 'm') && (s[3] == 'o') && (s[4] == '?') ) sendString("You are talking to the PC_Comm demo.\r");
break; case 'h':
if( (s[1] == 'e') && (s[2] == 'l') && (s[3] == 'l') && (s[4] == 'o') ) sendString("Hello yourself\r");
break;
default:
sendString("\rYou sent: '"); sendChar(s[0]);
sendString("' - I don't understand.\r"); break;
}
s[0] = '\0';
}
void Comm1(char s[])
{
responder(s,s[4]);
}
void Comm2(char s[])
{
responder(s,s[4]);
100
Chapter 6: C Functions and Program Structures
}
void Comm3(char s[])
{
responder(s,s[4]);
}
void Comm4(char s[])
{
responder(s,s[4]);
}
void responder(char s[], char c)
{
char sComm[11];
unsigned char i = 5, j = 0;
while( (s[i] != '\0') && (j <= 11) )
{
if( (s[i] >= '0') && (s[i] <= '9') )
{
sComm[j++] = s[i++];
}
else
{
sendString("Error - Comm"); sendChar(c);
sendString(" received a non integer: "); sendChar(s[i]);
sendChar('\r');
}
}
sComm[j] = '\0';
if(j>11)
{
sendString("Error - Comm"); sendChar(c);
sendString(" number too large\r"); sendChar('\r');
}
else
{
sendString("\rThank you for sending the number: "); sendString(sComm);
sendChar('\r');
}
}
101