100% found this document useful (1 vote)
8 views

Introductory Programs with the 32-bit PIC Microcontroller: A Line-by-Line Code Analysis and Reference Guide for Embedded Programming in C 1st Edition Hubert Ward pdf download

The document is a reference guide for embedded programming using the 32-bit PIC microcontroller, authored by Hubert Ward. It provides a line-by-line code analysis and covers various topics including creating C programs, using header files, and controlling displays. The guide is designed for beginners and includes practical exercises and examples to enhance learning.

Uploaded by

tiepenbaasch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
8 views

Introductory Programs with the 32-bit PIC Microcontroller: A Line-by-Line Code Analysis and Reference Guide for Embedded Programming in C 1st Edition Hubert Ward pdf download

The document is a reference guide for embedded programming using the 32-bit PIC microcontroller, authored by Hubert Ward. It provides a line-by-line code analysis and covers various topics including creating C programs, using header files, and controlling displays. The guide is designed for beginners and includes practical exercises and examples to enhance learning.

Uploaded by

tiepenbaasch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Introductory Programs with the 32-bit PIC

Microcontroller: A Line-by-Line Code Analysis


and Reference Guide for Embedded Programming in
C 1st Edition Hubert Ward install download
https://ebookmeta.com/product/introductory-programs-with-
the-32-bit-pic-microcontroller-a-line-by-line-code-analysis-and-
reference-guide-for-embedded-programming-in-c-1st-edition-hubert-
ward/

Download more ebook from https://ebookmeta.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmeta.com
to discover even more!

Introductory Programs with the 32-bit PIC


Microcontroller: A Line-by-Line Code Analysis and
Reference Guide for Embedded Programming in C 1st
Edition Hubert Ward
https://ebookmeta.com/product/introductory-programs-with-
the-32-bit-pic-microcontroller-a-line-by-line-code-analysis-and-
reference-guide-for-embedded-programming-in-c-1st-edition-hubert-
ward/

Programming Arduino Projects with the PIC


Microcontroller: A Line-By Line Code Analysis and
Complete Reference Guide for Embedded Programming in C
1st Edition Hubert Henry Ward
https://ebookmeta.com/product/programming-arduino-projects-with-
the-pic-microcontroller-a-line-by-line-code-analysis-and-
complete-reference-guide-for-embedded-programming-in-c-1st-
edition-hubert-henry-ward/

C Programming for the PIC Microcontroller: Demystify


Coding with Embedded Programming Hubert Henry Ward

https://ebookmeta.com/product/c-programming-for-the-pic-
microcontroller-demystify-coding-with-embedded-programming-
hubert-henry-ward/

Lonely Planet Seoul 10th Edition Thomas O'Malley

https://ebookmeta.com/product/lonely-planet-seoul-10th-edition-
thomas-omalley/
Let s Taco Bout Love 1st Edition Mcfadden Melrose

https://ebookmeta.com/product/let-s-taco-bout-love-1st-edition-
mcfadden-melrose/

Medical Microbiology 8th Edition Michael A. Pfaller

https://ebookmeta.com/product/medical-microbiology-8th-edition-
michael-a-pfaller/

Shadow Hill 1st Edition Thomas Kies

https://ebookmeta.com/product/shadow-hill-1st-edition-thomas-
kies/

Machine Learning with PySpark: With Natural Language


Processing and Recommender Systems Pramod Singh

https://ebookmeta.com/product/machine-learning-with-pyspark-with-
natural-language-processing-and-recommender-systems-pramod-singh/

Statistics and Analysis of Scientific Data (Graduate


Texts in Physics), 3rd Edition Massimiliano Bonamente

https://ebookmeta.com/product/statistics-and-analysis-of-
scientific-data-graduate-texts-in-physics-3rd-edition-
massimiliano-bonamente/
Ultrashort Pulse Lasers and Ultrafast Phenomena 1st
Edition Takayoshi Kobayashi

https://ebookmeta.com/product/ultrashort-pulse-lasers-and-
ultrafast-phenomena-1st-edition-takayoshi-kobayashi/
MAKER
I N N O VAT I O N S
SERIES

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

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

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

ISBN-13 (pbk): 978-1-4842-9050-7 ISBN-13 (electronic): 978-1-4842-9051-4


https://doi.org/10.1007/978-1-4842-9051-4

Copyright © 2023 by Hubert Ward


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.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Susan McDermott
Development Editor: James Markham
Coordinating Editor: Jessica Vakili
Distributed to the book trade worldwide by Springer Science+Business Media New York,
1 New York Plaza, New York, NY 10004. 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.
For information on translations, please e-mail [email protected]; for
reprint, paperback, or audio rights, please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on the Github repository: https://github.com/Apress/Introductory-­
Programs-­with-the-32bit-PIC-Microcontroller. For more detailed information, please visit http://
www.apress.com/source-code.
Printed on acid-free paper
Dedicated to my wife Ann.
You have been so supportive and patient while
I typed away, forever, on my laptop.
Love always.
Pinch Pinch Pinch
Table of Contents
About the Author��������������������������������������������������������������������������������xv

About the Technical Reviewer����������������������������������������������������������xvii

The Aims and Objectives of the Book������������������������������������������������xix

Introduction���������������������������������������������������������������������������������������xxi

Chapter 1: Creating Our First C Program����������������������������������������������1


What Is MPLAB X���������������������������������������������������������������������������������������������������1
Creating a Project in MPLAB X������������������������������������������������������������������������������2
The Initial Comments������������������������������������������������������������������������������������������11
The Speed of the Clock���������������������������������������������������������������������������������������13
Writing the Configuration Words�������������������������������������������������������������������������16
Our First Program������������������������������������������������������������������������������������������������20
Setting the PORTS�����������������������������������������������������������������������������������������������22
Exercise 1-1��������������������������������������������������������������������������������������������������������23
The Hexadecimal Number System����������������������������������������������������������������������23
The First Program Listing������������������������������������������������������������������������������������28
Analysis of Listing 1-1����������������������������������������������������������������������������������������30
Simulating the Program in MPLAB X�������������������������������������������������������������������36
Solution to Exercise 1-1��������������������������������������������������������������������������������������44
Summary������������������������������������������������������������������������������������������������������������44

v
Table of Contents

Chapter 2: Header Files and Delays����������������������������������������������������47


What Are Header Files and Why We Use Them?��������������������������������������������������47
Creating a Header File�����������������������������������������������������������������������������������48
Using the Header File������������������������������������������������������������������������������������52
Slowing the PIC Down�����������������������������������������������������������������������������������56
Creating a Delay��������������������������������������������������������������������������������������������56
The Variable Delay Subroutine�����������������������������������������������������������������������61
What Is a Subroutine�������������������������������������������������������������������������������������61
The Analysis of the Delay Subroutine������������������������������������������������������������62
Creating a 1ms Delay������������������������������������������������������������������������������������66
Good Programming Practice�������������������������������������������������������������������������������68
The Algorithm������������������������������������������������������������������������������������������������68
The Flowchart������������������������������������������������������������������������������������������������69
Our First Useful Program�������������������������������������������������������������������������������70
The Algorithm������������������������������������������������������������������������������������������������������70
The Allocation List�����������������������������������������������������������������������������������������72
The Flowchart for the Program����������������������������������������������������������������������72
Creating the Project���������������������������������������������������������������������������������������73
Analysis of Listing 2-3�����������������������������������������������������������������������������������76
Downloading the Program to a Prototype Board�������������������������������������������������77
Extending the Program to the Crossroads Traffic Lights�������������������������������80
The Algorithm������������������������������������������������������������������������������������������������82
Analysis of Listing 2-4�����������������������������������������������������������������������������������87
Simulating the Program Within MPLAB X������������������������������������������������������88
Summary������������������������������������������������������������������������������������������������������������91

vi
Table of Contents

Chapter 3: The Seven-Segment Display����������������������������������������������93


