SlideShare a Scribd company logo
/********PIC16F887*******
   HI-Tech C compiler
   Programmer : http://vto22012.blogspot.com/
***********************************
*/


#include <htc.h>
#define _XTAL_FREQ 20000000 //crystal speed 20Mhz
__CONFIG(0X3F32); //set configuration bits. Refer to PIC datasheet.
#include <stdio.h> // standard I/O c language


/********LCD Driver start ************************************************/

/*   LCD controller
//   (max characters is 40)
//   Initialization: lcd_init();
//   To print a string: printf("Hello World");
//   A few more formatting subroutines are available.
//   See towards the end of this page.

// macros to isolate interface dependencies
// LCD is connected to PORTD */


#define   LCDPort         PORTD
#define   LCDTris         TRISD
#define   Epin            6
#define   RWpin           5
#define   RSpin           4
#define   BUSYpin         3
#define   DATA0pin        0
#define   DATA1pin        1
#define   DATA2pin        2
#define   DATA3pin        3

#define EHIGH          bitset(LCDPort, Epin)
#define ELOW           bitclr(LCDPort, Epin)
#define E_OUTPUT       bitclr(LCDTris, Epin)
#define RSHIGH         bitset(LCDPort, RSpin)
#define RSLOW          bitclr(LCDPort, RSpin)
#define RS_OUTPUT      bitclr(LCDTris, RSpin)
#define RWHIGH         bitset(LCDPort, RWpin)
#define RWLOW          bitclr(LCDPort, RWpin)
#define RW_OUTPUT      bitclr(LCDTris, RWpin)
#define BUSY_FLAG      bittst(LCDPort, BUSYpin)
#define DATA_DIR_RD
{bitset(LCDTris,DATA3pin);bitset(LCDTris,DATA2pin);bitset(LCDTris,DATA1pin);bits
et(LCDTris,DATA0pin);}
#define DATA_DIR_WR
{bitclr(LCDTris,DATA3pin);bitclr(LCDTris,DATA2pin);bitclr(LCDTris,DATA1pin);bitc
lr(LCDTris,DATA0pin);}
#define OUTPUT_DATA(x) {int aval=LCDPort&0xF0; x = x & 0x0F; LCDPort=aval+x;}

// some common defines

#define bitset(var,bitno) ((var) |= (1 << (bitno)))
#define bitclr(var,bitno) ((var) &= ~(1 << (bitno)))
#define bittst(var,bitno) (var & (1 << (bitno)))


void epulse(void){
        __delay_us(1);
EHIGH;
       __delay_us(1);
       ELOW;
       __delay_us(1);
}

void lcd_write(
     unsigned char cmd, unsigned char data_flag, unsigned char chk_busy,
unsigned char dflag){
        char bflag,c;
        if (chk_busy) {
                 RSLOW;        //RS = 0 to check busy
                 // check busy
                 DATA_DIR_RD; //set data pins all inputs
                 RWHIGH;        // R/W = 1, for read
                 do {
                         EHIGH;
                         __delay_us(1); // upper 4 bits
                         bflag = BUSY_FLAG;
                         ELOW;
                         __delay_us(1);
                         epulse();
                 } while(bflag);
        } else {
                 __delay_ms(10); // don't use busy, just delay
        }
        DATA_DIR_WR;
        if (data_flag) RSHIGH;      // RS=1, data byte
        else     RSLOW;    // RS=0, command byte
        // device is not busy
        RWLOW;        // R/W = 0, for write
        c = cmd >> 4; // send upper 4 bits
        OUTPUT_DATA(c);
        epulse();
        if (dflag) {
                 c = cmd & 0x0F; //send lower 4 bits
                 OUTPUT_DATA(c);
                 epulse();
        }
}


void lcd_init(void) {
        // configure, see control pins as outputs
        // initialize as low
        E_OUTPUT; RS_OUTPUT; RW_OUTPUT;
        ELOW; RSLOW; RWLOW;

       __delay_ms(25); //wait for device to settle
       __delay_ms(25);
       lcd_write(0x20,0,0,0); // 4 bit interface
       lcd_write(0x28,0,0,1); // 2 line display, 5x7 font
       lcd_write(0x28,0,0,1); // repeat
       lcd_write(0x06,0,0,1); // enable display
       lcd_write(0x0C,0,0,1); // turn display on; cursor, blink is off
       lcd_write(0x01,0,0,1); // clear display, move cursor to home
       __delay_ms(3);   // wait for busy flag to be ready
}

