PIC in Practice: A Project-based Approach / Edition 2

Paperback (Print)
Used and New from Other Sellers
Used and New from Other Sellers
from $17.16
Usually ships in 1-2 business days
(Save 58%)
Other sellers (Paperback)
  • All (8) from $17.16   
  • New (4) from $42.58   
  • Used (4) from $17.16   

Overview

PIC in Practice is a graded course based around the practical use of the PIC microcontroller through project work. Principles are introduced gradually, through hands-on experience, enabling students to develop their understanding at their own pace.

Dave Smith has based the book on his popular short courses on the PIC for professionals, students and teachers at Manchester Metropolitan University. The result is a graded text, formulated around practical exercises, which truly guides the reader from square one.

The book can be used at a variety of levels and the carefully graded projects make it ideal for colleges, schools and universities. Newcomers to the PIC will find it a painless introduction, whilst electronics hobbyists will enjoy the practical nature of this first course in microcontrollers.

PIC in Practice introduces applications using the popular 16F84 device as well as the 16F627, 16F877, 12C508, 12C629 and 12C675. In this new edition excellent coverage is given to the 16F818, with additional information on writing and documenting software.

* Gentle introduction to using PICs for electronic applications
* Principles and programming introduced through graded projects
* Thoroughly up-to-date with new chapters on the 16F818 and writing and documenting programs

PIC in Practice is a graded course based around the practical use of the PIC microcontroller through project work. Principles are introduced gradually, through hands-on experience, enabling students to develop their understanding at their own pace.

Dave Smith has based the book on his popular short courses on the PIC for professionals, students and teachers at Manchester Metropolitan University. The result is a graded text, formulated around practical exercises, which truly guides the reader from square one.

The book can be used at a variety of levels and the carefully graded projects make it ideal for colleges, schools and universities. Newcomers to the PIC will find it a painless introduction, whilst electronics hobbyists will enjoy the practical nature of this first course in microcontrollers.

PIC in Practice introduces applications using the popular 16F84 device as well as the 16F627, 16F877, 12C508, 12C629 and 12C675. In this new edition excellent coverage is given to the 16F818, with additional information on writing and documenting software. Gentle introduction to using PICs for electronic applications

Read More Show Less

Product Details

  • ISBN-13: 9780750668262
  • Publisher: Elsevier Science
  • Publication date: 2/10/2006
  • Edition number: 2
  • Pages: 320
  • Product dimensions: 6.32 (w) x 9.26 (h) x 0.71 (d)

Meet the Author

David Smith has had 30 years experience in the Electronics Industry. Before arriving at MMU he worked as an Electronics Design Engineer for ICL and Marconi. His teaching interests are focused on enabling Design and Technology students to implement microcontroller designs into their projects.
Read More Show Less

Read an Excerpt

PIC Projects and Applications using C

A Project-based Approach


By David W. Smith

Elsevier

Copyright © 2013 Elsevier Ltd.
All rights reserved.
ISBN: 978-0-08-097153-7



<h2>CHAPTER 1</h2>

<b>Introduction to the Microcontroller and C</b></p>

A microcontroller is an integrated circuit that has a number of memory locations embedded inside it which are used to store instructions that are to be executed. These locations are called registers, and instructions are written to these registers to enable the microcontroller to perform an operation.</p>

The memory location is 8 bits wide which means it can store 8 bits of information (<b>Figure 1.1</b>). The 8 bits in the memory are identified by numbers starting on the right with the least significant bit, bit 0, and moving to the left to the most significant bit, bit 7.</p>

Suppose we wish to turn on an LED connected to an output pin, as shown in <b>Figure 1.2</b>. An instruction has to be written to the output port register to output a logic 1 to turn the LED on or output a logic 0 to turn it off.</p>

