Category: PIC16F877A – MPLABX with XC8


 

16 X 2 LCD is most popular in Embedded projects .16 is the number of Columns & 2 is the no. of Rows.Generally it needs 8 bits to display the data as the Data bus is of 8 bits.

In this post the LCD is used in 4 bit mode ,splitting the data Byte in Nibbles.So we can save 4 data port pins of the PIC microcontroller.

Complete datasheet of  this LCD is available here .

The Data or command is sent in nibble form (1 nibble= 4 bit) in the 4-bit mode. The higher nibble is sent first followed by the lower nibble. The function of RS, RW and EN pins remains similar to 8-bit mode.

The pin out of LCD is as follows:

1. Ground
2. VCC (+3.3 to +5V)
3. Display Contrast adjustment (VO)
4. Register Select (RS). RS=0: Command, RS=1: Data
5. Read/Write (R/W). R/W=0: Write, R/W=1: Read
6. Clock (Enable). Falling edge triggered
7,8,9,10   Bit 0 to bit3  (Not used in 4-bit operation)
11. D4 Bit 4
12. D5 Bit 5
13. D6 Bit 6
14. D7 Bit 7
15.Backlight Anode (+)
16.Backlight Cathode (-)

 

PIC_LCD

For interfacing LCD with microcontroller we need system-bus (data-bus, address-bus and control-bus). In our case for 4 bit mode the  data pins (D4-D7) are the ‘data and address’ bus while the 3 control pins(RS, R/W and E) are the control bus

These displays generally have a 16-wire interface, of which 11 IO lines from the PIC micro are needed. 8 of these lines are for DATA.

As we use LCD in 4 BIT Mode , only 4 lines are actually needed.

The Read/Write R/W pin is pulled to Ground making the LCD write only.

Dropping the 4 Data lines & the R/W line , we need only 6 I/O lines (4 data lines D4-D7 along with RS and Enable)  for basic operation

 

To start with create a Folder say inside F:\myprojects to store all our work.

Open the MPLABX IDE & click on File –> New Project

Choose Project –> Microchip Embedded –> Standalone Project  click on Next & select the Device as PIC16F877A

Select Tool as PICKIT2  & Compiler as XC8

Provide project name as LCD & project location as F:\myprojects ( we’ve created earlier).

Image 4

 

Click on FINISH

 

Image 1 

Inside FILE PANE you can see the default File folders created by the IDE.

For the XC compiler to understand the LCD hardware , a Header file is used.Download this file & store it inside the LCD.X folder. HEADER FILE DOWNLOAD HERE

Copy & paste the lcd.h header file into the LCD.X folder.

Right click on Header Files –> Add Existing Item  & browse to the location where you’ve stored the lcd.h  header file & select it.

Now lcd.h will appear under Header Files folder

Image 2

 

Now Right click on Source Files –> New  –>  C Main File

Provide a file name , or leave it to default name “newmain” .

Select the extension as .c from the drop down.

You can see newmain.c file created inside the LCD.X project folder.

 

Image5

 

Click on Finish to see the newmain.c file under Source Files folder .The LCD.h file is seen under Header Files folder.

Delete the generated code for now .

Image 6

Click on Window –> PIC Memory Views  –> Configuration bits & set the config bits as FOSC = HS ,WDTE =OFF ,LVP = OFF ,etc.. as seen below

Image 7

 

Click on “Generate Source code to output “ button seen at the bottom.

Image8 

 

You can copy & paste  the Configuration code on to the Editor Pane.

Or you can enter the following config. code in a single line as :

————————————————–

#include <xc.h>

#pragma config FOSC=HS , WDTE=OFF , LVP =OFF , PERTE=OFF

—————————————————

As we use an external crystal of 20 MHz on demo board , we declare FOSC as HS & other settings WatchDog, Low Voltage Programming ,etc . as OFF

Below the #include <xc.h> statement define the crystal value as

#define _XTAL_FREQ 20000000

20 followed by 6 zeroes represents 20 MHz value.

Image9

 

 

Image 10

#include “lcd.h” statement indicates that the lcd.h file resides inside the project folder.Previously you should’ve downloaded this file & stored it inside the LCD.X folder.Otherwise you’ll get red error squiggles against this statement.

