Introductory Microcontroller Programming
Total Page:16
File Type:pdf, Size:1020Kb
Introductory Microcontroller Programming by Peter Alley A Thesis Submitted to the Faculty of the WORCESTER POLYTECHNIC INSTITUTE in partial fulfillment of the requirements for the Degree of Master of Science in Robotics Engineering May 2011 Prof. William Michalson Advisor Prof. Taskin Padir Prof. Susan Jarvis Committee member Committee member Abstract This text is a treatise on microcontroller programming. It introduces the ma- jor peripherals found on most microcontrollers, including the usage of them, focusing on the ATmega644p in the AVR family produced by Atmel. Gen- eral information and background knowledge on several topics is also presented. These topics include information regarding the hardware of a microcontroller and assembly code as well as instructions regarding good program structure and coding practices. Examples with code and discussion are presented throughout. This is intended for hobbyists and students desiring knowledge on programming microcontrollers, and is written at a level that students entering the junior level core robotics classes would find useful. Contents Preface i 1 What is a Microcontroller? 1 1.1 Micro-processors, -computers, -controllers . .1 1.1.1 Microprocessor . .1 1.1.2 Microcomputer . .2 1.1.3 Microcontroller . .2 1.2 Memory Models . .3 1.2.1 Von Neumann . .3 1.2.2 Harvard Architecture . .4 1.2.3 Modified Harvard Architecture . .5 1.3 The Stack . .6 1.4 Conclusion . .7 2 Datasheets, SFRs and Libraries 9 2.1 Datasheets . .9 2.1.1 Part Name . 10 2.1.2 Description and Operation . 10 2.1.3 Absolute Maximum Ratings . 11 2.1.4 Electrical Characteristics . 12 2.1.5 Physical Characteristics . 13 2.1.6 Other Information . 13 2.2 Special Function Registers . 14 2.2.1 SFRs in Datasheets . 14 2.2.2 Addressing SFRs . 15 2.3 Libraries . 17 2.3.1 Including libraries . 18 2.3.2 Commonly used libraries . 19 2.4 Conclusion . 20 3 Hello World 21 3.1 Example: Lighting an LED . 22 3.2 Programming \Hello World" . 27 3.3 Digital Input . 30 3.4 DIO Operation . 36 3.5 Conclusion . 41 4 Analog Digital Converter 43 4.1 Terminology . 43 4.2 Converting methods . 44 4.3 Sources of Error . 45 4.4 Return to the LED example . 47 4.4.1 The ATmega644p ADC . 47 4.4.2 Understanding the ADC . 48 4.4.3 Programming with the ADC . 51 4.5 Conclusion . 55 5 Code Styles 56 5.1 Headers and Header Files . 56 5.1.1 #ifndef MY PROGRAM H ................... 56 5.1.2 Additional Inclusions . 57 5.1.3 Macro Definitions . 58 5.1.4 Global Variable Declaration . 59 5.1.5 Function Prototypes . 59 5.2 Commenting . 59 5.2.1 Documentation Comments . 59 5.2.2 Non-Documentation Comments . 62 5.3 Code reuse: Functions and Libraries . 63 5.3.1 When to use functions . 63 5.3.2 Variable Scope and Arguments . 63 5.3.3 structs............................ 64 5.3.4 Libraries . 64 5.4 ADC program revisited . 65 5.5 Conclusion . 69 6 C Data Structures 70 6.1 Arrays . 70 6.1.1 Declaring Arrays . 70 6.1.2 Accessing Arrays . 71 6.1.3 Multi-Dimensional Arrays . 71 6.2 structs................................ 72 6.2.1 Declaring structs ...................... 72 6.2.2 Uses for structs....................... 74 6.3 Pointers . 78 6.3.1 Pointer Syntax . 78 6.3.2 Pointers and arrays . 79 6.3.3 Pointers and other data structures . 79 6.3.4 Pointers in pass by reference . 81 6.3.5 Function pointers . 81 6.4 Conclusion . 82 7 Serial Communications 83 7.1 USART and RS232 Serial . 83 7.1.1 RS232 . 83 7.1.2 USART . 84 7.1.3 USART Communications Example . 86 7.2 Serial Peripheral Interface (SPI) . 86 7.2.1 SPI Bus . 87 7.2.2 Operation . 87 7.2.3 SPI Communications Example . 89 7.2.4 Daisy-Chaining SPI devices . 94 7.3 Two-Wire Interface (TWI) . 96 7.3.1 Operation . 96 7.3.2 Example . 97 7.4 Comparing TWI and SPI . 98 7.5 Conclusion . 99 8 Interrupts and Timers 100 8.1 Interrupts . 100 8.1.1 How interrupts work . 100 8.1.2 Using Interrupts . 101 8.1.3 The Interrupt Process . 102 8.2 Program startup assembly . 105 8.3 Timing . 106 8.3.1 Basic Methods . 106 8.3.2 Using the Timers . 109 8.4 Conclusion . 111 9 External Devices 112 9.1 Motors . 112 9.1.1 Linear Drivers . 112 9.1.2 PWM Motor Driver . 115 9.2 Servos . 118 9.3 Sensors . 119 9.4 Conclusion . 121 10 Real-Time Operating Systems 122 10.1 Preemptive schedulers . 122 10.2 Cooperative schedulers . 123 10.3 Round-Robin example . 123 10.4 Conclusion . 129 11 The final example 130 11.1 Problem statement . 130 11.2 Design decisions . 130 11.2.1 Control . 131 11.2.2 Sense . 132 11.2.3 Decision . 133 11.3 File structure . 135 11.3.1 RTOSmain.c .......................... 135 11.3.2 RTOSmain.h .......................... 136 11.3.3 tasks.h ............................ 137 11.3.4 tasks.c ............................ 137 11.3.5 motor.h and motor.c .................... 140 11.3.6 ADC, SPI and USART libraries . 140 11.4 Conclusion . 141 Afterword 142 A regstructs.h 143 B Code for final RTOS example 174 B.1 RTOSmain.h . 174 B.2 RTOSmain.c . 176 B.3 tasks.h . 178 B.4 tasks.c . 179 B.5 motor.h . 183 B.6 motor.c . 185 B.7 ADC.h . 187 B.8 ADC.c . 189 B.9 SPI.h . 190 B.10 SPI.c . 192 B.11 USART.h . 194 B.12 USART.c . 196 C Setting up and using Eclipse 198 C.1 Set up Eclipse for use with the AVR . ..