Controlling a Seven-Segment Display�����������������������������������������������������������������93
The Seven-Segment Display�������������������������������������������������������������������������93
Common Anode Seven-Segment Display������������������������������������������������������94
Common Cathode Seven-Segment Display���������������������������������������������������95
Controlling the Display with the PIC��������������������������������������������������������������97
The Seven-Segment Display Program�����������������������������������������������������������99
The Algorithm������������������������������������������������������������������������������������������������99
The Flowchart����������������������������������������������������������������������������������������������100
The Listing for the Seven-Segment Display Program����������������������������������102
Analysis of Listing 3-1���������������������������������������������������������������������������������104
Improving the Seven-Segment Display Program�����������������������������������������106
The Problem with the Program��������������������������������������������������������������������107
Arrays����������������������������������������������������������������������������������������������������������107
Using Pointers���������������������������������������������������������������������������������������������110
Analysis of Listing 3-2���������������������������������������������������������������������������������111
The Improved Program��������������������������������������������������������������������������������113
Exercise 3-1������������������������������������������������������������������������������������������������118
Solution to Exercise 3-1������������������������������������������������������������������������������118
Summary����������������������������������������������������������������������������������������������������������118

Chapter 4: The LCD���������������������������������������������������������������������������119


The 1602 LCD����������������������������������������������������������������������������������������������������119
Instruction or Command Mode��������������������������������������������������������������������������121
Data Mode���������������������������������������������������������������������������������������������������������121
The Control Pins of the LCD������������������������������������������������������������������������������122
The LCD Header File for PORTE�������������������������������������������������������������������������123
Analysis of Listing 4-1��������������������������������������������������������������������������������������128
Analysis of Listing 4-2��������������������������������������������������������������������������������������149

vii
Table of Contents

Creating Your Own Symbols to Display on the LCD�������������������������������������������152


The Pixel Maps��������������������������������������������������������������������������������������������������153
The 8-Bit Binary Values for the Four Special Characters����������������������������������156
The Program “Pixel to Matrix”��������������������������������������������������������������������������158
The Special Character Program������������������������������������������������������������������������160
The Program Listing for the Special Character Program����������������������������������161
Analysis of Listing 4-3��������������������������������������������������������������������������������������166
Summary����������������������������������������������������������������������������������������������������������171

Chapter 5: The Dot Matrix Display����������������������������������������������������173


The 8 by 8 Dot Matrix Board�����������������������������������������������������������������������������173
The Single Dot Matrix Display���������������������������������������������������������������������������173
The Max7219 Driver IC��������������������������������������������������������������������������������������174
Writing to the Max7219�������������������������������������������������������������������������������������175
Analysis of Listing 5-1��������������������������������������������������������������������������������������183
Creating the Data for Each Row in the Two-Dimensional Array������������������������184
Controlling Four 64-Bit Dot Matrix Boards��������������������������������������������������������196
Analysis of Listing 5-3��������������������������������������������������������������������������������������225
Analysis of Listing 5-2��������������������������������������������������������������������������������������227
A Program to Scroll Text on the Matrix Display�������������������������������������������������233
Summary����������������������������������������������������������������������������������������������������������250

Chapter 6: Communication���������������������������������������������������������������251
The 25LC256 EEPROM��������������������������������������������������������������������������������������256
What Is an EEPROM Device�������������������������������������������������������������������������������256
Writing to the EEPROM��������������������������������������������������������������������������������������258
Reading from the EEPROM��������������������������������������������������������������������������������260

viii
Table of Contents

The Connections of the EEPROM on the Explorer 16 Development Board��������261


The Algorithm for the EEPROM Program�����������������������������������������������������������262
Analysis of Listing 6-1��������������������������������������������������������������������������������������267
The Setting of the CKE and CKP Bits�����������������������������������������������������������������276
The Message “Ann Ward”���������������������������������������������������������������������������������286
Using the Parallel Master Port��������������������������������������������������������������������������287
The Data for the PMMODE Control Register������������������������������������������������288
The PMCON Control Register�����������������������������������������������������������������������289
The PMAEN Control Register�����������������������������������������������������������������������290
Analysis of Listing 6-2��������������������������������������������������������������������������������������295
Analysis of Listing 6-3��������������������������������������������������������������������������������������300
Summary����������������������������������������������������������������������������������������������������������309

Chapter 7: The I2C Communication���������������������������������������������������311


The I2C Protocol�������������������������������������������������������������������������������������������������311
I2C Communication Protocol������������������������������������������������������������������������������311
Writing to the Slave�������������������������������������������������������������������������������������������313
The Start Bit�������������������������������������������������������������������������������������������������313
The Address Bits and Control Bit in the First Byte���������������������������������������313
The Acknowledgment Bit�����������������������������������������������������������������������������314
The Data Byte����������������������������������������������������������������������������������������������314
The Stop Bit�������������������������������������������������������������������������������������������������314
Reading from the Slave�������������������������������������������������������������������������������������315
The NACK or Not Acknowledgment Bit��������������������������������������������������������������317
Analysis of the I2C Protocol Header File������������������������������������������������������������321
The I2C Expander Module����������������������������������������������������������������������������������330
The LCD2004�����������������������������������������������������������������������������������������������������331

ix
Table of Contents

The Connections of the I2C Expander����������������������������������������������������������������332


Analysis of Listing 7-2��������������������������������������������������������������������������������������339
The Program to Use the I2C Expander to Control the LCD2004�������������������������354
Analysis of Listing 7-3��������������������������������������������������������������������������������������357
Summary����������������������������������������������������������������������������������������������������������362

Chapter 8: Interrupts������������������������������������������������������������������������365
Interrupts����������������������������������������������������������������������������������������������������������365
The Fetch and Execute Cycle����������������������������������������������������������������������������367
The Program Counter or “PC”����������������������������������������������������������������������367
Single Vectored Interrupts���������������������������������������������������������������������������368
Multivectored Interrupts������������������������������������������������������������������������������369
How Does the PIC Keep Track of Where It Must Go and Where It Must
Get Back To?������������������������������������������������������������������������������������������������369
The Stack and Its Main Use�������������������������������������������������������������������������370
The Sources of Interrupts in a 32-Bit PIC����������������������������������������������������������372
Analysis of Listing 8-1��������������������������������������������������������������������������������������380
Using More Than One Interrupt Source with Single Vector Mode���������������������391
Analysis of Listing 8-2��������������������������������������������������������������������������������������394
Interrupt Priority������������������������������������������������������������������������������������������������397
Multivectored Interrupts with Priority���������������������������������������������������������������398
Analysis of Listing 8-3��������������������������������������������������������������������������������������401
Summary����������������������������������������������������������������������������������������������������������404

Chapter 9: The Real-Time Clock�������������������������������������������������������405


The External Crystal 32.768kHz Oscillator��������������������������������������������������������406
The 24-Hour LCD Clock Program����������������������������������������������������������������������406
Analysis of Listing 9-1��������������������������������������������������������������������������������������422
The TM1637 and the Four Seven-Segment Displays����������������������������������������436

x
Table of Contents

The TM1637 Driver IC����������������������������������������������������������������������������������������437


Analysis of Listing 9-2��������������������������������������������������������������������������������������455
Summary����������������������������������������������������������������������������������������������������������463

Chapter 10: The Real-Time Clock and the DS3231���������������������������465


The DS3231 RTC Module�����������������������������������������������������������������������������������465
The Order the DS3231 Expects the Data�����������������������������������������������������������476
The Program Algorithm�������������������������������������������������������������������������������������477
Displaying the Temperature������������������������������������������������������������������������������478
Binary Numbers������������������������������������������������������������������������������������������������479
Adding Binary Numbers������������������������������������������������������������������������������������482
Reading the Temperature from the TC72����������������������������������������������������������483
Examples of the Two’s Complement Process����������������������������������������������������484
Using the UART and a Terminal Software����������������������������������������������������������490
Using the Alarms of the DS3231�����������������������������������������������������������������������490
The UART�����������������������������������������������������������������������������������������������������������491
The UxMODE Control Register���������������������������������������������������������������������������492
The UxSTA Register�������������������������������������������������������������������������������������������496
Using Tera Term�������������������������������������������������������������������������������������������������500
Connecting the Devices to the PIC32����������������������������������������������������������������505
Analysis of Listing 10-1������������������������������������������������������������������������������������528
Summary����������������������������������������������������������������������������������������������������������556

