Learn how to use microcontrollers without all the frills and math. This book uses a practical approach to show you how to develop embedded systems with 8 bit PIC microcontrollers using the XC8 compiler. It's your complete guide to understanding modern PIC microcontrollers. Are you tired of copying and pasting code into your embedded projects? Do you want to write your own code from scratch for microcontrollers and understand what your code is doing? Do you want to move beyond the Arduino? Then __Programming PIC Microcontrollers with XC8__is for you! Written for those who want more than an Arduino, but less than the more complex microcontrollers on the market, PIC microcontrollers are the next logical step in your journey. You'll also see the advantage that MPLAB X offers by running on Windows, MAC and Linux environments. You don't need to be a command line expert to work with PIC microcontrollers, so you can focus less on setting up your environment and more on your application. **What You’ll Learn** * Set up the MPLAB X and XC8 compilers for microcontroller development * Use GPIO and PPS * Review EUSART and Software UART communications * Use the eXtreme Low Power (XLP) options of PIC microcontrollers * Explore wireless communications with WiFi and Bluetooth **Who This Book Is For** Those with some basic electronic device and some electronic equipment and knowledge. This book assumes knowledge of the C programming language and basic knowledge of digital electronics though a basic overview is given for both. A complete newcomer can follow along, but this book is heavy on code, schematics and images and focuses less on the theoretical aspects of using microcontrollers. This book is also targeted to students wanting a practical overview of microcontrollers outside of the classroom. Table of Contents 4 About the Author 12 Introduction 13 Chapter 1: Preparing for Development 18 Gathering Your Hardware 18 Microcontroller 19 Programmer 21 Gathering the Software 23 MPLAB® X IDE 23 XC Compilers 24 Setting Up Shop 25 Multimeter 25 Oscilloscope 25 Power Supply 25 Shopping for Supplies 26 Conclusion 30 Chapter 2: The C Programming Language 31 C 31 C Programming 31 C Program Structure 32 Comments 33 Variables and Constants 33 Arrays, Pointers, and Structures 35 Arrays 35 Pointers 36 Structures 37 Operators 38 Controlling Program Flow 39 if Statement 39 else Statement 40 else if Statement 40 switch statement 41 for Loop 42 while Loop 42 do Loop 43 break Statement 43 continue Statement 43 goto Statement 44 Preprocessor Directives 44 #define 45 #if, #ifdef, #ifndef, #elif, and #else 46 #pragma 47 Assembly vs. C 47 Conclusion 48 Chapter 3: Basic Electronics for Embedded Systems 49 Electronics 49 Resistors 49 Potentiometer 52 Digital Potentiometer 53 Photoresistor 54 Capacitor 55 Inductor 57 Transformers 58 Diode 59 Zener Diode 60 Light Emitting Diode 61 Laser Diode 62 Transistors 63 Bipolar Junction Transistors 63 Darlington Transistor 65 Field Effect Transistor 67 Metal Oxide Semiconductor Field Effect Transistor (MOSFET) 67 Junction Field Effect Transistor 68 Operational Amplifier 69 Digital Electronics 70 The AND Gate 70 The OR Gate 71 The NOT Gate 71 The NAND Gate 72 The NOR Gate 72 The Buffer Gate 73 The XOR Gate 73 Logic-Level Conversion 74 Run the Entire System on 3.3v 75 Use a Voltage Divider 75 Use a Bi-Directional Logic Level Shifter 76 Conclusion 77 Chapter 4: PIC® Microcontrollers 78 PIC® Microcontrollers Overview 78 Baseline PIC® Microcontrollers 78 Mid-Range PIC® Microcontrollers 79 Enhanced Mid-Range PIC® Microcontrollers 79 High-Performance PIC® Microcontrollers 79 PIC® 16F1717 Block Diagram 80 Program Flash Memory 81 Random Access Memory 81 Timing Generation 82 !MCLR 83 Ports 83 Onboard Peripherals 83 Analog to Digital Converter 83 Digital to Analog Converter 84 Capture/Compare/Pulse Width Modulation Module 84 Pulse Width Modulation Module 84 Timers 85 Comparators 85 Fixed Voltage Reference 85 Temperature Indicator 85 EUSART 86 CLC 86 MSSP 86 NCO 86 ZCD 86 COG 87 Operational Amplifiers 87 High Endurance Flash Block 87 The Enhanced Mid-Range CPU Core 87 Power-Up Timer 89 Oscillator Start-Up Timer 89 Power-On Reset 89 Watchdog Timer 89 Brown-Out Reset 90 Conclusion 90 Chapter 5: Connecting and Creating 91 Let’s Get Started 91 A Look at Programmers 91 A Look at Programming 95 Traps for Beginners 100 Additional Information 101 Conclusion 101 Chapter 6: Input and Output 102 Let’s Begin I/O 102 TRIS Register 102 PORT Register 104 Output Latch Registers 104 Analog Select Registers 104 Weak Pull-Up 105 Making an LED Blink 105 Using a Pushbutton 123 Seven Segment Displays 132 Seven Segment Display Multiplexing 138 Project: Countdown Timer 145 Peripheral Pin Select 154 Conclusion 155 Chapter 7: Interfacing Actuators 156 Introducing Actuators 156 DC Motor 156 Servo Motor 161 Stepper Motor 170 Conclusion 177 Chapter 8: Interrupts, Timers, Counters, and PWM 178 Introduction to Interrupts 178 Using Timers 185 Timer 0 in Timer Mode 185 Timer 0 in Counter Mode 190 Timer 0 with Interrupts 196 Using the CCP Module 202 Understanding PWM 202 Using PWM 202 Project: Using PWM with a Motor Driver 207 Project: Using CCP and Dedicated PWM with RGB LED 213 Conclusion 220 Chapter 9: USART, SPI, and I2C: Serial Communication Protocols 221 Using USART (Universal Synchronous Asynchronous Receiver Transmitter) 221 Serial Character LCD 222 USART to PC Communication 231 Text to Speech 231 Using GPS (Global Positioning Systems) 236 NMEA Commands 236 Software USART 245 GSM Module 245 AT Commands 246 Using SPI (Serial Peripheral Interface) 254 Digital Potentiometer 256 Character Display 261 Character: The Hitachi HD44780 LCD 261 The Samsung KS0066U 275 Using the I2C (Inter-Integrated Circuit) Protocol 275 EEPROM 276 Conclusion 288 Chapter 10: Interfacing Displays 289 Displays 289 OLED Displays 290 Touch Screen LCD 307 Resistive Touch 308 Capacitive Touch 309 Selecting a Touch Screen LCD 309 Using the Touch LCD 310 Creating a Layout 310 Adding Widgets 311 Adding Code 311 Reading on the Microcontroller 313 Conclusion 322 Chapter 11: ADC and DAC 323 Data Conversion 323 ADC (Analog to Digital Conversion) 323 Project: Digital Thermometer 328 DAC (Digital to Analog Converter) 334 Conclusion 337 Chapter 12: NCO, Comparator, and FVR 338 CLC (Configurable Logic Cell) 338 NCO (Numerically Controlled Oscillator) 341 Comparator 344 FVR (Fixed Voltage Reference) 349 Conclusion 353 Chapter 13: Wi-Fi and Bluetooth 354 Low-Cost Wireless Connectivity 354 Integrating Wi-Fi 354 Using the ESP8266 355 Testing the ESP8266 355 Project: Wi-Fi Data Logger 356 Integrating Bluetooth 370 Using the HC05 Bluetooth Module 370 AT Mode 371 Communicating via Bluetooth 371 Conclusion 376 Chapter 14: Watchdog Timer and Low Power 377 Low Power 8-Bit vs 32-Bit 377 Sleep Mode 378 Watchdog Timer 383 Other Ways to Conserve Power 388 Reduce the Clock Frequency 389 Reduce the Operating Voltage 389 Power External Devices from I/O Lines 389 Conclusion 390 Chapter 15: PIC® Microcontroller Projects 391 Project: Temperature Controlled Fan 391 Project: Touch Screen Clock 400 Conclusion 432 Appendix A: Resources 433 Appendix B: Making Your Own PCBs and Schematics 435 Fritzing 435 Altium Circuit Maker 435 Scheme-it 436 Index 437 Learn how to use microcontrollers without all the frills and math. This book uses a practical approach to show you how to develop embedded systems with 8 bit PIC microcontrollers using the XC8 compiler. It's your complete guide to understanding modern PIC microcontrollers. Are you tired of copying and pasting code into your embedded projects? Do you want to write your own code from scratch for microcontrollers and understand what your code is doing? Do you want to move beyond the Arduino? Then Programming PIC Microcontrollers with XC8 is for you! Written for those who want more than an Arduino, but less than the more complex microcontrollers on the market, PIC microcontrollers are the next logical step in your journey. You'll also see the advantage that MPLAB X offers by running on Windows, MAC and Linux environments. You don't need to be a command line expert to work with PIC microcontrollers, so you can focus less on setting up your environment and more on your application. What You’ll Learn Set up the MPLAB X and XC8 compilers for microcontroller development Use GPIO and PPS Review EUSART and Software UART communications Use the eXtreme Low Power (XLP) options of PIC microcontrollers Explore wireless communications with WiFi and Bluetooth Who This Book Is For Those with some basic electronic device and some electronic equipment and knowledge. This book assumes knowledge of the C programming language and basic knowledge of digital electronics though a basic overview is given for both. A complete newcomer can follow along, but this book is heavy on code, schematics and images and focuses less on the theoretical aspects of using microcontrollers. This book is also targeted to students wanting a practical overview of microcontrollers outside of the classroom. Front Matter ....Pages i-xvii Preparing for Development (Armstrong Subero)....Pages 1-13 The C Programming Language (Armstrong Subero)....Pages 15-32 Basic Electronics for Embedded Systems (Armstrong Subero)....Pages 33-61 PIC® Microcontrollers (Armstrong Subero)....Pages 63-75 Connecting and Creating (Armstrong Subero)....Pages 77-87 Input and Output (Armstrong Subero)....Pages 89-142 Interfacing Actuators (Armstrong Subero)....Pages 143-164 Interrupts, Timers, Counters, and PWM (Armstrong Subero)....Pages 165-207 USART, SPI, and I2C: Serial Communication Protocols (Armstrong Subero)....Pages 209-276 Interfacing Displays (Armstrong Subero)....Pages 277-310 ADC and DAC (Armstrong Subero)....Pages 311-325 NCO, Comparator, and FVR (Armstrong Subero)....Pages 327-342 Wi-Fi and Bluetooth (Armstrong Subero)....Pages 343-365 Watchdog Timer and Low Power (Armstrong Subero)....Pages 367-380 PIC® Microcontroller Projects (Armstrong Subero)....Pages 381-422 Back Matter ....Pages 423-434 Learn how to use microcontrollers without all the frills and math. This book uses a practical approach to show you how to develop embedded systems with 8 bit PIC microcontrollers using the XC8 compiler. It's your complete guide to室erstanding modern PIC microcontrollers. Are you tired of copying and pasting code into your embedded projectsDo you want to write your own code from scratch for microcontrollers and understand what your code is doingDo you want to move beyond the ArduinoThen Programming PIC Microcontrollers with XC8 is for you! 粩tten for those who want more than an Arduino, but less than the more complex microcontrollers on the market, PIC microcontrollers are the next logical step in your journey. You'll also see the advantage that MPLAB X offers by running on Windows, MAC and Linux environments. You don't need to be a command line expert to work with PIC microcontrollers, so you can focus less on setting up your environment and more on your application