An Introduction to Microcontrollers and Embedded Systems
Total Page:16
File Type:pdf, Size:1020Kb
AN INTRODUCTION TO MICROCONTROLLERS AND EMBEDDED SYSTEMS Tyler Ross Lambert MECH 4240/4250 Supplementary Information Last Revision: 6/7/2017 5:30 PM Summary Embedded systems in robotics are the framework that allows electro-mechanical systems to be implemented into modern machines. The key aspects of this framework are C programming in embedded controllers, circuits for interfacing microcontrollers with sensors and actuators, and proper filtering and control of those hardware components. This document will cover the basics of C/C++ programming, including the basics of the C language in hardware interfacing, communication, and algorithms for state machines and controllers. In order to interface these controllers with the world around us, this document will also cover electrical circuits required to operate controllers, sensors, and actuators accurately and effectively. Finally, some of the more commonly used hardware that is interfaced with microcontrollers is gone over. Table of Contents 1. Introduction ....................................................................................................................................... 4 2. Numbering Systems .......................................................................................................................... 5 3. Variable Types and Memory............................................................................................................. 6 4. Basic C/C++ Notes and Code::Blocks .............................................................................................. 9 5. Bitwise Operations .......................................................................................................................... 10 6. Arrays and Matrices ........................................................................................................................ 11 7. Loops............................................................................................................................................... 12 8. Logical Statements .............................................................................................................................. 13 9. Enumerations ...................................................................................................................................... 14 10. Compiler Directives ........................................................................................................................ 15 11. Pointer Variables ............................................................................................................................. 16 12. Functions ......................................................................................................................................... 18 13. Structures ........................................................................................................................................ 20 14. Microcontrollers and the Arduino IDE ........................................................................................... 22 15. Electricity and Basic Electronic Components ................................................................................. 25 Electricity ................................................................................................................................................ 25 Resistors .................................................................................................................................................. 30 Capacitors ............................................................................................................................................... 33 Inductors ................................................................................................................................................. 36 A Note on Reactive Power and AC Power Sources ................................................................................ 38 Solid State Relays ................................................................................................................................... 41 Diodes ..................................................................................................................................................... 42 Bipolar Junction Transistors (BJT) ......................................................................................................... 45 Metal Oxide Semiconductive Field Effect Transistors (MOSFET) ........................................................ 47 Operational Amplifiers ........................................................................................................................... 50 Batteries .................................................................................................................................................. 51 1 Last Revision: 6/7/2017 5:30 PM Voltage Regulators .................................................................................................................................. 53 16. Kirchoff’s Laws and Basic Useful Circuits .................................................................................... 54 Simple Voltage Divider .......................................................................................................................... 54 Analog First Order Low Pass Filter ........................................................................................................ 55 Analog First Order High Pass Filter ....................................................................................................... 57 Amplifier/Follower ................................................................................................................................. 58 17. Analog to Digital Conversion (ADC) ............................................................................................. 60 ADC Library for Teensy Microcontrollers ............................................................................................. 63 18. Pulse-Width Modultion and Digital to Analog Conversion (DAC) ................................................ 68 Logic Level Conversion and H-Bridges ................................................................................................. 71 20. Mechanical Switches and Switch Debouncing ............................................................................... 78 21. Interrupts and Interrupt Service Routines (ISRs) ............................................................................ 82 22. Analog Sensors (Accelerometers) ................................................................................................... 84 23. Serial Communication..................................................................................................................... 87 UART Signals ......................................................................................................................................... 87 SPI Signals .............................................................................................................................................. 95 I2C Signals ............................................................................................................................................ 102 CAN Bus ............................................................................................................................................... 108 OneWire Bus ......................................................................................................................................... 110 24. Digital Filters ................................................................................................................................ 112 Discrete Low Pass Filter ....................................................................................................................... 112 Moving Average Filter .......................................................................................................................... 113 Over-Sampling ...................................................................................................................................... 113 Median Filter ......................................................................................................................................... 114 Velocity Filters...................................................................................................................................... 114 25. Creating Your Own C++ Libraries ............................................................................................... 115 26. Rotary Encoders ............................................................................................................................ 118 27. Load Cells ..................................................................................................................................... 122 28. Electric Motors .............................................................................................................................. 125 Stepper Motors ...................................................................................................................................... 125 DC Motors ............................................................................................................................................ 131 29. Thermocouples .............................................................................................................................. 134 30. Telemetry