Chapter 11: The RTCC Module����������������������������������������������������������559


The RTCC Module of the 32-Bit PIC�������������������������������������������������������������������559
BCD (Binary-Coded Decimal)����������������������������������������������������������������������������565
Displaying the 32-Bit Value on the LCD�������������������������������������������������������������572
RTCC Module Program��������������������������������������������������������������������������������������572

xi
Table of Contents

Analysis of Listing 11-1������������������������������������������������������������������������������������591


Understanding Instruction on Line 504 RTCALRM = 0x8403;����������������������612
Summary����������������������������������������������������������������������������������������������������������618

Chapter 12: The Real Analog World��������������������������������������������������619


The Real-World Signals�������������������������������������������������������������������������������������619
An Analog Signal�����������������������������������������������������������������������������������������������620
The Digital World�����������������������������������������������������������������������������������������������620
A Simple Voltmeter Program�����������������������������������������������������������������������������621
The Algorithm of the Voltmeter Program�����������������������������������������������������622
Analysis of Listing 12-1������������������������������������������������������������������������������������625
The ADC Process�����������������������������������������������������������������������������������������������628
The Acquisition Time�����������������������������������������������������������������������������������������628
The 4–20mA Transducer������������������������������������������������������������������������������640
Controlling Two Analog Inputs���������������������������������������������������������������������������641
Analysis of Listing 12-2������������������������������������������������������������������������������������644
Summary����������������������������������������������������������������������������������������������������������648

Chapter 13: The DHT11 Transducer��������������������������������������������������649


The DHT11 Humidity and Temperature Sensor�������������������������������������������������649
Communicating with the DHT11�����������������������������������������������������������������������650
The Use of a Pull-Up Resistor���������������������������������������������������������������������������655
The DHT11 Program Listing������������������������������������������������������������������������������655
Analysis of Listing 13-1������������������������������������������������������������������������������������659
The Logical OR and AND Truth Tables���������������������������������������������������������������669
A Design Procedure������������������������������������������������������������������������������������������671
Know the Events You Want to Control����������������������������������������������������������671
Analysis of Listing 13-2������������������������������������������������������������������������������������678

xii
Table of Contents

Simulating the Program in MPLAB X�����������������������������������������������������������������683


Summary����������������������������������������������������������������������������������������������������������689

Chapter 14: Creating a Square Wave������������������������������������������������691


Creating a Simple Square Wave������������������������������������������������������������������������691
Using the Output Compare Module (OCMP) of the PIC32����������������������������������693
Using the Logic Analyzer Within MPLAB X��������������������������������������������������������698
Creating Some Musical Notes���������������������������������������������������������������������������704
Creating a PWM Square Wave with the OC1MP Module�����������������������������������708
The Average of a Square Wave�������������������������������������������������������������������������708
Analysis of Listing 14-4������������������������������������������������������������������������������������717
Varying the Brightness of a Lamp���������������������������������������������������������������������720
Analysis of Listing 14-5������������������������������������������������������������������������������������724
Summary����������������������������������������������������������������������������������������������������������728

Appendix�������������������������������������������������������������������������������������������729

Appendix 1: Data Types��������������������������������������������������������������������731

Appendix 2: Some Useful Definitions������������������������������������������������733

Appendix 3: Mathematical and Logic Operators�������������������������������735

Appendix 4: Keywords����������������������������������������������������������������������737

 ppendix 5: Numbering Systems Within Microprocessor-Based


A
Systems��������������������������������������������������������������������������������������������739

Appendix 6: The ASCII Character Set������������������������������������������������749

Appendix 7: The LCD Instruction Set������������������������������������������������751

Index�������������������������������������������������������������������������������������������������753

xiii
About the Author
Hubert Ward has nearly 25 years of experience as a college lecturer
delivering the BTEC, and now Pearson’s, Higher National Certificate and
Higher Diploma in Electrical and Electronic Engineering. Hubert has a 2.1
Honors Bachelor’s Degree in Electrical and Electronic Engineering. Hubert
has also worked as a consultant in embedded programming. His work has
established his expertise in the assembler and C programming languages,
within the MPLAB X IDE from Microchip, as well as designing electronic
circuits and PCBs using ECAD software. Hubert was also the UK technical
expert in Mechatronics for three years, training the UK team and taking
them to enter in the Skills Olympics in Seoul 2001, resulting in one of the
best outcomes to date for the UK in Mechatronics.

xv
About the Technical Reviewer
Mike McRoberts is the author of Beginning Arduino by Apress. He is
the winner of Pi Wars 2018 and a member of Medway Makers. He is an
Arduino and Raspberry Pi enthusiast.

xvii
The Aims and Objectives
of the Book
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.
The Objectives of the Book
After reading this book, you should be able to do some or all of the
following:

• Create a project using MPLAB X

• Configure the 32-bit PIC

• Write local and global header files


• Write programs using timers to create a delay

• Understand a range of C instructions

• Create one-dimensional and two-dimensional


arrays in C

• Write C programs to control and use the following


displays:

• Seven-segment displays

• 8 by 8 dot matrix displays

• Liquid Crystal Display, LCD

xix
The Aims and Objectives of the Book

• Create a real-time clock

• Use analog inputs

• Create square waves and PWM

• Control RGB LEDs using the PWM

• Write programs that use interrupts

• Understand and use a range of logic operations

• Understand and use binary and hexadecimal


number systems

xx
Introduction
This book will introduce you to the exciting world of embedded
programming. It will teach you how the main functions of C programming
work and how you can use them to control a 32-bit PIC microcontroller.
You might think that a 32-bit PIC is too far advanced to start with and
that you should try a simpler PIC first. Well, really, that’s not the case. You
can start with all the simple programs, such as simply turning on and off
an LED, using a simple delay, controlling a seven-segment display, and
writing a simple message to an LCD, with this 32-bit PIC. However, you can
progress to the more advanced programs that are out of reach with most of
the 8-bit PICs.
It really comes down to cost. I use the explorer 16 development
board with a plugin board for the 32-bit PIC. The development board,
shown in Figure 2, comes in at around £100, and the plugin 32-bit PIC,
shown in Figure 1, comes in at around £30. On top of that, you will need a
programming tool to download your programs to the PIC. I use the ICD3
can or the PICKIT3 tool. The ICD3 can costs around £70. However, this
is no longer available, and the ICD4 can is very expensive. They do have
the PICKIT4 which I believe programs 32-bit PICs, and that costs around
£70. So, you could possibly be looking at spending around £200 or more.
But you should see it as an investment in your career as an embedded
programmer. Working as an embedded programmer, one can earn around
£40 to £100 an hour if not more.
If you went down the 8-bit PIC route first, you would spend less, but
your programming experience could be greatly reduced.

xxi
Introduction

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.

MPLAB X
This is the IDE, integrated development environment, from Microchip. I
will be using versions 5.25 and 5.40, but any version from 2.2 will be fine
as there will be only some minor differences. Also, as I know Microchip is
constantly updating its IDE, I am confident that the changes will only be
minor. This means the procedures for the IDE that you will learn from this
book will still stand you in good stead for any later versions of the IDE.

Compiler Software
You will need a 32-bit compiler software to compile your programs within
MPLAB X. I use v2.41, although for a lot of my programs, I use the older
version 1.32, as the newer versions have some of the libraries missing.
MPLAB X and the compiler software are freely available from the
Microchip website. You may have to search the archive section for some of
the older compiler versions. You could upgrade to the paid version of the
compiler software, but I have not done that for any of my programs. If you
were to make a career in embedded programming, then the paid version of
the compiler may have some advantages, such as more efficient code that
uses less memory.

