Armstrong Subero

Programming PIC Microcontrollers with XC8

Armstrong Subero

Moruga, Trinidad and Tobago

Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.​apress.​com/​978-1-4842-3272-9 . For more detailed information, please visit http://​www.​apress.​com/​source-code .

ISBN 978-1-4842-3272-9

e-ISBN 978-1-4842-3273-6

https://doi.org/10.1007/978-1-4842-3273-6

Library of Congress Control Number: 2017962909

© Armstrong Subero 2018

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Printed on acid-free paper

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

Introduction

With the onset of the Internet of Things (IoT) revolution, embedded systems development is becoming very popular in the maker community and the professional space as well. IoT is a trillion-dollar business. PICⓇ microcontrollers are one of the technologies that can be used to develop IoT devices. This is due to the low cost, wide availability, and low power consumption of these devices. Additionally, due to the wide range of PICⓇ microcontrollers available, there are PICⓇ microcontrollers that can match your designs, from 8 pins to over 144 pins. They covers 8-, 16-, and 32-bit architectures.

People argue that 8-bit architecture is irrelevant in the complex embedded systems of today. However, 8-bit microcontrollers are here to stay, even if it is for the simple purpose of learning about microcontroller architecture. The relatively simple and beautifully engineered architecture of 8-bit PICⓇ microcontrollers makes them invaluable for learning the inner workings of microcontrollers. It is a lot easier to learn all the registers of these simple 8-bit devices and follow the path of program execution than with more complex ones. After learning about PICⓇ microcontrollers, I found it easy to move on to the more popular 16-bit and then 32-bit devices. In this book, I hope to share the tips and tricks I learned along the way.

Why Did I Write This Book?

When I first started programming PICⓇ microcontrollers, I imagined that a lot of information would be available on the Internet on which people could base their designs. Little did I know at the time that programming these useful devices would take a lot of work, dedication, and finding code that actually worked. In addition, when I did find code, it was usually for ancient PICⓇ microcontrollers that are NRND or have a lack of modern peripherals and capabilities. When I finally did find a suitable language in the name of HI-TECH C for the PICⓇ microcontroller series, I found out that XC8 would be released to take its place. Despite being compatible with HI-TECH C, I realized upon using the compiler that a lot of the code did not work out of the box and a lot of my libraries had to be rewritten. This was a time-consuming process and the lack of information on the language was frustrating, leaving me to think that XC8 was not everything I expected it to be.

A lot has changed since then. Now I love XC8 and all the features it provides and I’m thankful that Microchip provides it free of cost. A lot of people might wonder why I chose XC8 to program PICⓇ microcontrollers when other simpler options in BASIC and C exist with a lot of libraries available. To them I say that even if those languages are easier to use, there are some versions where the libraries are not open and thus cannot be examined. In cases where software must receive government approval, closed libraries are not an option. Also, the knowledge gained from writing your own libraries is invaluable. Microchip technology provides the MPLABⓇ Code Configurator (MCC) that can generate code to use the onboard peripherals of a lot of PICⓇ microcontrollers and, even more recently, for Click boards using the ubiquitous mikroBUS for communication. The other reason is that by learning to use the compiler provided by the manufacturer, you avoid the problem of a chip with a killer new feature not being supported by the manufacturer of a third-party compiler. For the sake of understanding exactly what is happening, I make minimal use of the MCC in this book despite its ease of use.

Who Is This Book For?

For this book, you will need some basic electronic devices and some electronic equipment and knowledge of how to use them. I expect that the reader has knowledge of the C programming language. Knowledge of variables, loops, and basic data structures will suffice. I also assume you have knowledge of basic digital electronics. I also make the presumption that you have used another simpler platform, such as Arduino, since the focus of this book is on the specifics of the PICⓇ microcontroller. 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.

What You Will Need for This Book?

You will need a few components to get all the examples up and running. All of these are covered in Chapter 1 . I know of individuals who build microcontroller circuits in simulation. I recommend building the actual circuits to gain hands-on experience that will help you in the industry. Unlike other programming disciplines, embedded systems development allows you to build things that can be used in our physical world, not just push pixels around the screen. I have also found it more enjoyable to prototype circuits, as you also learn valuable skills in circuit design and troubleshooting that you will have for a lifetime. Although for many people using a development board is simpler, for those wanting a true “hands-on” approach to learning, prototyping on breadboards is a valuable skill.

What Will You Learn in This Book?

