Introductory Programs with the 32-bit PIC Microcontroller: A Line-by-Line Code Analysis and Reference Guide for Embedded Programming in C

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Embark on a journey into the world of embedded programming. This book introduces you to the 32bit PIC and will teach you how the main functions of C programming work and can be used with a PIC micro. My main aim in writing this book is to introduce you to the 32-bit micro and help you understand how to program them in the C programming language. I also want to show you that you can program them and that you can make a career as an embedded programmer. I hope that after reading this book, you will have developed your understanding of C and that you will have the confidence to go and develop your own programs. A one-stop reference for the would-be embedded programmer, you'll explore the electronics needed for a variety of programs as well as how to use different devices with the PIC. The book starts with downloading the environment and creating a simple project, one that uses different oscillators, Phase Lock Loop, and circuitry needed to create the different system clocks—an easy entry point to this exciting environment. You'll also review the MPLABX integrated development environment (IDE) and see how to program the 32Bit PIC, which can be adapted to different PICs. Throughout subsequent chapters, you’ll learn how to use a range of programs that use PIC modules such as the SPI, I2C, UART communication modules, the ADC module, the Capture, Compare, and Pulse Width Modulation module, and the RTCC, Real Time Clock and Calendar Module. What You'll Learn: Create a project in MPLABX Configure the different clock frequencies that are used in the 32bit PIC Build a variable delay subroutine to be used in a simple traffic lights program Use the MAX 7219 driver IC to control the 8by8 matrix display Program an I2C expander module to control the display on a LCD Prerequisites: As to any prerequisites you need before reading this book, there are none. I will assume you are a complete novice, and I will explain every step as you progress through the book. Every instruction will be analyzed to explain how the C instruction works and how it achieves what we want from the instructions. The only things you will definitely need to use this book are as follows. Who This Book Is For: Readers who want to try their hand at embedded programming, newcomers to programming the PIC 32, programmers who want to look into using MPLAB Harmony 3, and programmers who want to understand how C instructions work with respect to 32bit PIC. Also students who are studying for an engineering qualification.

Author(s): Hubert Ward
Series: Maker Innovations Series
Publisher: Apress
Year: 2023

Language: English
Pages: 779