xxii
Introduction

Simulation Software
MPLAB X does come with a simulation option for its PICs. This does mean
that you can try some of the programs without spending any money on
equipment. However, the simulation capabilities are not very extensive,
and they are used more for debugging your programs. I will take you
through using the simulation option within MPLAB X for some of the
programs in the book, but most will be carried out on the explorer 16
development board. However, simulating the programs will give you a
chance to see if you can enjoy programming these PICs. It is not just about
making money, as you will earn more if you enjoy what you do.
There are some ECAD (electronic computer-aided design) software
that you can use to simulate the programs. PROTEUS is one that I have
used for 8-bit PICs. Multisim is another one, but you must make sure that it
can support the PICs you want to use, and they are not free.

MPLAB Harmony
Microchip has invested a lot of time and money in creating its
programming environment named MPLAB Harmony. It is aimed at saving
a lot of time in developing PIC programs. If you just want to get something
working and are not really interested in learning how it works, then this
environment can be useful. However, be aware that once you go down
that route, you can become dependent upon the environment, as you are
not really learning how the programs work. That’s great for Microchip but
not for you. I believe this approach is counterproductive, as it does far
too much for you and doesn’t explain how it works. If you really want to
become a versatile and productive programmer, then you need to really
understand how every bit of your program works. Only then can you really
say you wrote it and not just cobbled together bits of code that you don’t
understand. My main aim in writing this book is to show that it is not too

xxiii
Introduction

difficult to learn how to program these PICs yourself. If you learn how PICs
work and how we can write C instructions to make them do what we want,
then you will become that versatile and experienced programmer that will
find an exciting and useful career as an embedded programmer.
Why am I writing this book and not working as that embedded
programmer? Well, really, I ventured into programming PICs a bit too
late in my life. I have been in education, teaching electrical and electronic
engineering at HND level, for over 25 years. So now I am ready to retire
and concentrate on trying to pass my knowledge and experience onto you
younger guys. I hope that in reading this book, you will learn enough to
become that versatile and productive embedded programmer.

Why the 32-Bit PIC


I have been working with PIC microcontrollers since 1996, using them to
teach the micros unit in the BTEC Higher National Certificate and Higher
National Diploma course. In those early days, I used the 8-bit PICs starting
off with the 16F88 and the 16F884, writing programs in assembly language.
I actually started teaching the micros unit with the “Emma” boards that
used the 6502 microprocessor. That did give me a great foundation in
micros, but the PICs were much more versatile and relevant. My favorite
8-bit PIC is the 18F4525. I have written three textbooks on that PIC, but
now it is time to start writing about an amazing 32-bit PIC that I have used,
the 32MX360F512L. A picture of this PIC is shown in Figure 1.

xxiv
Introduction

Figure 1.The PIC32MX360F512L Microcontroller

The name of the PIC is quite a mouthful, but then that is Microchip for
you. Microchip does make an extensive range of PICs.
There is one big drawback with this PIC, compared to the PIC18F4525,
and that is, it is a surface mount device, whereas the PIC18F4525 comes in
a 40-pin dual in-line package. This does make the 32-bit PIC much more
difficult to use if you want to make simple circuit boards, on say some
veroboard. It is a 100-pin device, and it can be bought from Microchip on
a small self-contained board, as shown in Figure 1, that you could mount
on some veroboard, but you would have to create a 100-pin housing for the
PIC. The cost of this self-contained board with the PIC is around £30.
A simple way of using this type of PIC is to use the explorer 16
development board that I use. A picture of this is shown in Figure 2. When I
bought it, some eight years ago, it came with a 24-bit PIC already included
with it, so I had to buy the 32-bit PIC separately.

xxv
Introduction

Figure 2.The Explorer 16 Development Board from Microchip

This is a very useful board that has the following peripheral devices:

• An LCD, Liquid Crystal Display

• A variable resistor to provide an analog input

• Four switches connected to four inputs

• Eight LEDs connected to PORTA

• A 25LC256 256k serial EEPROM

• A TC1074A temperature sensor

These are just a few of the devices that are available to you on the
explorer board. All these make the board a very useful addition to your
embedded programming equipment. This can easily justify the cost of
around £100.
You will need a programming tool to download your programs from
MPLAB X to your PIC. There are a couple of tools that you can use, such as

xxvi
Introduction

• ICD3 can that I use but Microchip has now stopped


supplying this. Instead, they supply the ICD4
can. However, this is quite expensive at around £210.

• The PICKIT3 is a much cheaper device at around £30;


however, Microchip has now moved on to the PICKIT4
programmer which they sell for around £70.

• You can buy plugin boards to help expand the explorer


board. One such plugin board is shown in Figure 3. You
will see that I use this extensively in my book.

Figure 3.A Plugin Extension Board

You could be spending around £250 plus, but you should find it a
useful investment as you can earn a high rate of pay as an embedded
programmer. There are cheaper tools you can use, but I can only write
about the ones I have used.
There are many other programming environments, but I know these
that I have shown you work and work well. Indeed, I have used my explorer
boards, with the plugin expansion slot, to control a sophisticated industrial
control system.

xxvii
Introduction

Some Important Aspects of C Programming


Before we move deep into the book, I think it would be useful to mention
some aspects of C programming that we will come across in the analysis of
the program listings.

The Main Loop


All C programs work in loops, usually more than one. However, all C
programs must have the “main” loop. It is to this main loop that the PIC
will go to find the very first instruction of the C program. All the other loops
are within this main loop or are called, as subroutines, from the main loop.

Curly Brackets
C programming uses brackets to define the confines of these loops. These
are what I call “curly brackets,” that is, the opening curly bracket “{“ and the
closing curly bracket “}”. These are used to group a series of instructions
together. For example:

void main ()
{
    instructions go here
}

Normal Brackets
The C programming also uses what I call “normal brackets,” that is, the
opening normal bracket “(“ and the closing normal bracket “)”. These are
used in conjunction with subroutines and subroutine calls.
We will look at these brackets as we analyze the programs in the book.

xxviii
Introduction

Subroutines or Functions or Methods


Subroutines are sections of instructions that lie outside the main loop
of the program. They are used, primarily, to save memory, as if you
want the PIC to carry out a section of instructions in exactly the same
way more than once, then you should write them as a subroutine. Some
programmers may call these “functions” or “methods,” as they normally
carry out a function of some sort. I am an old hat at programming, and so I
call them subroutines.
Subroutines may or may not want values to be passed up to them and
send data back to the main loop of the program. To create a subroutine, we
use the general format as shown here:
keyword name of subroutine (request type of possible value to be
sent up)
The keyword is used to indicate what type of data the subroutine will
send back to the main program. If the subroutine will not send any data
back, which is the most common type of subroutine, then the keyword
would be “void.” However, if the subroutine will be sending data back to
the main program, then the keyword would indicate what data type the
data would be, for example:
unsigned char
int
For example: void delay250 (unsigned char t)
Note the use of the “normal brackets.”
This will create a subroutine that will not be sending data back to
the main program, that is, the keyword “void.” It will be expecting the
main program to pass up a value of the data type “unsigned char.” The
subroutine will copy that value into the local variable “t.”

xxix
Introduction

When the main loop calls these subroutines, it is done using the
general format:
subroutine name (send any variables that the subroutine may require)
For example: delay250(4);
This will call the subroutine called “delay250” and send the value “4” to
be copied into the subroutine’s local variable “t.”

Local and Global Variables


As I have mentioned the term local variable, I thought I should explain
what they are. C programming uses variables, which are newly created
memory locations, whose number of bits depends upon the data type the
variable will be, to store useful values that you will use in your program.
You should give the variable a suitable name that helps explain what the
variable is used for. However, we can use just simple letters if we wanted to.
C has two main types of variables:

• Local variables: These are variables that are created


