Upload
epi-n-justiniano-jr
View
95
Download
0
Embed Size (px)
Citation preview
1
Introduction to PIC Microcontroller using
C Programming Language
1.1 Introduction
Learning how to program the PIC microcontroller using the C language is advantageous to any
embedded programmer. This section will provide the necessary steps on how to install the MPLAB
IDE with the PICC-lite C compiler for PIC microcontrollers.
The MPLAB IDE will provide the development environment for the C program from which the
PICC-lite C compiler can be invoke to generate the machine code (i.e HEX file).
1.2 MPLAB and Hitech PICC-lite Installation Guide
Step 1. Download the free MPLAB IDE from the Microchip website and install.
Figure 1. MPLAB IDE download page
2
Figure 2. MPLAB Installation
STEP 2. Download the free student version of the H-Tech PICC-lite compiler either from the
Microchip website or the Hi-tech website. After download, install the compiler.
Figure 3. PICC-lite download page
3
Figure 4. PICC-lite installation
1.3 First C Program
To test the installed MPLAB and PICC-lite, we will create a “blinker” project using the
PIC16F84a microcontroller chip.
First, open the MPLAB IDE by double-clicking on the MPLAB icon on your windows desktop
or click on Start >> Programs >> Microchip >> MPLAB IDE as shown in figure below.
4
Figure 5. Run the MPLAB IDE
A blank MPLAB workspace will appear.
Figure 6. Blank MPLAB Workspace
5
To create a new project, select Project >> Project Wizard. A Project Wizard window will appear.
Click Next and choose the device in the drop-down menu. We will select the PIC16F84a chip.
Figure 7. Project Wizard: Select Device
6
In the Active Toolsuite drop-down menu, select the PICC-lite toolsuite. Click Next.
Figure 8. Project Wizard: Select a Language Toolsuite
7
Choose an appropriate name for the project. In this example, “blinker” will be the name of the
project. Save the project in a desired directory and click Next.
Figure 9. Project Wizard: Project Name and Directory
8
We will be prompt to add any existing files to the project. Since we don’t have any existing file,
simply click Next.
Figure 10. Project Wizard: Add files to Project
The final screen on the Project Wizard is a summary of the previous selections. Click Finish.
Figure 11. Project Wizard: Summary
9
The project workspace below will appear. Only the Project window is visible at this time.
Figure 12. Initial Project workspace
Our next step is to create a new C source file. Click the New button (or press CTRL+N) and a blank editor
window will appear as shown below.
Figure 13. New editor window
10
Now, type the example C source code below for the blinker project.
#include <pic.h>
void delay(void)
{
int i,j;
for(i=0;i<=0xFF;i++)
for(j=0;j<=0xFF;j++)
; //do nothing
}
void main( )
{
TRISB & = ~0x01; //initialize RB0 pin as output
PORTB & = ~0x01; //LED is initially off
while(1) //infinite loop
{
PORTB ^ = 0x01;
delay();
PORTB ^ = 0x01;
delay();
}
}
11
Save the file as blinker.c in the project directory.
Figure 14. Save blinker.c
12
Now, add the blinker.c file to the project. To do this, select Project >> Add Files to Project, and
locate and the blinker.c. Click Open.
Figure 15. Add blinker.c file to project
13
In the project window, the blinker.c file will now be listed under Source Files.
Figure 16. Blinker.c added to project
14
We are now ready to compile the blinker.c program. In the Project menu, select Build. An Output
window will diplay the build process. It will indicate whether the build process is successful or not, as
well as some other build statistics. In this example, the build process or the blinker project is successful,
with no warnings or errors.
Figure 17. Build Process
15
A successful build process will generate several output files in the project directory. The most
important output file is .hex file since this the actual binary code that will run inside the microcontroller.
In the figure below, the blinker.hex is visible and can now be programmed to the microcontroller using
the appropriate hardware programmer.
Figure 18. Output files in the project directory
16
Review
A blinker project is the probably the simplest microcontroller program, equivalent to the “Hello
World” program of many high-level language. The blinker program runs inside the microcontroller and
causes an LED connected to one output pin of the microcontroller to blink. (i.e. turn on/off say, every
second)
Figure 19. Blinker circuit. LED is off
Figure 20. Blinker circuit. LED is on