+ Post New Thread
Results 1 to 8 of 8
  1. #1
    Newbie level 3
    Points: 128, Level: 1

    Join Date
    Sep 2012
    Posts
    4
    Helped
    0 / 0
    Points
    128
    Level
    1

    PIC16F877A MPLAB and LCD 2x16

    how do i interface with the PIC and LCD 2x16 with MPLAB, and high tech C???

    is there anyone that can point me to a sample code and how to code it? i tried adding header files into the project but its still not working, it wont build or anything..


    i dont even know how to get started

    ive tried following these
    http://extremeelectronics.co.in/micr...rocontrollers/

    but there arent any proper tutorials for pic16f877a. please help

    •   Alt7th October 2012, 01:58

      advertising

        
       

  2. #2
    Administrator
    Points: 68,615, Level: 63
    Achievements:
    Created Album pictures Created Blog entry 7 years registered
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    9,796
    Helped
    2345 / 2345
    Points
    68,615
    Level
    63
    Blog Entries
    4

    Re: PIC16F877A MPLAB and LCD 2x16

    The following is a 4-bit implementation, tested successfully using the Hi-Tech C Compiler.

    lcd.h
    Code:
    /*
     *	LCD interface header file
     *	See lcd.c for more info
     */
    
    /* write a byte to the LCD in 4 bit mode */
    
    extern void lcd_write(unsigned char);
    
    /* Clear and home the LCD */
    
    extern void lcd_clear(void);
    
    /* write a string of characters to the LCD */
    
    extern void lcd_puts(const char * s);
    
    /* Go to the specified position */
    
    extern void lcd_goto(unsigned char pos);
    	
    /* intialize the LCD - call before anything else */
    
    extern void lcd_init(void);
    
    extern void lcd_putch(char);
    
    /*	Set the cursor position */
    
    #define	lcd_cursor(x)	lcd_write(((x)&0x7F)|0x80)


    lcd.c
    Code:
     *	
     *	PORTD bits 0-3 are connected to the LCD data bits 4-7 (high nibble)
     *	PORTA bit 3 is connected to the LCD RS input (register select)
     *	PORTA bit 1 is connected to the LCD EN bit (enable)
     *	
     *	To use these routines, set up the port I/O (TRISA, TRISD) then
     *	call lcd_init(), then other routines as required.
     *	
     */
    
    #ifndef _XTAL_FREQ
     // Unless specified elsewhere, 4MHz system frequency is assumed
     #define _XTAL_FREQ 4000000
    #endif
    
    
    #include	<htc.h>
    #include	"lcd.h"
    
    #define LCD_RS	RA3
    #define LCD_RW	RA2
    #define LCD_EN	RA1
    
    #define LCD_DATA	PORTD
    
    #define	LCD_STROBE()	((LCD_EN = 1),(LCD_EN=0))
    
    /* write a byte to the LCD in 4 bit mode */
    
    void
    lcd_write(unsigned char c)
    {
    	__delay_us(40);
    	LCD_DATA = ( ( c >> 4 ) & 0x0F );
    	LCD_STROBE();
    	LCD_DATA = ( c & 0x0F );
    	LCD_STROBE();
    }
    
    /*
     * 	Clear and home the LCD
     */
    
    void
    lcd_clear(void)
    {
    	LCD_RS = 0;
    	lcd_write(0x1);
    	__delay_ms(2);
    }
    
    /* write a string of chars to the LCD */
    
    void
    lcd_puts(const char * s)
    {
    	LCD_RS = 1;	// write characters
    	while(*s)
    		lcd_write(*s++);
    }
    
    /* write one character to the LCD */
    
    void
    lcd_putch(char c)
    {
    	LCD_RS = 1;	// write characters
    	lcd_write( c );
    }
    
    
    /*
     * Go to the specified position
     */
    
    void
    lcd_goto(unsigned char pos)
    {
    	LCD_RS = 0;
    	lcd_write(0x80+pos);
    }
    	
    /* initialise the LCD - put into 4 bit mode */
    void
    lcd_init()
    {
    	char init_value;
    
    	ADCON1 = 0x06;	// Disable analog pins on PORTA
    
    	init_value = 0x3;
    	TRISA=0;
    	TRISD=0;
    	LCD_RS = 0;
    	LCD_EN = 0;
    	LCD_RW = 0;
    	
    	__delay_ms(15);	// wait 15mSec after power applied,
    	LCD_DATA	 = init_value;
    	LCD_STROBE();
    	__delay_ms(5);
    	LCD_STROBE();
    	__delay_us(200);
    	LCD_STROBE();
    	__delay_us(200);
    	LCD_DATA = 2;	// Four bit mode
    	LCD_STROBE();
    
    	lcd_write(0x28); // Set interface length
    	lcd_write(0xF); // Display On, Cursor On, Cursor Blink
    	lcd_clear();	// Clear screen
    	lcd_write(0x6); // Set entry Mode
    }

    I have attached a zip file with the above two files along with a demo main.c.

    I would recommend testing the code with the default connections as described in lcd.c, also adjust the _XTAL_FREQ to the proper clock frequency if necessary.




    BigDog
    The Edge... there is no honest way to explain it, because the only people who really know where it is are the ones who have gone over.
    -- Hunter S. Thompson


    1 members found this post helpful.

    •   Alt7th October 2012, 05:06

      advertising

        
       

  3. #3
    Newbie level 3
    Points: 128, Level: 1

    Join Date
    Sep 2012
    Posts
    4
    Helped
    0 / 0
    Points
    128
    Level
    1

    Re: PIC16F877A MPLAB and LCD 2x16

    ive used your files but when i try to add lcd.h to my program i get the error

    can't open include file "lcd.h": No such file or directory

    and ive added the lcd.h file to my project file.

    - - - Updated - - -

    ok to fix my previous post the lcd.h lcd.c have to be in the same folder as the program you are programming

    but i still have this error of

    Error [499] ; 0. undefined symbol:
    _lcd_init(demolcd.obj)



    •   Alt7th October 2012, 09:40

      advertising

        
       

  4. #4
    Member level 3
    Points: 1,039, Level: 7

    Join Date
    Apr 2012
    Posts
    66
    Helped
    2 / 2
    Points
    1,039
    Level
    7

    Re: PIC16F877A MPLAB and LCD 2x16

    when i was learning how to use lcd i made a small program to display 'motor on' or 'motor off' using assembly language
    here it is

    - - - Updated - - -

    or if you like C language (Mikroc recommended)



  5. #5
    Administrator
    Points: 68,615, Level: 63
    Achievements:
    Created Album pictures Created Blog entry 7 years registered
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    9,796
    Helped
    2345 / 2345
    Points
    68,615
    Level
    63
    Blog Entries
    4

    Re: PIC16F877A MPLAB and LCD 2x16

    Quote Originally Posted by lokster View Post
    ok to fix my previous post the lcd.h lcd.c have to be in the same folder as the program you are programming

    but i still have this error of

    Error [499] ; 0. undefined symbol:
    _lcd_init(demolcd.obj)
    Have you added both the header file (lcd.h) and source file (lcd.c) to the Project Files Window?

    If not right click on the Source Files or Header Files and select Add Files...., then recompile.




    BigDog
    The Edge... there is no honest way to explain it, because the only people who really know where it is are the ones who have gone over.
    -- Hunter S. Thompson



  6. #6
    Newbie level 3
    Points: 128, Level: 1

    Join Date
    Sep 2012
    Posts
    4
    Helped
    0 / 0
    Points
    128
    Level
    1

    Re: PIC16F877A MPLAB and LCD 2x16

    Thanks guys ive figured it out.

    lcd.h and lcd.c should both be in the same folder as your program, BUT why when i build the program it shows that its compiling the LCD.c and not my program? i have to right click my program file and compile it to see if it built successfully



    •   Alt11th October 2012, 00:22

      advertising

        
       

  7. #7
    Administrator
    Points: 68,615, Level: 63
    Achievements:
    Created Album pictures Created Blog entry 7 years registered
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    9,796
    Helped
    2345 / 2345
    Points
    68,615
    Level
    63
    Blog Entries
    4

    Re: PIC16F877A MPLAB and LCD 2x16

    Quote Originally Posted by lokster View Post
    lcd.h and lcd.c should both be in the same folder as your program, BUT why when i build the program it shows that its compiling the LCD.c and not my program? i have to right click my program file and compile it to see if it built successfully
    It sounds as if you have not properly setup the project.

    Zip up your project directory with all the required files and upload.

    I'll take a look and see if I can remedy the situation.


    BigDog
    The Edge... there is no honest way to explain it, because the only people who really know where it is are the ones who have gone over.
    -- Hunter S. Thompson



  8. #8
    Newbie level 3
    Points: 324, Level: 3

    Join Date
    May 2013
    Posts
    3
    Helped
    0 / 0
    Points
    324
    Level
    3

    Re: PIC16F877A MPLAB and LCD 2x16

    Hi, what version of MPLAB that u used?
    Is it each MPLAB have a different programming language or structure or syntax?

    *sorry for my question, im new with this.

    Thanks.



--[[ ]]--