100% found this document useful (5 votes)
135 views

Download Complete Intermediate C Programming for the PIC Microcontroller: Simplifying Embedded Programming Hubert Henry Ward PDF for All Chapters

Henry

Uploaded by

arthosuranmw
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 (5 votes)
135 views

Download Complete Intermediate C Programming for the PIC Microcontroller: Simplifying Embedded Programming Hubert Henry Ward PDF for All Chapters

Henry

Uploaded by

arthosuranmw
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/ 55

Download the Full Version of textbook for Fast Typing at textbookfull.

com

Intermediate C Programming for the PIC


Microcontroller: Simplifying Embedded Programming
Hubert Henry Ward

https://textbookfull.com/product/intermediate-c-programming-
for-the-pic-microcontroller-simplifying-embedded-
programming-hubert-henry-ward/

OR CLICK BUTTON

DOWNLOAD NOW

Download More textbook Instantly Today - Get Yours Now at textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Programming PIC Microcontrollers with XC8 1st Edition


Armstrong Subero

https://textbookfull.com/product/programming-pic-microcontrollers-
with-xc8-1st-edition-armstrong-subero/

textboxfull.com

Stm32 Arm Programming for Embedded Systems 1st Edition


Muhammad Ali Mazidi

https://textbookfull.com/product/stm32-arm-programming-for-embedded-
systems-1st-edition-muhammad-ali-mazidi/

textboxfull.com

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-1st-edition-
nicholas-h-tollervey/
textboxfull.com

Programming with MicroPython embedded programming with


Microcontrollers and Python First Edition Nicholas H.
Tollervey
https://textbookfull.com/product/programming-with-micropython-
embedded-programming-with-microcontrollers-and-python-first-edition-
nicholas-h-tollervey/
textboxfull.com
C# Programming for Absolute Beginners Radek Vystav■l

https://textbookfull.com/product/c-programming-for-absolute-beginners-
radek-vystavel/

textboxfull.com

Learning embedded Android N programming 1st Edition Ivan


Morgillo

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

C Programming For Dummies (For Dummies (Computer/Tech))


Gookin

https://textbookfull.com/product/c-programming-for-dummies-for-
dummies-computer-tech-gookin/

textboxfull.com

Swift Programming The Big Nerd Ranch Guide 3rd Edition


Mikey Ward

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

Hubert Henry Ward


Intermediate C Programming for the PIC Microcontroller: Simplifying
Embedded Programming
Hubert Henry Ward
Leigh, UK

ISBN-13 (pbk): 978-1-4842-6067-8 ISBN-13 (electronic): 978-1-4842-6068-5


https://doi.org/10.1007/978-1-4842-6068-5

Copyright © 2020 by Hubert Henry 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: Natalie Pao
Development Editor: James Markham
Coordinating Editor: Jessica Vakili
Distributed to the book trade worldwide by Springer Science+Business Media New York,
1 NY 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 www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub via the book’s product page, located at www.apress.com/
978-1-4842-6067-8. For more detailed information, please visit www.apress.com/source-code.
Printed on acid-free paper
Table of Contents
About the Author���������������������������������������������������������������������������������ix

About the Technical Reviewer�������������������������������������������������������������xi


Introduction���������������������������������������������������������������������������������������xiii

Chapter 1: Creating a Header File���������������������������������������������������������1


Creating a Header File������������������������������������������������������������������������������������������2
Creating a Project in MPLABX��������������������������������������������������������������������������3
Including the Header File�������������������������������������������������������������������������������18
Creating the Project Source File��������������������������������������������������������������������19
Creating a Global Header File������������������������������������������������������������������������23
The PICSetUp.h Header File���������������������������������������������������������������������������26
Analysis of Listing 1-1�����������������������������������������������������������������������������������28
Synopsis��������������������������������������������������������������������������������������������������������46
Answer to Exercise����������������������������������������������������������������������������������������46

Chapter 2: Controlling a Seven-­Segment Display�������������������������������47