when we create a subroutine, and they are only valid
for use in the subroutine they were created in.

• Global variables: These are variables that we create


when we construct the whole program. I usually list all
the global variables at the beginning of the program
listing. These global variables can be used anywhere
within the program, that is, the main loop and any
subroutines, etc.

We will look more closely at these aspects of C programming as we


analyze the program listings.

xxx
Introduction

Summary
In this introduction, we have looked at MPLAB X, the IDE from Microchip,
and the possible development boards you may have to buy. I have tried to
explain the aims of this book, and I have detailed some of my experiences
in the hope of convincing you I do know enough about C programming for
PIC micros.
In the first chapter, we will learn how to create a project in MPLAB
X. We will look at creating a header file, within MPLAB X, and use it in our
programs. This header file will look at how we use the configuration words
to set the clocks of the PIC and some other important aspects of the PIC. I
hope you will find this book useful and it will help you get started on your
career as an embedded programmer.

xxxi
CHAPTER 1

Creating Our First C


Program
In this chapter, we will study the process of creating a project for the 32-bit
PIC within Microchip’s IDE, MPLAB X. We will study the important aspect
of creating a clock for the PIC. Then we will create our first program that
will look at the ports and learn how to set the direction of data through the
ports. We will then write a simple program to light an LED and simulate it
within MPLAB X.
After reading this chapter, you will have studied how to create a project
in MPLAB X. You will also have a good understanding of the configuration
words and how we can control the ports, so that we can write our first
simple program.
You will also have used the simulation in MPLAB X to confirm our first
program works correctly.

What Is MPLAB X
MPLAB X is Microchip’s IDE, integrated development environment. It is a
collection of programs designed to make the development of programs for
the PIC microcontrollers an easy and enjoyable process. It uses a dedicated
text editor that has coloring for keywords and uses an IntelliSense that
works like predictive text on a mobile phone. It has a range of debugging

© Hubert Ward 2023 1


H. Ward, Introductory Programs with the 32-bit PIC Microcontroller,
https://doi.org/10.1007/978-1-4842-9051-4_1
Chapter 1 Creating Our First C Program

tools that aid the debugging of your programs. It also has a simulation
software that allows you to simulate most of your PIC programs. All
this and the required compilers are freely available from the Microchip
website.

Creating a Project in MPLAB X


Assuming you have downloaded the software and the compiler, you simply
click the mouse on the following icon to start the program.

Once the software has started, the opening screen, shown in


Figure 1-1, should appear.

Figure 1-1. The Opening Screen of the IDE

2
Chapter 1 Creating Our First C Program

You should appreciate that when you create a project, a new folder will
be created on your hard drive, where all the files and directories for the
project will be stored.
To create a new project, simply click the word “File” in the main menu,
and then select “New Project” from the fly-out menu that appears. An
alternative approach would be to click the mouse on the small orange
box with the green cross in it, which is the second item from the left on
the second menu bar. The “Choose Project” window shown in Figure 1-2
should appear.

Figure 1-2. The Choose Project Window

Most of our projects will be stand-alone Microchip embedded, which


means we can simply click Next here to move on to the next window, as
those options should be highlighted. If the options, shown in Figure 1-2,
are not already highlighted, you should select them now, then click Next.
The next window is the “Select Device” window as shown in Figure 1-3.

3
Chapter 1 Creating Our First C Program

Figure 1-3. The Select Device Window

In this window, we can select the actual PIC we will be using. In the
family slot, you should choose the 32-bit MCUs (PIC32) as shown in
Figure 1-3. Then, in the device slot, select the 32MX360F512L. Once you
are happy with your selections, you should click Next. If you are using a
different 32-bit PIC, then you should select it here.
This will move us onto the “Select Tool” window as shown in
Figure 1-4.

4
Chapter 1 Creating Our First C Program

Figure 1-4. The Select Tool Window

Here, you can select the actual tool you are using to download the
program from MPLAB X to the PIC. In this case, we will simply use the
simulator option of MPLAB X. With this option, we don’t need an actual
PIC, as we will be using the PIC simulator from within MPLAB X.
Once you have selected your chosen tool, you should click Next to
move on to the “Select Compiler” window as shown in Figure 1-5.

5
Chapter 1 Creating Our First C Program

Figure 1-5. The Select Compiler Window

I am assuming you have downloaded the appropriate compiler


software. I have selected the up-to-date version 2.41. However, I also have
the earlier version v1.32 as shown here. As I said earlier, we will need this
older version; you will have to look in the archive section of the Microchip
website to find it. Indeed, it is when we look at using interrupts that we will
use this older version of the compiler software.
Once you have selected your compiler, simply click Next to move on to
the “Select Project Name and Folder” window as shown in Figure 1-6.

6
Chapter 1 Creating Our First C Program

Figure 1-6. The Select Project Name and Folder

With this window, you can give the project a name and decide where to
save the project. I have named the project “ourFirst32Bit,” and I am using
what is termed “Camel Font.” This is where we combine multiple words
to make one long word. The first letter of the first word is in lowercase,
and the first letter of all subsequent words is in uppercase. In this way, the
separate words of the newly created long word are still discernible.
I tend to save the projects on my root directory, but you must save it
where you want.
Once you are happy with your selection, you should click Finish, and
your project will be created.
The screen will now change to the main editing window as shown in
Figure 1-7.

7
Chapter 1 Creating Our First C Program

Figure 1-7. The Screen with the Project Side Menu

Before we finish this chapter, we will create the program file for this
first project. We will call it “ourFirst32BitProg.” To create the file, we will
need to expand the project tree that is shown on the left-side plane in
Figure 1-7. This is simply done by clicking the mouse on the small box
with the + sign in it, at the side of the “ourFirst32Bit” name in the project
tree, as shown in Figure 1-7. If the project tree is not shown, you can click
the mouse on the word “Window” on the main menu bar. You should
then simply click the first choice, which depicts the orange box and word
project. The project tree should then appear. You may have to move the
box with the project tree in it around the screen. This can be done by
clicking and dragging the mouse with the box selected. It will take some
time to master the moving of the different boxes around the screen, so I
will just leave it for you to practice.
Once the project tree is opened, you should right-click the mouse on
the phrase Source Files. Then click the mouse on the option “New” from
the fly-out menu. Then click the option “main_c” that appears. This is
shown in Figure 1-8.

8
Chapter 1 Creating Our First C Program

Figure 1-8. The New Main_C Source File

Once you do that, you will be presented with the “Name and Location”
for the source file as shown in Figure 1-9.

9
Chapter 1 Creating Our First C Program

Figure 1-9. The Name and Location for the Source File

Here, you can give your c file a suitable name. I have given it the name
“ourFirst32BitProg.” The extension for the file is c, as shown in the small
extension window. Once you are happy with the name, you should click
Finish to create the file.
When you do this, you will create a new file that will appear in the main
editing window. However, Microchip does insert a lot of text that you will
not need at this starting level, if ever. Therefore, to make sure we all have
the same window, I would ask you to delete all the text that is inserted in
the file. After deleting that material, you should now have a screen that
looks like that shown in Figure 1-10.

10
Chapter 1 Creating Our First C Program

Figure 1-10. The Empty Source File

We have now created our first project within MPLAB X. I suggest you
go through this process for every project you make. The more times you go
through this process, the easier it becomes.

The Initial Comments


You should always lay claim to your program, you have written it, and you
should be proud of what you have achieved. Therefore, the first thing you
should write in the file is a paragraph of comments along the lines of what
is suggested here:

/* A simple program to control an LED


Written by Mr. H. H. Ward for the PIC32
Dated 12/01/2020.*/

There are two types of comments:

• Single-line comments: This is anything written on


the current line only after two forward slashes. For
example, //the following are just comments, and the
compiler will ignore them.

11
Chapter 1 Creating Our First C Program

• Multiple lines or a paragraph of comments: This is


anything written on one or more lines that are between
the following symbols, for example, /* The next lines
are comments*/.

