Intermediate C Programming for the PIC Microcontroller: Simplifying Embedded Programming Hubert Henry Ward download
Intermediate C Programming for the PIC Microcontroller: Simplifying Embedded Programming Hubert Henry Ward download
https://textbookfull.com/product/intermediate-c-programming-for-
the-pic-microcontroller-simplifying-embedded-programming-hubert-
henry-ward/
https://textbookfull.com/product/programming-pic-
microcontrollers-with-xc8-1st-edition-armstrong-subero/
https://textbookfull.com/product/stm32-arm-programming-for-
embedded-systems-1st-edition-muhammad-ali-mazidi/
https://textbookfull.com/product/programming-with-micropython-
embedded-programming-with-microcontrollers-and-python-1st-
edition-nicholas-h-tollervey/
https://textbookfull.com/product/programming-with-micropython-
embedded-programming-with-microcontrollers-and-python-first-
edition-nicholas-h-tollervey/
C# Programming for Absolute Beginners Radek Vystav■l
https://textbookfull.com/product/c-programming-for-absolute-
beginners-radek-vystavel/
https://textbookfull.com/product/learning-embedded-android-n-
programming-1st-edition-ivan-morgillo/
C++ Programming D. S
https://textbookfull.com/product/c-programming-d-s/
https://textbookfull.com/product/c-programming-for-dummies-for-
dummies-computer-tech-gookin/
https://textbookfull.com/product/swift-programming-the-big-nerd-
ranch-guide-3rd-edition-mikey-ward/
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
Random documents with unrelated
content Scribd suggests to you:
14th Regiment.
HISTORICAL RECORD
OF
THE FOURTEENTH,
OR
F O O T.
1685
1686
1687
After passing in review before the King and Queen, and other
members of the royal family, the regiment struck its tents and
marched to Plymouth, where it was stationed during the winter.
1688
1689
1690
1691
1692
1693
1694
When the army took the field in the spring of 1694, the regiment
was left, with several other corps, under Brigadier-General Sir David
Collier, encamped near Ghent, to form a guard for the artillery, which
was conveyed by water to Malines. The regiment joined the army at
the camp near Louvain, on the 4th of June, and on the 6th it was
reviewed by His Majesty, who was pleased to express to Colonel
Tidcomb his high approbation of its appearance. The regiment was
afterwards employed in several movements, and it formed part of
the splendid body of troops encamped at Mont St. André, near the
village of Ramilies, where the forces of the confederate states were
assembled under King William III., and presented a magnificent
spectacle of war.
The Fourteenth was one of the corps which attempted, by a forced
march, to pass the enemy's fortified lines, and penetrate French
Flanders; but by extraordinary exertions the French gained the pass
first, and thus preserved their country from an invasion. The
regiment was subsequently encamped near Rousselaer, forming part
of the covering army during the siege of Huy. The vicinity of the
camp was infested by detachments of the enemy, and on one
occasion the waggons conveying the bread to the army were
attacked, when a detachment of the Fourteenth, forming part of the
guard, was engaged, and the regiment had Captain Sacheverel
mortally wounded, who was the only British officer killed by the
enemy during this campaign.
Having to remain in the field during cold and wet weather, the
soldiers erected huts of wood and straw, and on the 1st of October
the huts of the Fourteenth Regiment were accidentally set on fire,
and destroyed: the Second Foot Guards had experienced the same
misfortune a few days previously. The fortress of Huy having
surrendered, the army separated for winter quarters, and the
regiment returned to Bruges in the second week in October.
1695
1696
1697
1698
1701
1702
1705
1706
1713
1714
1715
The decease of Queen Anne, and the accession of King George I.,
in 1714, was followed by renewed efforts on the part of the
partisans of the Pretender to procure his elevation to the throne;
these exertions began to assume an alarming appearance in the
summer of 1715, when the well-known attachment of the Fourteenth
Regiment to the Protestant succession, occasioned it to be recalled
from Ireland, and ordered to Scotland, where the Jacobites were
numerous, and it landed at Saltcoats in Ayrshire early in the summer.
In the autumn the Earl of Mar assembled his vassals, erected the
standard of the Pretender in the Highlands, and summoned the clans
to take arms. The royal forces in Scotland were encamped at Stirling
under Major-General Wightman; the Fourteenth Regiment joined the
camp in October, and the Duke of Argyle assumed the command;
but his Grace had not four thousand men to confront ten thousand
under the Earl of Mar.
When the rebel army advanced towards the Firth, the King's
troops quitted the camp at Stirling and proceeded towards
Dumblain; and on the morning of the 13th of November the hostile
forces confronted each other on Sheriffmuir: the Fourteenth foot
were posted in the left wing of the royal army. The rebels advanced
to commence the engagement, and at that moment it was deemed
necessary to make some alteration in the position of the royal
forces; as the left wing was taking up the new alignment, it was
attacked by a body of the clans of very superior numbers, and put
into some confusion: at the same time the right wing of the royal
army overpowered the left wing of the rebel host, and drove it from
the field; each commander having one wing triumphant and one
wing defeated. The Fourteenth, and several other corps on the left,
resisted the charge of the clans a short time, but being attacked in
the act of forming, and engaged by very superior numbers, they fell
back a short distance; they thus became separated from the
remainder of the army, and retiring beyond Dumblain, took
possession of the passes to prevent the clans penetrating towards
Stirling. Both armies retained their position during the day, and the
rebels, being defeated in their design of penetrating southward,
afterwards retired; when the King's troops returned to their camp at
Stirling.
The Fourteenth Foot had one lieutenant and six rank and file killed;
fourteen rank and file wounded; Captain Barlow, Lieutenant Griffin,
and several private soldiers were made prisoners.
The Pretender arrived in Scotland soon afterwards, and his
presence appeared to give new life to his adherents.
1716
Additional forces joined the army under the Duke of Argyle: the
Fourteenth was formed in brigade with the Third, Twenty-first, and
Thirty-sixth regiments, under Brigadier-General Morrison; and in
January, 1716, the royal troops advanced, marching through snow,
over ice, and exposed to severe weather, when the Pretender
retreated, and losing all hope of success he escaped, with the
leaders of the rebellion, to France: the Highlanders, finding
themselves deserted by their commanders, dispersed. After pursuing
the insurgents some distance, the Fourteenth was quartered a short
time at Dunkeld.
1717
1718
1719
1721
After this service the regiment marched to the castle of Bran, near
Kainloch-Benchven, Inverness-shire; and in 1721 it proceeded to
Edinburgh.
1722
1723
1725
1726
1727
1729
1739
1740
1742
War between Great Britain and Spain was resumed in 1739; and
the claims of the Elector of Bavaria on the kingdoms of Bohemia and
Hungary, which were attempted to be enforced after the death of
the Emperor, Charles VI., in 1740, involved Great Britain in hostilities
with France and Bavaria. King George II. resolved to support the
House of Austria; the garrison of Gibraltar was reinforced, and the
Fourteenth Regiment, having been relieved from duty at that fortress,
arrived at Portsmouth in September, 1742. After reposing a few days
in barracks at Portsmouth, the regiment marched into quarters in
Yorkshire, the head-quarters being at York.
1743
1744
1745
1746
1747
1749
1750
1751
1752
1753
1755
1756
1759
1760
1761
1762
1763
1765
1766
Towards the end of May, 1766, the regiment marched into village
quarters near Hounslow Heath, where it was reviewed on the 4th of
June by the King, who was graciously pleased to express his royal
approbation of its appearance and movements in the field. After the
review the regiment marched to Salisbury and adjacents.
1771
1772
1773
The island of St. Vincent's was captured from the French in 1762,
and was ceded to Great Britain at the peace in 1763; it was found to
contain two tribes of natives called the red and black Caribs, the
former being the Aborigines, and the latter having sprung from a
cargo of African slaves, who escaped from a vessel which was
wrecked on the island. The Caribbees were devoted to the French
interest; they were dangerous and troublesome neighbours to the
English planters, and it was found necessary to restrain their
conduct, and enforce obedience to a few salutary regulations. They
were, however, of a determined spirit, possessed many thickly-
wooded fastnesses, and so resolutely resisted all attempts to restrain
their roving propensities and mode of life, that it was found
necessary to augment the military force on the island. The
Fourteenth Foot were employed against the refractory Caribbees in
1772 and 1773; numerous skirmishes occurred among the thickly-
wooded parts of the country, and several soldiers were killed and
wounded, in the bush fighting, which took place daily for some time.
At length the Caribbees were reduced to submission: and the
regiment returned to North America, leaving a number of sick men
and others in the West Indies.
1774
1775
1776
1778
1779
During the year 1778 the regiment was stationed in the south of
England; and in the summer of 1779 it pitched its tents on
Coxheath, where a camp was formed of the Sixth, Fourteenth,
Fiftieth, Sixty-fifth, and Sixty-ninth Regiments, with sixteen
battalions of militia, under Lieutenant-General Pierson.
1780
1781
1782
1787
1788
1789
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