Controlling a Seven-Segment Display�����������������������������������������������������������������47
Common Anode Seven-Segment Display������������������������������������������������������49
Common Cathode Seven-Segment Display���������������������������������������������������50
Controlling the Display with the PIC��������������������������������������������������������������52
The Seven-Segment Display Program�����������������������������������������������������������53
The Listing for the Seven-Segment Display Program������������������������������������57
The Analysis of Listing 2-1����������������������������������������������������������������������������59

iii
Table of Contents

Improving the Seven-Segment Display Program�������������������������������������������71


The Problem with the Program����������������������������������������������������������������������71
Arrays������������������������������������������������������������������������������������������������������������72
Using Pointers�����������������������������������������������������������������������������������������������72
Analysis of The Pointer Example�������������������������������������������������������������������73
The Improved Program����������������������������������������������������������������������������������74
Synopsis��������������������������������������������������������������������������������������������������������79
Answers to the Exercises������������������������������������������������������������������������������79

Chapter 3: The 24-Hour Clock�������������������������������������������������������������81


The Seven-Segment Display�������������������������������������������������������������������������������82
The Algorithm for the 24-Hour Clock Using Seven-Segment Displays���������������82
The Initialization of the PIC���������������������������������������������������������������������������������86
Analysis of Listing 3-1����������������������������������������������������������������������������������������93
A 24-Hour Clock with the LCD Display��������������������������������������������������������������104
Analysis of the Header File for the LCD�������������������������������������������������������121
The Analysis of Listing 3-2��������������������������������������������������������������������������133
Improvements for the 24-Hour Clock LCD Program������������������������������������145
Using Switch and Case Keywords���������������������������������������������������������������������147
Analysis of the New Subroutine������������������������������������������������������������������149
Synopsis������������������������������������������������������������������������������������������������������151

Chapter 4: Creating a Square Wave��������������������������������������������������153


Why Create a Square Wave?�����������������������������������������������������������������������������153
Musical Notes���������������������������������������������������������������������������������������������������154
The Speed of the Simple DC Motor�������������������������������������������������������������������156
Pulse Width Modulation������������������������������������������������������������������������������������156
Creating a Square Wave with the PWM Mode���������������������������������������������������158

iv
Table of Contents

Creating a 500Hz Square Wave�������������������������������������������������������������������������162


The Mark Time or Duty Cycle����������������������������������������������������������������������������164
Analysis of Listing 4-1��������������������������������������������������������������������������������������166
Creating Two Square Wave Outputs������������������������������������������������������������������169
Setting the Speed of a DC Motor�����������������������������������������������������������������������172
Driving the Motor����������������������������������������������������������������������������������������������175
Creating a Three-Speed DC Motor Program������������������������������������������������������176
Using a Variable Input Voltage to Change the Speed of a DC Motor�����������������179
Analysis of Listing 4-5��������������������������������������������������������������������������������������180
Creating a Musical Note������������������������������������������������������������������������������������186
Creating the Middle C Note�������������������������������������������������������������������������������187
Creating a Musical Keyboard����������������������������������������������������������������������������191
The Analysis Of Listing 4-7�������������������������������������������������������������������������������194
Summary����������������������������������������������������������������������������������������������������������196
Answers to the Exercises����������������������������������������������������������������������������������196

Chapter 5: DC Motors������������������������������������������������������������������������199


The Stepper Motor��������������������������������������������������������������������������������������������199
The Servo Motor������������������������������������������������������������������������������������������������209
Using the CCPM to Produce the Pulse Train Signal�������������������������������������210
Controlling the Positions of the Servo Motor with a Variable Resistor��������212
Improving The Servo Motor Program�����������������������������������������������������������215
The Algorithm for the Improved Program����������������������������������������������������216
Summary�����������������������������������������������������������������������������������������������������223
Solution to the Exercise�������������������������������������������������������������������������������224

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

Chapter 7: Frequency Measurement and the EEPROM���������������������273


Using the Capture Function of the CCP�������������������������������������������������������������273
Setting the CCP to the Capture Mode����������������������������������������������������������������274
What the CCP Captures in the Capture Mode����������������������������������������������������274
The Algorithm for the Frequency Measurement Program���������������������������������276
Example Frequency Measurement��������������������������������������������������������������������278
The Program to Measure the 500Hz Square Wave��������������������������������������������280