PORT D pins are used to connect with the LCD.

We need only 4 pins for DATA , as we use the LCD in 4 bit mode. Pins RD4 to RD7 are used for data bits D4 to D7.

RS (Register Select)  is connected to pin RD2.

When RS = 0 , D4 to D7 value is interpreted as COMMAND by the LCD.

When RS = 1 , D4 to D7 value is interpreted as DATA by the LCD.

To initialize & set the cursor location we set this RS as 0 (Command mode) & for displaying Data we set this RS to 1.

LCD enable pin is connected to RD3.

R/W READ/WRITE pin of LCD is connected to GND as we use the LCD to display DATA.

All these connections are declared using the #define preprocessor statements.

 

———————————————–

#define _XTAL_FREQ 20000000

#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
// BEGIN CONFIG
#pragma config FOSC = HS,WDTE=OFF ,PWRTE = OFF,BOREN = OFF, LVP = OFF
//END CONFIG
#include <xc.h>

#include "lcd.h";

void main()
{
  unsigned int a;
  TRISD = 0x00;
  Lcd_Init();
  while(1)
  {
    Lcd_Clear();
    Lcd_Set_Cursor(1,1);
    Lcd_Write_String("LCD Library for");
    Lcd_Set_Cursor(2,1);
    Lcd_Write_String("MPLAB XC8");
    __delay_ms(2000);
    Lcd_Clear();
    Lcd_Set_Cursor(1,1);
    Lcd_Write_String("WWW.ALSELECTRO.COM");
    __delay_ms(2000);
    Lcd_Clear();
    Lcd_Set_Cursor(1,1);
    Lcd_Write_String("Saravana Electronics");

    for(a=0;a<15;a++)
    {
        __delay_ms(1000);
        Lcd_Shift_Left();
    }

    for(a=0;a<15;a++)
    {
        __delay_ms(1000);
        Lcd_Shift_Right();
    }

    Lcd_Clear();
    Lcd_Set_Cursor(2,1);
    Lcd_Write_Char(‘S’);
    Lcd_Write_Char(‘E’);
    __delay_ms(2000);
  }
 
}

————————————————————-

After feeding in the code as above ,

Click on Run –> Build Project

If all the syntax are correct you get BUILD SUCCESSFUL.

Finally click on Run –> Run Main Project or press F6 to execute the program & see result on the LCD.

Support  Video :

 

PIC 16F877A WITH LCD
Advertisements

 

IMG_4

There are 2 types of  seven segment displays – CC (Common Cathode) & CA (Common Anode).In this post we shall see how to interface CA display with PIC 16F877A.

   The display , as the name indicates , is made up of seven segments of LED.Each one of the seven LEDs in the display is given a positional segment with one of its connection pins being brought out of the  plastic package. These  LED pins are labeled from “ a “ to “ g“ representing each individual LED. The other LED pins are connected together and wired to form a common pin.

In case of CA display +5v is tied to Common pin.

The  common pin is generally used to identify which type of 7-segment display it is. As each LED has two connecting pins, one called the “Anode” and the other called the “Cathode”, there are therefore two types of LED 7-segment display called: Common Cathode (CC) and Common Anode(CA).

Common anode displays are more popular as many logic circuits can sink more current than they can source.

    

Depending upon the decimal digit to be displayed, the particular set of LEDs is forward biased. For instance, to display the numerical digit 0, we will need to light up six of the LED segments corresponding to a, b, c, d, e and f. Then the various digits from 0 through 9 can be displayed using a 7-segment display as shown.

For a Common Anode Display , the COMMON pin should be connected to HIGH & the corresponding DIGIT segments should be made LOW.Each LED segment can tolerate up to only 1.5v DC .

As the HIGH level from PIC is around 5V , a current limiting resistor of 470 ohms must be used at the common pin.

For e.g., to display digit 1 , the segments b & c should be made LOW while the COM pin is made HIGH.

To display digit 2 , the segments a,b,d,e & g are made LOW while making the COM pin HIGH.

