Download Complete Intermediate C Programming for the PIC Microcontroller: Simplifying Embedded Programming Hubert Henry Ward PDF for All Chapters
Download Complete Intermediate C Programming for the PIC Microcontroller: Simplifying Embedded Programming Hubert Henry Ward PDF for All Chapters
com
https://textbookfull.com/product/intermediate-c-programming-
for-the-pic-microcontroller-simplifying-embedded-
programming-hubert-henry-ward/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/programming-pic-microcontrollers-
with-xc8-1st-edition-armstrong-subero/
textboxfull.com
https://textbookfull.com/product/stm32-arm-programming-for-embedded-
systems-1st-edition-muhammad-ali-mazidi/
textboxfull.com
https://textbookfull.com/product/c-programming-for-absolute-beginners-
radek-vystavel/
textboxfull.com
https://textbookfull.com/product/learning-embedded-android-n-
programming-1st-edition-ivan-morgillo/
textboxfull.com
C++ Programming D. S
https://textbookfull.com/product/c-programming-d-s/
textboxfull.com
https://textbookfull.com/product/c-programming-for-dummies-for-
dummies-computer-tech-gookin/
textboxfull.com
https://textbookfull.com/product/swift-programming-the-big-nerd-ranch-
guide-3rd-edition-mikey-ward/
textboxfull.com
TECHNOLOGY IN AC TION™
Intermediate
C Programming
for the PIC
Microcontroller
Simplifying Embedded
Programming
—
Hubert Henry Ward
Intermediate
C Programming
for the PIC
Microcontroller
Simplifying Embedded
Programming
iii
Table of Contents
iv
Table of Contents
v
Table of Contents
Chapter 6: Interrupts������������������������������������������������������������������������225
What Are Interrupts?�����������������������������������������������������������������������������������������225
The Fetch and Execute Cycle����������������������������������������������������������������������������226
The Program Counter����������������������������������������������������������������������������������������226
The Sources of Interrupts���������������������������������������������������������������������������������230
The Process for a Simple Interrupt with No Priorities���������������������������������231
Setting Up the PIC to Respond to the Interrupts������������������������������������������232
The PIE1 Register����������������������������������������������������������������������������������������237
The Algorithm for the Interrupt Test Program����������������������������������������������239
Compiler Software Version Issue�����������������������������������������������������������������240
The Analysis of Listing 6-1��������������������������������������������������������������������������243
Using the Compare Function of the CCP Module����������������������������������������������251
The Algorithm for the Compare Function�����������������������������������������������������253
The Analysis Of Listing 6-2��������������������������������������������������������������������������256
Using Priority Enabled Interrupts����������������������������������������������������������������������260
The Algorithm for the High/Low Priority Program����������������������������������������260
Analysis of Listing 6-3���������������������������������������������������������������������������������265
Explanation of How the High/Low Priority Program Works��������������������������270
Summary����������������������������������������������������������������������������������������������������������272
vi
Table of Contents
Appendix 3: Keywords����������������������������������������������������������������������321
Appendix 7:���������������������������������������������������������������������������������������337
Index�������������������������������������������������������������������������������������������������345
vii
About the Author
Hubert Henry 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 Honours 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 MPLABX 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 the Skills Olympics in Seoul 2001, resulting in one of the
best outcomes to date for the UK in Mechatronics.
ix
About the Technical Reviewer
Sai Yamanoor is an embedded systems engineer working for an industrial
gases company in Buffalo, NY. His interests, deeply rooted in DIY and
open source hardware, include developing gadgets that aid behavior
modification. He has published two books with his brother, and in his
spare time, he likes to build things that improve quality of life. You can find
his project portfolio at http://saiyamanoor.com.
xi
Introduction
This book looks at some useful aspects of the PIC microcontroller. It
explains how to write programs in C so that you can use the PIC micro to
control a variety of electronics and DC motors. After reading this book, you
will be well on your way to becoming an embedded programmer using the
C programming language.
• Using interrupts
xiii
Introduction
The Prerequisites
There are none really, but understanding the C programming language
will be useful. However, I will explain how each program works as we go
through them.
Also, if you understand the binary and hexadecimal number systems,
it will be an advantage but there is a section in the Appendix that will help
you with that.
However, to get the full use out of this book, you will need to install the
following software:
All of these programs are freely available from the Microchip web site.
xiv
Introduction
xv
Introduction
Before we move into the book for real, I think it will be useful to you if
I explained a bit about what MPLABX is. It is an industrial IDE created by
Microchip. The term IDE stands for integrated development environment.
It is actually a lot of programs collected together to create a programming
environment:
So this IDE is a very large collection of programs that make our job of
writing code much more efficient. Yet it’s free; well, I use the free version,
which is not as efficient as the paid version but it is more than good
enough for us.
I therefore hope that you not only learn how to program the PIC micro
but you also enjoy going through my book and that you produce some
useful projects along the way.
xvi
CHAPTER 1
If this is all true, you should create a header file for the LCD.
These are the three header files you will create in this book. There are
many more examples of when you should create a header file. The process
of creating and using header files makes your program writing more
efficient.
Header files can be made available for all of your projects, like global
header files as opposed to local header files. Local header files are
available only to the project they were created in.
Also, you can split projects up so that different programmers can write
different sections of the programs and save them as header files to be used
in all projects by all of the company’s programmers.
2
Chapter 1 Creating a Header File
The project window on the left-hand side may not be shown. If you
want it shown, you should select the word Window from the top menu
bar. Click the word Projects, with the orange boxes in front of it, and the
window should appear. You may have to move the window about to get it
in the position shown.
Now, assuming you are ready to create a project, you should either
click the word File, in the main menu bar, and select New project, or click
the orange box with the small green cross on the second menu bar. This is
the second symbol from the left-hand side of the second menu bar.
When you have selected the Create project option, you should see the
window shown in Figure 1-2.
3
Chapter 1 Creating a Header File
Most of the projects you will create are Microchip Embedded and
Standalone. Therefore, make sure these two options are highlighted and
then click the Next button. The Select Device window should now be
visible, as shown in Figure 1-3.
4
Chapter 1 Creating a Header File
In this window, you can choose which PIC you want to use. Select the
Advanced 8-bit MCUs (PIC18) in the small box alongside Family, as shown
in Figure 1-3. Then, in the Device window, select the PIC18F4525. The result
is shown in Figure 1-3. To make these options visible, you need to click the
small downward pointing arrow in the respective box. The different options
should then become visible. If the device window is highlighted in blue, you
could simply type in the PIC number you want, such as PIC18F4525. Your
selected device should appear in the window below.
If you are using a different PIC, select it here.
Once you are happy with your selection, click the Next button.
The next window to appear is the Select Tool window. This is shown
in Figure 1-4. With this window you can select the programming tool you
want to use to download the program to your prototype board. There are a
range of tools you can use. I mainly use the ICD3 CAN or the PICkit3 tool.
5
Chapter 1 Creating a Header File
Having selected the tool you want, click Next to move on to the next
window where you can select the compiler software you want to use,
assuming you have downloaded the appropriate compiler software (see
Figure 1-5).
6
Chapter 1 Creating a Header File
7
Chapter 1 Creating a Header File
In this window, you will specify the name of the project and where you
want to save it. The software will create a new directory on your computer
with the project name you create here. It is recommended that you don’t
use long-winded, complicated path names for the new folder so I normally
save all my projects on the root directory of my laptop.
I have suggested a project name for this new project as
advanceProject1. Note that I am using camelcase, where two words,
or more, are combined together. The first letter of the first word is in
lowercase and the first letters of any subsequent words are in uppercase. In
this way multiple words can be combined together to make one long word.
As you type the name for your project, you should see that the folder is
created on the root drive, or wherever you have specified it should be. The
folder name will have a .X added to it.
It will be in this new folder that all the files associated with the project
will be saved as well as some important subdirectories that are created.
8
Chapter 1 Creating a Header File
Once you are happy with the naming of the project, simply click the
Finish button and the project will be created. The window will now go back
to the main window, as shown in Figure 1-7.
You should see the project window at the left-hand side of your screen,
as shown in Figure 1-7. Note that you may need to move the window about
to get it the same as that shown in Figure 1-7.
Now that you have the new project created, you need to create a
header file that you will use in all of your projects in this book.
To create the header file, right-click the subdirectory in the project tree
named Header Files. When you do this, the flyout menu will appear, as
shown in Figure 1-8.
9
Chapter 1 Creating a Header File
Figure 1-8. The flyout menu for the new header file
From that flyout menu, select New. From the second flyout menu,
select xc8_header.h, as shown in Figure 1-8.
The window shown in Figure 1-9 will appear.
10
Chapter 1 Creating a Header File
Figure 1-9. The name and location for the new header file
All you need to do here is give the file a name. I have chosen the name
conFigInternalOscNoWDTNoLVP as it gives a good description of what I
want to do in this header file, which is set these three main parameters of
the configuration words. Note the configuration words specify how you
want to configure and so use the PIC.
The main concern is that PICs have a wide variety of primary oscillator
sources and you need to tell the PIC which one you will be using. The
oscillator is the device or circuit that provides a signal from which the
clock signal, the signal that synchronizes the operations of the PIC,
is derived. I prefer to use the internal oscillator block as the primary
oscillator source. This saves buying an oscillator crystal. It also saves two
inputs that would be used if I used an external oscillator. This is because I
would connect the external oscillator to the PIC via those two input pins,
normally RA6 and RA7.
11
Chapter 1 Creating a Header File
The second major item I change is to turn off the WDT, which is the
watch dog timer. This is a timer that will stop the micro if nothing has
happened for a set period of time. This is a facility that you don’t want in
these programs, so you must turn it off. Note that the WDT is mainly used
in continuous production lines. In that situation, the fact that nothing has
happened for a set time usually means something has gone wrong so it’s
best to turn everything off.
The third item to turn off is the low voltage programming (LVP)
function. The low voltage programming affects some of the bits on
PORTB. Therefore, to keep the bits on PORTB available for general I/O, I
normally turn off the LVP.
So this explains the header file’s cryptic name. You should always give
your header files a name that relates to how you want to use the file.
Once you have named the header file, click Finish and the newly
created header file will be inserted into the main editing window in
the software. However, Microchip automatically inserts an awful lot of
comments and instructions that, at your level of programming, you don’t
really need. Therefore, simply select all that stuff and delete it so that you
have an empty file ready for you to insert the code that you really want.
Now that you have a clean file, you can control what goes into it. The
first thing you should do is put some comments in along the following
lines:
• You should say what PIC you wrote it for and when you
wrote it.
12
Chapter 1 Creating a Header File
You should insert your own comments into the editor similar to those
shown in Figure 1-10.
You will notice that I changed the colour of my comments to black and
bold size 14. This is to try and make them more visible than the default
grey.
If you want to change the colour, you can do so by selecting the word
Options from the drop-down menu that appears when you select the
Tools choice on the main menu bar. You will get the window shown in
Figure 1-11.
13
Chapter 1 Creating a Header File
Click the tag for Fonts and Colours and then select what you want to
change. Once you are happy with your choice, click OK. I changed the
colour of the comments to black, as shown in Figure 1-11.
Now you need to create the configuration words for your header file.
As this is something you must do for all your projects, Microchip has
developed a simple process for writing to the configuration words. This
can be achieved using a special window in the MPLABX IDE. To open this
window, click the word Window on the main menu bar and then select
Target Memory Views from the drop-down menu that appears. Then select
Configuration Bits from the slide-out menu that appears. This process is
shown in Figure 1-12.
14
Chapter 1 Creating a Header File
Once you have selected the configuration bits, your main window will
change to that shown in Figure 1-13.
15
Another Random Scribd Document
with Unrelated Content
music entirely from the formal division of the bar line placed at
regular intervals. Not that these composers dispense with the bar
line completely, but they place it in such disconcertingly irregular
places that the conductor’s task is doubly difficult even when he
attempts to indicate it merely with a single down-beat.
The two following examples from Igor Stravinsky’s
“Petrouchka”[3] illustrate this difficulty. The tempo is too fast to permit
the use of regularly divided gestures, and yet it is very difficult to
bring in the single beats with such metronomic precision that the
musicians can play all of the individual eighth notes evenly and
without hurrying.
[3] Copyright by Russischer Musikverlag, Berlin
EXAMPLE Nᵒ. 1
3 in a measure
[Listen]
EXAMPLE Nᵒ. 2
[Listen]
[Listen]
[Listen] [Listen]
[Listen]
[Listen]
[Listen]
The speed of the eighth notes must never vary.
[Listen]
Beat 2-in-a-measure, merely making the second beat one-eighth
note longer.
[Listen]
The Hymn of Jesus:—Gustave Holst (Copyright 1920 by Stainer
and Bell, London)
[Listen]
Beat 4-in-a-measure, merely making the fourth beat one-eighth
note longer.
I Diagram 1
II Diagram 2
III Diagram 3
[Listen]
[Listen]
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com