// send 8 bit char to LCD

 void putch (char c) {
    lcd_write(c,1,1,1);
}
void lcd_clear(void){
        lcd_write(0x01,0,0,1);   // clear display, move cursor to home
}

/* go to the specified position */
void lcd_goto(unsigned char row, unsigned char pos)
{
switch(row){
case 1:{lcd_write(0x80+pos,0,1,1);break;}
case 2:{lcd_write(0xC0+pos,0,1,1);break;}
case 3:{lcd_write(0x94+pos,0,1,1);break;}
case 4:{lcd_write(0xD4+pos,0,1,1);break;}
     }
}

void lcd_shiftleft(void){
        lcd_write(0x18,0,1,1);   // shift left
}

void lcd_shiftright(void){
        lcd_write(0x1C,0,1,1);   // shift right
}

void lcd_blinkcursor(void){
        lcd_write(0x0F,0,1,1);   // displays and blink a cursor
}

void lcd_printbar(int bar){
        for(int i=0; i<bar; i++){
                lcd_write(0xFF,1,1,1);           // print bars on the LCD
        }
}

void lcd_string(const char *s)                           //send a string to display
in the lcd
{
        unsigned char i=0;
           while (s && *s)lcd_write(*s++,1,1,1);

}

/********************LCD Driver
End****************************************************/


/*******************start main program *************************************/
void main(void)
{
      TRISD = 0b00000000;                      // configure all RD pin as output
      PORTD = 0b00000000;                      // reset all RD pin to 0 or off
condition
      ANSEL = 0;                         // Configure AN pins as digital
      ANSELH = 0;
      C1ON = 0;                      // Disable comparators
      C2ON = 0;

      lcd_init();   // initialise LCD

while(1){                                           // loop forever
    lcd_goto(1,0); //Display start at line 1 and position 0
      printf("Tutorial PIC"); //display
      lcd_goto(2,0); //Display start at line 2 position 0
      printf("visit:vto22012");
//can use normal printf function like printf("Convert number %d",temp);
}
}
/***********************main program
end****************************************/

More Related Content

PPT
Chapter5 dek3133
PDF
Pic microcontrollers for_beginners
PPTX
PIC16F877A interfacing with LCD
PDF
An introdution to MPLAB
PPTX
PIC introduction + mapping
PDF
Intrerfacing i
PDF
Introduction to MPLAB IDE
PPT
8-bit PIC Microcontrollers
Chapter5 dek3133
Pic microcontrollers for_beginners
PIC16F877A interfacing with LCD
An introdution to MPLAB
PIC introduction + mapping
Intrerfacing i
Introduction to MPLAB IDE
8-bit PIC Microcontrollers

What's hot (19)

PPTX
Microcontroller lec 2
PPT
PIC Microcontroller
PPT
PIC 16F877A by PARTHIBAN. S.
PDF
Programming pic microcontrollers
PDF
Programming with PIC microcontroller
PDF
Programming the Digital I/O Interface of a PIC microcontroller
PDF
Pic microcontroller step by step your complete guide
PDF
Embedded system (Chapter 3) io_port_programming
PPTX
Getting started with pic microcontrollers
PDF
Analog I/O in PIC16F877A
PDF
Introduction to pic microcontroller
PPTX
Atmel and pic microcontroller
PPTX
PIC 16F877 micro controller by Gaurav raikar
PPTX
Pic 18 microcontroller
PDF
Digital i o
PDF
PIC CONTROLLERS
PPTX
Pic16cxx instruction set
PPTX
Microcontroller lec 2
PIC Microcontroller
PIC 16F877A by PARTHIBAN. S.
Programming pic microcontrollers
Programming with PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontroller
Pic microcontroller step by step your complete guide
Embedded system (Chapter 3) io_port_programming
Getting started with pic microcontrollers
Analog I/O in PIC16F877A
Introduction to pic microcontroller
Atmel and pic microcontroller
PIC 16F877 micro controller by Gaurav raikar
Pic 18 microcontroller
Digital i o
PIC CONTROLLERS
Pic16cxx instruction set
Ad

Similar to PIC and LCD (20)