Then for a 7-segment display, we can produce a truth table giving the individual segments that need to be illuminated in order to produce the required decimal digit from 0 through 9 as shown below.

7seg_1

CONNECTIONS :  All segment pins are Enabled by LOW (0)

RD2  –>  DIGIT 1  Common Anode pin made HIGH to display a Digit.

RB7—>  D0               RB0 –> D7    

RB3 –> D4                RB6 –> D1 

RB2 –> D5                RB5 –> D2

RB1 –> D6               RB4 –> D3

 

The Demo board I’m using has 4 PNP Transistors which is connected to Digit1,Digit2,Digit3 & Digit4.When a LOW is applied to Base of transistor,it conducts and apply HIGH to the Common pin of Display.

seg1

 

Open a New project in MPLABX & follow the initial STEPS as described in previous post.

The code to drive a single Display is given below.CONFIG statements to be replaced by the code generated for configuration bits as described in earlier post.

——————————————————————

#define _XTAL_FREQ 16000000
#include <xc.h>

// CONFIG statements here….

unsigned char hexValue[10]={0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,0x01,0x09};
int i=0;

int main()
{
TRISB=0x00;  // Port B pins all OUTPUTs
TRISD2=0; // RD2 as OUTPUT
RD2=1;    // Disable display 1

while(1)
  {
    RD2=0;   // Enable Display 1
    for( i=0; i<10; i++)
    {
        PORTB= hexValue[i];
        __delay_ms(1000);
    }
  }
}

———————————————————————–

PNP transistors are used to drive the Enable common pin of CA displays. A LOW at the base of transistor will make it conduct & provide HIGH at the common Anode pin of Display.

According to the data pin (D0 to D7) levels , digits 0 to 9 are displayed. A LOW at data pin will make the segment GLOW & a HIGH will make it OFF.

7seg

The HEX values for the Digit to be displayed are achieved as shown in the Table above.These values are stored in an Array variable unsigned char hexValue , which is declared to store 10 values.

Port B is defined as Output by TRISB = 0x00;

Port pin D2 is declared as Output , which is used to enable the common pin of display.

Initially RD2 = 1 which makes the PNP transistor OFF & the display is disabled.

Under While loop , the  RD2 is made 0 to enable the Display & the HEX value data is loaded one by one at an interval of 1 sec.

You get display running from 0 to 9 at 1 sec interval repeatedly.

In the previous post we’ve seen the LED blinking program.In this post we shall see how to connect a push switch to a Digital Input of PIC 16F877A .

A micro switch is connected between port pin RB0 & Ground. Port pin RB0 is pulled High through a 10k resistor connected to +5v supply.On pressing the switch the LED glows & on releasing the switch the LED GOES Off.

 

Image 8

 

In the development board I’ve used , the pull up resistors are not provided.But this can be enabled internally by making the bit RBPU=0 of Option Register.

This can be achieved by declaring

OPTION_REG=0b01011110;

or   RBPU=0;   The NOT_RBPU bit (MSB) is cleared in the Option Register to apply PULL UP internally to all PORTB pins. 

opt2

The pull up resistor is mandatory to avoid floating values at digital inputs.

 

Following 10 Steps are common for all projects except the project name is different.

In future projects I will not mention the following 10 steps .These are the initial settings required for each project.Future posts will be focused on main C code straight away.

——————————————————————————-

STEP 1 :

Click on  File –> New Project

Under Choose Project select category  “Microchip Embedded “ & Projects as“ StandaloneProject

 

Image 1.1

 

STEP 2 :

Select the Target device as PIC16F877A.

Click Next

Image1.2

 

STEP 3 :

Select the TOOL as PICKIT2

Image1. 3

STEP 4 :

Select the Compiler as XC8 & click Next.

 

Image1. 5

 

STEP 5 :

Provide a name to the project & select the Project folder to store the results.

Click on FINISH

Image 2

STEP 6 :

Project folder appears in the File pane.

Right click on Source Files &  select  New  –>  C Main File

Image 3

 

STEP 7 :

Provide a name to the C file .The extension must be selected as c from the drop down below.

Click on FINISH

 

Image 4

STEP 8 :

