Pic Programming Tutorial
Pic Programming Tutorial
INTERNSHIP TUTORIAL
Currently, the best choice for application development, using PIC BASIC PRO are microcontrollers
from the family : PIC16F87x, PIC16F62X and of course the famous PIC16F84. With this family of PIC
microcontrollers, program memory is created using FLASH technology which provides fast erasing
and reprogramming, thus allowing faster debugging. By a single mouse click in the programming
software, microcontroller program can be instantly erased and then reloaded without removing chip
from device. Also, program loaded in FLASH memory can be stored after power supply has
been turned off. The older PIC microcontroller series (12C67x, 14C000, 16C55x, 16C6xx, 16C7xx
and 16C92x) have program memory created using EPROM/ROM technology, so they can either be
programmed only once (OTP version with ROM memory) or have glass window (JW version with
EPROM memory), which allows erasing by few minutes exposure to UV light. OTP versions are
usually cheaper and are used for manufacturing large series of products. Besides FLASH memory,
microcontrollers of PIC16F87x and PIC16F84 series also contain 64-256 bytes of internal EEPROM
memory, which can be used for storing program data and other parameters when power is off.
PIC BASIC PRO has built-in READ and WRITE instructions that can be used for loading and saving
data to EEPROM.
RA0-RA3 and RB0-RB7 pins are I/O (input/output) ports. You can access MCU by sending digital
signals to these ports and MCU process that commands then it sends digital signals from I/O ports.
Pin Descriptions
PIC16F84 has a 13 I/O ports. 5 of the I/O ports are RA0 to RA5 and 8 of them are RB0 to RB7. You
can use these 13 ports input or output. It has a special TRIS register is used for ports I/O directions.
You can find pin descriptions in Figure 3.
Figure 3: PIC16F84 Pin Descriptions
Memory Organization
There are two memory blocks in the PIC16F8X. These are the program memory and the data
memory. Each block has its own bus, so that access to each block can occur during the same
oscillator cycle. The data memory can further be broken down into the general purpose RAM and the
Special Function Registers (SFRs). The SFRs used to control the peripheral modules. The data
memory area also contains the data EEPROM memory.
PIC16F84 has 1 Kbytes program memory. Memory cells are between 000h and 3FFF. In each of the
cell, 14 bit program instructions are stored. Program memory has a Flash technology so it can be
erased or written electrically but while program is working it can just read.
Program memory in a PIC is actually 14 bits wide even though PICs are considered 8-bit
microcontrollers. The 8-bit terminology comes from the data memory buss, which is 8 bits wide.
Figure 3: PIC16F84 Program Memory Organization
Data memory is where all your variables are stored. This is RAM (Random Access Memory), which
means when the PIC is disconnected from power all the data memory is lost. The data memory
is 8 bits wide, which is why the PICs are considered 8-bit microcontrollers. The PIC data memory is
also banked just like the program memory. The first section of data memory is reserved for special
function registers. These registers are the key locations that control most of functions within the PIC.
Some of the registers are located in Bank 0 while some are located in Bank 1. Some are even
repeated so they are available in both banks. To select which bank to control, a bit is set or cleared in
the STATUS register. Because you need access to the STATUS register from both banks, it is located
in both banks. Figure 4 shows PIC16F84’s data memory organization and register file map.
Figure 4: PIC16F84 Register File Map
PIC Programming Overview
Programming PIC microcontrollers is a simple three step process: Write the code, compile the code,
and upload the code into a microcontroller.
The PicBasic Pro compiler both function is, saved program code (text file) is run through a PicBasic
Pro compiler. The compiler reads through the text file and creates (compiles) an equivalent machine
code instruction listing (.hex file) of the program.
The machine code (.hex file) is a list of hexadecimal numbers that represent the PicBasic Pro
program. The list of hexadecimal numbers (.hex file) is uploaded (programmed) into the
microcontroller. When the microcontroller is started, its CPU will run through the programmed list of
hexadecimal numbers, running the PicBasic Pro program. Uploading the machine code (.hex file) into
the microcontroller is the job of the Programmer board and software. The PicBasic Pro version offers
enhanced and richer basic command syntax. The compiler program may be run manually in DOS or in
an “MSDOS Prompt” window but I offer Microchip’s MPLAB IDE program in my graduation project I
used Pic Basic Pro and MPLAB together.
Micro Engineering Lab’s Pic Basic Pro Compiler gives us powerful and flexible features. You can write
your PIC code very fast instead of assembly programming. Additionally, that is easy to learn PBP’s
commands and properties because it is very similar to basic programming language if you are familiar
with high level language programming, especially basic programming you can learn PBP in a short
time period.
The PicBasic Pro Compiler makes it even quicker and easier for you to program Microchip
Technology’s powerful PICmicro microcontrollers (MCUs). The English-like BASIC language is much
easier to read and write than the quirky Microchip assembly language.
Decisions were made that we hope improve the language overall. One of these was to add a real
IF..THEN..ELSE..ENDIF instead of the IF..THEN(GOTO).
Here is a list of the current PBPro commands:
Note: In PicBasic Pro Compiler Manual, you can find extensive information
about PicBasic Pro,
MPLAB Integrated Development Environment (IDE) is a free, integrated toolset for the development of
embedded applications employing Microchip's PICmicro® and dsPIC® microcontrollers. MPLAB IDE
runs as a 32-bit application on MS Windows®, is easy to use and includes a host of free software
components for fast application development and super-charged debugging. MPLAB IDE also serves
as a single, unified graphical user interface for additional Microchip and third party software and
hardware development tools. You can use PicBasic Pro compiler with MPLAB IDE.
Figure 6: MPLAB user interface
I will explain to setting up MPLAB and PBP step by step, please follow up these steps
respectively.
1) First, Install MPLAB and its components. Please use default directory. In the following
examples, the default installation directory (C:\Program Files\Microchip\...) will be used.
2) Install the PICBASIC or PICBASIC PRO Compiler. Please just copy and paste PBP folder into
your dirve C. Some versions will have problems if installed in C:\Program Files\PBP. In the following
examples, we assume that PICBASIC PRO has been installed in C:\PBP.
3) In "C:\PBP", run the file "PBregister.bat" (Start - Run - C:\PBP\PBregister.bat). Restart MPLAB
for the changes to take effect. (Note: In C:\PBP folder there are two PBP plug-in, if you run
"PBregister.bat", these plug-ins will be activated.)
1- To get there, right-click the My Computer icon on your desktop and select Properties.
2- Click the Advanced tab, then the button labeled Environment Variables....
3- Find the Path variable in the System Variables window, highlight the line, and click Edit.
4- Add the paths to MPLAB and PICBASIC PRO to the end of the line in the Variable Values text box,
separating each path with a semicolon.
5- If necessary, restart your computer after making changes to the path variable.
Start MPLAB and select Set Language Tool Locations under the Project menu. Select the appropriate
Toolsuite Name (PICBASIC or PICBASIC PRO Compiler). Use the browse button to select PBC.EXE
or PBPW.EXE within the subdirectory where it was installed.
Creating a BASIC project within MPLAB
Name the project. Choose the directory where the source file exists.
1) Blinking a Led
Task: Connect a led to PIC16F84’s port B (any of the pin you can select). You will try to accomplish
blinking your led (turn on, then turn off, then turn on….). Your pause time will be 1sec.
Program your MCU with PBP. After all steps will be done, your led must blink.
Schematic:
PBP Program:
'----------------------Simple Blinking Program---------------------
OUTPUT PORTB.7 ' Our output is Led. (or you can write like this -->
"TRISB.7 = 0")
HIGH PORTB.7 ' We make portb's 7. pin high, so portb's 7. pin's voltage
level is 5V.
PAUSE 1000 ' Wait 1000ms (1 sec)
LOW PORTB.7 ' We make portb's 7. pin low, so portb's 7. pin's voltage
level is 0V.
PAUSE 1000 ' Wait 1000ms (1 sec)
END ' End of our blinking program. (-: I think our first
program is so easy.
2) Led Show
Task: Connect leds to PIC16F84’s port Bs all ports. Initially our leds are off state then firstly, turn on
the leds from right to left respectively then turn on the leds left to right respectively after that turn on
two middle leds, then turn on the leds from inside to outside, then turn on them outside to inside.
Schematic:
PBP Program:
'------------------------------LED SHOW----------------------------
Task: Buttons are connected to pins RA0, RA1 and RA2. Pressing any of these executes part of
the code for generating impulse sequence on RA3 pin, which can be heard as different sounds or a
melody on buzzer. Use FOR NEXT loop for generating different sounds.
In the following program, pressing buttons T0, T1 and T2 generate different sounds on a buzzer.
PBP Program:
'-------------------------------SOUND PROGRAM----------------------------
INPUT PORTA.0 'Set PORTA pin 0 to an input. For button T0.(or -> TRISA.0
= 1)
INPUT PORTA.1 'Set PORTA pin 1 to an input. For button T1.(or -> TRISA.1
= 1)
INPUT PORTA.2 'Set PORTA pin 2 to an input. For button T2.(or -> TRISA.2
= 1)
OUTPUT PORTA.3 'Set PORTA pin 3 to an output. For BUZZER.(or -> TRISA.2 =
1)
'Or you can define I/O port like this; TRISA = %00000111 which means first
three pins are inputs.
'For Button T0
IF PORTA.0 = 0 THEN ' If button T0 is pressed then generate different
sounds. Counter increment rate is 1.
FOR counter=10 to 130
SOUND PORTA.3, [counter,10,60,10] ' Generate Sound
NEXT counter
ENDIF
'For Button T1
IF PORTA.1 = 0 THEN ' If button T1 is pressed then generate different
sounds. Counter increment rate is 10.
FOR counter=10 to 130 STEP 10
SOUND PORTA.3, [counter,10,60,10] ' Generate Sound
NEXT counter
ENDIF
'For Button T2
IF PORTA.2 = 0 THEN ' If button T2 is pressed then generate different
sounds. Counter increment rate is 20.
FOR counter=10 to 130 STEP 20
SOUND PORTA.3, [counter,10,60,10] ' Generate Sound
NEXT counter
ENDIF