The microcontroller we will use in this book is a PIC18F1220 manufactured by Microchip, although the codes can easily be adapted for other Microchip microcontrollers. The PIC18F1220 has 16 inputs/outputs (I/O) which means it has 16 inputs or outputs which can be configured as inputs or outputs by instructing the microcontroller via a register, the tristate (TRIS) register (<b>Figure 1.3</b>). TRIS means the port pin can be (i) an input, or an output which is switched (ii) high or (iii) low, three states.</p>

The memory locations in the microcontroller are 8 bits wide so 16 I/O will require two 8 bit registers called PORTA and PORTB.</p>

Suppose we wish to turn on an LED which we are going to connect to bit 4 on PORTB. We first of all have to instruct the microcontroller to ensure that PORTB bit 4 is an output. At the moment it does not matter what the rest of PORTB is doing, so now let's make bit 4 an output and the other 7 bits inputs. We do this with the following instruction:</p>

TRISB = 0b11101111;</p>

0b means the number is a binary one.</p>

Note a 1 sets the pin as an input, a 0 sets the pin as an output.</p>

Now that PORTB bit 4 is an output, we can write a logic1 to it with: PORTBbits.RB4 = 1; (<b>Figure 1.4</b>).</p>

There are several ways in which we can give the microcontroller instructions, called programming. These program languages are assembly, basic, C, or a number of flowchart languages. The language that we are going to use in this book is the C programming language, which is a high-level language that is very versatile. The previous book "PIC in Practice" written by the author, DW Smith, used the assembly language to program the microcontroller.</p>

C is a very comprehensive and versatile language, which usually means there is a lot to learn. Throughout this book I will introduce the C language as and when required and only those instructions that are needed to perform the control. So you will not need to become a C programmer in order to program the micro in C!</p> <h2>CHAPTER 2</h2>

<b>First C Program</b></p>

In order to program the microcontroller we are going to:</p>

• Write the code in C.</p>

• Convert the code to a hex file using a compiler.</p>

• Program the hex file into the microcontroller.</p>

The code which we are going to write in the C language can be written on any text editor such as WORD. Any suitable C compiler can be used to convert the code to a hex file and there are numerous programmers on the market that will blow your hex file into the microcontroller.</p>

Throughout this book I am going to use a dedicated piece of software called MPLAB integrated development environment (IDE) written by the PIC microcontroller manufacturer, Microchip. This acts as a text editor, compiler, and driver for the Microchip programmer. MPLAB IDE is free and can be downloaded from the Microchip Web site at <b>Microchip.com</b></p>

At the time of writing Microchip have upgraded MPLAB v8 and have called it MPLABX. I have discussed both IDEs here and left it up to the reader to decide which one they prefer to use.</p>

MPLAB and MPLABX also include a simulator that help to debug your code. I use Microchips own programmer/in-circuit debugger (ICD) called Microchip MPLAB ICD3 and PICkit3. The ICDs allow you to connect your circuit to the computer so that you can view the registers inside the micro when the program is running. But we can see more of the simulator and debugger later.</p>

<b>MPLAB AND MPLABX INSTALLATION</b></p>

Install the latest version of MPLAB, as of 7/1/20111 that is MPLAB v8.73a., and the C compiler is MPLAB C v3.40 LITE. NB. MPLAB and the LITE version of the C compiler are free from <b>Microchip.com</b></p>

Or install MPLABX IDE and the C compiler XC8.<;/p>

Make a new folder to keep your programs in, say PicProgs on your desktop.</p>

For our first program we are going to flash an LED on and off at 1 s intervals on the output pin, PORTB,4.</p>

The pin connection for the 18F1222220 is shown in <b>Figure 2.1</b>.</p>

But before we program our device we need to understand a little of the C language.</p>

<b>A BRIEF INTRODUCTION TO C FOR THE MICROCONTROLLER</p>

Turning an output on/off</b></p>

If we wish to turn an LED on PORTB bit4 on, the C code is:</p>

PORTBbits.RB4 = 1;
This is called a statement. NB all C statements end in ;
If we wish to turn the LED off the code is:
PORTBbits.RB4 = 0;
</p>

<b>Delays</b></p>

