Hubert Henry Ward
Leigh, UK
ISBN 978-1-4842-7232-9 e-ISBN 978-1-4842-7230-5
https://doi.org/10.1007/978-1-4842-7230-5
Hubert Henry Ward 2022
This work is subject to copyright. All rights are solely and exclusively licensed 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.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
This Apress imprint is published by the registered company APress Media, LLC part of Springer Nature.
The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.
Introduction
This book looks at using some useful aspects of the PIC microcontroller. It explains how to write programs in C so that you can use the PIC micro to control a variety of electronics devices and DC motors. After reading this book, you will be well on your way to becoming an embedded programmer using the C program language.
I have two main aims in writing this book. First and foremost, I have a real passion for writing programs in that I am enthralled by the way we can use simple 1s and 0s, really 5V and 0V, to make a circuit do almost anything we want it to do. The extent of what we can get these microcontrollers to do is limited only by our imagination and experience. In my youth, we thought the flip phone used by Captain James T. Kirk in Star Trek was just a gimmick and we would never get there. Well, our mobile phones do a lot more than just video calls. All of what we do with our phones is done with just 1s and 0s. In writing this book, my primary goal is to hopefully inspire you guys into wanting to understand how we make the 1s and 0s work for us. That is why I will explain how all the instructions in my programs actually work and how they achieve the outcomes we want.
The second aim in writing this book is to transfer all those Arduino Sketches, which use a range of useful peripheral devices, and show you how to get them to work on a PIC microcontroller. In doing so, I hope to show you that the world of PIC controllers is not complicated and really anyone can work with them. It does not matter what experience you have with PICs, as I will assume you know nothing. This book will take the complete novice and not only allow them to run those Arduino Sketches on the PIC, but it will explain in detail every aspect of how the programs work. After reading this book, you will hopefully not only enjoy programming PIC micros in the C programming language, but should be well prepared to take on a new career as an embedded programmer.
In addition to getting to know how to program in C, I will show you how to build a range of circuits so that you end up using the peripherals in a practical environment. I will show you how, if you so wish, you can make your own prototype boards to program your PICs and run the programs you write. We will learn about the electronic principles of the components we use to interface the PIC to a wide range of useful peripherals.
Goals of the Book
After reading through this book, you should be able to program the PIC to use all the following peripherals:
The seven-segment display
The eight-by-eight matrix display
The joystick controller
The HC-04 ultrasonic sensor
The stepper motor
THE SERVO MOTOR
The DS1307 real-time clock module
The DHT11 humidity and temperature sensor
The DC motor and fan
The membrane keypad module
You should also gain a good understanding of some of the basic and advanced programming techniques for PIC micros, such as the following:
The configuration words
The TRIS registers and PORTS
The timer registers
The ADC (analog-to-digital converter) module
The CCPM (capture, compare, and pulse width modulation) module.
The MSSP (master synchronous serial port) module using SPI (serial peripheral interface) and I2C (inter-integrated circuit) communication protocols.
The EUSART (enhanced universal synchronous/ asynchronous, receive and transmit) module
Interrupts
You will be able to use an industrial IDE to write your programs in C, compile and download your programs using a variety of tools, and use the simulator in MPLABX, the IDE, to debug your programs.
You should be able to download your programs to your PIC in a practical situation where you have the ability to design and build some useful projects.
You will have learned the basic principles of a range of electronic components such as resistors, capacitors, transistors and the Darlington array, diodes, and light-emitting diodes.
The Prerequisites
There are none really, but if you understand C programming it would be useful. However, I will explain how each of the programs work as we go through them.
Also, if you understand the binary and hexadecimal number systems, it would be an advantage, but there is a section in the appendix that will help you with that and I will explain the important aspects as we need them.
However, to get the full use out of this book, you will need to have installed the following software:
MPLABX, which is the IDE from Microchip. The version in the book is MPLABX version 5.25. However, any version later than 2.20 will be OK.
A C compiler for the eight-bit micro. I use XC8 (V2.10), but with some programs I use XC8 (V1.35) compiler software. However, you should be aware that some of the later compilers have some useful libraries missing. That is why I sometimes use version 1.35.
All these programs are freely available from the Microchip website.
Another useful piece of software would be a suitable ECAD (electronic computer-aided design) software that supports eight-bit micros. The ECAD software that I have used in the past is PROTEUS. However, this is not free and so I will show you how to use a suitable prototype board to run the programs in a practical situation.