Thursday, 20 June 2013

PIC18F252 Ports Configuration and Demo LED blinking C Code

In this post I'll guide you how to configure ports and simulate a demo LED blinking C code on Proteus and MPLAB simulator.


I/O Ports:


Since I have chosen PIC18F252 microcontroller to work with one must know that it is a 28 pin high performance enhanced flash microcontroller. There are three bidirectional ports available with this  microcontroller that are PORTA, PORTB and PORTC. Some pins of the I/O ports are multiplexed / shared with an alternate function from the peripheral features on the device. When a specific peripheral is enabled, that pin may not be used as a general purpose I/O pin. Each port pin is associated with three registers. These registers are: 
  • TRIS register (Controls the data direction)
  • PORT register (Read voltages and logic level present at the pin)
  • LAT register (Output latch register)

Configuring PORT as an Input / Output: 

  1. Writing '1' to a specific bit of TRISx register configures the PORTx pin as an input.
  2. Writing '0' to a specific bit of TRISx register configures the PORTx pin as an output.

Schematics:

The basic Proteus schematics is shown below. This schematics is purely for code simulation and testing purposes only. The reason I am stating this is that most people say code running in Proteus does not work in real environment. Please note that the SPICE model of PIC18F252 in Proteus does not show the power and ground pins. They are internally attached to the VCC and VSS. Although there are pins available for the oscillator but you still need to define oscillator frequency in proteus. To do this you need to double click the IC and set oscillator frequency in clock section. If you want to make your hardware work in real environment one must follow the basic circuitry given in PIC18F252 datasheet.



LED Blinking Code:

The main application function is shown below


Port configuration function is done here



In the above written C code the first function to be executed is "SetupPorts()". In this function you can see that all pins of the port are made output. Writing a '0' to "TRISx" pin configures the pin as digital output. The next line clears any garbage data in "LATB" register. Control is then passed to an infinite loop which toggles the "LATB" register after every 500 milliseconds. Before we test the simulation in Proteus it is better to First test it in MPLAB SIM. Go to Debugger > Select Tool > MPLAB SIM


MPLAB SIM is selected as debugger the next step is to set some global values. Go to Debugger > Settings. See figure below.


Once you are in debugger "Settings" the first thing to do is to change the oscillator value. See figure. Click "OK" to continue. 


There are other settings too. For further details you have to read the documentation for MPLAB SIM available at www.microchip.com. I'll try to cover rest of the options in next posts. 
We are now going to simulate and test the above written code in MPLAB SIM. For logic analysis of pin MPLAB SIM provides a tool called "Simulator Logic Analyzer". Here you can see the transition of signals and pin as you navigate through the code. To enable this option go to View > Simulator Logic Analyzer. The following window should appear.



You must add pins to see the transition going on to the pin here is how you add pins to this analyzer. 


Select the desired pins and click add. For this chunk of code the associated port is PORTB. Select RB0 to RB7. 


Click "OK" to continue. Pins added are being highlighted below.


The next step is to compile the code. Add a "Break Point" by double clicking at a specific code line.


Press "Play" button. 

The code stops at the "Break Point". The following picture is a result of pressing "Play" button three to four times. See the transition against the pin.



There is lot more to discover in "Logic Analyzer". From my point of view it is a virtual oscilloscope in one's hand. You can probe each and every peripheral of PIC devices using this feature. Further details and information can be found at http://www.cis.upenn.edu/~lee/06cse480/data/MPLAB_IDE_User_guide.pdf and http://nnp.ucsd.edu/phy120b/pic_manuals_pdf/mplab_simu_edit_user.pdf.

Tracing code:

Lets say that you have written a code that has included many files and after some time you need to edit some function or you forgot where it is written. Here is a method which lets you keep track of your functions and data types.

In the above chunk of code lets trace "DelayBigUs(100);" function. Right click on the function and you will see a number of option available. Once you right click some function, struct member or data type you will see the following. 


Click "Go to Locator" it will automatically take you to the file where the code was written. Doing this IDE took me to the required file and it seems some thing like this.



Note that the code pointer is pointing to "line 18".

Some time doing the above thing do not take effect. To solve this you have to click the "Symbol" tab see below.


Right click in the the empty space and select "Update Tags now".


Click the file tab next to symbols. Look for the function/struct member to trace and follow the procedure explained above for tracing functions.

You can find the source code and Proteus simulations from the following links. 

https://dl.dropboxusercontent.com/u/77452173/PORTS/C%20Code.rar
https://dl.dropboxusercontent.com/u/77452173/PORTS/Proteus%20Simulation.rar

Video file is coming soon.
Reader comments are always welcomed. Thanks for reading.

Monday, 3 June 2013

Microchip MPLAB IDE Project Setup

In this post I'll guide you how to make simple project and setup MPLAB IDE environment. 

An Introduction to MPLAB: 
This introduction takes you through the stages of creating a project and setting up the Integrated Development Environment (IDE) for our ease of use. What you require is that you should download and install the latest version of MPLAB.
Open the MPLAB IDE, which should appear as in Figure. If a blank Output window also opens, close it. The main screen is blank, a part from the Work space window at top left, which can not be closed.


Creating a project:
Click the Project button on the tool bar to access the pull-down menu, as shown in Figure.




As you click on project wizard button the following screen will be shown. 


Click next and choose a device; click next to continue.



After selecting a microcontroller choose language tool suit. First check "store tool location in project". Click on the desired tool suit. if you didn't found the installed tool check "show all installed tool suit". 



Click next and choose a desired location for file storage click "Browse". Its a good habit that you make a separate folder for your work. Give your project a name. Click "Save". Click "Next".




If you have already some source code already in that folder click those files and click add. Press "Next" to continue. Click "Finish".



You will now see a blank project is created now. 

You are now ready to add files to your project. But before adding there are some basic settings that I would suggest. Click on the the project window. Select "Dockable". 


After selecting "Dockable" click and hold this window and move to the left or right on the screen. A place  comes where this window is automatically adjusted in transparent rectangular block at the corner of screen. I have highlighted this area for visual clarity. Do this for the output window. 


Create a new file by clicking File > New. A blank file will appear.


Before writing any code to this new File. I would suggest you to do some basic IDE settings. I am explaining the basics for further study; read the documentation available at www.microchip.com. Right click on the source file and click properties. You are now going to change properties of the editor. 



You will see a new window with many options in it. First option is the Tabbed window option. If your project has multiple files checking this option will open any file in a new tabbed window.



The effect of the "Use Tabbed Window" can seen below. 


Next window is "File Type". I would like to have line numbers in my program. I also use "Line Wrap" and "Auto Indent" options.  


Next to "File Type" Window is "Tool Tips". Here you will use the intelligence of the IDE. Couple of my colleagues said that MPLAB IDE  does not have intelligence to show functions and struct members. I always answer them that you have not explored more of this IDE. Here you will set them and enable that intelligence.


Next tab is "Text". Here you can change the font size and the colors for special data types. You are free to choose different font styles and sizes. Just play around and choose what ever style fits to you.


Close MPLAB once to save your settings. As I already wrote that these are the basics settings that I use. For detail information you have to read the MPLAB IDE documentation available at www.microchip.com. Readers comments are always welcomed here. Thanks for reading.