0% found this document useful (0 votes)
20 views81 pages

Intermediate C Programming For The Pic Microcontroller Simplifying Embedded Programming 1st Ed Hubert Henry Ward Instant Download

The document is a resource for learning Intermediate C Programming specifically for the PIC Microcontroller, authored by Hubert Henry Ward. It covers various topics such as creating header files, controlling displays, and managing motors, with detailed explanations and code examples. The book is designed to simplify embedded programming and is available for download through a provided link.

Uploaded by

koningantyah
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
0% found this document useful (0 votes)
20 views81 pages

Intermediate C Programming For The Pic Microcontroller Simplifying Embedded Programming 1st Ed Hubert Henry Ward Instant Download

The document is a resource for learning Intermediate C Programming specifically for the PIC Microcontroller, authored by Hubert Henry Ward. It covers various topics such as creating header files, controlling displays, and managing motors, with detailed explanations and code examples. The book is designed to simplify embedded programming and is available for download through a provided link.

Uploaded by

koningantyah
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/ 81

Intermediate C Programming For The Pic

Microcontroller Simplifying Embedded Programming


1st Ed Hubert Henry Ward download

https://ebookbell.com/product/intermediate-c-programming-for-the-
pic-microcontroller-simplifying-embedded-programming-1st-ed-
hubert-henry-ward-22417478

Explore and download more ebooks at ebookbell.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

Python Programming Illustrated For Beginners Intermediates Learn By


Doing Approachstep By Step Ultimate Guide To Mastering Python The
Future Is Here Sullivan

https://ebookbell.com/product/python-programming-illustrated-for-
beginners-intermediates-learn-by-doing-approachstep-by-step-ultimate-
guide-to-mastering-python-the-future-is-here-sullivan-22122814

The Ultimate Python Programming Guide For Beginner To Intermediate


William Alvin Newton

https://ebookbell.com/product/the-ultimate-python-programming-guide-
for-beginner-to-intermediate-william-alvin-newton-48906050

Python Programming Python Programming For Beginners Python Programming


For Intermediates Adam Stewart

https://ebookbell.com/product/python-programming-python-programming-
for-beginners-python-programming-for-intermediates-adam-
stewart-5726612

Python Programming For Beginners Basic Language From Absolute


Beginners To Intermediate Learn Easily And Fast Data Science And Web
Development In A Simple And Practical Way Stepbystep Mark Coding

https://ebookbell.com/product/python-programming-for-beginners-basic-
language-from-absolute-beginners-to-intermediate-learn-easily-and-
fast-data-science-and-web-development-in-a-simple-and-practical-way-
stepbystep-mark-coding-11157304
An Intermediate Guide To Spss Programming Using Syntax For Data
Management 1st Edition Boslaugh

https://ebookbell.com/product/an-intermediate-guide-to-spss-
programming-using-syntax-for-data-management-1st-edition-
boslaugh-54492926

Intermediate C Programming 2nd Edition Yunghsiang Lu George K


Kuriakose Thiruvathukal

https://ebookbell.com/product/intermediate-c-programming-2nd-edition-
yunghsiang-lu-george-k-kuriakose-thiruvathukal-57676222

Intermediate C Programming 2nd Edition Yunghsiang Lu George K


Thiruvathukal

https://ebookbell.com/product/intermediate-c-programming-2nd-edition-
yunghsiang-lu-george-k-thiruvathukal-57793356

Intermediate C Programming Second Edition 2nd Edition Yunghsiang Lu


George K Thiruvathukal

https://ebookbell.com/product/intermediate-c-programming-second-
edition-2nd-edition-yunghsiang-lu-george-k-thiruvathukal-58159524

Intermediate C Programming Yunghsiang Lu

https://ebookbell.com/product/intermediate-c-programming-yunghsiang-
lu-10984206
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
Chapter 1 Creating a Header File

You may have to drag the window up to make it as larger as shown in


Figure 1-13.
This configuration window allows you, as the programmer, to select
some very important options for the PIC, the most important being the
primary oscillator type and source used and if you want the watch dog
timer or not.
There are three main options you need to change at this point. You
should change

• The OSC to INTIO67. This is done by selecting the


small arrow alongside the box next to the OSC option.
The default setting is usually RCI06, the resistor
capacitor oscillator with bit6 on PORTA left as a normal
I/O bit. You need to change this. When you click the
small arrow next to the OSC option RCIO6, a small
window will open. If you move the selection up to the
next one, it will be the one you want, INTIO67, which
means you will use the internal oscillator block as
the primary source and leave Bits 6 and 7 on PORTA
as normal I/O bits. Note that when you select this, a
description of the change will appear in the description
window alongside this tag and it will have a blue colour
to the text.

• The next change is simpler. Set the WDT to OFF. It


important to turn the WDT off because if nothing
happens for a predefined period of time in a program,
the WDT will stop the program. You don’t want this to
happen so you must turn the WDT off.

• The third change is to turn the LVP off.

Once you have changed these settings, you can generate the source
code and then paste this code into your program. To do so, click the

16
Chapter 1 Creating a Header File

Generate Source Code to Output tab shown at the bottom of the IDE. The
source code should appear in the output window on the screen, as shown
in Figure 1-14.

Figure 1-14. The source code for the configuration words

Use the mouse to select this code and the comments but do not select
the phrase #include <xc.h> because I want to discuss the importance
of this include file later in the book. So, once you have copied just the
configuration words and not the #include <xc.h>, paste the selection into
the header file you have in the open window.
I pasted these source instructions and comments into my open file
window starting at line 5 and ending at line 62. Yours may differ due to
what comments you have put in.
Your screen should look like that shown in Figure 1-15. Note that the
#include <xc.h> is not in the file.

17
Chapter 1 Creating a Header File

Figure 1-15. The configuration words inserted into the header file

You can now save this file in the usual way of saving a file because
you have all you need for this header file. This is done by clicking the File
option from the main menu bar and selecting Save from the flyout menu.

Including the Header File


Now that you have created this header file, let’s go through how to include
it in a program. However, before you can do that, you need to create the
program file in which you will include it. This means you must have some
idea of what your first program will be. It will be a program to drive a
seven-segment display. The main concept for the program is that you will
use a seven-segment display to count from 0 to 9 in intervals of 2 seconds.
This will involve creating a source file for this program. I will not go
through the details of the program until the next chapter; in this chapter,
you are only concerned with creating and using header files.

18
Chapter 1 Creating a Header File

Creating the Project Source File


To create the source file, you must first right-click the source files
subdirectory in the project tree area. When you do this, the screen shown
in Figure 1-16 will appear and you should select New and then main.c.

Figure 1-16. Creating a new main.c program file

Once you have selected the new main.c option, a new window will
open, which is where you will create the name for the source file. You
should name it sevenSegmentDisplay. The extension for the file name is c
for the C language. The window should look like the one in Figure 1-17.

19
Chapter 1 Creating a Header File

Figure 1-17. Naming the source file

Once you are happy with the file name, click Finish and the screen
will now have a new window open in the editing screen. This will include
a lot of text that Microchip automatically inserts. Again, you don’t need it
so delete all this text so that you have a clean editing window, as shown in
Figure 1-18.

20
Chapter 1 Creating a Header File

Figure 1-18. The empty source file

The next thing you should do is add some comments to show that
you own this source file. Therefore, using the multiple line option for your
comments, you should insert some comments along the following lines:

/*A program to control a seven segment display.


Written by Mr H. H. dated 02/01/2019
For the PIC 18F4525*/

You can amend the comments as you feel fit. The screen should now
look similar to that shown in Figure 1-19.

Figure 1-19. The comments added to the source file

21
Chapter 1 Creating a Header File

