If you have just started learning PIC18 you will have to study a lot of materials about PIC18, like Instruction Set Architecture, Memory Organization etcetera if you want to know the details. However if you want quick start to program a PIC18 Microcontroller in C you should probably write a simple program in C which will flash an LED connected to one of the IO pin of the Microcontroller like we write the famous “Hello Word” program while learning C.
This Tutorial demonstrates how to code a simple LED Blinking program in C for the Microchip PIC18 platform and then simulate it using Proteus VSM.
Pre-requisites
It’s assumed that you are familiar with MPLAB IDE Ver. 8 and Proteus Design Suite.Introduction to PIC18
PIC stands for Peripheral Interface Controller. PIC18 is a series of 8-bit Microcontrollers made by Microchip. There are different families of PIC18 each having different features. The number of IOs, Data RAM, Flash Memory and Peripherals changes from device to device in a family. A family’s vast feature set provides flexibility to select a specific device for a certain applications depending upon the resources needed.
For the purposes of this tutorial I have selected PIC18F8722 family. This family has devices with 64 and 80 pins, and also with different resources. we will be using PIC18F8722 from this family for this series of tutorials. For more details refer to the datasheet.
Getting Started
When you install MPLAB IDE Ver. 8 it will install the C18 Compiler as well which will convert our source code written in C to Machine code. In case you haven’t used MPLAB before refer to the MPLAB Getting Started manual to get a basic idea on how to create and compile a new project.
Microchip has extensive documentations available for their products, which includes Application Notes, Reference Design manuals and many other which can be found on the Microchip Website.
C Code for our LED Blinking Project
After you have created your new MPLAB Project create a new .c file copy the following Code into it and add the file into the project. To compile the Project Click Build All from the Toolbar or find Build All in the Project drop down menu.
1: /*
2: ###############################################################################
3: **** PIC18 LED Blinky
4: **** IDE : MPLAB Ver 8.91
5: **** Compiler : Microchip C18 Ver 3.43
6: ###############################################################################
7: */
8:
9: // Includes
10: #include <p18cxxx.h>
11: #include <delays.h>
12:
13: // Configurations
14: #pragma config WDT = OFF // Watchdog Timer Disabled
15: #pragma config OSC = HS // OSC Mode - High Speed Crystal/Resonator
16: #pragma config XINST = OFF // Extended Instructions Disabled
17:
18: // LEDs
19: #define LED1 LATDbits.LATD0
20: #define LED2 LATCbits.LATC0
21:
22: // main
23: void main(void)
24: {
25: // As the LED pin is also an analog input pin we have to declare it as
26: // digital by writing the port configuration control bits(PCFG<3:0>) in
27: // ADCON1 Register - Refer to the datasheet for more details
28: ADCON1 = 0x00001111;
29:
30:
31: // Set PORTD bit 0 as digital output, the LED is connected on this pin
32: TRISDbits.TRISD0 = 0;
33: TRISCbits.TRISC0 = 0; // The second LED we want to flash is connected on
// PORTC pin 0
34: // As this pin is not analog so we just need to
// set its direction
35: // infinite loop
36: while(1)
37: {
38: Delay10KTCYx(10); //wait
39: LED1 = 0; // turn LED1 OFF
40: LED2 = 0; // turn LED2 OFF
41: Delay10KTCYx(10); //wait
42: LED1 = 1; // turn LED1 ON
43: LED2 = 1; // turn LED2 ON
44: }
45: }
Explaining the Code
Although the code is heavily commented, but I will explain some points to my best. The fist two lines
// Includes
#include <p18cxxx.h>
#include <delays.h>
includes the device specific header file and the delays.h is for the C18 built-in delay routines which will be used while flashing the LED. I used the built-in delay routine for the delay, but we can also use one of the PIC18 timers for the delay. We will be covering timers in another tutorial.
The other three lines are for the configuration settings of the device, their may be other configurations depending on the application. For our purposes these are enough. You can find the device specific configuration settings in the file “hlpPIC18ConfigSet.htm” in the install directory of MPLAB. On Windows 7 64-bit it will be something like C:\Program Files (x86)\Microchip\MPLAB C18 Suite\v3.43\doc
In order to ease our life I have defined the LED pins as LED1 and LED2 in the next two lines.
Then we enter into the main loop. In the main loop we set the direction of the LED pins as digital outputs. I have used one analog and one digital pin to demonstrate the use of an analog pin as digital IO.
The other important line of code is the Delay10KTCYx(10) this function is used for the delay between LED on and LED off cycles. This generates delay in multiple of 10,000 instruction cycles. One Instruction cycle is equal to 1/FOSC/4 and Clock cycle is equal to 1/FOSC, Where FOSC is our Clock Frequency. Help files for these library function can also be found in the above mentioned doc folder.
Now that we have compiled our code and our HEX file is ready to be burned the PIC18. We will simulate the code in Proteus.
Simulation
I have created a simple circuit in Proteus which contains PIC18F8722 with minimum connections. As shown below
For the purpose of simulations Proteus doesn’t need the power and the Clock circuits I have included this if we want to build a real PCB for this project.
After adding the HEX file to PIC18 Click the Play Button to run the Simulation.
This Concludes our LED blinking Tutorial. We will be covering other features of PIC18 in the upcoming tutorials.
Please use the comment box below to leave your feedback.
Keep sharing this kind of wonderful post!Best Electronics retail software
ReplyDeleteWow, really this is nice blog and most valuable electronics Thanks for sharing.
ReplyDelete