Transcription of Introductory Microcontroller Programming
1 Introductory MicrocontrollerProgrammingbyPeter AlleyA ThesisSubmitted to the Facultyof theWORCESTER POLYTECHNIC INSTITUTEin partial fulfillment of the requirements for theDegree of Master of ScienceinRobotics EngineeringMay 2011 Prof. William MichalsonAdvisorProf. Taskin PadirProf. Susan JarvisCommittee memberCommittee memberAbstractThis 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 topics include information regarding the hardware of a microcontrollerand assembly code as well as instructions regarding good program structure andcoding practices. Examples with code and discussion are presented is intended for hobbyists and students desiring knowledge on programmingmicrocontrollers, and is written at a level that students entering the junior levelcore robotics classes would find What is a Microcontroller ?
2 Micro-processors, -computers, -controllers .. Memory Models .. Neumann .. Architecture .. Harvard Architecture .. The Stack .. Conclusion ..72 Datasheets, SFRs and Datasheets .. Name .. and Operation .. Maximum Ratings .. Characteristics .. Characteristics .. Information .. Special Function Registers .. in Datasheets .. SFRs .. Libraries .. libraries .. used libraries .. Conclusion .. 203 Hello Example: Lighting an LED .. Programming Hello World .. Digital Input .. DIO Operation .. Conclusion .. 414 Analog Digital Terminology .. Converting methods .. Sources of Error .. Return to the LED example .. ATmega644p ADC.
3 The ADC .. with the ADC .. Conclusion .. 555 Code Headers and Header Files .. #ifndef MYPROGRAMH.. Inclusions .. Definitions .. Variable Declaration .. Prototypes .. Commenting .. Comments .. Comments .. Code reuse: Functions and Libraries .. to use functions .. Scope and Arguments .. ADC program revisited .. Conclusion .. 696 C Data Arrays .. Arrays .. Arrays .. Arrays .. forstructs .. Pointers .. Syntax .. and arrays .. and other data structures .. in pass by reference .. pointers .. Conclusion .. 827 Serial USART and RS232 Serial .. Communications Example .. Serial Peripheral Interface (SPI).
4 Bus .. Communications Example .. SPI devices .. Two-Wire Interface (TWI) .. Comparing TWI and SPI .. Conclusion .. 998 Interrupts and Interrupts .. interrupts work .. Interrupts .. Interrupt Process .. Program startup assembly .. Timing .. Methods .. the Timers .. Conclusion .. 1119 External Motors .. Drivers .. Motor Driver .. Servos .. Sensors .. Conclusion .. 12110 Real-Time Operating Preemptive schedulers .. Cooperative schedulers .. Round-Robin example .. Conclusion .. 12911 The final Problem statement .. Design decisions .. Control .. Sense .. Decision .. File structure .. ADC, SPI and USART libraries .. Conclusion.
5 141 Afterword142A Code for final RTOS .. 196C Setting up and using Set up Eclipse for use with the AVR .. Making a project .. 200 List of Figures1 Pin configuration for ATmega644p.. Basic components of a microprocessor.. Basic components of a microcomputer .. Block diagram of the von Neumann architecture.. A block diagram of the Harvard architecture.. Origin of requested data in LIFO storage (left) and FIFO (right). Description section of AND gate datasheet.[18] .. Absolute Maximum Ratings section of AND gate datasheet.[18] . Electrical Characteristics section of AND gate datasheet.[18] .. ADCSRA Register Excerpt from ATmega644p datasheet.[11] .. ADPS2:0 Bit field from ADCSRA Register from ATmega644pdatasheet.[11] .. Diagram of a basic controlled system.
6 Excerpt from data sheet for LED part LTL-2F3 VEKNT.[16] .. Excerpt from pp325-326 of datasheet for ATmega644p regardingmaximum sink/source capabilities.[11] .. Circuit diagram for using the Microcontroller as the current source(left) or as a current sink (right).. Excerpt from pp325-326 of datasheet for ATmega644p regardingdigital input voltage levels.[11] .. Three options for feedback signal.. Comparator circuit design from LM193 datasheet.[20] .. Schematic and calculations for a voltage divider .. Logical OR of the output of 2 comparator circuits. From LM193datasheet.[20] .. Circuit diagram of the entire LED control and feedback system.. General Digital I/O schematic from ATmega644p datasheet.[11] . Segment of DIO schematic relating toPORT xregister.
7 [11] .. Segment of DIO schematic relating toDDRxregister.[11] .. Segment of DIO schematic relating toPINxregister.[11] .. Segment of DIO schematic relating to pull-up resistor.. Path ofPORT xnsignal when a pin is set as an output.. Path ofPORTxn(heavy, solid line) and input (heavy, dashed line)signals when a pin is set as an input.. Aliasing caused by sampling a signal which is above theNyquist rate of 5Hz. Sampling rate is 10Hz.. Complete circuit for LED example using the ADC .. Schematic of the ATmega644p ADC. Found in section of thedatasheet.[11] .. Reference voltage selection schematic. Excerpt from figure . Signal selection schematic. Excerpt from figure .. File structure for relative path include.. 3 devices connected to a Microcontroller using SPI.
8 The four modes of SPI clock polarity and phase.. How to form commands for the LS7366R Quadrature EncoderCounter. [17] .. Bit field descriptions forMDR0andMDR1. [17] .. States of all four SPI lines during operation to read 16 bit CNTR register from LS7366R Quadrature Counter.. 3 devices daisy-chained to a Microcontroller using SPI.. 3 devices connected to a Microcontroller using TWI.. Differential op amp.. Schematic and equation for completed differential op-amp design. Two modes of H-bridge driving. A) Switches 1 and 4 closed, CWspin of motor. B) Switches 2 and 3 closed, CCW spin.. Common servo timings for three angles.. Range vs Voltage data for the GP2D12 SHARP IR sensor. [19] . Calculating angle formed with the wall based off distance mea-surements.
9 File structure of final example showing all files not installed withthe compiler.. Install New Software window from Eclipse .. Eclipse window with Project Explorer and new project buttonmarked.. C Project window.. Properties window for setting MCU type and clock frequency.. Worksheet for setting internal fuses.. 203 List of Voltage values for the various feedback possibilities.. Truth table for thePORT xnlatch.. Pin assignments for RS232 standard. [5] .. Schedule of tasks in round-robin RTOS example.. 126 PrefaceWhy this topic?In the spring of 2009, Worcester Polytechnic Institute s junior level roboticscourses were first offered, with me as one of the teacher s assistants for thecourses. At the time, I had never worked with microcontrollers before, exceptingthe VEX system used in the lower level WPI robotics classes, which providesa layer of abstraction between the programmer and the actual hardware.
10 Thiswas the first time that many of the students and my self actually had to worryabout special function registers, peripheral initialization and other similar the period of being the TA for these courses multiple times, I learned agreat deal, but also noticed several recurring issues the students seemed toface. In addition to furthering my own knowledge on the care and feeding ofmicrocontrollers, this thesis is an attempt to provide students with a resourceto overcome some of their difficulties, to provide a better understanding of thehardware level operation of a Microcontroller , and as instruction into improvingtheir knowledge and problem I noticed, was that many students viewed a microcontrolleras a little black box in which magic happens. By the end of the course theblack box in their imagination may have shrunk slightly and grown a few moreinputs and outputs, but they still did not know what was actually occurring inthe hardware to generate the results they desired and saw.