Now you need to tell the compiler to include the header file you just
created. There are two ways you can add the header file. One is as a local
header file, which is not much good really. If you have created a local
header file, it will only be available to this local project. However, I will
show you how to include the header file in this way first.
Make sure the cursor is waiting at the start of the next empty line in the
source file you just created. Now start to write the phrase #include. You do
need the # sign. Indeed, as you write the # sign, a pop-up menu will appear
with some options for you to choose from. This is the IntelliSense part of the
compiler software. It is like predictive text on your phone. The software tries to
guess what you are doing, and the # sign has a specific meaning. You should
see the word “include” at the bottom of the pop-up menu. You can either
continue to write include or select the word from the pop-up menu. However,
if the pop-up menu does not appear, simply write the word “include.”
No matter which way you insert the word include, when you next press
the space bar, after inserting the word include, to move the cursor away
from the word include, you should see another pop-up menu appear, as
shown in Figure 1-20.

Figure 1-20. The include option

You should see the name of the header file you just created in the pop-up
menu. Select this file by clicking it to paste the file name into your source file.
Again, if the pop-up menu does not appear, you can write "conFigInternal
OscNoWDTNoLVP.h". Note that you must use the quotation marks. This is the
name of the header file you want to include in your program.

22
Chapter 1 Creating a Header File

It should then be present, as shown in Figure 1-21.

Figure 1-21. The header file included in your program

Creating a Global Header File


However, this is not the best way to use the header file because it is only
available to this project since it is a local header file. You need to make it into
a global header file, which means the header file will be available to be used
in all of your projects. The way to do this is to save or copy the header file
into the compiler software directory. This is slightly more involved because
you need to find the include directory for Microchip on your computer. I
found it on my laptop and the path for this directory is shown here:

C:\ProgramFiles(x86)\Microchip\xc8\v2.10\pic\include

You will have to find your directory. However, assuming you used
the default installation, then the path should be the same as mine stated
here. In earlier versions, it may be slightly different. You are looking for the
include directory in your compiler software. You may need to use the file
explorer to find the header file in the current project directory and copy
the file to the include directory as stated above.
Assuming you have been able to copy this header file into the correct
directory, this header file is now a global file and you can include it in any
of your projects. It’s a much better way to use the header file.
To include this global header file, delete the previous include line
of your program. Now insert the # and include statements as before but

23
Chapter 1 Creating a Header File

now when you hit the space bar to move the cursor away from the include
word, ignore the pop-up menu and type in the following symbol: <. The
IntelliSense will take over and insert the > symbol with the cursor flashing
inside the two symbols. Also, a pop-up menu will appear listing all of the
include files that are stored in the include directory of the xc8 compiler
software. If you now type the letter c in the space between the two < >
symbols, the fly-out menu will filter out all the files that don’t start with
the letter c. You should see the header file you have just copied into the
include directory, as shown in Figure 1-22.

Figure 1-22. The include pop-up menu

If you click the header file you just copied, that file name should
appear between the < > symbols, as shown in Figure 1-23.

Figure 1-23. The included conFigInternalNoWDTNoLVP header file

If you have difficulty getting the pop-up menus to appear or work, you
can simply write the complete instruction as stated here:

24
Chapter 1 Creating a Header File

#include <conFigInternaOscNoWDTNoLVP.h>
You do need the < > symbols. This approach can be used to include
any global header file: simply write the name of the header file you want to
include between the < > symbols.
Now you need to think about the very important header file that must
be included in all of your projects. The header file you need is the xc.h
file. To include it into your source file, you need to put the cursor onto the
next line in the file (i.e. line 5) by pressing the Enter key on your keyboard.
Now write the #include and press the space bar as before. Then, ignoring
the pop-up window, type the first < and then the type x between the two
symbols < > as before. The available header files will reduce to those
shown in Figure 1-24.

Figure 1-24. The pop-up window showing the filtered options

Again you should see the name of the header file you are looking for in
the pop-up window. Click the xc.h file to add it to the source file. The file
should now look like that shown in Figure 1-25.

Figure 1-25. The two include files

25
Chapter 1 Creating a Header File

You will look at the importance of this xc.h file later.

The PICSetUp.h Header File


Now that you have created and inserted your first global header file, you
will create your second one.
This header file will set up the PIC to use the ports, the internal
oscillator block, and the TMR0 timer as you intend to use them in all of your
projects. Therefore, it’s useful to list how you want to use these items now.

• PORTA: Set all 8 bits to be digital inputs.


• PORTB: Set all 8 bits to be digital outputs.
• PORTC: Set all 8 bits to be digital outputs.
• PORTD: Set all 8 bits to be digital inputs.
• PORTE: Set all 3 bits to be digital outputs.
• Turn the ADC off.
• The internal oscillator block will be set to produce an
8Mhz frequency that will be stable.
• Timer0, or TMR0, will be set to an 8-bit register that will
apply the maximum divide rate so that it counts at a
frequency of 7812.5Hz. This means that one tick takes
128μs.

Assuming you want all these exact same settings for all of your projects,
you can create a header file for this setup. If you want something slightly
different, you must write to the SFR (special function register) after this file
has been used.
To create the header file, right-click the header files subdirectory in
the project tree. Then select New from the flyout menu and XC8_header.h
from the second flyout menu. Name the header file as PICSetUp and click
Finish to create the header file.

26
Chapter 1 Creating a Header File

This new file should be open in the editing window. Again, you should
delete all the stuff that MPLABX automatically inserts in the file since you
don’t need it and I really want your screens to look the same as mine.
Assuming that you now have an empty screen, you can write the
instructions for this header file. They are shown here as Listing 1-1. You
do not have to write the line numbers as shown here. These numbers are
there to enable me to refer to each instruction in my analysis of how the
instructions work.

Listing 1-1. Instructions for the PICSetup.h Header File

1. void initialise ()
2. {
3. PORTA = 0;
4. PORTB = 0;
5. PORTC = 0;
6. PORTD = 0;
7. PORTE = 0;
8. TRISA = 0XFF;
9. TRISB = 0;
10. TRISC = 0;
11. TRISD = 0b11111111;
12. TRISE = 0;
13. ADCON0 = 0;
14. ADCON1 = 0b00001111;
15. OSCTUNE = 0;
16. OSCCON = 0b01110100;
17. T0CON = 0b11000111;
18. }

27
Chapter 1 Creating a Header File

A
 nalysis of Listing 1-1
line 1 void initialise ()
To understand this instruction, you should appreciate that all C
programs run in a series of loops. There must be a main loop because this
is where the micro must go to get the first instruction of the program.
There are then a series of what are sometimes referred to as functions
or methods, but I prefer to call them subroutines, as they are small sections
of programs that the main loop can call as many times as the main program
needs to execute the instructions in the subroutine.
With this header file you are creating a subroutine. The subroutine
will only be called once from the main program and it will set up the SFRs
(special function registers) in the way you want to use them in the PIC.
All subroutines may or may not require the main program to pass
parameters up to the subroutine. Also, the subroutine may or may not pass
parameters back to the main program loop. All subroutines need a name
that describes the purpose of the subroutine.
In this case, this subroutine will not be passing any parameters back
to the main loop. This is signified by the use of the keyword void. Note as
you type this keyword void into the text window inside the IDE, you will
see it is case-sensitive and it must be in lowercase. As you finish writing
the word, the font colour changes to blue. IntelliSense uses colours to
recognize the significance of special words.
You must give the subroutine a name and in this case it is called
initialise, as it does initialize the PIC. Then, as you enter the first normal
opening bracket, IntelliSense automatically adds the normal closing bracket
and inserts the cursor between the two brackets. Here you can define what
type of variable the subroutine is expecting to be passed up to it when it is
called from the main program. If the subroutine does not want a variable
to be passed up to it, leave this space empty, as I have done here. Some
programmers insert the word “void” here but its just personal preference.