In the C language suite we have installed a file called Delay.h. As its name suggests there are a number of routines in this file which can create a delay in our program. The address for this file if you want to read it is "C:\Program Files\ Microchip\mplabc18\v3.40\h" after installing MPLAB from <b>Microchip.com</b></p>

The subroutines are:</p>

Delay1TCY()
Delay10TCYx()
Delay100TCYx()
Delay1KTCYx()
Delay10KTCYx()
If you wish to call a subroutine in C you just state its name, i.e.,
Delay1KTCYx();
</p>

These delays are multiples of timing cycles, i.e., 1, 10, 100, 1000, and 10,000.</p>

A timing cycle is the time taken to execute an instruction and it is the basis of the timing in the microcontroller system. The timing comes from the oscillator which can be an external clock source, an external crystal, or an internal oscillator. For now we are going to use the internal oscillator set at 31.25 kHz.</p>

The timing cycle runs at one-fourth of this frequency, i.e., at 7.8125 kHz. This means the period of the timing cycle is 0.128 ms.</p>

So the Delay100TCYx() subroutine will have a time of 100×0.128 ms = 12.8 ms.</p>

In order to achieve a delay of 1 s we would need 78 of these 12.8 ms.</p>

78 × 12.8 ms = 0.9984 s, not quite 1 s but near enough for this application.</p>

To do this the C code is:</p>

Delay100TCYx(78);</p>

Note the number of times the subroutine is executed is written in the brackets (78) in this case. NB. 255 is the maximum value that can be entered.</p>

<b>Loops</b></p>

In order to make the program execute some code a number of times or indefinitely, we use a loop.</p>

The WHILE LOOP as it is called looks like this:</p>

while ( )
{
}
</p>

The code to be executed is written between the brackets { } while the condition for executing the code is written between the brackets ( )</p>

Suppose we wish to turn an alarm on if the temperature goes above 60°C, the code would look like:</p>

while (Temperature>60)
{
PORTBbits.RB0 = 1; // turn on PORTB bit0
}
</p>

If we wish to execute a piece of code indefinitely such as flashing our LED on PORTB bit 4 on and off continuously, the loop is:</p>

while (1)
{ PORTBbits.RB4 = 1; // turn on PORTB bit4
Delay100TCYx(78); // wait 1 s
PORTBbits.RB4 = 0; // turn off PORTB bit4
Delay100TCYx(78); // wait 1 s
}
</p>

The function while (1) means while 1 is true! But 1 is always 1, so the loop is always executing. Note the function while (1) does not end with a ;</p>

The // code means ignore what follows on the line; it is for our reference not for the compiler.</p>

A useful while loop involves just 1 line of code, i.e., while (PORTBbits.RB4==1);</p>

This means continue the loop (just 1 line) until PORTBbits.RB4==1 is no longer true, i.e., wait until PORTBbits.RB4==0 before moving on in the program.</p>

Note: == means is equal to,=means equals. One is a question and the other a statement.</p>

<b>Entering Numbers in the Program</b></p>

Numbers can be entered in the program as hexadecimal, binary or decimal.</p>

A hexadecimal number 7 F is written as 0 × 7 F.</p>

A binary number 1111 0101 is written as 0b11110101.</p>

A decimal number 45 is just written as 45.</p>

We are now ready to write our C program.</p>

NOTE</p>

In the remainder of this book we will be writing C files and putting them into projects. You can choose to do this with <b>MPLAB</b> or <b>MPLABX.</p>

WRITING THE CODE USING MPLAB IDE</b></p>

Run MPLAB and the screen shown in <b>Figure 2.2</b> will open.</p>

For our first program we are going to flash an LED on and off at 1 s intervals on the output pin, PORTB,4. We will call this, flash.c</p>

Under the file menu select New (or if you have the code Open flash.c) (<b>Figure 2.3</b>).</p>

The screen shown in <b>Figure 2.4</b> showing the file editor will open.</p>

If the line numbers are not visible on the left hand side turn them on with Edit/Properties as shown in <b>Figure 2.5</b>.</p>

