Immediate download Intermediate C Programming for the PIC Microcontroller: Simplifying Embedded Programming Hubert Henry Ward ebooks 2024
Immediate download Intermediate C Programming for the PIC Microcontroller: Simplifying Embedded Programming Hubert Henry Ward ebooks 2024
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
sous les étoiles. Quand ainsi la Voix la faisait veuve, elle restait
fidèle cependant et, sans répondre aux cris d’appel de ses frères
sauvages, attendait le retour de Kazan. Parfois, lorsque Kazan
prêtait l’oreille à la Voix, elle le mordillait légèrement, pour lui
témoigner son déplaisir, et grognait vers la Voix.
Or, ce jour-là, comme la Voix retentissait pour la troisième fois,
Louve Grise, au lieu de s’attacher à Kazan et de tenter de le retenir,
lui tourna soudain le dos et se tapit au fond de sa tanière.
Elle s’y coucha, et Kazan ne vit plus, dans l’obscurité, que ses
deux yeux qui flamboyaient, farouches.
Il grimpa vers la pointe extrême du Sun Rock, par la piste étroite,
usée sous ses griffes, et demeura indécis. Depuis la veille, un
certain trouble, qu’il ne pouvait s’expliquer, était en lui. Il y avait du
nouveau qui rôdait sur le Sun Rock. Il ne le voyait point, mais le
sentait.
Il redescendit vers Louve Grise et passa sa tête à l’entrée de la
tanière. Il fut accueilli, non par la plainte caressante coutumière,
mais par un grondement menaçant, qui fit retrousser sur ses crocs
les lèvres de la louve.
Pour la quatrième fois, la Voix se fit entendre et Louve Grise
claqua farouchement des mâchoires. Kazan, après avoir encore
hésité, se décida à descendre du Sun Rock et fila dans la direction
de la cabane.
Par l’instinct de défiance du Wild, qui était en lui, il ne prévenait
jamais de son arrivée, par aucun aboi. Il apparut si soudainement, et
sans crier gare, que Jeanne, qui tenait son bébé dans ses bras,
sursauta, en apercevant dans la porte la tête broussailleuse et les
larges épaules de Kazan. Mais, nullement effrayé, l’enfant s’agita et
se contorsionna de plaisir, et tendit vers le chien-loup ses deux
menottes, en émettant à son adresse un gentil baragouin.
— Kazan ! s’écria Jeanne doucement, avec un geste de
bienvenue. Viens ici, Kazan !
La lueur sauvage, ardente comme du feu, qui luisait dans les
prunelles de Kazan, tomba. Il s’était arrêté, une patte sur le seuil de
la cabane, et semblait répugner à avancer. Puis, tout à coup,
abaissant le panache de sa queue, il s’écrasa sur le sol et entra, en
rampant sur son ventre, comme un chien pris en faute.
Il affectionnait les créatures qui vivaient dans la cabane. Mais la
cabane elle-même, il la haïssait. Car toute cabane sentait le gourdin,
le fouet et la servitude. Pour lui-même, et comme tous les chiens de
traîneau, il préférait, pour dormir, aux parois d’une niche bien close,
le sol couvert de neige et le toit du ciel ou des sapins.
Sous la caresse de la main de Jeanne, Kazan éprouva ce
délicieux petit frisson coutumier, qui était sa récompense, lorsqu’il
quittait pour la cabane Louve Grise et le Wild. Lentement il leva la
tête, jusqu’à ce que son museau noir vînt se poser sur les genoux de
la jeune femme. Puis il ferma béatement les yeux, tandis que la
mystérieuse petite créature qu’était bébé Jeanne l’asticotait, à coups
de pieds mignons, et, de ses mains menues, lui tirait ses poils rudes.
Plus encore que la caresse de la jeune femme, ces jeux enfantins
dont il était l’objet faisaient son bonheur.
Immobile et concentré sur lui-même, comme un sphinx, et
comme pétrifié, Kazan demeurait là sans bouger, respirant à peine.
Le mari de Jeanne n’aimait point à le voir ainsi et s’inquiétait
toujours, en ce cas, de ce que pouvait ruminer le cerveau mystérieux
du chien-loup. Mais la jeune femme était plus confiante en Kazan et
le savait incapable d’une trahison.
— Bon vieux Kazan, disait-elle, je te remercie d’être venu à mon
appel. Bébé et moi nous serons seuls cette nuit. Le papa du petit est
parti au Poste le plus proche et nous comptons sur toi pour nous
garder.
Elle lui chatouillait le museau, du bout de la longue tresse
soyeuse de ses cheveux. Ce qui, quel que fût son désir de n’en point
broncher, faisait souffler et éternuer Kazan, à la grande joie du
marmot.
Puis Jeanne, s’étant levée, s’occupa, durant le reste de la
journée, à empaqueter toutes sortes d’objets qui se trouvaient dans
la cabane, Kazan, qui l’observait, fut fort étonné de ce manège
énigmatique. Quelque chose qu’il pressentait, mais ne pouvait
comprendre, se préparait sans nul doute.
Le soir venu, après l’avoir beaucoup caressé, Jeanne lui dit :
— N’est-ce pas, Kazan, que tu nous défendrais cette nuit, si un
danger nous menaçait ? Maintenant je vais verrouiller la porte, car tu
dois demeurer avec nous jusqu’à demain matin.
Elle se remit à caresser le chien-loup, avec émotion. Sa main
tremblait nerveusement.
— Bientôt, sais-tu, reprit-elle, nous allons partir, nous en
retourner chez nous. Notre récolte de fourrures est terminée. Oui,
nous repartons tout là-bas, là où vivent les parents de mon mari, là
où il y a des villes, avec de grandes églises, des théâtres et des
concerts de musique, et un tas d’autres des plus belles choses qui
sont en ce monde. Et nous t’emmènerons avec nous, Kazan !
Kazan ne comprenait pas ce que lui disait Jeanne, mais il agitait
sa queue, tout heureux de voir que la jeune femme lui parlait. Il en
oubliait Louve Grise et sa rancune contre la cabane, et il alla se
coucher tranquillement dans un coin.
Mais, lorsque Jeanne et l’enfant se furent endormis, quand le
silence fut retombé dans la nuit, son malaise le reprit. Il se mit sur
ses pattes et tourna furtivement tout autour de la chambre, reniflant
les murs, la porte, et tous les objets empaquetés par Jeanne.
Il gémit. La jeune femme, à demi réveillée, l’entendit et murmura :
— Tiens-toi tranquille, Kazan ! Va dormir… Va dormir…
Alors il ne bougea plus et demeura immobile au milieu de la
chambre, inquiet et écoutant.
Et, à travers les murs de bûches de la cabane, il entendit une
plainte lointaine qui venait faiblement jusqu’à lui. C’était le cri de
Louve Grise. Ce cri différait toutefois de celui qu’il avait coutume
d’ouïr. Ce n’était pas un appel de solitude. C’était tout autre chose.
Il courut vivement vers la porte close et se reprit à gémir. Mais
Jeanne et l’enfant dormaient profondément et ne l’entendirent point.
Une fois encore le cri retentit et tout se tut à nouveau. Kazan
s’étala devant la porte et y passa le reste de la nuit.
C’est là que Jeanne le trouva, tout alerté, lorsque le lendemain
matin, de bonne heure, elle s’éveilla. Elle entr’ouvrit la porte et, en
une seconde, il fut dehors. A toute volée, il s’élança vers le Sun
Rock, dont le soleil levant teintait le faîte d’une lueur d’or.
Il y grimpa rapidement, par la piste étroite et raboteuse, et ne
trouva point que Louve Grise fût venue au-devant de lui.
Il atteignit la tanière et huma l’air, le dos hérissé et les pattes
raides. Quelque chose de nouveau flottait dans l’air et ce quelque
chose était de la vie. Il se glissa dans la fente pratiquée entre deux
rochers et, avançant la tête, se trouva nez à nez avec Louve Grise,
qui n’était pas seule.
Louve Grise poussa un gémissement plaintif. Les poils
s’aplatirent sur le dos de Kazan et il répondit par un grognement
attendri. Puis lentement il recula et, dans la lumière de l’aurore, il se
coucha devant l’entrée de la tanière, faisant à la louve, de son corps,
comme un bouclier.
Louve Grise était mère.
XI
LA TRAGÉDIE SUR LE SUN ROCK
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