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>
In order to ease our life I have defined the LED pins as LED1 and LED2 in the next two lines.
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.
No comments:
Post a Comment