As this is a paragraph of comments, we should enclose them between


the following symbols: /* */.
My initial comments are shown in Figure 1-11.

Figure 1-11. The Comments for the Program

You will notice I have changed the font of the comments. This is to
make them clearer in the book. If you want to do this, then you should
select the word “Tools” on the main menu. Then select the word “Options”
from the fly-out menu, and you should then be presented with the window
shown in Figure 1-12. You will notice I have selected the term “Fonts &
Colors” from the menu.
You should then be able to change the comments to what you want.
Once you are happy with your changes, simply click OK to close the
window and apply the changes.

12
Chapter 1 Creating Our First C Program

Figure 1-12. The Options Window

The Speed of the Clock


The operation of all microcontrollers is synchronized to a clock signal.
With all PICs, the programmer has total control over the frequency of this
clock signal. It is therefore an important aspect of PIC programs and one
that you should understand how we can control.
Like all PICs, the 32-bit PIC has a wide selection of possible sources for
the oscillator that will form the basis of the clock. There are also different
clocks for different aspects of the 32-bit PIC. Figure 1-13 depicts the block
diagram of the system clock sources.

13
Chapter 1 Creating Our First C Program

Figure 1-13. Block Diagram of the 32-Bit PIC Clock System

There are four sources of oscillators for the PIC. The two external
sources are shown as the primary oscillator with terminals OSCI (oscillator
in) and OSCO (oscillator out) and the secondary oscillator, shown as
SOSCO and SOSCI. The other two oscillator sources are the FRC (fast
resistor capacitor) and the LPRC (low-power resistor capacitor).
The primary oscillator and the FRC can be fed, unaltered, to produce
the clock signal, or they can be passed through a divider circuit, divider
one, then through a PLL (phase lock loop) circuit, where the frequency can
be multiplied, then through a final divider circuit, divider two, before being
used for the various clock signals.

14
Chapter 1 Creating Our First C Program

The secondary oscillator and the LPRC sources are used to generate
a relatively low frequency around 32kHz for the LPRC and an exact
32.768kHz from a crystal oscillator. Both of these can be used for the
system clocks. The secondary oscillator can be used as an accurate source
for timer1 and for the RTCC (Real-Time Clock Calendar). The LPRC can
be used as the source for the WDT (the Watch Dog Timer) and the PWRT
(Power Watch Reset Timer).
The FRC and LPRC are both internal oscillators, and the ability to use an
internal oscillator is useful if you were short of I/O, as with the PIC18F4525.
However, with this 32-bit PIC, I/O, the number of input and output pins, is
not really a problem. Also, as the RC oscillators are not as accurate as crystal
oscillators, I will use the external oscillator sources for my projects.
The primary oscillator on the explorer 16 development board is an
8MHz crystal. The board also has the 32.768kHz crystal as the secondary
source, which can be used to run the RTCC (Real-Time Clock and
Calendar). We have the option of increasing the frequency of the oscillator
to produce a faster clock signal, and to do this, we must run the 8MHz
crystal oscillator through the phase lock loop circuit. However, the phase
lock loop circuit requires an input frequency of 4MHz. That is why we
have the ability to run the oscillator through the divider one first. As we are
using the 8MHz crystal, we must set the first divider to divide by 2.
Once in the phase lock loop, we have the ability to multiply the signal
by a factor of 15 up to a factor of 24. At present time, the maximum clock
frequency for the 32-bit PIC is 72MHz; therefore, we will multiply the
4MHz signal by 18. This means that the output of the phase lock loop will
be a 72MHz clock signal. This means we don’t need to divide the clock
further, so set the divider two to divide by 1, that is, no change.
There is one final division we can make if we wanted to. This is to make
the peripheral bus clock, the PBCLK, run at a lower frequency than the
system clock, the CPU clock. We will choose to run the PBCLK at 36MHz.
This means we need to divide the CPU clock by 2 before applying it to
the PBCLK.

15
Chapter 1 Creating Our First C Program

All of these changes can be achieved by writing the correct data to


the configuration words. As this is something you have to do in all your
projects, then Microchip has made this process as simple as possible.

Writing the Configuration Words


To start the process, simply click the mouse on the word “Window” on the
main menu bar. Then select “Target Memory Views” from the first fly-out
menu. Then select “Configuration Bits” from the second fly-out menu. This
is shown in Figure 1-14.

Figure 1-14. Selecting the Configuration Bits

The display will now show the configuration bits in the bottom half of
the editing window. This is shown in Figure 1-15.

16
Chapter 1 Creating Our First C Program

You can now make the changes that we need as follows:

• Change the second option FPLLIDIV, the input divider


to the phase lock loop, to DIV_2.

• Change the third option FPLLMUL, the PLL multiplier,


to MUL_18.

• Change the fourth option FPLLODIV, the PLL output


divider, to DIV_1.

• Next, change the FNOSC option to PRIPLL, primary


oscillator with phase lock loop. This tells the PIC that
we will be passing the oscillator through the phase lock
loop circuit.

• Next, change the FSOCEN, the secondary oscillator


enable bit, to off to disable the secondary oscillator.

• Next, change the IESO to off to disable the two-speed


startup.

• Next, set the POSCMOD, the primary oscillator mode,


to XT. This is because the 8MHz external crystal is
less than 10MHz which is within the XT range of
frequencies.

• Next, set the FPBDIV, the frequency of the peripheral


bus divider, to DIV_2. This is to reduce the frequency of
the peripheral bus to 36MHz.

• Change the FWDTEN to off. The WDT is the Watch Dog


Timer which will stop the program if nothing happens
for a set period of time. This is really used in production
lines, and we don’t need this for our programs, so we
must turn it off.

17
Another Random Scribd Document
with Unrelated Content
Slowly, his lips stiff, Joe took a pen from his pocket, wrote a note,
and left it on the desk:

Dear Tony: Pop was fronting; he cracked after he left here.


You’ve been swell to me, but this isn’t my game. I’ll be
through next Saturday. I’m sorry.
Joe