Select File Type/Line Numbers.</p>

Click Apply then.</p>

Click OK as shown in <b>Figure 2.6</b>.</p>

We need to open a project to store our code in. The project will contain our code flash.c and also when we have compiled it a new file, flash.hex which is the machine code file which will be blown into our microcontroller with the aid of a programmer. The project also contains the workspace which is the user screen that would show the registers and the watch window. We will use these later when we look at the simulator.</p>

To make the project, select Project/Project Wizard as shown in <b>Figure 2.7</b>.</p>

Click Next on the dialogue box of <b>Figure 2.8</b>.</p>

Step 1: select the microcontroller you require, the PIC18F1220, from the Project Wizard as shown in <b>Figure 2.9</b>.</p>

Step 2: select the language toolsuite to use, i.e., the C18 compiler.</p>

If not already selected choose Microchip C18 Toolsuite as your Active Toolsuite as shown in <b>Figure 2.10</b>.</p>

Then click, Next.</p>

Step 3: create the project, browse for the directory you have created, i.e., PICProgs, and create a new project file, flash, or open it if it is already created, as shown in <b>Figure 2.11</b>.</p>

Click Save, then.</p>

Click Next.</p>

Step 4: add existing files to your project (<b>Figure 2.12</b>).</p>

At the moment we do not have any existing files. If you have a copy of flash.c you can add it now. If not just click Next.</p>

That's the project made.</p>

Click Finish (<b>Figure 2.13</b>) to finish making the project and return to the workspace.</p>

We now have a project called flash saved in the PICProgs folder. Now we can write our C program called flash.c.</p>

We have already discussed the steps involved in the program.</p>

<b>Flashing an LED On and Off</b></p>

The LED is connected to PORTB pin 4 and is to be flashed on and off every second. The circuit for this is shown in Figure 1.2.</p>

The code is:</p>

#include <p18f1220.h>
#pragma config WDT=OFF, OSC=INTIO2, PWRT=ON, LVP=OFF, MCLRE = OFF
#include <delays.h>

void main (void)
{
//SET UP
// OSCCON defaults to 31 kHz. So no need to alter it.

ADCON1 = 0×7 F; //all IO are digital or 0b01111111 in binary

TRISA = 0b11111111; //sets PORTA as all inputs

PORTA = 0b00000000; //turns off PORTA outputs, not required, no outputs

TRISB = 0b00000000; //sets PORTB as all outputs

PORTB = 0b00000000; //turns off PORTB outputs, good start position
while (1)
{

PORTBbits.RB4 = 1; // turn on PORTB bit4

Delay100TCYx(78); // wait 1 s

PORTBbits.RB4 = 0; // turn off PORTB bit4

Delay100TCYx(78); // wait 1 s
}
}
</p>

If you do not have this file saved click File New as shown in <b>Figure 2.14</b> and copy/paste it into the untitled space and then save it in your folder as flash.c.</p>

If you already have the file saved then select Open—PicProg/flash.c (<b>Figure 2.14</b>).</p>

Your screen should look like <b>Figure 2.15</b> with the code entered.
(Continues...)


Excerpted from PIC Projects and Applications using C by David W. Smith. Copyright © 2013 Elsevier Ltd.. Excerpted by permission of Elsevier.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Read More Show Less

Table of Contents

Introduction; Introduction to the PIC microcontroller; Programming the 16F84 microcontroller; Introductory projects; Headers, porting code – which micro?; Using inputs; Understanding the headers; Keypad scanning; Program examples; The 16C54 microcontroller; Alpha numeric displays; Analogue to digital conversion; Radio transmitters and receivers; EEPROM data memory; Interrupts; The 12 series 8 pin microcontroller; The 16F87X Microcontroller; The 16F62X Microcontroller; Projects; Instruction set, files and registers; Appendices; Index
Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star

(0)

4 Star

(0)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously

    If you find inappropriate content, please report it to Barnes & Noble
    Why is this product inappropriate?
    Comments (optional)