vi
Table of Contents

Analysis of Listing 7-1��������������������������������������������������������������������������������������283


Using the EEPROM Inside the 18f4525�������������������������������������������������������������296
Analysis of Listing 7-3��������������������������������������������������������������������������������������304
Simulation of the EEPROM Program Using MPLABX�����������������������������������������312
Summary����������������������������������������������������������������������������������������������������������314

Appendix 1: Some Useful Definitions������������������������������������������������317

Appendix 2: Mathematical and Logic Operators�������������������������������319

Appendix 3: Keywords����������������������������������������������������������������������321

Appendix 4: Data Types��������������������������������������������������������������������323

Appendix 5: The ASCII Character Set������������������������������������������������325

 ppendix 6: Numbering Systems Within


A
Microprocessor-Based Systems�������������������������������������������������������327
B
 inary Numbers������������������������������������������������������������������������������������������������328
Converting Decimal to Binary���������������������������������������������������������������������������328
Converting from Binary to Decimal�������������������������������������������������������������������329
Adding and Subtracting Binary Numbers����������������������������������������������������������331
Subtracting Binary Numbers�����������������������������������������������������������������������������332
The Hexadecimal Number System��������������������������������������������������������������������334

Appendix 7:���������������������������������������������������������������������������������������337

 ppendix 8: The Frequency and Wavelength of the


A
Main Musical Notes��������������������������������������������������������������������������339

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.

The Aims and Objectives of This Book


The main aim of this book is to introduce you to some useful applications
of programming PIC micros such as

• Creating header files

• Controlling seven-segment displays

• Using an LCD display with two lines of 16 characters

• Pulse width modulation

• Using driver ICs such as the ULN2004A

• Controlling DC motors, including stepper motors and


servo motors

• Using every aspect of the Capture, Compare and PWM,


CCP module in the PIC

• Using interrupts

• Writing to the EEPROM

xiii
Introduction

The Objectives of This Book


After reading through this book, you should be able to program the PIC
to use all of the above. You should have a good understanding of some of
the advance programming techniques for PIC micros. You should be able
to download your programs to your PIC in a practical situation where you
have the ability to design and build some useful projects.

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:

• MPLABX, which is the IDE from Microchip. The version


in the book is MPLABX Version 5.25. However, any
version later than 2.20 is OK.

• A C compiler for the 8-bit micro. I use XC8 (V2.10)


but with some programs I use XC8 (V1.35) compiler
software. However, you should be aware that some of
the later compilers are missing some useful libraries.
This is why I sometimes use version 1.35.

All of these programs are freely available from the Microchip web site.

xiv
Introduction

Another useful piece of software is a suitable ECAD (electronic


computer-aided design) software program that supports 8-bit micros.
The ECAD software I use is PROTEUS. However, it is not free, so as well as
showing you how to simulate the programs in PROTEUS, I will show you
how to use a suitable prototype board to run the programs in a practical
situation.
If you want to go down the practical route, you will need to purchase a
programming tool and a prototype board.
The tools I use are either the ICD3 can (Microchip has now moved
onto the ICD4 can) or the PICkit3 programmer to download the programs
from MPLABX to the PIC.
The prototype board I use is the picdem2 plus DEMO BOARD and
a prototype board from Matrix Multimedia (although Matrix no longer
produces the more versatile board that I use).
This book was written based around using MPLABX V5.25.
However, the principles of how to create projects and write programs
are transferable to earlier and later versions of MPLABX. There may be
some slight differences in the details, but they shouldn't cause too many
problems.
The PIC that this book is based around is the PIC18F4525. This is a
very versatile 8-bit micro that comes in a 40-pin dual-inline package. As
long as the PIC you want to use has the same firmware modules, then the
programs in the book can easily be used on other PIC micros with some
minor modifications. However, you should always refer to the data sheet
for the particular PIC you use because some of the SFRs (special function
registers) may differ. For example, the PIC18F4525 uses the ADCON0,
ADCON1, and ADCON2 SRFs to control the ADC module but the 16F88
uses the ANSEL, ADCON0, and ADCON1 registers.
Throughout the book, I include program listings and I go through an
analysis of any new instructions that the listings introduce. With respect to
the first listing, I will assume that all of the instructions are new to you, the
reader.

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:

• There is an editor, which is slightly more than a simple


text editor. However, in my early days, I used to write
my programs in Notepad.

• There is also a compiler program that converts your


program instructions from C to the machine code that
all microprocessor-based systems use. In the very early
days of programming, the programmers used to write
in this machine code. This was a bit before my time,
although in my early days, I wrote all my programs
in assembler. Assembler is the closet language to the
actual machine code that all micros use.

• There is also a linker program that will bring together


any include files that we wish to use in our projects.

• As well as these programs, there are a range of


programs that we can use to help debug our programs
or simply simulate them.

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

Creating a Header File


In an effort to reduce the amount of text in the program listings and reduce
the amount whereby I simply repeat myself, let’s create and use a series of
header files. Header files are used when your programs use the same series
of instructions in exactly the same way in all your projects and programs.
In this book, you will create three header files. The first will be
concerned with the configuration words you write for your projects.
The configuration words are used to configure how the PIC applies the
essential parameters of the PIC. They have to be written for every project
and program you create. Therefore, if you are going to write the same
configuration words for all of your projects, you should use a header file.
The second header file will be associated with setting up the PIC to
use the ports, the oscillator, the timers, etc. You will set them in exactly the
same way in all of your projects, so it’s useful to create a header file for this.
However, in some projects you may need to modify some of the settings, so
be careful when using this header file.
The third header file you will create will be used if your programs use
the LCD (liquid crystal display) in exactly the same way such that

• The LCD is always connected to PORTB.

• The LCD uses just four data lines instead of eight to


save I/O.

• The RS pin is always on Bit4 of PORTB and the E pin on


Bit5 of PORTB.

© Hubert Henry Ward 2020 1


H. H. Ward, Intermediate C Programming for the PIC Microcontroller,
https://doi.org/10.1007/978-1-4842-6068-5_1
Chapter 1 Creating a Header File

• The LCD always increments the cursor position after


each character has been displayed.

• The LCD always uses 2 lines of 16 characters.

• The actual characters are always on a 5 by 8 grid.

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.

Creating a Header File


Now that I have explained what header files are and why you would use
them, let’s create one. The first header file you will create will be for the
configuration words that you will use for most of the projects in this
book. It will also give me the chance to go through creating a project in
MPLABX for those readers who have never used MPLABX before. The
version I am using is MPLABX V5.25. It is one of the latest versions of the
IDE from Microchip. Microchip is always updating the software, but the
main concepts of creating a project and writing programs do not change.
You will be able to follow the process even if you have an earlier version of
MPLABX or a later version.

2
Chapter 1 Creating a Header File

Creating a Project in MPLABX


Assuming you have downloaded both the MPLABX software and the XC8
(V2.10) compiler software or XC8 (V1.35), when you open the software, the
opening screen will look like Figure 1-1.

Figure 1-1. The opening screen in MPLABX

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

Figure 1-2. The New Project window

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

Figure 1-3. The Select Device window

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

However, if I am only simulating the program, I use the simulator option.


Note that the MPLABX IDE comes with its own simulations for the PICs
you may use. It also has a wide range of tools that allow us to simulate
and test programs within MPLABX all without having a real PIC. You will
use the simulator in this project, so select the simulator option shown in
Figure 1-4.

Figure 1-4. The Select Tool window

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

Figure 1-5. The Select Compiler window

You should select the XC8(V2.10) compiler software, although with


some later projects you will use V1.35, as shown in Figure 1-5. Then click
Next to move to the Select Project Name and Folder window shown in
Figure 1-6.

7
Chapter 1 Creating a Header File

Figure 1-6. The Select Project Name and Folder window

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.

Figure 1-7. The main window with the project created

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 tell everyone that you wrote this code.

• You should say what PIC you wrote it for and when you
wrote it.

• You should explain what you are trying to do with it.

12
Chapter 1 Creating a Header File

There are two types of comments in C programs, which are

• Single-line comments: They start with two forward