DOCX
codings related to avr micro controller
PDF
Experiment 16 x2 parallel lcd
PDF
Embedded Systems Project 3rd Year
PDF
Combine the keypad and LCD codes in compliance to the following requ.pdf
PPTX
LCD_Example.pptx
PPTX
131080111003 mci
DOCX
Direct analog
RTF
Lcd n PIC16F
DOCX
Dam gate open close lpc prog
PPTX
ESD -DAY 24.pptx
DOC
Microcontroller Programming Assignment
DOCX
PPTX
Vechicle accident prevention using eye bilnk sensor ppt
DOCX
#include LPC17xx.h#include Lights.h#include traffic_fo.docx
PDF
Lecture mp 7(interface)
PDF
DEF CON 23 - Rodringo Almeida - embedded system design from electronics
PDF
Automatic room light controller with visible counter
PPTX
Embedded systems design @ defcon 2015
PDF
Gain Experience with GPIO, LED Interface and Functions in TI TIVA C Launchpad...
DOCX
codings related to avr micro controller
Experiment 16 x2 parallel lcd
Embedded Systems Project 3rd Year
Combine the keypad and LCD codes in compliance to the following requ.pdf
LCD_Example.pptx
131080111003 mci
Direct analog
Lcd n PIC16F
Dam gate open close lpc prog
ESD -DAY 24.pptx
Microcontroller Programming Assignment
Vechicle accident prevention using eye bilnk sensor ppt
#include LPC17xx.h#include Lights.h#include traffic_fo.docx
Lecture mp 7(interface)
DEF CON 23 - Rodringo Almeida - embedded system design from electronics
Automatic room light controller with visible counter
Embedded systems design @ defcon 2015
Gain Experience with GPIO, LED Interface and Functions in TI TIVA C Launchpad...
Ad

More from hairilfaiz86 (7)

TXT
DOC
Lp teori multimeter
DOC
Ws multimeter
DOC
Lp amali multimeter
DOC
I.s multimeter
DOC
As1 multimeter
DOC
Lp teori multimeter
Lp teori multimeter
Ws multimeter
Lp amali multimeter
I.s multimeter
As1 multimeter
Lp teori multimeter

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Pre independence Education in Inndia.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
master seminar digital applications in india
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Complications of Minimal Access Surgery at WLH
Cell Structure & Organelles in detailed.
O5-L3 Freight Transport Ops (International) V1.pdf
Pharma ospi slides which help in ospi learning
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Sports Quiz easy sports quiz sports quiz
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pre independence Education in Inndia.pdf
Anesthesia in Laparoscopic Surgery in India
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
RMMM.pdf make it easy to upload and study
2.FourierTransform-ShortQuestionswithAnswers.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
master seminar digital applications in india
VCE English Exam - Section C Student Revision Booklet
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Complications of Minimal Access Surgery at WLH

