Baixe Programando microcontroladores PIC e outras Manuais, Projetos, Pesquisas em PDF para Engenharia Eletrônica, somente na Docsity!
Programming a PIC
Microcontroller
A Short Tutorial
by Yesu Thommandru
Iowa State University – ECpE
November 2006
Table of Contents
Table of Contents .......................................................................................................................... ii
- Programming a PIC Microcontroller Page ii of
- Introduction.............................................................................................................................. Revision Record iii
- 1.1 Purpose.........................................................................................................................................
- 1.2 Document Conventions................................................................................................................
- 1.3 Intended Audience and Reading Suggestions..............................................................................
- 1.4 References....................................................................................................................................
- Choosing a PIC Microcontroller ............................................................................................
- 2.1 Introduction..................................................................................................................................
- 2.2 Types of PICs...............................................................................................................................
- Integrated Development Environment...................................................................................
- Programming Language..........................................................................................................
- Compiler, Assembler, and Linker ..........................................................................................
- Using MPLab IDE....................................................................................................................
- Writing Software....................................................................................................................
- The “Burning” Process..........................................................................................................
- Breadboarding a PIC Microcontroller ................................................................................
- Other Considerations.............................................................................................................
1. Introduction
1.1 Purpose
The purpose of this document is to provide a simple, easy to use tutorial on programming PIC
microcontrollers. The tutorial begins with instructions on selecting a specific PIC and ends with
directions for breadboarding the microcontroller.
1.2 Document Conventions
In this document different styles of text and visuals are used to help the reader separate different
types of information. General description text will be in this format, size 11 italicized Arial.
Pseudo-code or source code will be written in multi-color, size 10 Courier New font as in the
following example:
#include
void main(int argc, char *argv[]) { printf("Star Wars!\n”); return;
}
Buttons and menu items will be in standard Arial text such as Button with the first letter underlined.
Important notes and pieces of information will appear in normal text in shaded boxes as in the
following example:
NOTE: I think Darth Vader would win in a fight against Boba Fett!
1.3 Intended Audience and Reading Suggestions
The intended audience of this document is students in the Department of Electrical and Computer
Engineering enrolled in EE/CprE 491 or 492 Senior Design. This document can also be used by
any student or individual who wishes to learn the basics of how to program a PIC microcontroller.
There are a number of suggested readings for any users of this document. The following books
are suggested for specific PIC programming tasks:
Introduction to microelectronic systems: the PIC 16F84 microcontroller by Martin Bates.
PIC microcontroller: an intro to software and hardware interfacing by Han-Way Huang.
The PIC microcontroller: your personal introductory course by John Morton.
PIC microcontroller project book by John Lovine.
Programming and customizing the PIC microcontroller by Myke Predko.
The quintessential PIC microcontroller by Sid Katzen.
NOTE: In my experience most of software in these books is written in Assembly and are thus not
useful to students wishing to program in a high-level programming language.
1.4 References
Schildt, Herbert. C/C++ Programmer’s Reference 2nd Edition. McGraw-Hill Publishing. New
York, 2000.
Morton, John. PIC Your Personal Introductory Course. Newnes. Boston, 1998.
Bergquist, Carl. Guide to PICMICRO Microcontrollers. Sams Technical Publications. Indianapolis,
Predko, Myke. Handbook of Microcontrollers. McGraw-Hill Publishing. New York, 1999.
Predko, Myke. PICMicro Microcontroller Pocket Reference. McGraw-Hill. New York. 2000.
Smith, D.W. PIC in Practice. Newnes. Oxford. 2002.
Microchip.com. PIC16F877A. 2006.
MicrochipC.com PICMicros and C
Best Microcontroller Projects
The reason for the number of packages is that there are some PICs with 100 I/O pins! The
microcontrollers are basically rectangular or square shaped. The easiest package to work with is
DIP or PDIP because it is easily breadboardable and can easily be soldered.
NOTE: Use a mid-level dual inline package PIC microcontroller. You will not be able to burn
software into a QUAD chip and SOP chips will require Schmartboards.
3. Integrated Development Environment
In order to develop your software and organize your files you will have to use an integrated
development environment. The number one IDE used with PIC microcontrollers is MPLab IDE by
Microchip Technology. MPLab IDE is free and easy to use. Just go to http://www.microchip.com/
to download the latest version.
NOTE: The latest version of MPLab IDE ends in zero. e.g. v7.50. Files not ending in zero are
interim versions of MPLab IDE.
You can also download the MPLab IDE User’s Guide, Quick Chart, and Quick Start Guide. After
you have downloaded the latest version of MPLab IDE install the software on your local drive.
4. Programming Language
PIC microcontrollers can be programmed in Assembly, C or a combination of the two. Other high-
level programming languages can be used but embedded systems software is primarily written in
C. The following three examples demonstrate the programming styles.
Example 1 – Assembly
MAIN
clrf PORTB ;Clear PORTB output latches bsf STATUS,RP0 ;Switch to bank 1 movlw b'11110000' ;Load value to make lower 4 bits outputs movwf TRISB ;Move value to TRISB bcf STATUS,RP0 ;Switch to bank 0 LOOP bsf PORTB,0 ;Turn on LED on RB call DELAY ;Call delay routine bcf PORTB,0 ;Turn off LED on RB call DELAY ;Call delay routine goto LOOP ;Repeat main loop
DELAY
decfsz COUNTERL ;Decrement COUNTERL goto DELAY ;If not zero, keep decrementing COUNTERL decfsz COUNTERH ;Decrement COUNTERH goto DELAY ;If not zero, decrement COUNTERL again return END
Example 2 – Assembly and C
main() { short first_pass = TRUE;
//----- Set up port direction reg's ----- #asm movlw 0 // Set port b to outputs tris port_b clrf port_b
movlw 0xff // Set port a to inputs tris port_a #endasm
//----- Wait for powerup, Initilize LCD ----- delay_ms(200); init_lcd();
//----- Write a startup message ----- msg_1();
//----- Write status message ----- msg_2(); …
Example 3 – C
void main() {
U8 i = 0; // General purpose loop var. U16 num ; // General purpose number var. U8 row = 0; // Current display row. U16 blinkc = 0; // LED blinker counter. U16 blink_onoff = 1; // LED state. U8 bcd_h,bcd_m,bcd_s; // BCD numbers.
init_ports(); init(); enable_interrupts();
ROW_RESET;
for (;;) { // infinite loop
// FLASH LED @ RA
if (++blinkc>500) { // time to change state? blinkc=0; …
I suggest writing your code completely in C because it is much faster and easier than writing your
code in Assembly or a combination of languages.
NOTE: The version of C that you use to write you software will depend on the C compiler you
choose to use in your project.
6. Using MPLab IDE
Let’s start writing software in MPLab IDE in the C programming language by creating a new
project.
Open MPLab IDE and observe the Workspace and Output windows. The Workspace window
organizes the files in your project in an easy to see hierarchy.
Select Project Project Wizard to create a new project as shown in the following image:
You should see the following welcome message in a dialog window:
Hit Next > and you will see enter Step One: Select a device. There will be a single pull down
menu with a huge amount of PIC microcontrollers to choose from.
The last window in the Project Wizard is a summary of the options you have selected. If
everything looks ok hit Finish to create the project.
After hitting Finish you will be presented with a dialog window asking you to save your workspace.
A workspace is a file that allows a user to gather and organize various sources and resources.
Rename and save your workspace in the project directory.
Your workspace window now contains a hierarchy of folders for your project.
7. Writing Software
We will now write basic software in C using MPLab IDE. The following program flashes an LED on
one of the PORTA pins of the PIC microcontroller (look at the PIC’s datasheet).
main.c
//LED example program written for //PIC programming tutorial.
//standard include files #include #include #include “delay.h”
//main function void main() { PORTA = 0x00; //set RA0-RA5 low TRISA = 0x00; //set PORTA to output
//superloop while(1) { PORTA = !PORTA; DelayMs(250); } }
For basic C operations and delays the files stdlib.h and delay.h are needed. The file pic.h is
required for access to the PIC microcontrollers I/O pins, memory locations, and other components.
The following diagram shows how I/O pins on a PIC16f877A correspond to software variables:
PORTA in the file main.c refers to the 6-bit I/O port on the PIC microcontroller. Each pin can be
set high or low using simple masking commands: PORTA = 0x01 sets RA0 high. Multiple pins can
be set: PORTB = 0xFF. Check the datasheet for the number and size of ports on your PIC.
Memory Usage Map:
Program space:
CODE used 21h ( 33) of 800h words ( 1.6%) CONST used 0h ( 0) of 800h words ( 0.0%) ENTRY used 0h ( 0) of 800h words ( 0.0%) STRING used 0h ( 0) of 800h words ( 0.0%)
Data space:
BANK0 used 3h ( 3) of 60h bytes ( 3.1%) BANK1 used 0h ( 0) of 50h bytes ( 0.0%) COMBANK used 0h ( 0) of 10h bytes ( 0.0%)
EEPROM space:
EEDATA used 0h ( 0) of 100h bytes ( 0.0%)
ID Location space:
IDLOC used 0h ( 0) of 4h bytes ( 0.0%)
Configuration bits:
CONFIG used 0h ( 0) of 1h word ( 0.0%)
Summary:
Program space used 21h ( 33) of 800h words ( 1.6%) Data space used 3h ( 3) of B0h bytes ( 1.7%) EEPROM space used 0h ( 0) of 100h bytes ( 0.0%) ID Location space used 0h ( 0) of 4h bytes ( 0.0%) Configuration bits used 0h ( 0) of 1h word ( 0.0%)
Loaded C:\MyProject\MyProject.cof.
BUILD SUCCEEDED: Sat Nov 18 01:34:23 2006
When you build your project a large amount of files are created and stored in your project
directory. The most important file created is the hexadecimal file as shown in the following image:
This is the file that will be “burned” into your PIC microcontroller. Copy this HEX file and take it to
the computer connected to your available programmer.
NOTE: The senior design lab (Town Engineering room 316) has an easy to use Dataman
Universal Programmer used to program PIC microcontrollers.
Place your PIC microcontroller in the black ZIF socket and place the silver lever in the down
position to clamp onto the I/O pins.
Start the Dataman Programmer software by going to Start All Programs Dataman
Programmers Dataman-48XP. There are basically three steps in the Dataman Programmer
“burning” process:
1. Select device
2. Load HEX file
3. Program PIC
Right now you have loaded your HEX file from your project into the Dataman buffer. The PIC
microcontroller isn’t programmed yet! The Dataman software allows you to view and edit the
buffer as shown in the following image but this step should be unnecessary.
Before programming the PIC microcontroller you can set certain device options in the Dataman
software by pressing the Config button in the menu bar. A window with several sections of
checkboxes and radio buttons will appear for your specific device. Read your PIC’s datasheet to
fully understand these configuration bits. Make your appropriate choices and click OK.
Now it is time to program your PIC microcontroller. The correct device has been selected, the
HEX file has been loaded and configuration bits have been set. Select Device Program Auto
or press Alt + P to program your PIC microcontroller. You should see a progress bar at the center
of your screen:
If the “burn” process has been successful a Programming Complete message will be printed to the
screen. There are many other functions provided by the software. For more details read the
datasheet on the Dataman 48UXP Universal Programmer available at
http://dataman.com/Webpages/Programmers/Product48UXPInformation.aspx.
NOTE: When reprogramming a PIC you may receive the error messages: “Poor contact at pin 13”
or “Over current detected”. These messages most likely mean you have destroyed your PIC.