- •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 4: C Types, Operators, and Expressions
unsigned
If the modifier unsigned is used in the definition of a char variable: ‘unsigned char’, the value is from 0 to 255. Many C compilers will have ‘byte’ or ‘Byte’ defined as equaling ‘unsigned char’. The ‘byte’ keyword is not part of C, but it is very convenient, since in microcontrollers we usually use a lot of numbers, but not a lot of ‘char’acters.
int
On AVR microcontrollers int declares a 16 bit data variable as having values from –32768 to +32767. A variable declared with ‘unsigned int’ will have a value from 0 to 65535.
The long and short of it
Everybody else makes that dumb joke at this point, so why be different?
You can declare variables as ‘short int’ and ‘long int’. For C the size is machine dependent, but on many systems a short int is the same as an int, 16 bits, while a long int is 32 bits.
Variable Names
The changeable data you are processing is stored in bytes of RAM, Random Access Memory, at specific addresses. Variables are names that provide an alias for the address being used. We’ll look at the gory details in the ‘Variables External, Static, and Register’ section of.
Constants
Constants are data that cannot be changed by the program and are usually stored in ROM, Read Only Memory. We could just type in the constant value wherever needed, but that will get old quick, so we alias the value with a name. We usually do this in a header file or at the start of the software module, which adds the advantage that if we ever want to change the constant we can do it once in the definition instead of at each occurrence in the code. By convention, constant names are all caps. For example we might want to use pi in calculation (pi containts a decimal so we use the float data type) so we define as follows:
#define PI 3.1415926
49
Chapter 4: C Types, Operators, and Expressions
We can then use PI anywhere in our software and the compiler will automatically substitute the numerical value for it:
float pieCircumference = 0.0; float piePanRadius = 0.0;
pieCircumference = PI * (piePanRadius^2);
Declarations
A declaration is a text statement that declares to the complier how your words are to be used. When you declare ‘unsigned char counter = 0’ you are telling the compiler that when it encounters the word ‘counter’ to consider it as data stored at some specific location with the alias name ‘counter’ that can have values from 0 to 255, but in this case initially has a value of 0.
Arithmetic Operators
Operators seem like ordinary arithmetic or algebra symbols, and they mostly are. But they are different from arithmetic or algebra often enough that you need to pay attention when operations don’t act like you think they should. The compiler might just be doing what you told it to do, rather than what you wanted it to do. An example of the kind of confusion you can run into when you use the ‘=’ assignment operator and the ‘==’ ‘is equal to’ operator:
x = y;
if(x==y) _delay_loop_2(30000);
The first statement assigns x to the value of y. The second calls the _delay_loop_2(30000) function if x is equal to y. What about:
if(x=y) _delay_loop_2(30000); //BAD STATEMENT
This will set x equal to y, and then call the _delay_loop_2(30000) function. The ‘if’ becomes meaningless because the condition, x=y, is always true, so the delay will always run. The WinAVR compiler will think something is strange and issue this warning:
Warning: suggest parentheses around assignment used as truth value
50
Chapter 4: C Types, Operators, and Expressions
Which will scroll by so fast you won’t see it, so you’ll assume the compile was good. Notice how clear (NOT) this warning was? Most complier warnings are even more cryptic. Not all compilers will flag this error with a warning. It is a very easy mistake to make, and you will feel really dumb after an hour of debugging, looking for something obscure, only to find a lousy missing ‘=’ character. I do this all the time.
Note: Some of these operators may seem strange at this point, but they are explained fully in later sections. Then they’ll seem really strange.
Table 1: Arithmetic Operators
Operator |
Name |
Example |
Defined |
* |
Multiplication |
x*y |
Multiply x times y |
/ |
Division |
x/y |
Divide x by y |
% |
Modulo |
x%y |
Provide the remainder of x divided by y |
+ |
Addition |
x+y |
Add x and y |
- |
Subtraction |
x-y |
Subtract y from x |
++ |
Increment |
x++ |
Increment x after using it |
-- |
Decrement |
--x |
Decrement x before using it |
- |
Negation |
-x |
Multiply x by –1 |
+ |
Unary Plus |
+x |
Show x is positive (not really needed) |
Table 2: Data Access and Size Operators
Operator |
Name |
Example |
Defined |
[] |
Array element |
x[6] |
Seventh element of array x |
. |
Member selection |
PORTD.2 |
Bit 2 of Port D |
-> |
Member selection |
pStruct->x |
Member x of the structure pointed to |
|
|
|
by pStruct |
* |
Indirection |
*p |
Contents of memory located at |
|
|
|
address p |
& |
Address of |
&x |
Address of the variable x |
51
Chapter 4: C Types, Operators, and Expressions
Table 3: Miscellaneous Operators
Operator |
Name |
Example |
Defined |
() |
Function |
wait(10) |
call wait with an argument of 10 |
(type) |
Type cast |
(double)x |
x converted to a double |
?: |
Conditional |
x?y:z |
If x is not 0 evaluate y, otherwise evaluate |
|
|
|
z |
, |
Sequential |
x++,y++ |
Increment x first, then increment y |
|
evaluation |
|
|
Relational and Logical Operators
Table 4: Logical and Relational Operators
Operator |
Name |
Example |
Defined |
> |
Greater than |
x>y |
1 if x is greater than y, otherwise 0 |
>= |
Greater than |
x>=y |
1 if x is greater than or equal to y, |
|
or equal to |
|
otherwise 0 |
< |
Less than |
x<y |
1 if x is less than y, otherwise 0 |
<= |
Less than or |
x<=y |
1 if x is less than or equal to y, otherwise |
|
equal to |
|
0 |
|
|
|
|
== |
Equal to |
x==y |
1 if x equals y, otherwise 0 |
!= |
Not equal to |
x!=y |
1 if x is not equal to y, otherwise 0 |
! |
Logical NOT |
!x |
1 if x is 0, otherwise 0 |
&& |
Logical AND |
x&&y |
0 if either x or y is 0, otherwise 1 |
|| |
Logical OR |
x||y |
0 if both x and y are 0, otherwise 1 |
52