Table of Contents
About the Author
About the Technical Reviewer
The Aims and Objectives of the Book
Introduction
Chapter 1: Creating Our First C Program
What Is MPLAB X
Creating a Project in MPLAB X
The Initial Comments
The Speed of the Clock
Writing the Configuration Words
Our First Program
Setting the PORTS
Exercise 1-1
The Hexadecimal Number System
The First Program Listing
Analysis of Listing 1-1
Simulating the Program in MPLAB X
Solution to Exercise 1-1
Summary
Chapter 2: Header Files and Delays
What Are Header Files and Why We Use Them?
Creating a Header File
Using the Header File
Slowing the PIC Down
Creating a Delay
The Variable Delay Subroutine
What Is a Subroutine
The Analysis of the Delay Subroutine
Creating a 1ms Delay
Good Programming Practice
The Algorithm
The Flowchart
Our First Useful Program
The Algorithm
The Allocation List
The Flowchart for the Program
Creating the Project
Analysis of Listing 2-3
Downloading the Program to a Prototype Board
Extending the Program to the Crossroads Traffic Lights
The Algorithm
Analysis of Listing 2-4
Simulating the Program Within MPLAB X
Summary
Chapter 3: The Seven-Segment Display
Controlling a Seven-Segment Display
The Seven-Segment Display
Common Anode Seven-Segment Display
Common Cathode Seven-Segment Display
Controlling the Display with the PIC
The Seven-Segment Display Program
The Algorithm
The Flowchart
The Listing for the Seven-Segment Display Program
Analysis of Listing 3-1
Improving the Seven-Segment Display Program
The Problem with the Program
Arrays
Using Pointers
Analysis of Listing 3-2
The Improved Program
Exercise 3-1
Solution to Exercise 3-1
Summary
Chapter 4: The LCD
The 1602 LCD
Instruction or Command Mode
Data Mode
The Control Pins of the LCD
The LCD Header File for PORTE
Analysis of Listing 4-1
Analysis of Listing 4-2
Creating Your Own Symbols to Display on the LCD
The Pixel Maps
The 8-Bit Binary Values for the Four Special Characters
The Program “Pixel to Matrix”
The Special Character Program
The Program Listing for the Special Character Program
Analysis of Listing 4-3
Summary
Chapter 5: The Dot Matrix Display
The 8 by 8 Dot Matrix Board
The Single Dot Matrix Display
The Max7219 Driver IC
Writing to the Max7219
Analysis of Listing 5-1
Creating the Data for Each Row in the Two-Dimensional Array
Controlling Four 64-Bit Dot Matrix Boards
Analysis of Listing 5-3
Analysis of Listing 5-2
A Program to Scroll Text on the Matrix Display
Summary
Chapter 6: Communication
The 25LC256 EEPROM
What Is an EEPROM Device
Writing to the EEPROM
Reading from the EEPROM
The Connections of the EEPROM on the Explorer 16 Development Board
The Algorithm for the EEPROM Program
Analysis of Listing 6-1
The Setting of the CKE and CKP Bits
The Message “Ann Ward”
Using the Parallel Master Port
The Data for the PMMODE Control Register
The PMCON Control Register
The PMAEN Control Register
Analysis of Listing 6-2
Analysis of Listing 6-3
Summary
Chapter 7: The I2C Communication
The I2C Protocol
I2C Communication Protocol
Writing to the Slave
The Start Bit
The Address Bits and Control Bit in the First Byte
The Acknowledgment Bit
The Data Byte
The Stop Bit
Reading from the Slave
The NACK or Not Acknowledgment Bit
Analysis of the I2C Protocol Header File
The I2C Expander Module
The LCD2004
The Connections of the I2C Expander
Analysis of Listing 7-2
The Program to Use the I2C Expander to Control the LCD2004
Analysis of Listing 7-3
Summary
Chapter 8: Interrupts
Interrupts
The Fetch and Execute Cycle
The Program Counter or “PC”
Single Vectored Interrupts
Multivectored Interrupts
How Does the PIC Keep Track of Where It Must Go and Where It Must Get Back To?
The Stack and Its Main Use
The Sources of Interrupts in a 32-Bit PIC
Analysis of Listing 8-1
Using More Than One Interrupt Source with Single Vector Mode
Analysis of Listing 8-2
Interrupt Priority
Multivectored Interrupts with Priority
Analysis of Listing 8-3
Summary
Chapter 9: The Real-Time Clock
The External Crystal 32.768kHz Oscillator
The 24-Hour LCD Clock Program
Analysis of Listing 9-1
The TM1637 and the Four Seven-Segment Displays
The TM1637 Driver IC
Analysis of Listing 9-2
Summary
Chapter 10: The Real-Time Clock and the DS3231
The DS3231 RTC Module
The Order the DS3231 Expects the Data
The Program Algorithm
Displaying the Temperature
Binary Numbers
Adding Binary Numbers
Reading the Temperature from the TC72
Examples of the Two’s Complement Process
Using the UART and a Terminal Software
Using the Alarms of the DS3231
The UART
The UxMODE Control Register
The UxSTA Register
Using Tera Term
Connecting the Devices to the PIC32
Analysis of Listing 10-1
Summary
Chapter 11: The RTCC Module
The RTCC Module of the 32-Bit PIC
BCD (Binary-Coded Decimal)
Displaying the 32-Bit Value on the LCD
RTCC Module Program
Analysis of Listing 11-1
Understanding Instruction on Line 504 RTCALRM = 0x8403;
Summary
Chapter 12: The Real Analog World
The Real-World Signals
An Analog Signal
The Digital World
A Simple Voltmeter Program
The Algorithm of the Voltmeter Program
Analysis of Listing 12-1
The ADC Process
The Acquisition Time
The 4–20mA Transducer
Controlling Two Analog Inputs
Analysis of Listing 12-2
Summary
Chapter 13: The DHT11 Transducer
The DHT11 Humidity and Temperature Sensor
Communicating with the DHT11
The Use of a Pull-Up Resistor
The DHT11 Program Listing
Analysis of Listing 13-1
The Logical OR and AND Truth Tables
A Design Procedure
Know the Events You Want to Control
Analysis of Listing 13-2
Simulating the Program in MPLAB X
Summary
Chapter 14: Creating a Square Wave
Creating a Simple Square Wave
Using the Output Compare Module (OCMP) of the PIC32
Using the Logic Analyzer Within MPLAB X
Creating Some Musical Notes
Creating a PWM Square Wave with the OC1MP Module
The Average of a Square Wave
Analysis of Listing 14-4
Varying the Brightness of a Lamp
Analysis of Listing 14-5
Summary
Appendix
Data Types and Memory
The Program Memory Area
The Data RAM
The Data EEPROM
Variables
Appendix 1: Data Types
Appendix 2: Some Useful Definitions
Appendix 3: Mathematical and Logic Operators
Appendix 4: Keywords
Appendix 5: Numbering Systems Within Microprocessor-Based Systems
Introduction
Binary Numbers
Converting Decimal to Binary
Example 1
Converting from Binary to Decimal
Example 2
Exercise 1
Adding and Subtracting Binary Numbers
Exercise 2
Worked Example
Subtracting Binary Numbers
Exercise 3
Worked Example
The Hexadecimal Number System
Exercise 4
Worked example
Appendix 6: The ASCII Character Set
Appendix 7: The LCD Instruction Set
Index
Capture.PNG
Capture.PNG