Friday, 29 August 2014

Defining code regions in MPLAB X IDE


     In this post I would like to share "How to define code in regions using MPLAB X". This post has very much importance in good programming practice that's why I want to share it with you guys. I personally like this feature in MPLAB X remember this feature is only available in MPLAB X. With this feature you can define code in regions likewise you may have done in Microsoft Visual Studio. When you define certain code in regions you may easily handle the code you are interesting in. For example if you define certain interrupt setting registers code in one region and frequency setting code in other regions.


    Following figure 1 shows how an organized code looks like. Which I do in coding as general practice. In this code I organized all Debug LEDs, Spare Pins, Input buttons etc in separate regions. So whenever I want to change certain regions I quickly click on "+"  sign and made changes without looking at other regions code.
Figure 1. Organized code in regions.


     In the following figure 2. I unfold the comment and I have all the #defines for Input Buttons Pins. So this would help you to quickly identify certain regions as quickly as possible. you have seen that the <editor ... > statement.

Figure 2. unfolding


How to write Folding Comments:

     To write code in regions you need to write following statement at the top of code which you want to place it in region. Your statement which would appear above folded code write it in comment(shown in bold).

// <editor-fold defaultstate="collapsed" desc="comment">

and at the end of regional code you need to have following statement.

// </editor-fold>

your code should be in between these statements which you want to collapse.

     Now you are thinking that this method is little awful so don't be upset there is a shortcut direct method of writing these statements. Now simply type fcom and then press tab key you will quickly get these statements given in figure 3 below.
   
     To remember this I decoded this instruction as fcom = f (folding) com (Comment). So when ever I want to put code in regions I use folding comments.

Figure 3. Folding Comment using fcom

     Hope that this would helped you to get rid of awful codes and you would start writing awesome codes like I am doing or even better than me. Remember these statements only working in MPLAB X IDE. For more details on Folding code Click here.

     If you learned something interesting please consider donating some amount so that I continue producing such interesting free content for young engineers thank you.





All the text and graphics contained on this page belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 

© Copyright 2014 IbrahimLabs. All rights reserved.

Saturday, 16 August 2014

Graphical LCD interfacing with PIC16F877 Code + Schematic + Proteus Simulation

   
     This tutorial is all about "How to interface Graphical LCD with PIC16F877". You may download code, schematic and Proteus simulation from the download section at the bottom of this page. 

     From this point I assumed that you know following (if not then read them before proceeding any further in this post).

  1. How to use Mplab X IDE (Click Here).
  2. How to set Configuration bits in code (Click Here).
  3. How to blink an LED (Click Here).
Proteus Simulation:

     In the following figure 1. We have proteus simulation in which I am using graphical LCD 128x64 with PIC16F877. This LCD is similar to JHD12864E (datasheet) with only addition of two pins those for LCD backlight. I have tested this code on real graphical LCD it is working.

Figure 1. Proteus Simulation.

    In the following figure 2. PortD is connected to DATA and ADDRESS bus of graphical LCD. whereas PortC higher bits are connected to LCD control signals. As you have seen the Reset (RST) pin is active low.A variable resister is for LCD contrast. MCLR pin is pulled high for micro-controller normal operation. I am using 10MHz crystal. You may see PIC16F877 datasheet for more details

Figure 2. Schematic Graphical LCD
General Details:

     As I mentioned before this graphical LCD is similar to JHD12864 (Controller K0108 type) so I would like to go briefly into details rest of it you may read from its datasheet. This LCD is split logically into two half left and right with two control pins ChipSelect1(CS1) and ChipSelect2 (CS2) respectively. CS1 is responsible for displaying in left half and CS2 is responsible for displaying in right half. further more each chip has 8 vertical sections named as pages (page 0~7). Every single page is further divided into 64 horizontal sections named columns and each column has 8 bits. When you stack them horizontally they are 64 bytes as shown in figure 3. So if you want to display one picture having resolution of 128x64 on this LCD you have total 1024 bytes. For example (pages) times (columns) times (no. of chips) => 8*64*2 = 1024.
Figure 3. Graphical LCD explained.
     Now we should move ahead for "How to generate display Screens for 128x64 Graphical LCD". To generate code for one picture we need to have a picture of size 128x64. So Paint it in Microsoft paint. The first step is to set the size of picture go into file>Properties a dialog box will open select colour "black and white", width "128" and height "64". as shown in figure 4.

Figure 4. Pixel settings
      Now you can zoom this page and draw any picture like I have drawn following figure 5. The important step is to save this picture as "monochrome bitmap (.bmp)" format. 

Figure 5. monochrome (.bmp) picture.
     Now we have to translate this picture into its equivalent binary data or constant array of bytes so here is a software for translating this into its binaries called LCD Assistant by Radoslaw (All rights reserved for Radoslaw). You may download it (Click Here).  as shown in figure 6. To export code you need to go file>export this would generate a file open it in Notepad and copy the code and paste it in Mplab X and compile.

Figure 6. LCD Assistant.