He pushed back the chair and stood in thought beside the desk.
As slowly as he had taken the pen from his pocket he reached for
the telephone and called Vic Wylie’s office.
“Will you give Vic a message for me, Miss Robb? We’ve been
auditioning a cast and—I guess I can’t take it. Tell him Pop Bartell’s
been dropped from the Larry show. I’m throwing up my job.”
Miss Robb was dumfounded. “Tony told us he was pleased with
you....”
“I know,” Joe said wearily. “Vic’ll understand.”
Office buildings were emptying, and Royal Street was a packed,
shuffling, slow-moving canyon. Joe moved along with the mass until
he came to the Thomas Carlin store. It was almost the closing hour,
and a lone customer waited for a clerk to wrap a purchase. The
lights were on and facets of radiance gleamed from the polished
glass of show-cases and the polished wood of shelves. To-day Joe
saw the scene with a vision that had been cleaned and washed—its
bright cleanliness, its subdued brilliance of display, its subtle breath
of pride and courtesy and alertness. The faces of all the clerks—
strange that he had never noticed them before: friendly faces that
were not merely a friendly front. The lone customer left. “Hi, Joe.”
The clerks grinned at him. “Hi, boys.” He grinned at them.
“Some new books came in to-day, Joe,” Mr. Fairchild announced.
“Not now,” said Joe. He added an explanation. “Something on my
mind.”
“No!” Mr. Fairchild prodded his ribs. “They’ll toss you out of the
Jitterbug Union for that.”
It wasn’t a brilliant gag. As a gag, it would have been scorned by
any actor making the rounds. But it was good; it was homey.
The store closed, and Joe walked with his father to the parking
lot. The man pushed the key into the ignition lock and reached his
foot for the starter button.
“What is it, Joe? Trouble?”
“Not exactly.” Joe was still thinking of Pop Bartell. “I’m through at
the agency. I’d like to come into the store.”
Tom Carlin’s foot did not touch the button. Horns honked and cars
backed out of symmetrical lines as an attendant directed parking-lot
traffic. Silence, brittle and strange, lingered in the Carlin sedan.
“I—I thought you’d be glad,” Joe said uncertainly.
“Is that your reason for telling me?”
The boy was startled. “Don’t you want me?”
Tom Carlin stepped on the button. The attendant waved an arm
and he shook his head. The motor idled, warming.
“When you told me you wanted to be an actor,” the man said as
though he debated each word, “I let you have your fling. Was there
anything else for me to do? I was afraid of making you a round peg
in a square hole. You failed as an actor. But you still stayed in show
business and turned to production. That’s the rub, Joe.”
“What?” Joe demanded.
“Let me put it this way: After you’d failed—don’t get the idea I’m
rubbing that in; as I told you, I made a miserable failure of my first
job—had you come to me after that first failure, I’d have welcomed
you instantly. A lot of persons change their first desires. I couldn’t
ask for a better man than Fairchild; he started out to be a
mechanical engineer. But you made show business both your first
and second choices. You’ve made the store a very bad third choice.
You leave the impression that you’re coming to me now because
there’s no place else to go. That doesn’t set so well, Joe.”
Joe’s nerves were raw from a day of chafing, and this
disappointment was crushing. “You mean I needn’t come around?”
“I didn’t say that. I’ll have to think this over. I’m trying to see the
situation from your angle as well as from my own. What will it
benefit you in the long run to use the business as a door of escape?
If you come to the store as a place of last resort, you’re still a round
peg in a square hole. It might be better for you to get a job on your
own and try yourself out in somebody else’s business. For a while,
anyway.”
Disappointment and raw nerves united to make the boy stiff with
anger. “All right, Dad, if that’s how you want it.”
“That isn’t how I want it,” Tom Carlin said. “That’s how it seems to
be.”
They said little more to each other on the ride out to the
Northend. But they were talking as they entered the house, and talk
was a lively stream around the dinner table. Front, Joe thought,
wasn’t confined to show business; you could wear a front at home.
The meal over, he found that morning’s Journal and went to his
room.
Something that was part of this house worked its spell. His jagged
nerves relaxed and anger died in him. He began to see his father’s
point of view. He hadn’t been absolutely denied the store; it was
simply that his father questioned the wisdom of having him come in
now. After all, there was a lot to what his father had said. He had
made the store a third choice. He must look like an irresponsible
madcap, a harum-scarum who didn’t know his own mind. Well, he’d
have to prove himself. He could do that. He’d find an outside job and
make good at it. Then, after a year or two ... but a year or two
seemed so long.
He opened the Journal and studied the Help Wanted columns.
Christmas was approaching and all the large stores along Royal
Street were clamoring for packers, sales people, delivery men, and
cashiers. He could probably get a seasonal job at half a dozen
places; certainly, if you rolled up your sleeves and pitched in,
temporary employment might become permanent employment. But
he couldn’t, he thought with a fresh pang, get a job with his own
father.
There was a tap on the door and Tom Carlin walked in. “Joe,
Mother thinks I may have been hasty. Her reactions are usually right.
You’ve heard what I have to say, but I haven’t heard your side. Do
you mind telling me why you were fired?”
Joe stared. “Fired? I wasn’t fired. What gave you that idea? I quit.”
The man reached for a chair, took a pipe from his pocket, and sat
down slowly. “Why did you quit?”
Joe tried to tell him. The story used up a great many words, and
he felt that he was telling it badly.
“I might have made a good producer. Vic thought I would and
Tony told Vic I was doing well. But if I became a producer I’d always
be meeting the sordid side of show business. All I’d see would be
men and women wearing an artificial front. They’d make me a big
stop when I had a show coming up. They’d stay awake nights
planning to catch my attention as Mander caught Tony’s. They’d
audition and try to hide how much a part meant to them. I don’t
mean there isn’t struggle and uncertainty in every other line, but—
oh, show business is different. I’d know too much about small time.
I’d see too much: little corners of shabbiness, things that were mean
and grubby, fine people trying to hide worry and apprehension
behind a front. You get so you hate a front. I’d know they were
getting hard knocks to-day and would get the same hard knocks to-
morrow. I’d know that probably only one in a thousand would ever
make the big time, just as I couldn’t make it and for the same
reason. They’re good, but not good enough. Good enough for small
time, and sustaining shows that pay no salaries, but only good
enough for that. It got under my skin and did something to me. I
used to turn on a radio, and listen to a show, and think it was all
glamorous; there’s very little glamour in show business when you
get behind the scenes. You see too many of the wounds.”
Tom Carlin filled his pipe.
“And then, to-day, Pop Bartell. That finished me. Sooner or later
I’d have been finished, anyway. Vic says I feel too much. Maybe he’s
right. I’m not sorry I tried show business. If I hadn’t, I’d probably
always feel I’d thrown away a chance to see my name up in lights.”
Smoke was a thick cloud around Tom Carlin’s head. “Joe, I
apologize for what I said in the car. I didn’t understand.”
“Does what I told you make a difference?”
“All the difference in the world. Instead of being up in lights, your
name will some day be on a store window.”
“You mean I’m going into the store—now?”
“Any time you’re ready.”
Joe had never known it was possible to feel so good. “I’ll be ready
Monday.”
Tom Carlin knocked ashes from his pipe and refilled the bowl. “I
found a script in your bureau, Joe,” he said serenely.
Joe sat up straight.
“I had copies made and brought the script back. I saw no use
talking to you until I had something to talk about. I wrote to five
publishers—four in New York and one in Boston. Three are willing to
pay a fair share of the cost on a dramatization of their books. One is
doubtful, and one is definitely unfavorable.”
Joe’s voice was eager. “Are we going on the air?”
The man caught that “we.” “I’m more than half convinced we’ll
have a Carlin show next September.”
Joe went hot and cold. Not because this was radio, but because it
was Thomas Carlin Presents.
Tom Carlin puffed his contentment. “Every day we have customers
who telephone and ask to have some purchase delivered at once.
That forces me to take a clerk from behind the counter and send
him out. I’ve been thinking of hiring somebody to take care of some
light work and make these deliveries. This Pop Bartell. Do you think
he could fit into that job after forty years of show business?”
Joe was hot and cold again. “After what happened to-day, Dad,
he’d think somebody had given him a piece of Heaven.”
“Send him in to see me.”
“Perhaps,” said Joe, “I’ll run over a little later to where he lives.
He’ll sleep better to-night.”
After that they sat together in understanding silence.
A light step sounded in the hall and the door was pushed open
timidly. Kate Carlin stood on the threshold. Her eyes, clouded with
concern, went from her husband to her son and back to her
husband again.
“Come in, Kate.”
Suddenly her eyes were clear. She smiled. “This is a night you’ve
looked forward to, Tom, isn’t it?”
“Yes.” He held out his hand to her. “Come in, Kate, and sit with us.
Carlin and Son are in conference.”
*** END OF THE PROJECT GUTENBERG EBOOK YOU'RE ON THE
AIR ***

Updated editions will replace the previous one—the old editions


will be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States
copyright in these works, so the Foundation (and you!) can copy
and distribute it in the United States without permission and
without paying copyright royalties. Special rules, set forth in the
General Terms of Use part of this license, apply to copying and
distributing Project Gutenberg™ electronic works to protect the
PROJECT GUTENBERG™ concept and trademark. Project
Gutenberg is a registered trademark, and may not be used if
you charge for an eBook, except by following the terms of the
trademark license, including paying royalties for use of the
Project Gutenberg trademark. If you do not charge anything for
copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such
as creation of derivative works, reports, performances and
research. Project Gutenberg eBooks may be modified and
printed and given away—you may do practically ANYTHING in
the United States with eBooks not protected by U.S. copyright
law. Redistribution is subject to the trademark license, especially
commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the