This book consists of 15 chapters that will help you get on your way to programming PICⓇ microcontrollers in XC8.

  • Chapter 1 looks at setting up shop, including the hardware and software necessary to get the most out of this book.

  • Chapter 2 covers the basics of the C programming language.

  • Chapter 3 reviews the basics of electronics.

  • Chapter 4 presents the basics of PICⓇ microcontrollers and looks at the PIC16F1717.

  • Chapter 5 covers the basics of connecting your PICⓇ microcontroller to your computer.

  • Chapter 6 presents the basics of I/O, including PPS, interfacing LEDs, push buttons, and seven segment displays.

  • Chapter 7 demonstrates using actuators, DC motors, servos, and stepper motors.

  • Chapter 8 examines the use of interrupts, timers, counters, and PWM.

  • Chapter 9 presents the use of serial communication protocols, including USART with GPS and GSM, SPI, and I2C.

  • Chapter 10 looks at using displays including the SSD1306 and Nextion touch screen displays.

  • Chapter 11 consists of understanding the ADC and DAC.

  • Chapter 12 covers using the onboard peripherals of the CLC, NCO, Comparator, and FVR.

  • Chapter 13 takes us into the wireless connectivity with Wi-Fi and Bluetooth.

  • Chapter 14 demonstrates the use of the low-power features of the microcontroller, minimizing power consumption and the WDT.

  • Chapter 15 is a project-based chapter where we build two projects.

Upon finishing this book, I hope that you will have the foundation you need to take on the world of embedded systems design and build useful gadgets, IoT devices, and beyond. This is the book I wish I had when I was getting started with PICⓇ microcontrollers.

