PIC16F877A MPLAB and LCD 2x16

Status
Not open for further replies.

lokster

Newbie level 3
Joined
Sep 19, 2012
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,315
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.. :sad:


i dont even know how to get started

ive tried following these
https://extremeelectronics.co.in/mi...facing-lcd-modules-with-pic-microcontrollers/

but there arent any proper tutorials for pic16f877a. please help
 

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
 

Attachments

  • LCDemo.zip
    1.8 KB · Views: 175
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)
 

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)
 

Attachments

  • LCD.tar.gz
    71.2 KB · Views: 181
  • LCD.zip
    66.9 KB · Views: 157

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
 

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
 

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
 

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.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…