free distribution of electronic works, by using or distributing this
work (or any other work associated in any way with the phrase
“Project Gutenberg”), you agree to comply with all the terms of
the Full Project Gutenberg™ License available with this file or
online at www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand,
agree to and accept all the terms of this license and intellectual
property (trademark/copyright) agreement. If you do not agree
to abide by all the terms of this agreement, you must cease
using and return or destroy all copies of Project Gutenberg™
electronic works in your possession. If you paid a fee for
obtaining a copy of or access to a Project Gutenberg™
electronic work and you do not agree to be bound by the terms
of this agreement, you may obtain a refund from the person or
entity to whom you paid the fee as set forth in paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only


be used on or associated in any way with an electronic work by
people who agree to be bound by the terms of this agreement.
There are a few things that you can do with most Project
Gutenberg™ electronic works even without complying with the
full terms of this agreement. See paragraph 1.C below. There
are a lot of things you can do with Project Gutenberg™
electronic works if you follow the terms of this agreement and
help preserve free future access to Project Gutenberg™
electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright
law in the United States and you are located in the United
States, we do not claim a right to prevent you from copying,
distributing, performing, displaying or creating derivative works
based on the work as long as all references to Project
Gutenberg are removed. Of course, we hope that you will
support the Project Gutenberg™ mission of promoting free
access to electronic works by freely sharing Project Gutenberg™
works in compliance with the terms of this agreement for
keeping the Project Gutenberg™ name associated with the
work. You can easily comply with the terms of this agreement
by keeping this work in the same format with its attached full
Project Gutenberg™ License when you share it without charge
with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.

1.E. Unless you have removed all references to Project


Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project
Gutenberg™ work (any work on which the phrase “Project
Gutenberg” appears, or with which the phrase “Project
Gutenberg” is associated) is accessed, displayed, performed,
viewed, copied or distributed:

This eBook is for the use of anyone anywhere in the United


States and most other parts of the world at no cost and
with almost no restrictions whatsoever. You may copy it,
give it away or re-use it under the terms of the Project
Gutenberg License included with this eBook or online at
www.gutenberg.org. If you are not located in the United
States, you will have to check the laws of the country
where you are located before using this eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is


derived from texts not protected by U.S. copyright law (does not
contain a notice indicating that it is posted with permission of
the copyright holder), the work can be copied and distributed to
anyone in the United States without paying any fees or charges.
If you are redistributing or providing access to a work with the
phrase “Project Gutenberg” associated with or appearing on the
work, you must comply either with the requirements of
paragraphs 1.E.1 through 1.E.7 or obtain permission for the use
of the work and the Project Gutenberg™ trademark as set forth
in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is


posted with the permission of the copyright holder, your use and
distribution must comply with both paragraphs 1.E.1 through
1.E.7 and any additional terms imposed by the copyright holder.
Additional terms will be linked to the Project Gutenberg™
License for all works posted with the permission of the copyright
holder found at the beginning of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files
containing a part of this work or any other work associated with
Project Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute


this electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the
Project Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™
works unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or


providing access to or distributing Project Gutenberg™
electronic works provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project


Gutenberg™ electronic work or group of works on different
terms than are set forth in this agreement, you must obtain
permission in writing from the Project Gutenberg Literary
Archive Foundation, the manager of the Project Gutenberg™
trademark. Contact the Foundation as set forth in Section 3
below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on,
transcribe and proofread works not protected by U.S. copyright
law in creating the Project Gutenberg™ collection. Despite these
efforts, Project Gutenberg™ electronic works, and the medium
on which they may be stored, may contain “Defects,” such as,
but not limited to, incomplete, inaccurate or corrupt data,
transcription errors, a copyright or other intellectual property
infringement, a defective or damaged disk or other medium, a
computer virus, or computer codes that damage or cannot be
read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except


for the “Right of Replacement or Refund” described in
paragraph 1.F.3, the Project Gutenberg Literary Archive
Foundation, the owner of the Project Gutenberg™ trademark,
and any other party distributing a Project Gutenberg™ electronic
work under this agreement, disclaim all liability to you for
damages, costs and expenses, including legal fees. YOU AGREE
THAT YOU HAVE NO REMEDIES FOR NEGLIGENCE, STRICT
LIABILITY, BREACH OF WARRANTY OR BREACH OF CONTRACT
EXCEPT THOSE PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE
THAT THE FOUNDATION, THE TRADEMARK OWNER, AND ANY
DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE
TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL,
PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE
NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of
receiving it, you can receive a refund of the money (if any) you
paid for it by sending a written explanation to the person you
received the work from. If you received the work on a physical
medium, you must return the medium with your written
explanation. The person or entity that provided you with the
defective work may elect to provide a replacement copy in lieu
of a refund. If you received the work electronically, the person
or entity providing it to you may choose to give you a second
opportunity to receive the work electronically in lieu of a refund.
If the second copy is also defective, you may demand a refund
in writing without further opportunities to fix the problem.

1.F.4. Except for the limited right of replacement or refund set


forth in paragraph 1.F.3, this work is provided to you ‘AS-IS’,
WITH NO OTHER WARRANTIES OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of
damages. If any disclaimer or limitation set forth in this
agreement violates the law of the state applicable to this
agreement, the agreement shall be interpreted to make the
maximum disclaimer or limitation permitted by the applicable
state law. The invalidity or unenforceability of any provision of
this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the


Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and
distribution of Project Gutenberg™ electronic works, harmless
from all liability, costs and expenses, including legal fees, that
arise directly or indirectly from any of the following which you
do or cause to occur: (a) distribution of this or any Project
Gutenberg™ work, (b) alteration, modification, or additions or
deletions to any Project Gutenberg™ work, and (c) any Defect
you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new
computers. It exists because of the efforts of hundreds of
volunteers and donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project
Gutenberg™’s goals and ensuring that the Project Gutenberg™
collection will remain freely available for generations to come. In
2001, the Project Gutenberg Literary Archive Foundation was
created to provide a secure and permanent future for Project
Gutenberg™ and future generations. To learn more about the
Project Gutenberg Literary Archive Foundation and how your
efforts and donations can help, see Sections 3 and 4 and the
Foundation information page at www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-
profit 501(c)(3) educational corporation organized under the
laws of the state of Mississippi and granted tax exempt status
by the Internal Revenue Service. The Foundation’s EIN or
federal tax identification number is 64-6221541. Contributions
to the Project Gutenberg Literary Archive Foundation are tax
deductible to the full extent permitted by U.S. federal laws and
your state’s laws.

The Foundation’s business office is located at 809 North 1500


West, Salt Lake City, UT 84116, (801) 596-1887. Email contact
links and up to date contact information can be found at the
Foundation’s website and official page at
www.gutenberg.org/contact
Section 4. Information about Donations to
the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission
of increasing the number of public domain and licensed works
that can be freely distributed in machine-readable form
accessible by the widest array of equipment including outdated
equipment. Many small donations ($1 to $5,000) are particularly
important to maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws


regulating charities and charitable donations in all 50 states of
the United States. Compliance requirements are not uniform
and it takes a considerable effort, much paperwork and many
fees to meet and keep up with these requirements. We do not
solicit donations in locations where we have not received written
confirmation of compliance. To SEND DONATIONS or determine
the status of compliance for any particular state visit
www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states


where we have not met the solicitation requirements, we know
of no prohibition against accepting unsolicited donations from
donors in such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot


make any statements concerning tax treatment of donations
received from outside the United States. U.S. laws alone swamp
our small staff.

Please check the Project Gutenberg web pages for current


donation methods and addresses. Donations are accepted in a
number of other ways including checks, online payments and
credit card donations. To donate, please visit:
www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could
be freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose
network of volunteer support.

Project Gutenberg™ eBooks are often created from several


printed editions, all of which are confirmed as not protected by
copyright in the U.S. unless a copyright notice is included. Thus,
we do not necessarily keep eBooks in compliance with any
particular paper edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg
Literary Archive Foundation, how to help produce our new
eBooks, and how to subscribe to our email newsletter to hear
about new eBooks.

You might also like