Click on   Window  –> PIC MEMORY VIEWS –> Configuration Bits

Image 9

 

On the Task pane window you get the Configuration Bits .

Each bit can be modified as ON or OFF by the drop down menu following the bit

Set FOSC to HS for High Frequency External oscillator & other bits all OFF.

Click on “ Generate Source code to Output” button at the bottom.

 

Image 10

STEP 9 :

Configuration bit setting code is generated.

Delete the default code seen in the Editor pane on top.

Now Paste the Configuration code here.

 

Image 11

STEP 10 :

Finally add the Crystal Frequency definition to the top of #include <xc.h> statement

#define _XTAL_FREQ 16000000

Image 14

 

——————————————————————————-

——————————————————————————-

Now you can feed the following C main code below the configuration code.

Image 5

 

OPTION_REG MSB bit is cleared to enable internal PULL UP Resistors for PORTB.

Port B pin 0 is declared as INput where switch is connected

Port D Pin 2 is declared as Output where LED is connected  .

Initially LED is OFF by making RD2 pin low (RD2=0).

Under the never ending loop the condition of RB0 is verified & if the switch is pressed RD2 is made High , thus making the LED glow.

After a delay of 2 secs the LED is made OFF.

 

After feeding the Code press CTRL+SHIFT+S  to save all the project files.

Now click on RUN –> Build the Main Project 

 

Image 6

After getting BUILD SUCCESSFUL click on “Run Main Project “

The generated HEX file will be loaded on to the Target chip .

Image 7

If you press the switch at RB0 the LED at RD2 glows.

IMG_2

 

The same code can be used to check the Buzzer circuit & the Relay circuit ( if you’ve purchased the development board shown above)

Remove the wire at LED point & connect it to Relay input (connector CN12 ) which is seen near the yellow sugar cube relay on top right of the board.Now by pressing the switch the relay gets ON ,just like the LED.

To test the buzzer , connect the  wire from  RD2 port pin to CN21 .The buzzer is with a white round sticker on its top , seen  just above the LCD.Pressing the key will activate the buzzer now.

For availability of this development board contact :

cooltext753793315   cooltext753790696

http://www.alselectro.com/pic-development-kit-with-lcd.html

In this blog series I’ll post tutorials on using PIC16F877A .You’ll learn the basics starting from installation of the new MPLABX IDE along with the C compiler XC8 .

Further you’ll learn all demo programs like blinking LED, driving a 7 segment display, driving a relay,driving a servo motor,controlling a stepper motor, interfacing keypad matrix, interfacing 16 x 2 LCD, UART control for serial communication,etc.,

To start with we shall walk through the installation of IDE with XC8 compiler.

Download the MPLABX IDE  (375 Mb)  from :

http://www.microchip.com/mplabx-ide-windows-installer

& XC8 compiler (180 Mb) for 12F,16F,18F series of PIC controllers from :

http://www.microchip.com/mplabxc8windows

MPLAB XC Compiler licenses are available as Free editions and have the option of a 60-day PRO evaluation trial.After trial time you can continue using the compiler but without optimization .i.e your final hex  code will be larger.

Following is the screenshot of the installer files downloaded for MPLABX installation.

  Image 1

Double click on MPLABX installer application (version 2.26 at the time of this post) .

Setup window opens up.Leave the installation directory to default settings C:\ProgramFiles\Microchip\MPLABX

 

Image 2            Image 3

           

Select both the IDE and IPE .Click on Next button to start installing the IDE.

 

Image 4      Image 5

 

Once the installation is over you’ll be guided to download XC8 compiler if the XC compiler button is checked.

Image 6     Image 7

Now double click the XC8 compiler installer application

 

Image 8            Image 9

 

Leave the installation directory to default.

               Image 10    

On the next screen select all the options as Compiler settings.

      Image 11

 

 

 

Image 12           Image 13

 

Once the installation is completed you can see the MPLABXIDE & MPLABXC8 folders under All Programs.

 

Image 14      Image 15

 

Click on the MPLABX IDE v2.26 to start the IDE.

 

Image 16

 

In the next post we shall see how to blink an LED , a HELLO WORLD program to start with any Microcontroller.