Line 2 {

28
Chapter 1 Creating a Header File

This is the opening curly bracket of the subroutine. As you type


this opening curly bracket, and then press the Enter key, the software
automatically adds a closing curly bracket. It then inserts the cursor
between the two curly brackets but indented by one tab. The two curly
brackets define the confines of the subroutine; indeed they will be
connected by a line alongside the two brackets in the editing window.
The cursor is now waiting inside the confines of the subroutine for you
to type in the instructions of the subroutine.

Line 3 PORTA = 0;

To fully appreciate this instruction, you should know that every PIC has
ports, which get their name from naval ports that take goods into and out
of the country. In the same way, the ports of the PIC take data into and out
of the PIC. Inputs can be simple switches, sensors, or transducers, whereas
outputs can be lamps, motors, or pumps.
What you are doing in the next five lines is making sure that anything
that might be connected to the ports of the PIC is not switched on. Note a
logic 0 means that the bits have 0v on them. The instruction PORTA = 0;
means all 8 bits on PORTA are at logic 0 or 0V.
One more thing before I describe the next instruction is the semicolon
symbol. It actually denotes the end of the current instruction. However,
with lines 1 and 2 there is no need for the semicolon because they are
statements for the compiler software, not instructions for the PIC.
Lines 4 to 7 simply turn off anything that might be connected to the
other ports of the PIC.

Line 8 TRISA = 0XFF;

To understand this instruction, you must realize that with any port
data can go into the PIC or out of the PIC. Indeed, with any bit of the port
this can happen. With the PC18F4525 all ports have 8 bits, as this is an 8-bit
PIC, except for PORTE, which has 3 bits. Note that the bits are numbered

29
Chapter 1 Creating a Header File

b7, b6, b5, b4, b3, b2, b1, and b0, going from left to right. Bit0 is the LSB
(least significant bit) and bit7 is the MSB (most significant bit).
Well, you have to tell the PIC which way the data is to flow through the
bits of each port, in or out. To facilitate this, each port has another SFR to
set the direction of data through the bits of the port. This SFR is called a
TRIS, so TRISA, TRISB, etc.
Each bit of the TRIS maps directly onto the same numbered bit of the
port. If the bit in the TRIS is a logic 1, then the corresponding bit in the port
will be an input. If the bit in the TRIS is a logic 0, then the corresponding
bit in the port will be an output.
For example, if the data in TRISA is 00110101 then the bits in PORTA is

• B7 is an output.

• B6 is an output.

• B5 is an input.

• B4 is an input.

• B3 is an output.

• B2 is an input.

• B1 is an output.
• B0 is an input.

In line 8, the instruction is TRISA = 0XFF;. The 0X tells the compiler


that the radix, or number system, for the number is hexadecimal. This
converts to 11111111 in binary or 255 in decimal. Appendix tk explains
how to convert numbers.
This instruction means that all the bits in TRISA are forced to go to a
logic 1, which in turn makes all the bits in PORTA input. This is what you
want.

30
Chapter 1 Creating a Header File

Exercise 1.1 The answers to all exercises will be given at the end


of each chapter.
In this exercise, write the data required to set the bits in PORTD as
follows:
B7 is an output.
B6 is an output.
B5 is an input.
B4 is an input.
B3 is an input.
B2 is an input
B1 is an output.
B0 is an input.
State which SFR it must be written to.

Line 9 TRISB = 0;

Here, as you have not stated what radix, or number system, you are
using, the compiler assumes it is the default radix, which is decimal. This
means that this instruction is loading the TRISB with the value 0, which is
00000000 in binary. Therefore, all the bits in TRISB are forced to a logic 0,
which in turn means that all the bits in PORTB are set as outputs.

Line 10 TRISC = 0;

This makes all bits in PORTC output.

Line 11 TRISD = 0b11111111;

31
Chapter 1 Creating a Header File

The 0b in front of the number means the radix is binary and what
follows is a binary number. This makes all the bits in TRISD logic 1 and so
sets all bits in PORTD to inputs.

Line 12 TRISE = 0;

This simply sets all bits in PORTE to outputs.

Line 13 ADCON0 = 0;

To appreciate what you are doing here, you must realize that there can
be two different types of inputs to the PIC. They are

• Digital: Simply off or on, open or closed, logic 0 or logic


1. They could be from simple switches or sensors.

• Analog: They are usually voltages that can range from


0v to 5v and are obtained from transducers that are
used to measure physical parameters such as velocity,
temperature, voltage, etc. These are presented to the
PIC as analog signals and then the PIC converts them,
using an ADC (analog-to-digital converter) to digital
values to be used by the program.

The default setting is that all the inputs can be either digital or analog,
and there are 13 such inputs in the PIC 18F4525, which are set as analog.
This means you must do two things:

• Set all the inputs to digital inputs.

• Turn the ADC off since you are not using it.

To help understand how you get the correct number to write to


the correct SFRs, here are two tables taken from the data sheet for the
PIC18F4525. If you are using any device, be it a PIC, OPAMP, or any
electronic device, you must be able to use the data sheets for that device.
Tables 1-1 and 1-2 explain how to control the ADC.

32
Chapter 1 Creating a Header File

Table 1-1. The ADCON0 Register


Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0

Not used Not used CHS3 CHS2 CHS1 CHS0 GO/DONE ADON

Bit7 Not used, read as 0


Bit6 Not used, read as 0
Bit5 Bit4 Bit3 Bit2 ADC channel selected
0 0 0 0 AN0
0 0 0 1 AN1
0 0 1 0 AN2
0 0 1 1 AN3
0 1 0 0 AN4
0 1 0 1 AN5
0 1 1 0 AN6
0 1 1 1 AN7
1 0 0 0 AN8
1 0 0 1 AN9
1 0 1 0 AN10
1 0 1 1 AN11
1 1 0 0 AN12
1 1 0 1 Not used
1 1 1 0 Not used
1 1 1 1 Not used

(continued)

33
Chapter 1 Creating a Header File

Table 1-1. (continued)

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0

Bit1 1 means start a conversion and a conversion is taking place


0 means the conversion has finished
Bit0 1 means enable the ADC
0 means disable the ADC

The main purpose of this control register is to allow the programmer


to choose which analog input, or channel, is connected to the ADC. I have
stated that there are 13 possible analog inputs but that doesn’t mean there
are 13 ADC circuits. There is only one ADC circuit but it can be connected
to any one of the 13 inputs that can take an analog signal in. This is a form
of multiplexing where many inputs feed into one device one at a time. The
choice of which input is connected to the ADC is controlled by the data in
Bits 5, 4, 3, and 2 of the ADCON0 register. For example, if these four bits are
set to 0011, going from b5 to b2 left to right, then it is the fourth input, AN3,
that is connected to the ADC; see Table 1-1. Note that Bits 7 and 6 are not
used so they are set to logic 0.
Bit0 is the bit that actually turns the ADC on or not. A logic 1 means the
ADC is enabled whereas a logic 0 means it is disabled.
The last remaining bit, Bit1, is used to start the ADC conversion
and tell the programmer when the conversion is finished or done. The
programmer must set this bit to a logic 1 to start the ADC conversion.
Then, when the conversion is finished, the microprocessor sets this bit
back to a logic 0 automatically. This is a signal to tell the programmer that
the ADC conversion has finished.

34
Chapter 1 Creating a Header File

Table 1-2. The ADCON1 Register


Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0

Not used Not used VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

Bit7 Not used, read as 0


Bit6 Not used, read as 0
Bit5 1 negative reference from AN2
0 negative reference from VSS
Bit4 1 positive reference from AN3
0 positive reference from VDD
Bit Channel AN
3 2 1 0 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 A A A A A A A A A A A A A
0 0 0 1 A A A A A A A A A A A A A
0 0 1 0 A A A A A A A A A A A A A
0 0 1 1 D A A A A A A A A A A A A
0 1 0 0 D D A A A A A A A A A A A
0 1 0 1 D D D A A A A A A A A A A
0 1 1 0 D D D D A A A A A A A A A
0 1 1 1 D D D D D A A A A A A A A
1 0 0 0 D D D D D D A A A A A A A
1 0 0 1 D D D D D D D A A A A A A
1 0 1 0 D D D D D D D D A A A A A
1 0 1 1 D D D D D D D D D A A A A
1 1 0 0 D D D D D D D D D D A A A
(continued)

35
Chapter 1 Creating a Header File

Table 1-2. (continued)

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0


Bit Channel AN
3 2 1 0 12 11 10 9 8 7 6 5 4 3 2 1 0
1 1 0 1 D D D D D D D D D D D A A
1 1 1 0 D D D D D D D D D D D D A
1 1 1 1 D D D D D D D D D D D D D

This register mostly controls whether the 13 inputs are to be used as


analog or digital. It is the first four bits (b0, b1, b2, and b3) that do this.
Table 1-2 shows how this is achieved. If all four bits are a logic 0, then all
13 channels are set to analog inputs. However, if the four bits are 0011,
going from B3 to B0 left to right, then the 13th channel, AN12, is set to
digital and the rest are set to analog.
The ADC needs a reference voltage to help determine the level of the
analog input. Bit4 controls where the PIC gets the positive reference. The
default, and so normal setting, is to use the supply to the PIC (VCC or VDD).
Bit5 controls where the PIC gets the negative reference. The default,
and so normal setting, is to use the supply to the PIC (VSS or ground).
Bits 6 and 7 are not used.
Armed with this knowledge, you can see that the instruction at line 13,
ADCONO = 0;, simply turns off the ADC. It also connects the first channel,
or input, AN0, to the ADC but as the ADC is turned off this does not matter.

Line 14 ADCON1 = 0b00001111;

This sets the bits on the SFR ADCON1 to 00001111. This instruction uses
Table 1-2. From it you can see that as the first four bits of the ADCON1 SRF are
set to 1111, this makes all the inputs digital. This is what you want. When you
eventually need some analog inputs, you will have to make some changes.

Line 15 OSCTUNE = 0;

36
Exploring the Variety of Random
Documents with Different Content
amends for his first foolish act. He found a strong lance in the
hermitage, took it, and departed.
When Orilus and his lady had been received with honour at Arthur’s
Court, the king with all his knights set forth towards Munsalvaesch to
find the mighty man calling himself the Red Knight, who had sent so
many conquered pledges of his prowess; for he wished to make him
a knight of the Round Table. It was winter. Parzival—the Red Knight
—came riding from the opposite direction. As he drew near the
encampment of the king, his eye lighted on three drops of blood
showing clear red in the fresh-fallen snow; in mid air above, a wild
goose had been struck by a falcon. The knight paused in reverie—
red and white—the colours carried his thoughts to his heart’s queen,
Condwiramurs. There he sat, as a statue on his horse, with poised
spear; his thoughts had flown to her whose image now closed his
eyes to all else. A lad spied the great knight, and ran breathless to
Arthur, to tell of the stranger who seemed to challenge all the Round
Table. Segramors gained Arthur’s permission to accost him. Out he
rode with ready challenge; Parzival neither saw nor heard, till his
horse swerved at the knight’s approach, so that he saw the drops no
longer. Then his mighty lance fell in rest, Segramors was hurled to
the ground, and took himself back discomfited, while Parzival
returned to gaze on the drops of blood, lost in reverie as before.
Now Kay the quarrelsome rode out, and roused the hero with a rude
blow. The joust is run again, and Kay crawls back with broken leg
and arm. Again Parzival loses himself in reverie. And now courtly
Gawain, best of Arthur’s knights, rides forth, unarmed. Courteously
he addresses Parzival, who hears nothing, and sits moveless. Gawain
bethinks him it is love that binds the knight. Seeing that Parzival is
gazing on three drops of blood, he gently covers them with a silken
cloth. Parzival’s wits return; he moans: “Alas, lady wife of mine, what
comes between us? A cloud has hidden thee.” Then, astonished, he
sees Gawain—a knight without lance or shield—does he come to
mock? With noble courtesy Gawain disclosed himself and led the
way to Arthur’s Court, where fair ladies and the king greeted the
hero whom they had come to seek. A festival was ordained in his
honour. The fair company of knights and ladies are seated about the
Round Table; the feast is at its height, when suddenly upon a
gigantic mule, a scourge in her rough hand, comes riding the
seeress Cundrie, harsh and unlovely. Straight she addresses Arthur:
“Son of King Uterpendragon, you have shamed yourself and this
high company, receiving Parzival, whom you call the Red Knight.”
She turns on Parzival: “Disgrace fall on your proud form and
strength! Sir Parzival, tell me, how came it that you met that joyless
fisher, and did not help him? He showed you his pain, and you, false
guest, had no pity for him. Abhorred by all good men, marked for
hell by heaven’s Highest, you ban of happiness and curse of joy! No
leech can heal your sickened honour. Greater betrayal never shamed
a man so goodly. Your host gave you a sword; you saw them bear
the Grail, the silver dishes, and the bloody spear, and you,
dishonoured Parzival, were silent. You failed to win earth’s chiefest
prize; your father had not done so—are you his son? Yes, for
Herzeloide was as true as he. Woe’s me, that Herzeloide’s child has
so let honour slip!” Cundrie wrung her hands; her tears fell fast; she
turned her mule and cried: “Woe, woe to thee Munsalvaesch, mount
of pain; here is no aid for thee!” And bidding none farewell, she rode
away, leaving Parzival to his shame, the knights to their
astonishment, the ladies to their tears.
Cundrie was hardly out of sight, before another shame was put on
the Round Table. An armed knight rode in, and, accusing Gawain of
murdering his king and cousin, summoned him to mortal combat
within forty days before the King of Askalon. Arthur himself was
ready to do battle for Gawain, but that good knight accepted the
challenge with all courtesy.
Parzival’s lineage was first known to the Court from Cundrie’s calling
him by name and speaking of his mother. Now Clamide, once
Condwiramurs’s cruel wooer, begged the hero to intercede for him
with another fair one, the lady Cunneware. Parzival courteously
complied. A heathen queen then saluted him with the news that he
had a great heathen half-brother, Feirefiz, the son of Parzival’s father
by a heathen queen. Thanking her, Parzival spoke to the company:
“I cannot endure Cundrie’s reproach;—what knight here does not
look askance? I will seek no joy until I find the Grail, be the quest
short or long. The worthy Gurnemanz bade me refrain from
questions. Honoured knights, your favour is for me to win again, for
I have lost it. Me yet unshamed you took into your company; I
release you. Let sorrow be my comrade; for I forsook my happiness
on Munsalvaesch. Ah! helpless Anfortas! You had small help from
me.”
Knights and ladies were grieved to see the hero depart in such
sorrow, and many a knight’s service was offered him. The lady
Cunneware took his hand; Lord Gawain kissed him and said: “I know
thy way is full of strife; God grant to thee good fortune, and to me
the chance to serve thee.”
“Ah! what is God?” answered Parzival. “Were He strong He would not
have put such shame on me and you. I was His subject from the
hour I learned to ask His favour. Now I renounce His service. If He
hates me, I will bear it. Friend, in thine hour of strife let the love of a
woman pure and true strengthen thy hand. I know not when I shall
see thee again; may my good wishes towards thee be fulfilled.”
The hero’s arms are brought; his horse is saddled; his grievous toil
begins.
Why should long sorrow come to Parzival for not asking a question,
when his omission was caused neither by brutality nor ill will? when,
on the contrary, he would gladly have served his host? The relation
between his conduct and his fortune seems lame. Yet in life as well
as in literature, ignorance and error bring punishment. Moreover, to
mediaeval romance not only is there a background of sorcery and
magic, but active elements of magic survive in the tales.[725] And
nothing is more fraught with magic import and result than question
and answer. Wolfram did not treat as magical the effect upon his
hero’s lot of his failure to ask the question; but he retained the
palpably magic import of the act as affecting the sick Anfortas. It
was hard that the omission should have brought Parzival to sorrow
and despair; yet the fault was part of himself, and the man so
ignorant and unwise was sure to incur calamity, and also gain
sorrow’s lessons if he was capable of learning. So the sequence
becomes ethical: from error, calamity; from calamity, grief; and from
grief, wisdom. With Wolfram, Parzival’s fault was Parzival; failure to
ask the question was a symbol of his lack of wisdom. The poet was
of his time; and mediaeval thought tended to symbolism, and to
move, as it were, from symbol to symbol, and from symbolical
significance to related symbolical significance, and indeed often to
treat a symbol as if it were the fact which was symbolized.

At this point Wolfram’s poem devotes some cantos to the lighter-


hearted adventures of Gawain. This valiant, courtly, loyal knight and
his adventures are throughout a foil to the heavier lot and character
of Parzival. But when Gawain has had his due, the poet is glad to
return to his rightful hero. Parzival has ridden through many lands;
he has sailed many seas; before his lance no knight has kept his
seat; his praise and fame are spread afar. Though he has never been
overthrown, the sword given him by Anfortas broke; but with magic
water Parzival welded it again. In a forest one day he rode up to a
hut, where Sigune was living as a recluse, feeding her soul with
thoughts of her dead lover, barring all fancies that might disunite her
from the dead whom she still held as her husband. Parzival
recognized her, and she him, when he removed his helm: “You are
Sir Parzival—tell me, how is it with the Grail?”
“It has given me sorrow enough; I left a land where I was king, a
loving wife, fairest of women; I suffer anguish for her love, and
more because of that high goal of Munsalvaesch which is not
reached. Cousin Sigune, knowing my sorrow, you do wrong to hate
me.”
“My wrath is spent. You have lost joy enough since that time you
failed to question Anfortas, your host—your happiness as well. Then
that question would have blessed you; now joy is denied you; your
high mood halts; your heart is tamed by sorrow, which had stayed a
stranger to it had you asked the question.”
“I acted as a luckless man. Dear cousin, counsel me—but, say, how
is it with you? I should bemoan your grief were not my own greater
than man ever bore.”
“Let His hand help you who knows all sorrow. A path might bring
you yet to Munsalvaesch. Cundrie but now rode hence—follow her
track.”
Parzival started to follow the track of Cundrie’s mule, which soon
was lost, and with it the Grail was lost again. Without guidance he
rode on. He overthrew a Grail knight, and took his horse, his own
having been wounded in the combat. How long he rode I know not,
says the poet. One frosty morning he met an aged knight
unhelmeted, and walking barefoot with his wife and daughters. The
knight reproved him for riding armed on that holy day.
Parzival answered: “I do not know the time of year; it is long since I
kept count of days. Once I served Him who is called God—until He
graced me with His mockery. He helps, men say. I have not found it
so.”
“If you mean God who was born of a virgin,” replied the old knight,
“and believe that He took man’s nature, you do wrong to ride in
armour; for this is the day when He hung on the Cross for us. Sir,
not far from here dwells a holy man, who will give you counsel; you
may repent and be absolved from your sins.”
Parzival courteously took his leave. He had regarded his failure to
ask that question as a luckless error, had felt that God was unjust to
him, and had also doubted His power to aid. Now came wavering
thoughts: “What if God might help my pain? If He ever favoured a
knight, or if sword and shield might win His favour—if to-day is His
day of help, let Him help me if He can. If God’s craft can show the
way to man and horse, I’ll honour Him. Go then according to God’s
choosing.”
He flung the bridle on his horse’s neck, spurring him forward; and
the horse carried him straight to the hermitage of holy Trevrizent,
who fasted there to fit himself for heaven, his chastity warring with
the devil. Parzival recognized the place where he had sworn the oath
to Orilus, to clear Jesute’s honour. The hermit, seeing him,
exclaimed: “Alas! sir, that you ride equipped in this holy season.
Were you sore pressed? Another garb were fitter, did your pride
permit. Come by the fire. If you follow love’s adventure, think of that
afterward, and this day seek the love which this day gives.”
Dismounting, Parzival stood respectfully before the hermit: “Sir,
advise me; I am a man of sin.”
His host promised counsel and asked how he came there. Parzival
told of meeting the old knight, and inquired whether his host felt no
fear at seeing him ride up. “Believe me, no,” answered the hermit; “I
fear no man. I would not boast, but in my day my heart never
quailed in the fight. I was a knight as you are, and had many sinful
thoughts.”
Having placed the horse in shelter beneath a cliff, the hermit led the
knight into his cell. There was a fire of coals, before which Parzival
was glad to warm himself and exchange his steel armour for a cloak;
he seemed forest-weary. A door opened to an inner cell, where
stood an altar, bearing the very reliquary on which Parzival had laid
his hand in making oath. He told his host of this, and of the lance
which he had found there and taken. “A friend of mine left it there,
and chided with me afterwards. It is four years, six months, and
three days since you took that spear; I will prove it to you from this
Psalter.”
“I did not know how long I had journeyed, lost and unhappy. I carry
sorrow’s weight. Sir, I will tell you more: from that time no man has
seen me in church or minster, where they honour God. I have
sought battles only. I also bear a hate for God. He is my trouble’s
sponsor: had He borne aid, my joy had not been buried living! My
heart is sore. In reward of my many fights, sorrow has set on me a
crown—of thorns. I bear a grudge against that Lord of aid, that me
alone He helps not.”
The host sighed, and looked at him; then spoke: “Sir, be wise. You
should trust God well. He will help you, it is His office; He must help
us both. Tell me with sober wits, how did your anger against Him
arise? Learn from me His guiltlessness before you accuse Him. His
aid is never withheld. Even I, a layman, can read the meaning of
those unlying books; man must continue steadfast in service of Him
who never wearies in His steady aid to sinking souls. Keep troth, for
God is troth. Deceit is hateful to Him. We should be grateful; in our
behalf His nobility took on the form of man. God is called, and is,
truth. He can turn from no one; teach your thoughts never to turn
from Him. You can force nothing from Him with your wrath.
Whoever sees you carry hate toward Him will deem you sick of wit.
Think of Lucifer and all his comrades. Hell was their reward. When
Lucifer and his host had taken their hell-journey, a man was made.
God made from clay the worthy Adam. From Adam’s flesh He took
Eve, who brought us calamity when she listened not to her Creator,
and destroyed our joy. Two sons were born to them. One of these in
envious anger destroyed his grandmother’s maidenhood, by sin.”
“Sir, how could that be?”
“The earth was Adam’s mother, and was a maiden. Adam was Cain’s
father, who slew Abel; and the blood fell on the pure earth; its
maidenhood was sped. Thence arose hate among men—and still
endures. Nothing in the world is as pure as an innocent maid; God
was himself a maiden’s child, and took the image of the first maid’s
fruit. With Adam’s seed came sorrow and joy; through him our
lineage is from God, but through him, too, we carry sin, for which
God took man’s image, and so suffered, battling with troth against
untroth. Turn to Him if you would not be lost. Plato, Sibyl the
prophetess, foretold Him. With divine love His mighty hand plucked
us from hell. The joyful news they tell of Him the True Lover is this:
He is radiant light, and wavers not in His love. Men may have either
His love or hate. The unrepentant sinner flees the divine
faithfulness; he who does penance wins His clemency. God
penetrates thought, which is hidden to the sun’s rays and needs no
castle’s ward. Yet God’s light passes its dark wall, comes stealing in,
and noiselessly departs. No thought so quick but He discovers it
before it leaves the heart. The pure in heart He chooses. Woe to the
man who harbours evil. What help is there in human craft for him
whose deeds put God to shame? You are lost if you act in His
despite, who is prepared for either love or hate. Now change your
heart; with goodness earn His thanks.”
“Sir,” says Parzival, “I am glad to be taught by you of Him who does
not fail to reward both crime and virtue. With pain and struggle I
have so borne my young life to this day that through keeping troth I
have got sorrow.”
Parzival still feels his innocence; perhaps the host is not so sure:
“Prithee, be open with me. I would gladly hear your troubles and
your sins. May be I can advise you.”
“The Grail is my chief woe and then my wife—she is beyond
compare. For both of these I yearn.”
“Sir, you say well. Your grief is righteous if its cause is yearning for
your wife. If you were cast to hell for other sins, but loyal to your
wife, God’s hand would lift you out. As for the Grail, you foolish man,
pursuit will never win it. ’Tis for him only who is named in heaven. I
can say; for I have seen it.”
“Sir, were you there?”
“I was.”
Parzival did not say that he had been there too; but asked about the
Grail. His host then told him of the valiant Templars who dwelt on
Munsalvaesch, and rode thence on adventures as penance for their
sins. “They are nourished by a Stone of marvellous virtue; no sick
man seeing it could die that week; it gives youth and strength, and
is called the Grail. To-day, as on every Good Friday, a dove flies from
heaven and lays a wafer on the Grail, from which the Grail receives
its share of every food and every good the earth or Paradise affords.
The name of whosoever is chosen for the Grail, be it boy or girl,
appears inscribed upon it, suddenly, and when read disappears.
They come as children; glad the mother whose child is named; for
taken to that company, it will be held from sin and shame, and be
received in heaven when this life is past. Further, all those who took
neither side in the war between Lucifer and the Trinity, were cast out
of heaven to earth, and here must serve the Grail.”
Parzival spoke: “If knighthood might with shield and spear win
earth’s prize and Paradise for the soul—why I have fought wherever
I found fight; often my hand has touched the prize. If God is wise in
conflicts, He should name me, that those people there may learn to
know me. My hand never drew back.”
“First you must guard against pride, and practise modesty.” The old
man paused and then continued: “There was a Grail king named
Anfortas. You and I should pity his sad lot which befell him through
pride in youth and riches; he loved in the world’s light way—that also
goes not with the Grail. There came once to the castle one
unnamed, a simple man; he went away, his sins upon his head; he
never asked the host what ailed him. Before that time a prince,
Lahelein, approached and fought with a Grail knight, and slew him
and took his horse. Sir, are you Lahelein? you rode a Grail steed
hither. I know his trappings well, and the dove’s crest which Anfortas
gave his knights. The old Titurel also wore that crest, and after him
his son Frimutel, till he lost his life. Sir, you resemble him. Who are
you?”
Each looked on the other. Parzival spoke: “My father was a knight.
He lost his life in combat; sir, include him in your prayers. His name
was Gamuhret. I am not Lahelein; yet in my folly once I too robbed
the dead. My sinful hand slew Ither. I left him dead upon the sward
—and took what was to take.”
“O world! alas for thee! heart’s sorrow is thy pay!” the hermit cried.
“My nephew, it was your own flesh and blood you slew; a deed
which with God merits death. Ither, the pattern of all knights—how
can you atone? My sister too, your mother Herzeloide, you brought
her to her death.”
“Oh no! good sir, how say you that? If I am your sister’s child, oh tell
me all.”
“Your mother died when you left her. My other sister was Sigune’s
mother; our brother is Anfortas, who long has been the Grail’s sad
lord. We early lost our father, Frimutel; from him Anfortas, his first-
born, inherited the Grail crown, when still a child. As he grew a man,
all too eagerly he followed the service set by love of woman, chose
him a mistress and broke many a spear for her. He disobeyed the
Grail, which forbids its lords love’s service, save as it prescribes. One
day, for his lady’s favour, he ran a joust with a heathen knight. He
slew him, but the heathen spear struck him, and broke, leaving a
poisoned wound. In anguish he returned. No medicine or charm can
heal that wound, and yet he cannot die; that is the Grail’s power. I
renounced knighthood, flesh, and wine, in prayer that God would
heal him. We knelt before the Grail, and on it read that when a
knight should come, and, unadmonished, ask what ailed him, he
should be sound again. That knight should then be the Grail’s king,
in place of Anfortas. Since then a knight did come—I spoke of him to
you. He might as well have stayed away for all the honour that he
won or aid he brought us. He did not ask: My lord, what brought you
to this pass? Stupidity forbade him.”
The two made moan together. It was noon. The host said: “Let us
take food now, and tend your horse.” They went out; Parzival broke
up some branches for his horse, while the host gathered a repast of
herbs. Then they returned to the cell. “Dear nephew,” said the
hermit, “do not despise this food. At least, you will not find another
host who would more gladly give you better.”
“Sir, may God’s favour pass me by, if ever a host’s care was sweeter
to me.”
When they had eaten, they saw to the horse again, whose hungry
plight grieved the old man because of the saddle with Anfortas’s
crest. Then Parzival spoke:
“Lord and uncle mine, if I dare speak for shame, I should tell you all
my unhappiness. My troth takes refuge in you. My misdeeds are so
sore, that if you cast me off I shall go all my days unloosed from my
remorse. Take pity with good counsel on a fool. He who rode to
Munsalvaesch, and saw that pain, and asked no question, that was
I, misfortune’s child. Thus have I, sir, misdone.”
“Nephew! Alas! We both may well lament—where were your five
senses? Yet I will not refuse thee counsel. You must not grieve
overmuch, but, in lament and laying grief aside, follow right
measure. Would that I might refresh and hearten you, so that you
would push on, and not despair of God. You might still cure your
sorrow. God will not forsake you. I counsel thee from Him.”
His host then told Parzival more about Anfortas’s pains, and about
the Grail people, then the story of his own life before he renounced
knighthood, and also about Ither. “Ither was your kin. If your hand
forgot this kinship, God will not. You must do penance for this deadly
sin, and also for your mother’s death. Repent of your misdeeds and
think of death, so that your labour here below may bring peace to
your soul above.”
These two deadly sins of Parzival were done unwittingly, and
unwitting was his neglect to ask the question. His guilt was
thoughtlessness and stupid ignorance. It is impossible not to think of
Oedipus, and compare the Christian mediaeval treatment of
unwitting crimes with the classical Greek consideration of the same
dark subject. Oedipus sinned as unwittingly as Parzival, and as
impulsively. His ruin was complete. Afterwards—in the Oedipus
Coloneus—his character gathers greatness through submission to
the necessary consequences of his acts; here was his spiritual
expiation. On the other hand, mercy, repentance, hope, the uplifting
of the unwitting sinner, forgiveness and consolation, soften and
glorify the Christian mediaeval story.
Parzival stayed some days at the hermitage. At parting the hermit
spoke words of comfort to him: “Leave me your sins. I will be your
surety with God for your repentance. Perform what I have bidden
you, and do not waver.”
The story here turns to Gawain. In the tale of his adventures there
comes a glimpse of Parzival. A proud lady, for whose love Gawain is
doing perilous deeds, tells him, she has never met a man she could
not bend to her will and love, save only one. That one came and
overthrew her knights. She offered him her land and her fair self; his
answer put her to shame: “The glorious Queen of Pelrapeire is my
wife, and I am Parzival. I will have none of your love. The Grail gives
me other care.”
Gawain won this lady, and conducted her to Arthur’s Court, whither
his rival the haughty King Gramoflanz was summoned to do battle
with him. On the morning set for the combat Gawain rode out a little
to the bank of a river, to prove his horse and armour. There at the
river rode a knight; Gawain deemed it was Gramoflanz. They rush
together; man and horse go down in the joust. The knights spring to
their feet and fight on with their swords. Meanwhile Gramoflanz,
with a splendid company, has arrived at Arthur’s Court. The lists are
ready; Gramoflanz stands armed. But where is Gawain? He was not
wont to tarry. Squires hurry out in search, to find him just falling
before the blows of the stranger. They call, Gawain! and the
unknown knight throws away his sword with a great cry: “Wretched
and worthless! Accursed is my dishonoured hand. Be mine the
shame. My luckless arms ever—and now again—strike down my
happiness. That I should raise my hand against noble Gawain! It is
myself that I have overthrown.”
Gawain heard him: “Alas, sir, who are you that speak such love
towards me? Would you had spoken sooner, before my strength and
praise had left me.”
“Cousin, I am your cousin, ready to serve you, Parzival.”
“Then you said true! This fool’s fight of two hearts that love! Your
hand has overthrown us both.”
Gawain could no longer stand. Fainting they laid him on the grass.
Gramoflanz rides up, and is grieved to find his rival in no condition to
fight. Parzival offers to take Gawain’s place; but Gramoflanz declines,
and the combat is postponed till the morrow. Parzival is then
escorted to Arthur’s Court, where Gawain would have him meet fair
ladies; he holds back, thinking of the shame once put on him there
by Cundrie. Gawain insists, and ladies greet the knight. Arthur again
makes Parzival one of the Round Table. Early the next morning,
Parzival, changing his arms, meets Gramoflanz in the lists, before
Gawain has arrived; and vanquishes him. Then comes Gawain and
offers to postpone the combat as Gramoflanz had done. So the
combat is again set for the next day. In the meanwhile, however,
various matters come to light and explanations are had; Arthur
succeeds in reconciling the rival knights and adjusting their relations
to the ladies. So the Court becomes gay with wedding festivals, and
all is joy.
Except with Parzival. His heart is torn with pain and yearning for his
wife. He muses: “Since I could love, how has love dealt with me! I
was born from love; why have I lost love? I must seek the Grail; yet
how I yearn for the sweet arms of her from whom I parted—so long
ago! It is not fit that I should look on this joyful festival with anguish
in my heart.” There lay his armour: “Since I have no part in this joy,
and God wills none for me; and the love of Condwiramurs banishes
all wish for other happiness—now God grant happiness to all this
company. I will go forth.” He put his armour on, saddled his horse,
took spear and shield, and fled from the joyous Court, as the day
was dawning.
And now he meets a heathen knight, approaching with a splendid
following. They rode a great joust; and the heathen wondered to
find a knight abide his lance. They fought with swords together, till
their horses were blown; they sprang on the ground, and there
fought on. Then the heathen thought of his queen; the love-thought
brought him strength, and he struck Parzival a blow that brought
him to his knee. Now rouse thee, Parzival; why dost thou not think
on thy wife? Suddenly he thought of her, and how he won her love,
vanquishing Clamide before Pelrapeire. Straight her aid came to him
across four kingdoms, and he struck the heathen down; but his
sword—once Ither’s—broke.
The foolish evil deed of Parzival in slaying Ither seems atoned for in
the breaking of this sword. Had it not broken, great evil had been
done. The great-hearted heathen sprang up. “Hero, you would have
conquered had that sword not broken. Be peace between us while
we rest.”
They sat together on the grass. “Tell me your name,” said the
heathen; “I have never met as great a knight.”
“Is it through fear, that I should tell my name?”
“Nay, I will name myself—Feirefiz of Anjou.”
“How of Anjou? that is my heritage. Yet I have heard I had a
brother. Let me see your face. I will not attack you with your helmet
off.”
“Attack me? it is I that hold the sword; but let neither have the
vantage.” He threw his sword far from them.
With joy and tears the brothers recognized each other; and long and
loving was their speech. Then they rode back together to the Court.
They entered Gawain’s tent. Arthur came to greet them, and with
him many knights. At Arthur’s request each of the great brothers
told the long list of his knightly victories. The next day Feirefiz was
made a knight of the Round Table, and a grand tournament was
held. Then the feast followed; and again, as once before, to the
great company seated at the table, Cundrie came riding. She
greeted the king; then turned to Parzival, and in tears threw herself
at his feet and begged a greeting and forgiveness. Parzival forgives
her. She rises up and cries: “Hail to thee, son of Gahmuret—
Herzeloide’s child. Humble thyself in gladness. The high lot is thine,
thou crown of human blessing. Thou shalt be the Grail’s lord; with
thee thy wife Condwiramurs, and thy sons Lohengrin and Kardeiz,
whom she bore to thee after thy going. Thy mouth shall question
Anfortas—unto his joy. Now the planets favour thee; thy grief is
spent. The Grail and the Grail’s power shall let thee have no part in
evil. When young, thou didst get thee sorrow, which betrayed thy
joy as it came;—thou hast won thy soul’s peace, and in sorrow thou
hast endured unto thy life’s joy.”
Tears of love sprang from Parzival’s heart and fell from his eyes:
“Lady, if this be true, that God’s grace has granted me, sinful man,
to have my children and my wife, God has been good to me. Loyally
would you make good my losses. Before, had I not done amiss, you
would not have been angry. At that time I was yet unblessed. Now
tell me, when and how I shall go meet my joy. Oh! let me not be
stayed!”
There was no more delay. Parzival was permitted to take one
comrade; he chose Feirefiz. Cundrie guided them to the Grail castle.
They entered to find Anfortas calling on death to free him of his
pain. Weeping, and with prayer to God, Parzival asked what ailed
him, and the king was healed. Then Parzival rode again to
Trevrizent. The hermit breaks out in wonder at the power of God,
which man cannot comprehend; let Parzival obey Him and keep from
evil; that any one should win the Grail by striving was unheard of;
now this has come to Parzival, let him be humble. The hero yearns
for his wife—where is she? He is told; there by the meadow where
he once saw the drops of blood he finds her and his sons, asleep in
their tent. They are united; Parzival is made Grail king; and the
queen Repanse is given in marriage to Feirefiz, who is baptized and
departs with her. Lohengrin is named as Parzival’s successor, while
Kardeiz receives the kingdoms which had been Gahmuret’s and
Herzeloide’s.

END OF VOL. I

Printed by R. & R. Clark, Limited, Edinburgh.


INDEX
Note.—Of several references to the same matter the more important
are shown by heavy type.
Abaelard, Peter, career of, ii. 342-5;
at Paris, ii. 343, 344, 383;
popularity there, ii. 119;
love for Heloïse, ii. 4-5, 344;
love-songs, ii. 13, 207;
Heloïse’s love for, i. 585; ii. 3, 5, 8, 9, 15-16;
early relations with Heloïse, ii. 4-5;
suggestion of marriage opposed by her, ii. 6-9;
marriage, ii. 9;
suffers vengeance of Fulbert, ii. 9;
becomes a monk at St. Denis, ii. 10;
at the Paraclete, ii. 10, 344;
at Breton monastery, ii. 10;
St. Bernard’s denunciations of, i. 229, 401; ii. 344-5, 355;
letters to, from Heloïse quoted, ii. 11-15, 17-20, 23, 24;
letters from, to Heloïse quoted, ii. 16-17, 21-3, 24-5;
closing years at Cluny, ii. 25, 26, 345;
death of, ii. 27, 345;
estimate of, ii. 4, 342;
rationalizing temper, i. 229; ii. 298-9;
skill in dialectic, ii. 303, 345-6, 353;
not an Aristotelian, ii. 369;
works on theology, ii. 352-5;
De Unitate et Trinitate divina, ii. 10, 298-9, 352 and n. 3;
Theologia, ii. 303-4, 395;
Scito te ipsum, ii. 350-1;
Sic et non, i. 17; ii. 304-6, 352, 357;
Dialectica, ii. 346 and nn., 349-50;
Dialogue between Philosopher, Jew, and Christian, ii. 350, 351;
Historia calamitatum, ii. 4-11, 298-9, 343;
Carmen ad Astralabium filium, ii. 192;
hymns, ii. 207-9;
otherwise mentioned, ii. 134, 283 and n.

Abbo, Abbot, i. 294 and n., 324


Abbots:
Armed forces, with, i. 473
Cistercian, position of, i. 362-3 and n.
Investiture of, lay, i. 244
Social class of, i. 473

Accursius, Glossa ordinaria of, ii. 262, 263

Adalberon, Abp. of Rheims, i. 240, 282-3, 287

Adam of Marsh, ii. 389, 400, 487

Adam of St. Victor, editions of hymns of, ii. 87 n. 1;


examples of the hymns, ii. 87 seqq.;
Latin originals, ii. 206, 209-15

Adamnan cited, i. 134 n. 2, 137

Adelard of Bath, ii. 370

Aedh, i. 132

Agobard, Abp. of Lyons, i. 215, 232-3;


cited, ii. 247

Aidan, St., i. 174

Aimoin, Vita Abbonis by, i. 294 and n.

Aix, Synod of, i. 359

Aix-la-Chapelle:
Chapel at, i. 212 n.
School at, see Carolingian period—Palace school

Alans i 113 116 119


Alans, i. 113, 116, 119

Alanus de Insulis, career of, ii. 92-4;


estimate of, ii. 375-6;
works of, ii. 48 n. 1, 94, 375 n. 5, 376;
Anticlaudianus, ii. 94-103, 192, 377, 539;
De planctu naturae, ii. 192-3 and n. 1, 376

Alaric, i. 112

Alaric II., i. 117; ii. 243

Alberic, Card., i. 252 n. 2

Alberic, Markgrave of Camerino, i. 242

Alberic, son of Marozia, i. 242-3

Albertus Magnus, career of, ii. 421;


estimate of, ii. 298, 301, 421;
estimate of work of, ii. 393, 395;
attitude toward Gilbert de la Porrée, ii. 372;
compared with Bacon, ii. 422;
with Aquinas, ii. 433, 438;
relations with Aquinas, ii. 434;
on logic, ii. 314-15;
method of, ii. 315 n.;
edition of works, ii. 424 n. 1;
De praedicabilibus, ii. 314 and n., 315, 424-5;
work on the rest of Aristotle, ii. 420-1;
analysis of this work, ii. 424 seqq.;
attitude toward the original, ii. 422;
Summa theologiae, ii. 430, 431;
Summa de creaturis, ii. 430-1;
De adhaerendo Deo, ii. 432;
otherwise mentioned, i. 17; ii. 82 n. 2, 283, 312, 402, 541 n. 2
Albigenses, i. 49;
persecution of, i. 366-7, 461, 481, 572; ii. 168

Alboin the Lombard, i. 115

Alchemy, ii. 496-7

Alcuin of York, career of, i. 214;


works of, i. 216-21 and n. 2;
extracts from letters of, ii. 159;
stylelessness of, ii. 159, 174;
verses by, quoted, ii. 136-7;
on urbanitas, ii. 136;
otherwise mentioned, i. 212, 240, 343; ii. 112, 312, 332

Aldhelm, i. 185

Alemanni, i. 9, 121, 122, 145 n. 2, 174, 192

Alemannia, Boniface’s work in, i. 199

Alexander the Great, Pseudo-Callisthenes’ Life of, ii. 224, 225, 229-
230;
Walter of Lille’s work on, ii. 230 n. 1

Alexander II., Pope, i. 262 n., 263 and n. 1

Alexander de Villa-Dei, Doctrinale of, ii. 125-7, 163

Alexander of Hales—at Paris, i. 476; ii. 399;


Bacon’s attack on, ii. 494, 497;
estimate of work of, ii. 393, 395, 399;
Augustinianism of, ii. 403-4

Alfred, King of England, i. 144 and n. 2, 187-90


Allegory (See also Symbolism):
Dictionaries of, ii. 47-8 and n. 1, 49
Greek examples of, ii. 42, 364
Metaphor distinguished from, ii. 41 n.
Politics, in, ii. 60-1, 275-6, 280
Roman de la rose as exemplifying, ii. 103
Scripture, see under Scriptures
Two uses of, ii. 365

Almsgiving, i. 268

Alphanus, i. 253-4

Amadas, i. 565

Ambrose, St., Abp. of Milan, on miracles, i. 85-6;


attitude toward secular studies, i. 300; ii. 288;
Hexaëmeron of, i. 72-4;
De officiis, i. 96;
hymns, i. 347-8;
otherwise mentioned, i. 70, 75, 76, 104, 186, 354; ii. 45 n., 272

Anacletus II., Pope, i. 394

Anchorites, see Hermits

Andrew the Chaplain, Flos amoris of, i. 575-6

Angels:
Aquinas’ discussion of, ii. 324-5, 435, 457 seqq., 469, 473-5
Dante’s views on, ii. 551
Emotionalizing of conception of, i. 348 n. 4
Hugo of St. Victor on, ii. 68, 69
Symbols, regarded as, ii. 457
Vincent’s Speculum as concerning, ii. 319
W itings ega ding s mma of ii 457
Writings regarding, summary of, ii. 457

Angilbert, i. 234-5

Angles, i. 140

Anglo-Saxons:
Britain conquered by, i. 141
Characteristics of, i. 142, 196
Christian missions by, i. 196, 197
Christian missions to, i. 172, 174, 180 seqq.
Customs of, i. 141
Poetry of, i. 142-4
Roman influence slight on, i. 32

Aniane monastery, i. 358-9

Annals, i. 234 and n. 1

Anselm (at Laon), ii. 343-4

Anselm, St., Abp. of Canterbury, dream of, i. 269-70;


early career, i. 270;
at Bec, i. 271-2;
relations with Rufus, i. 273, 275;
journey to Italy, i. 275;
estimate of, i. 274, 276-7; ii. 303, 330, 338;
style of, i. 276; ii. 166-7;
influence of, on Duns Scotus, ii. 511;
works of, i. 275 seqq.;
Cur Deus homo, i. 275, 277 n. 1, 279; ii. 395;
Monologion, i. 275-7;
Proslogion, i. 276-8; ii. 166, 395;
Meditationes, i. 276, 279;
De grammatico, i. 277 n. 2;
otherwise mentioned, i. 16, 19, 301-2; ii. 139, 283, 297, 340
Anselm of Besate, i. 259

Anthony, St., i. 365-6;


Life of, by Athanasius, i. 47, 52 and n.

Antique literature, see Greek thought and Latin classics

Antique stories, themes of, in vernacular poetry, ii. 223 seqq.

Apollinaris Sidonius, ii. 107

Apollonius of Tyana, i. 44

Apollonius of Tyre, ii. 224 and n.

Aquinas, Thomas, family of, ii. 433-4;


career, ii. 434-5;
relations with Albertus Magnus, ii. 434;
translations of Aristotle obtained by, ii. 391;
Vita of, by Guilielmus de Thoco, ii. 435 n.;
works of, ii. 435;
estimate of, and of his work, i. 17, 18; ii. 301, 436-8, 484;
completeness of his philosophy, ii. 393-5;
pivot of his attitude, ii. 440;
present position of, ii. 501;
style, ii. 180;
mastery of dialectic, ii. 352;
compared with Eriugena, i. 231 n. 1;
with Albertus Magnus, ii. 433, 438;
with Bonaventura, ii. 437;
with Duns, ii. 517;
Dante compared with and influenced by, ii. 541 n. 2, 547, 549,
551, 555;
on monarchy, ii. 277;
on faith, ii. 288;
on diffe ence bet een philosoph and theolog ii 290
on difference between philosophy and theology, ii. 290;
on logic, ii. 313;
Summa theologiae, i. 17, 18; ii. 290 seqq.;
style of the work, ii. 180-1;
Bacon’s charge against it, ii. 300;
Peter Lombard’s work contrasted with it, ii. 307-10;
its method, ii. 307;
its classification scheme, ii. 324-9;
analysis of the work, ii. 438 seqq., 447 seqq.;
Summa philosophica contra Gentiles, ii. 290, 438, 445-6;
otherwise mentioned, i. 69 n. 2; ii. 283, 298, 300, 312, 402

Aquitaine, i. 29, 240, 573

Arabian philosophy, ii. 389-90, 400-1

Arabs, Spanish conquest by, i. 9, 118

Archimedes, i. 40

Architecture, Gothic:
Evolution of, i. 305; ii. 539
Great period of, i. 346

Argenteuil convent, ii. 9, 10

Arianism:
Teutonic acceptance of, i. 120, 192, 194
Visigothic abandonment of, i. 118 nn.

Aristotle, estimate of, i. 37-8;


works of, i. 37-8;
unliterary character of writings of, ii. 118, 119;
philosophy as classified by, ii. 312;
attitude of, to discussions of final cause, ii. 336;
the Organon, i. 37, 71;
p og essi e cha acte of its t eatises ii 333 4
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookbell.com

You might also like