slashes (//). Anything on the same line after the two
forward slashes is ignored by the compiler as they are
simply comments. For example,

//these words are just comments

• Multiple lines of comments or a paragraph of


comments: This is text inserted between the following
symbols: /* */. For example,

/* Your comments are written in here */

So insert a paragraph of comments as shown in Figure 1-10.

Figure 1-10. The comments for the 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

Figure 1-11. Changing the font and colours

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

Figure 1-12. Selecting the configuration bits

Once you have selected the configuration bits, your main window will
change to that shown in Figure 1-13.

Figure 1-13. The configuration bits

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]

The author, in his conducting class at New York University has


experimented with several methods, and has finally hit upon the
following system of teaching the intricate baton technic involved in
the conducting of works like Stravinsky’s “Petrouchka.”
The student is made to sit at the piano and play simple five finger
figures with a single accent on the first note which is always played
by the thumb.
[Listen] [Listen]

[Listen]

[Listen] [Listen]
[Listen]

[Listen]

Playing the eighth notes in a rather quick tempo each exercise is


to be repeated until the feeling of the recurrence of the down-beat
(which corresponds to the accented thumb stroke) becomes entirely
automatic. Care must be taken never to vary the speed of the eighth
notes and to accent only the first note.
Translated into terms of this exercise the two examples from
“Petrouchka” would be as follows:
[Listen]

[Listen]
The speed of the eighth notes must never vary.

Fractional or uneven time


[Listen]
Beat 3-in-a-measure, merely making the third beat one-eighth
note longer.

[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.

ON THE CONDUCTING OF WALTZES


To begin with, a dividing line must be drawn between a waltz
played for dancing and the concert waltz. The former is performed in
a regular rhythmic manner everywhere, except in Vienna and South
America, where the dancers are accustomed to little freedoms of
tempo. There is so much really good music written in this form, that it
is a pity to hear waltzes “ground out” in the reprehensible one-beat-
in-a-measure style of so many of our Military Bandmasters. Portions
of Strauss’ “Artist’s Life” Waltzes are given in the following examples,
which also contain various modes of beating waltz time to conform
with the spirit of the music.
There are many ways of conducting waltz time. Some conductors
beat all the beats, others again, only one beat to the measure.
Analysis of some of the methods of the great conductors who have
not disdained to play the waltzes of composers like Waldteufel or
Johann Strauss, has lead us to believe that the three styles of
conducting explained in the following diagrams are the ones most
generally used.
A—The one-beat-in-a-measure style for passages of flowing
melody and great verve.
In order to avoid a monotony of motion, it is best to start the
down-beats of each measure, alternately from the left and the right.
The dotted line in the diagram indicates the reflex or rebound
movement, which brings the hand and arm in a position to start the
next beat.

DIAGRAM Nᵒ.1 (Style 1)


(A) Starting the beat from left to right.

(B) Starting the beat from the right.

B—Following the heavy down-beat of the measure, the second


beat will be indicated by a sharp sideward wrist movement and in
lieu of the third beat, the hand and arm will be drawn up to the
original position in a more relaxed manner.

DIAGRAM Nᵒ. 2 (Style 2)

Light and delicate rhythmic figures are best indicated by this


method.
C—The third method is the regular gesture used in any 3/4 or 3/8
time and indicates each beat.

DIAGRAM Nᵒ. 3 (Style 3)


Same as 3/4 time.

In the following extract from Artists’ Life Waltz by Strauss, the


three different styles are applied. The various strains and the manner
of beating each measure, are indicated by the Roman Numerals
which correspond to the diagrams.

I Diagram 1
II Diagram 2
III Diagram 3
[Listen]

From Hector Berlioz’


“Treatise on Conducting”
A dilemma sometimes presents itself when certain parts—for the
sake of contrast—are given a triple rhythm, while others preserve the
dual rhythm.

[Listen]

If the wind-instrument parts in the above example are confided to


players who are good musicians, there will be no need to change the
manner of marking the bar, and the conductor may continue to
subdivide it by six, or to divide it simply by two. The majority of
players, however, seeming to hesitate at the moment when, by
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

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.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like