Code:

     In the following figure 7 we have a main function in which we have InitializeGraphicalLCD() which is responsible for initializing graphical LCD according to LCD datasheet. Then we have a fill screen function this will fill full screen with all pixels ON and then a one second delay after that all screen is cleared after this I have displayed a complete picture on LCD that I have generated as shown above in this post.

Figure 7. main function.
     Following figure 8 shows the header file in which I have #defined all the page addresses, indexes, necessary commands and Pins used to interface graphical LCD. The purpose of this defines are just to write a code that looks good, managed and readable. which is good code practice I recommend you to follow these good programming practices too.

Figure 8. Graphical LCD header file.
     In the following figure 9 we have InitializeGraphicalLCD() In which I configure all LCD pins as output. Then send some necessary commands to LCD which is described in its datasheet. As you have seen that I have to write these commands to LCDs both Chips so I send BothChip as parameter to function that is responsible for writing command to LCD.

Figure 9. Initialize Graphical LCD.
     In the following figure 10 we have ChipSelection() function which is responsible for selecting the chip either 1 , 2 or both. To select chip we need to pass chip no. as parameter.

Figure 10. ChipSelection.
     In the following figure 11 we have ToggleEnablePin() function which is responsible for toggling enable pin. After every data or address send to LCD we have to toggle its Enable pin state as described in LCD datasheet.

Figure 11. Toggle Enable Pin.
     In the following figure 12 we have WriteCommandToLCD() function which is responsible for writing commands to LCD by passing command and chip no.

Figure 12. Write command to LCD.
     In the following figure 13 we have WriteDataToLCD() which is responsible for writing data to LCD by passing data and chip no. as parameter to this function.

Figure 13. write data to LCD. 
     In the following figure 14 we have WriteAddressToLCD() which is responsible for sending address value on which data is written on LCD by passing addressX and addressY valve as well as chip no. to this function as parameter.

Figure 14. write address to LCD.
     In the following figure 15 we have DisplayPicture() which is responsible for displaying complete picture on LCD by passing a address of first byte of data as you can see there is a pointer for memory location. This function writes 1024 bytes of data to LCD. 

Figure 15. Display Picture on LCD.

Downloads:

 You may download Code which is written in C language. Using MPLAB X IDE and compiler XC8 for 8 bit Microcontrollers. Click here


If you learned something interesting please consider donation. Which would help me to produce such content for young engineers Thank you.




Reading Suggestions:
 For all topics Click here


All the text and graphics contained on this page belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 

© Copyright 2014 IbrahimLabs. All rights reserved.

Sunday, 10 August 2014

LCD interfacing with PIC16F877 Code and Simulation



          In this tutorial you will find Proteus simulation and Code of  "How to interface 16x2 LCD with PIC16F877 using MPLAB X IDE and XC8 compiler". You may download code, schematic and Proteus simulation from download section at the bottom of this page.

In this post I assumed that you must know following if not then please read them before proceeding any further in this post:

    1. How to use MPLAB X IDE (Click Here).
    2. How to set Configuration bits in code (Click Here).
    3. How to Blink an LED (Click Here).

Proteus Simulation:

In figure 1. I have used PIC16F877 with PortB connected to an LCD as data and address bus whereas Register Select (RS) and Enable (E) pins are connected to Pins RC0 and RC1 respectively. Whereas MCLR is connected to 5VDC which means Controller is in Normal Operating mode (see datasheet for further details). I have set Crystal frequency 10MHz.

Figure 1: Proteus Simulation.

Code:

In the following figure 2. We have a main function in which we initialize LCD and a while loop to display strings on LCD and update LCD after every 1 seconds. approx. Note that first string is displayed in first line by moving cursor to first line address and second string is displayed on second line by moving cursor to second line address.

Figure 2: main C file.

In the following figure 3 we have LCD.h file in which we have all the #define like LCD data bus, LCD enable and register select pins. First and second line addresses.

Figure 3. LCD.h


In Initialize_LCD() I made Enable and RS(Register Select) pins and portB  as output by setting their Tristate registers. And then I write some commands to LCD which are necessary for initialization as describes in LCD datasheet.

Figure 4. Initialize LCD.
In figure 5. we have Toggle_Enable_Pin_LCD() which is used to toggle Enable pin after every command or data send to LCD according to datasheet. In Initialize_LCD() I made Enable and RS(Register Select) pins and portB  as output by setting their Tristate registers. And then I write some commands to LCD which are necessary for initialization as describes in datasheet.

Figure 5. Toggle Enable pin.


This function is used to write commands to LCD In which we have RS low.
Figure 6. Write command to LCD.
This function is used to write Data to LCD In which we have RS High. 

Figure 7. Write Data to LCD.

Downloads:

                You may download Code which is written in C language. Using MPLAB X IDE and compiler xc8 for 8 bit Microcontrollers. Click here

If you learned something interesting please consider donation. Which would help me to produce such content for young engineers Thank you.




Reading Suggestions:
 For all topics Click here



All the text and graphics contained on this page belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 

© Copyright 2014 IbrahimLabs. All rights reserved.

Getting started with Arduino coding. (A beginner’s quick guide)

Welcome to my first Arduino coding guide. This would take you to kickstart with Arduino boards. I would go through following things real...