Saturday, August 31, 2013

LED Blinking in PIC18 using C with Proteus Simulation

PIC18_LEDanimation
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: }

build

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

circuit


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.

GIF animation depicting LED Simulation in Proteus


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.
Read More

Tuesday, August 20, 2013

Tutorial - Placing your company logo on PCB in Proteus ARES


pic32-title
Today we will talk about how to place logo or other graphics on PCB in Proteus ARES. After we design a PCB its important to place logo of your company on the PCB so that people know who made it. In ARES we can do some artwork using the built-in graphics tools like drawing a circle, line, arc, rectangles etc. but its not possible to make your company logo using these tools. So you have to place your company logo using import features in ARES. Let’s place a logo on a PCB to illustrate the process.
First of all you need to convert your company’s logo to monochromatic bmp format in order to import it to ARES. you can do the conversion either with Paint (a small photo editing program that comes with Windows) or any other online or offline tool you can find. I have converted the logo to monochromatic bmp using Paint in Windows using the following procedure…
  • Open the logo image with Paint
  • From the Save As choose the Save as type “Monochromatic Bitmap” from the drop down menu and Click Save.
pic32-1
Now that you have the .bmp Image of your logo. Open the PCB in Proteus ARES do the following steps to place the image on
  • From the File menu select Import Bitmap
  • Navigate to the .bmp image you have saved and import it
  • The image will be attached with the mouse place it wherever you want and resize if required.
pic32-2
That’s it you can import any image you want to place on the PCB but first you have to convert it to Monochromatic Bitmap format.
After placing and re-sizing the image the Board looks like as below

pic32-3

Please comment you questions and suggestions and follow for updates. Thanks
Read More

Thursday, August 15, 2013

Tutorial - Creating Multi-element Heterogeneous device Symbol in Proteus ISIS


title
In today’s tutorial we will learn “How to make  multi-element heterogeneous device symbol in Proteus ISIS”. The image on the left shows a device symbol consists of 7 elements where each elements have different number of pins.
We will make this 7 elements device symbol for NXP Semiconductors ARM Based Microcontroller LPC2478 using Proteus ISIS 8 demo version.





Why make Multi-elements heterogeneous device symbol instead of One single element symbol?

The answer to this question in my opinion is, its easy to deal with multiple elements of a device when it have hundreds of pins. So we split the device symbol into multiple elements which eases capturing our schematic.
Lets start our work to make one such symbol.
We will be making device symbol for LPC2478. LPC2478 is a 208 pin device available in LQFP as well as BGA package. We will make device symbol and then assign LQFP Package, You can also assign BGA Package to the symbol the process is the same. Datasheet of LPC2478 can be downloaded from here 
Proteus 8 Demo Version can be downloaded from Lab Center Electronics Website if you don’t have it.

Step 1 – Opening Proteus ISIS

Open Proteus 8 and click on the ISIS icon in the toolbar to open ISIS.

Step 2 – Make The first element in the multi-element device Symbol

Make device symbol for the Power part of the device. Assign names to pins and don’t assign number at this stage, we will assign numbers in the Package Assigning step. Select the symbol and invoke the Make Device tool using the toolbar button. In the warning window click OK, The Make Device window appears. In case you don’t know how to make device symbol in Proteus ISIS, read the tutorial Making Device Symbol in Proteus first.

step 2

Step 3  - Naming elements of the multi-element symbol package

In the Name Device field write LPC2478:A (<Name of Device> : <A,B,C….Z>), write U in the Reference Prefix field click Next

step 3

The Next Dialog says package cannot be assigned (we will do that later) click Next. Add datasheet if you want, select Category, subcategory, Manufacturer etc. for the Device, Select a Library and Hit OK to finish the dialog. We have created and saved the first element of Heterogeneous multi-element device symbol.

step 4
 

Step 4 -

Repeat Step 2 and Step 3 for all other parts, assign names as LPC2478:B, LPC2478:C and so on. Now you have complete device in different elements. We will assign a package to it in the next step to complete the tutorial.

Step 5 – Assigning Package to the multi-element device symbol

Add one instance of each element to the work area in ISIS. Select all and invoke the Packaging tool from the toolbar as shown in the image below

5

In the dialog that appears click add to add the PCB Package(footprint) of the device. To know how to create a device package in ARES read the tutorial Making Device Package in Proteus ARES
The Package assignment is not finished now. remember we didn't assigned numbers to pins while making symbols for them, here we have to assign numbers to the pins. The pin number assignment is time consuming process in making multi-element heterogeneous device symbol as you have to refer to the datasheet.
In the next window we have a column for each element and we will write the pin numbers in each column for the pins shown in the left most column, After assigning writing pin numbers in each column click Assign Package to finish.

6

This completes the tutorial. In this tutorial we have learned how to make multi-element device symbol in Proteus ISIS. Please feel free to comment for questions and suggestions.


Read More

Popular Posts

Followers

Like Us

Follow me on Twitter