## Table of contents for Embedded C programming and the Atmel AVR / Richard Barnett, Larry O'Cull, Sarah Cox.

Bibliographic record and links to related information available from the Library of Congress catalog.

Note: Contents data are machine generated based on pre-publication provided by the publisher. Contents may have variations from the printed book or be incomplete or contain other coding.

```CONTENTS
PREFACE	xiii
INTRODUCTION	xxi
CHAPTER 1 EMBEDDED C LANGUAGE TUTORIAL
1.1 OBJECTIVES	1
1.2 INTRODUCTION	1
1.3 BEGINNING CONCEPTS	2
1.4 VARIABLES AND CONSTANTS 	4
1.4.1 Variable Types	4
1.4.2 Variable Scope	5
Local Variables 	5
Global Variables	5
1.4.3 Constants	6
Numeric Constants	7
Character Constants	7
1.4.4 Enumerations and Definitions	7
1.4.5 Storage Classes	9
Automatic	9
Static	9
Register	9
1.4.6 Type Casting	9
1.5 I/O OPERATIONS	11
1.6 OPERATORS AND EXPRESSIONS	12
1.6.1 Assignment and Arithmetic Operators	12
Bitwise Operators	13
1.6.2 Logical and Relational Operators	15
Logical Operators	15
Relational Operators	15
1.6.3 Increment, Decrement, and Compound Assignment	16
Increment Operators	16
Decrement Operators	17
Compound Assignment Operators 	17
1.6.4 The Conditional Expression	17
1.6.5 Operator Precedence	18
1.7 CONTROL STATEMENTS	19
1.7.1 While Loop	19
1.7.2 Do/While Loop	21
1.7.3 For Loop	22
1.7.4 If/Else	23
If Statement	23
If/Else Statement	24
Conditional Expression	26
1.7.5 Switch/Case	26
1.7.6 Break, Continue, and Goto 	28
Break	28
Continue	29
Goto	29
1.8 FUNCTIONS	33
1.8.1 Prototyping and Function Organization	34
1.8.2 Functions that Return Values	36
1.8.3 Recursion	37
1.9 POINTERS AND ARRAYS	41
1.9.1 Pointers	42
1.9.2 Arrays	46
1.9.3 Multidimensional Arrays	48
1.9.4 Pointers to Functions	50
1.10 STRUCTURES AND UNIONS	55
1.10.1 Structures	55
1.10.2 Arrays of Structures	57
1.10.3 Pointers to Structures	58
1.10.4 Unions	59
1.10.5 Typedef Operator	61
1.10.6 Bits and Bitfields	62
1.10.7 Sizeof Operator	63
1.11 MEMORY TYPES	64
1.11.1 Constants and Variables	64
1.11.2 Pointers	66
1.11.3 Register Variables	66
sfrb and sfrw	67
1.12 REAL-TIME METHODS	70
1.12.1 Using Interrupts	70
1.12.2 State Machines	73
1.13 CHAPTER SUMMARY	79
1.14 EXERCISES	79
1.15 LABORATORY ACTIVITIES	81
CHAPTER 2 THE ATMEL RISC PROCESSORS
2.1 OBJECTIVES	83
2.2 INTRODUCTION	83
2.3 ARCHITECTURAL OVERVIEW	84
2.4 MEMORY	85
2.4.1 FLASH Code Memory	85
2.4.2 Data Memory	85
Registers	86
I/O Registers	86
SRAM	88
2.4.3 EEPROM Memory	90
2.5 RESET AND INTERRUPT FUNCTIONS	92
2.5.1 Interrupts	93
2.5.2 Reset	97
Watchdog Timer and Reset	98
2.6 PARALLEL I/O PORTS	100
2.7 TIMER/COUNTERS	104
2.7.1 Timer/Counter Prescalers and Input Selectors	105
2.7.2 Timer 0 	105
2.7.3 Timer 1 	109
Timer 1 Prescaler and Selector	110
Timer 1 Input Capture Mode	110
Timer 1 Output Compare Mode	114
Timer 1 Pulse Width Modulator Mode	118
2.7.4 Timer 2 	123
Chapter 2 Example Project: Part C	125
One-second recording interval using Timer 0	125
Engine rpm measurement using Timer 1	126
Drive shaft rpm measurement using Timer 1	127
2.8 SERIAL COMMUNICATION USING THE UART	128
2.9.1 Analog to Digital Background	137
2.9.2 Analog-to-Digital Converter Peripheral	138
2.9.3 Analog Comparator Peripheral	142
Measuring engine temperature using the analog-to-digitalconverter
Sending collected data to the PC	145
2.10 SERIAL COMMUNICATION USING THE SPI	146
2.11 THE AVR RISC ASSEMBLY LANGUAGEINSTRUCTION SET	150
2.12 CHAPTER SUMMARY	154
2.13 EXERCISES	158
2.14 LABORATORY ACTIVITIES	159
CHAPTER 3 STANDARD I/O AND PREPROCESSOR FUNCTIONS
3.1 OBJECTIVES	161
3.2 INTRODUCTION	161
3.3 CHARACTER INPUT/OUTPUT FUNCTIONS - GETCHAR() AND PUTCHAR()
162
3.4 STANDARD OUTPUT FUNCTIONS	168
3.4.1 Put String - puts()	168
3.4.2 Put String FLASH - putsf() 	169
3.4.3 Print Formatted - printf()	169
3.4.4 String Print Formatted - sprintf() 	172
3.5 STANDARD INPUT FUNCTIONS	173
3.5.1 Get String - gets() 	173
3.5.2 Scan Formatted - scanf() 	174
3.5.3 Scan String Formatted - sscanf() 	176
3.6 PREPROCESSOR DIRECTIVES	177
3.6.1 The #include Directive	177
3.6.2 The #define Directive	178
3.6.3 The #ifdef, #ifndef, #else and #endif Directives	180
3.6.4 The #pragma Directive	181
#pragma warn	181
#pragma opt	186
#pragma optsize	186
#pragma savereg	187
#pragma regalloc 	188
#pragma promotechar 	188
#pragma uchar	189
#pragma library	189
3.6.5 Other Macros and Directives	189
3.7 CHAPTER SUMMARY	190
3.8 EXERCISES	191
3.9 LABORATORY ACTIVITIES	191
CHAPTER 4 THE CODEVISIONAVR C COMPILER AND IDE
4.1 OBJECTIVES	193
4.2 INTRODUCTION	193
4.3 IDE OPERATION	194
4.3.1 Projects	194
Open Existing Projects	194
Create New Projects	195
Configure Projects	196
Close Project	197
4.3.2 Source Files	197
Open an Existing Source File	197
Create a New Source File	197
Add an Existing File to the Project	197
4.3.3 Edit Files	199
4.3.4 Print Files	202
4.3.5 The File Navigator	202
4.4 C COMPILER OPTIONS	204
4.4.1 UART	206
4.4.2 Memory Model	206
4.4.3 Optimize For	206
4.4.4 Program Type	206
4.4.5 SRAM	207
4.4.6 Compilation	207
4.5 COMPILE AND MAKE PROJECTS	208
4.5.1 Compile a Project	208
4.5.2 Make a Project	209
4.6 PROGRAM THE TARGET DEVICE	211
4.6.1 Chip	211
4.6.2 FLASH and EEPROM	211
4.6.3 FLASH Lock Bits	214
4.6.4 Fuse Bits	214
4.6.5 Boot Lock Bit 0 and Boot Lock Bit 1	214
4.6.6 Signature	214
4.6.7 Chip Erase	215
4.6.8 Programming Speed	215
4.6.9 Program All	215
4.6.10 Other Programmers	215
4.7 CODEWIZARDAVR CODE GENERATOR	218
4.7.1 Chip Tab	219
4.7.2 Ports Tab	220
4.7.3 External IRQ Tab	221
4.7.4 Timers Tab	222
4.7.5 UART Tab	223
4.7.7 Project Information Tab	225
4.7.8 Generate Source Code	227
4.8 TERMINAL TOOL	239
4.9 THE ATMEL AVR STUDIO DEBUGGER	240
4.9.1 Create a COFF File for AVR Studio	241
4.9.2 Launch AVR Studio from CodeVisionAVR	241
4.9.3 Open a File for Debug	241
4.9.4 Start, Break, and Step	242
4.9.5 Set and Clear Breakpoints	242
4.9.6 View and Modify Registers and Variables	242
4.9.7 View and Modify the Machine State	243
4.10 CHAPTER SUMMARY	244
4.11 EXERCISES	245
4.12 LABORATORY ACTIVITIES	246
CHAPTER 5 PROJECT DEVELOPMENT
5.1 OBJECTIVES	247
5.2 INTRODUCTION	247
5.3 CONCEPT DEVELOPMENT PHASE	247
5.4 PROJECT DEVELOPMENT PROCESS STEPS	247
5.4.1 Definition Phase	248
5.4.2 Design Phase	249
5.4.3 Test Definition Phase	252
5.4.4 Build and Test the Prototype Hardware Phase	252
5.4.5 System Integration and Software Development Phase	253
5.4.6 System Test Phase	253
5.4.7 Celebration Phase	253
5.5 PROJECT DEVELOPMENT PROCESS SUMMARY	253
5.6 EXAMPLE PROJECT: A WEATHER MONITOR	254
5.6.1 Concept Phase	254
5.6.2 Definition Phase	255
Electrical Specification	256
Operational Specification	257
Basic Block Diagrams	258
5.6.3 Measurement Considerations for the Design	260
Temperature	260
Barometric Pressure	263
Humidity	264
Wind Speed	264
Wind Direction	267
Rainfall	268
Dew Point Computation	272
Wind Chill Computation	273
Battery Health	273
Real Time	274
5.6.4 Hardware Design, Outdoor Unit	275
Wind Speed Input	275
Rain Gauge Input	275
900 MHz Transmitter	277
Power Supply	277
5.6.5 Software Design, Outdoor Unit 	277
5.6.6 Hardware Design, Indoor Unit	278
Power Supply	281
5.6.7 Software Design, Indoor Unit	281
5.6.8 Test Definition Phase	283
Wind Direction	284
Wind Speed	284
Rain Gauge	284
Air Temperature	284
Barometric Pressure	284
Relative Humidity	285
System Test for Complete Project	285
5.6.9 Build and Test Prototype Hardware Phase	285
Outdoor Unit Checkout	286
Indoor Unit Checkout	287
5.6.10 System Integration and Software Development Phase, Outdoor
Unit	292
Temperature, Humidity, Wind Direction, and Battery Health	293
Rainfall 	293
Wind Speed	296
RF Telemetry	297
5.6.11 System Integration and Software Development Phase, Indoor
Unit	302
Keeping Time	302
Low Battery Indication	305
The Buttons and the Beeper	306
Decoding the RF Telemetry	308
Collecting and Protecting Rainfall Data	312
Converting from Counts to Real Units	315
Routines for Controlling the LCD	319
Keeping the Display Up to Date	323
Editing the Time and Date	327
5.6.12 System Test Phase	331
5.7 CHALLENGES	335
5.8 CHAPTER SUMMARY	336
5.9 EXERCISES	336
5.10 LABORATORY ACTIVITY	337
APPENDIX A LIBRARY FUNCTIONS REFERENCE	339
APPENDIX B GETTING STARTED WITH
CODEVISIONAVR AND THE STK500	427
APPENDIX C PROGRAMMING THE AVR
MICROCONTROLLERS	445
APPENDIX D INSTALLING AND USING THECABLEAVR	449
APPENDIX E THE MEGAAVR-DEV DEVELOPMENT BOARD	461
APPENDIX F ASCII CHARACTER TABLE	469
APPENDIX G AVR INSTRUCTION SET SUMMARY	475
APPENDIX H ANSWERS TO SELECTED EXERCISES	477
INDEX	483
```

Library of Congress Subject Headings for this publication:

C (Computer program language).
Embedded computer systems -- Design and construction.
RISC microprocessors.