Table of Contents

  1. Chapter 1:​ Preparing for Development
    1. Gathering Your Hardware
      1. Microcontroller
      2. Programmer
    2. Gathering the Software
      1. MPLAB® X IDE
      2. XC Compilers
    3. Setting Up Shop
      1. Multimeter
      2. Oscilloscope
      3. Power Supply
      4. Shopping for Supplies
    4. Conclusion
  2. Chapter 2:​ The C Programming Language
    1. C
    2. C Programming
      1. C Program Structure
      2. Comments
      3. Variables and Constants
      4. Arrays, Pointers, and Structures
      5. Operators
      6. Controlling Program Flow
      7. Preprocessor Directives
    3. Assembly vs.​ C
    4. Conclusion
  3. Chapter 3:​ Basic Electronics for Embedded Systems
    1. Electronics
    2. Resistors
    3. Potentiometer
      1. Digital Potentiometer
    4. Photoresistor
    5. Capacitor
    6. Inductor
    7. Transformers
    8. Diode
      1. Zener Diode
      2. Light Emitting Diode
      3. Laser Diode
    9. Transistors
      1. Bipolar Junction Transistors
      2. Darlington Transistor
      3. Field Effect Transistor
      4. Metal Oxide Semiconductor Field Effect Transistor (MOSFET)
      5. Junction Field Effect Transistor
    10. Operational Amplifier
    11. Digital Electronics
      1. The AND Gate
      2. The OR Gate
      3. The NOT Gate
      4. The NAND Gate
      5. The NOR Gate
      6. The Buffer Gate
      7. The XOR Gate
    12. Logic-Level Conversion
      1. Run the Entire System on 3.​3v
      2. Use a Voltage Divider
      3. Use a Bi-Directional Logic Level Shifter
    13. Conclusion
  4. Chapter 4:​ PIC® Microcontrollers​
    1. PIC® Microcontrollers​ Overview
      1. Baseline PIC® Microcontrollers​
      2. Mid-Range PIC® Microcontrollers​
      3. Enhanced Mid-Range PIC® Microcontrollers​
      4. High-Performance PIC® Microcontrollers​
    2. PIC® 16F1717 Block Diagram
    3. Program Flash Memory
    4. Random Access Memory
    5. Timing Generation
    6. !MCLR
    7. Ports
    8. Onboard Peripherals
      1. Analog to Digital Converter
      2. Digital to Analog Converter
      3. Capture/​Compare/​Pulse Width Modulation Module
      4. Timers
      5. Comparators
      6. Fixed Voltage Reference
      7. Temperature Indicator
      8. EUSART
      9. CLC
      10. MSSP
      11. NCO
      12. ZCD
      13. COG
      14. Operational Amplifiers
      15. High Endurance Flash Block
    9. The Enhanced Mid-Range CPU Core
      1. Power-Up Timer
      2. Oscillator Start-Up Timer
      3. Power-On Reset
      4. Watchdog Timer
      5. Brown-Out Reset
    10. Conclusion
  5. Chapter 5:​ Connecting and Creating
    1. Let’s Get Started
    2. A Look at Programmers
    3. A Look at Programming
    4. Traps for Beginners
    5. Additional Information
    6. Conclusion
  6. Chapter 6:​ Input and Output
    1. Let’s Begin I/​O
    2. TRIS Register
    3. PORT Register
    4. Output Latch Registers
    5. Analog Select Registers
    6. Weak Pull-Up
    7. Making an LED Blink
    8. Using a Pushbutton
    9. Seven Segment Displays
    10. Seven Segment Display Multiplexing
    11. Project:​ Countdown Timer
    12. Peripheral Pin Select
    13. Conclusion
  7. Chapter 7:​ Interfacing Actuators
    1. Introducing Actuators
    2. DC Motor
    3. Servo Motor
    4. Stepper Motor
    5. Conclusion
  8. Chapter 8:​ Interrupts, Timers, Counters, and PWM
    1. Introduction to Interrupts
    2. Using Timers
      1. Timer 0 in Timer Mode
      2. Timer 0 in Counter Mode
      3. Timer 0 with Interrupts
    3. Using the CCP Module
      1. Understanding PWM
      2. Using PWM
    4. Project:​ Using PWM with a Motor Driver
    5. Project:​ Using CCP and Dedicated PWM with RGB LED
    6. Conclusion
  9. Chapter 9:​ USART, SPI, and I2C:​ Serial Communication Protocols
    1. Using USART (Universal Synchronous Asynchronous Receiver Transmitter)
      1. Serial Character LCD
      2. USART to PC Communication
      3. Text to Speech
    2. Using GPS (Global Positioning Systems)
      1. NMEA Commands
      2. Software USART
    3. Using SPI (Serial Peripheral Interface)
      1. Digital Potentiometer
    4. Character Display
      1. Character:​ The Hitachi HD44780 LCD
      2. The Samsung KS0066U
    5. Using the I2C (Inter-Integrated Circuit) Protocol
      1. EEPROM
    6. Conclusion
  10. Chapter 10:​ Interfacing Displays
    1. Displays
    2. OLED Displays
    3. Touch Screen LCD
      1. Resistive Touch
      2. Capacitive Touch
      3. Selecting a Touch Screen LCD
      4. Using the Touch LCD
    4. Conclusion
  11. Chapter 11:​ ADC and DAC
    1. Data Conversion
    2. ADC (Analog to Digital Conversion)
    3. Project:​ Digital Thermometer
    4. DAC (Digital to Analog Converter)
    5. Conclusion
  12. Chapter 12:​ NCO, Comparator, and FVR
    1. CLC (Configurable Logic Cell)
    2. NCO (Numerically Controlled Oscillator)
    3. Comparator
    4. FVR (Fixed Voltage Reference)
    5. Conclusion
  13. Chapter 13:​ Wi-Fi and Bluetooth
    1. Low-Cost Wireless Connectivity
    2. Integrating Wi-Fi
      1. Using the ESP8266
      2. Testing the ESP8266
    3. Project:​ Wi-Fi Data Logger
    4. Integrating Bluetooth
      1. Using the HC05 Bluetooth Module
      2. Communicating via Bluetooth
    5. Conclusion
  14. Chapter 14:​ Watchdog Timer and Low Power
    1. Low Power 8-Bit vs 32-Bit
    2. Sleep Mode
    3. Watchdog Timer
    4. Other Ways to Conserve Power
      1. Reduce the Clock Frequency
      2. Reduce the Operating Voltage
      3. Power External Devices from I/​O Lines
    5. Conclusion
  15. Chapter 15:​ PIC® Microcontroller Projects
    1. Project:​ Temperature Controlled Fan
    2. Project:​ Touch Screen Clock
    3. Conclusion
  16. Appendix A: Resources
  17. Appendix B: Making Your Own PCBs and Schematics
  18. Fritzing
  19. Altium Circuit Maker
  20. Scheme-it
  21. Index

About the Author and About the Technical Reviewer

About the Author

Armstrong Subero has been tinkering with electronics for as long as he can remember. The thrill of creating something from the ground up and watching it work is something that he never tires of. His entire life changed when he discovered microcontrollers. They were so powerful and simple and complex all at the same time. When he finished school, he taught himself programming and, for a while, worked part-time from a home office. He landed his first job as a systems technologist completely self-taught and a lot of it was due to his in-depth knowledge and passion for the microcontroller technology. Armstrong has used many microcontroller families during the course of his work, but he has an affinity for PICⓇ microcontrollers. Armstrong currently works for the Ministry of National Security in his country. He designs robots and writes books, blogs, and software on trinirobotics.com and angelstemlabs.org in his free time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.39.252