PIC and LCD

  • 1. /********PIC16F887******* HI-Tech C compiler Programmer : http://vto22012.blogspot.com/ *********************************** */ #include <htc.h> #define _XTAL_FREQ 20000000 //crystal speed 20Mhz __CONFIG(0X3F32); //set configuration bits. Refer to PIC datasheet. #include <stdio.h> // standard I/O c language /********LCD Driver start ************************************************/ /* LCD controller // (max characters is 40) // Initialization: lcd_init(); // To print a string: printf("Hello World"); // A few more formatting subroutines are available. // See towards the end of this page. // macros to isolate interface dependencies // LCD is connected to PORTD */ #define LCDPort PORTD #define LCDTris TRISD #define Epin 6 #define RWpin 5 #define RSpin 4 #define BUSYpin 3 #define DATA0pin 0 #define DATA1pin 1 #define DATA2pin 2 #define DATA3pin 3 #define EHIGH bitset(LCDPort, Epin) #define ELOW bitclr(LCDPort, Epin) #define E_OUTPUT bitclr(LCDTris, Epin) #define RSHIGH bitset(LCDPort, RSpin) #define RSLOW bitclr(LCDPort, RSpin) #define RS_OUTPUT bitclr(LCDTris, RSpin) #define RWHIGH bitset(LCDPort, RWpin) #define RWLOW bitclr(LCDPort, RWpin) #define RW_OUTPUT bitclr(LCDTris, RWpin) #define BUSY_FLAG bittst(LCDPort, BUSYpin) #define DATA_DIR_RD {bitset(LCDTris,DATA3pin);bitset(LCDTris,DATA2pin);bitset(LCDTris,DATA1pin);bits et(LCDTris,DATA0pin);} #define DATA_DIR_WR {bitclr(LCDTris,DATA3pin);bitclr(LCDTris,DATA2pin);bitclr(LCDTris,DATA1pin);bitc lr(LCDTris,DATA0pin);} #define OUTPUT_DATA(x) {int aval=LCDPort&0xF0; x = x & 0x0F; LCDPort=aval+x;} // some common defines #define bitset(var,bitno) ((var) |= (1 << (bitno))) #define bitclr(var,bitno) ((var) &= ~(1 << (bitno))) #define bittst(var,bitno) (var & (1 << (bitno))) void epulse(void){ __delay_us(1);
  • 2. EHIGH; __delay_us(1); ELOW; __delay_us(1); } void lcd_write( unsigned char cmd, unsigned char data_flag, unsigned char chk_busy, unsigned char dflag){ char bflag,c; if (chk_busy) { RSLOW; //RS = 0 to check busy // check busy DATA_DIR_RD; //set data pins all inputs RWHIGH; // R/W = 1, for read do { EHIGH; __delay_us(1); // upper 4 bits bflag = BUSY_FLAG; ELOW; __delay_us(1); epulse(); } while(bflag); } else { __delay_ms(10); // don't use busy, just delay } DATA_DIR_WR; if (data_flag) RSHIGH; // RS=1, data byte else RSLOW; // RS=0, command byte // device is not busy RWLOW; // R/W = 0, for write c = cmd >> 4; // send upper 4 bits OUTPUT_DATA(c); epulse(); if (dflag) { c = cmd & 0x0F; //send lower 4 bits OUTPUT_DATA(c); epulse(); } } void lcd_init(void) { // configure, see control pins as outputs // initialize as low E_OUTPUT; RS_OUTPUT; RW_OUTPUT; ELOW; RSLOW; RWLOW; __delay_ms(25); //wait for device to settle __delay_ms(25); lcd_write(0x20,0,0,0); // 4 bit interface lcd_write(0x28,0,0,1); // 2 line display, 5x7 font lcd_write(0x28,0,0,1); // repeat lcd_write(0x06,0,0,1); // enable display lcd_write(0x0C,0,0,1); // turn display on; cursor, blink is off lcd_write(0x01,0,0,1); // clear display, move cursor to home __delay_ms(3); // wait for busy flag to be ready } // send 8 bit char to LCD void putch (char c) { lcd_write(c,1,1,1); }
  • 3. void lcd_clear(void){ lcd_write(0x01,0,0,1); // clear display, move cursor to home } /* go to the specified position */ void lcd_goto(unsigned char row, unsigned char pos) { switch(row){ case 1:{lcd_write(0x80+pos,0,1,1);break;} case 2:{lcd_write(0xC0+pos,0,1,1);break;} case 3:{lcd_write(0x94+pos,0,1,1);break;} case 4:{lcd_write(0xD4+pos,0,1,1);break;} } } void lcd_shiftleft(void){ lcd_write(0x18,0,1,1); // shift left } void lcd_shiftright(void){ lcd_write(0x1C,0,1,1); // shift right } void lcd_blinkcursor(void){ lcd_write(0x0F,0,1,1); // displays and blink a cursor } void lcd_printbar(int bar){ for(int i=0; i<bar; i++){ lcd_write(0xFF,1,1,1); // print bars on the LCD } } void lcd_string(const char *s) //send a string to display in the lcd { unsigned char i=0; while (s && *s)lcd_write(*s++,1,1,1); } /********************LCD Driver End****************************************************/ /*******************start main program *************************************/ void main(void) { TRISD = 0b00000000; // configure all RD pin as output PORTD = 0b00000000; // reset all RD pin to 0 or off condition ANSEL = 0; // Configure AN pins as digital ANSELH = 0; C1ON = 0; // Disable comparators C2ON = 0; lcd_init(); // initialise LCD while(1){ // loop forever lcd_goto(1,0); //Display start at line 1 and position 0 printf("Tutorial PIC"); //display lcd_goto(2,0); //Display start at line 2 position 0 printf("visit:vto22012");
  • 4. //can use normal printf function like printf("Convert number %d",temp); } } /***********************main program end****************************************/