pany2008
Junior Member level 3
Hi
I am using the 16x2 lcd and interfacing it with 4 bit mode, i am placing the source please suggest what is wrong, the display is showing only the cursor blink but it is not displaying the message.
main program
#include<pic.h>
#include "ioports1.h"
#include "delay.h"
#include "lcd.h"
__CONFIG (DEBUGDIS & WDTDIS & BORDIS & PWRTDIS & HS & LVPDIS & PROTECT);
extern init_ports();
void Sec(unsigned int);
void main()
{
init_ports();
Sec(200);
lcd_init(); //LCD initialized
Sec(100);
lcd_line1(0); //LCD address specified AT LINE 1
lcd_string("INTERFACING LCD"); //Displays message "LCD INTERFACING"
lcd_line2(0); //LCD address specified AT LINE 2
lcd_string(" PIC 16F877A");//Displays message " PIC 16f877a"
for(;
{
LED1 = !LED1;
Sec(300);
LED1 = !LED1;
Sec(300);
}
}
init_ports()
{
T_LED1 = 0; // pin enable to output
LED1 = 1;
T_LED2 = 0; // pin enable to output
LED2 = 1;
T_LED3 = 0; // pin enable to output
LED3 = 1;
}
void Sec(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<165;j++);
}
lcd.c file code
#include <htc.h>
#include "lcd.h"
#include "ioports.h"
//#define LCD_RS RB6
//#define LCD_RW RB5
//#define LCD_EN RB4
#define LCD_DATA PORTB
#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)
{
unsigned char d;
__delay_us(40);
LCD_DATA = ( ( c >> 4 ) & 0x0F );
LCD_STROBE();
LCD_DATA = (c & 0x0F);
LCD_STROBE();
__delay_us(40);
}
/*
* 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_string(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
/* write one character to the LCD */
void
lcd_character(char c)
{
LCD_RS = 1; // write characters
lcd_write( c );
}
/*
* Go to the specified position in line 1
*/
void
lcd_line1(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x80+pos);
}
/*
* Go to the specified position in line 2
*/
void
lcd_line2(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0xC0+pos);
}
/*
* Go to the specified position in line 3
*/
void
lcd_line3(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x94+pos);
}
/*
* Go to the specified position in line 4
*/
void
lcd_line4(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0xD4+pos);
}
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
char init_value;
T_LCD_RS = 0; // pin enable to output
LCD_RS = 0;
T_LCD_RW = 0; // pin enable to output
LCD_RW = 0;
T_LCD_EN = 0; // pin enable to output
LCD_EN = 0;
T_LCD_D0 = 0; // pin enable to output
LCD_D0 = 0;
T_LCD_D1 = 0; // pin enable to output
LCD_D1 = 0;
T_LCD_D2 = 0; // pin enable to output
LCD_D2 = 0;
T_LCD_D3 = 0; // pin enable to output
LCD_D3 = 0;
init_value = 0x3;
// PORTB = 0x00;
// TRISB = 0x00;
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
}
lcd.h
/* 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_string(const char * s);
/* Go to the specified position in line 1 */
extern void lcd_line1(unsigned char pos);
/* Go to the specified position in line 2 */
extern void lcd_line2(unsigned char pos);
/* Go to the specified position in line 3 */
extern void lcd_line3(unsigned char pos);
/* Go to the specified position in line 4 */
extern void lcd_line4(unsigned char pos);
/* intialize the LCD - call before anything else */
extern void lcd_init(void);
extern void lcd_character(char);
/* Set the cursor position */
#define lcd_cursor(x) lcd_write(((x)&0x7F)|0x80)
ioports.h
#ifndef BITNUM
#define BITNUM(adr, bit) ((unsigned)(&adr)*8+(bit)) //-- used for port defs
#endif
static bit LCD_RS @ BITNUM(PORTB,6);
static bit T_LCD_RS @ BITNUM(TRISB,6);
static bit LCD_RW @ BITNUM(PORTB,5);
static bit T_LCD_RW @ BITNUM(TRISB,5);
static bit LCD_EN @ BITNUM(PORTB,4);
static bit T_LCD_EN @ BITNUM(TRISB,4);
static bit LCD_D0 @ BITNUM(PORTB,0);
static bit T_LCD_D0 @ BITNUM(TRISB,0);
static bit LCD_D1 @ BITNUM(PORTB,1);
static bit T_LCD_D1 @ BITNUM(TRISB,1);
static bit LCD_D2 @ BITNUM(PORTB,2);
static bit T_LCD_D2 @ BITNUM(TRISB,2);
static bit LCD_D3 @ BITNUM(PORTB,3);
static bit T_LCD_D3 @ BITNUM(TRISB,3);
delay.c
void
DelayMs(unsigned char cnt)
{
#if XTAL_FREQ <= 2MHZ
do {
DelayUs(996);
} while(--cnt);
#endif
#if XTAL_FREQ > 2MHZ
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
} while(--i);
} while(--cnt);
#endif
}
delay.h
#ifndef XTAL_FREQ
#define XTAL_FREQ 12MHZ /* Crystal frequency in MHz */
#endif
#define MHZ *1000L /* number of kHz in a MHz */
#define KHZ *1 /* number of kHz in a kHz */
#if XTAL_FREQ >= 12MHZ
#define DelayUs(x) { unsigned char _dcnt; \
_dcnt = (x)*((XTAL_FREQ)/(12MHZ)); \
while(--_dcnt != 0) \
continue; }
#else
#define DelayUs(x) { unsigned char _dcnt; \
_dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \
while(--_dcnt != 0) \
continue; }
#endif
extern void DelayMs(unsigned char);
ioports1.h
#ifndef BITNUM
#define BITNUM(adr, bit) ((unsigned)(&adr)*8+(bit)) //-- used for port defs
#endif
static bit LED1 @ BITNUM(PORTD,7); //-- LED
static bit T_LED1 @ BITNUM(TRISD,7);
static bit LED2 @ BITNUM(PORTD,3); //-- LED
static bit T_LED2 @ BITNUM(TRISD,3);
static bit LED3 @ BITNUM(PORTD,2); //-- LED
static bit T_LED3 @ BITNUM(TRISD,2);
please if any one can guide me my problem
I am using the 16x2 lcd and interfacing it with 4 bit mode, i am placing the source please suggest what is wrong, the display is showing only the cursor blink but it is not displaying the message.
main program
#include<pic.h>
#include "ioports1.h"
#include "delay.h"
#include "lcd.h"
__CONFIG (DEBUGDIS & WDTDIS & BORDIS & PWRTDIS & HS & LVPDIS & PROTECT);
extern init_ports();
void Sec(unsigned int);
void main()
{
init_ports();
Sec(200);
lcd_init(); //LCD initialized
Sec(100);
lcd_line1(0); //LCD address specified AT LINE 1
lcd_string("INTERFACING LCD"); //Displays message "LCD INTERFACING"
lcd_line2(0); //LCD address specified AT LINE 2
lcd_string(" PIC 16F877A");//Displays message " PIC 16f877a"
for(;
{
LED1 = !LED1;
Sec(300);
LED1 = !LED1;
Sec(300);
}
}
init_ports()
{
T_LED1 = 0; // pin enable to output
LED1 = 1;
T_LED2 = 0; // pin enable to output
LED2 = 1;
T_LED3 = 0; // pin enable to output
LED3 = 1;
}
void Sec(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<165;j++);
}
lcd.c file code
#include <htc.h>
#include "lcd.h"
#include "ioports.h"
//#define LCD_RS RB6
//#define LCD_RW RB5
//#define LCD_EN RB4
#define LCD_DATA PORTB
#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)
{
unsigned char d;
__delay_us(40);
LCD_DATA = ( ( c >> 4 ) & 0x0F );
LCD_STROBE();
LCD_DATA = (c & 0x0F);
LCD_STROBE();
__delay_us(40);
}
/*
* 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_string(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
/* write one character to the LCD */
void
lcd_character(char c)
{
LCD_RS = 1; // write characters
lcd_write( c );
}
/*
* Go to the specified position in line 1
*/
void
lcd_line1(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x80+pos);
}
/*
* Go to the specified position in line 2
*/
void
lcd_line2(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0xC0+pos);
}
/*
* Go to the specified position in line 3
*/
void
lcd_line3(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x94+pos);
}
/*
* Go to the specified position in line 4
*/
void
lcd_line4(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0xD4+pos);
}
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
char init_value;
T_LCD_RS = 0; // pin enable to output
LCD_RS = 0;
T_LCD_RW = 0; // pin enable to output
LCD_RW = 0;
T_LCD_EN = 0; // pin enable to output
LCD_EN = 0;
T_LCD_D0 = 0; // pin enable to output
LCD_D0 = 0;
T_LCD_D1 = 0; // pin enable to output
LCD_D1 = 0;
T_LCD_D2 = 0; // pin enable to output
LCD_D2 = 0;
T_LCD_D3 = 0; // pin enable to output
LCD_D3 = 0;
init_value = 0x3;
// PORTB = 0x00;
// TRISB = 0x00;
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
}
lcd.h
/* 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_string(const char * s);
/* Go to the specified position in line 1 */
extern void lcd_line1(unsigned char pos);
/* Go to the specified position in line 2 */
extern void lcd_line2(unsigned char pos);
/* Go to the specified position in line 3 */
extern void lcd_line3(unsigned char pos);
/* Go to the specified position in line 4 */
extern void lcd_line4(unsigned char pos);
/* intialize the LCD - call before anything else */
extern void lcd_init(void);
extern void lcd_character(char);
/* Set the cursor position */
#define lcd_cursor(x) lcd_write(((x)&0x7F)|0x80)
ioports.h
#ifndef BITNUM
#define BITNUM(adr, bit) ((unsigned)(&adr)*8+(bit)) //-- used for port defs
#endif
static bit LCD_RS @ BITNUM(PORTB,6);
static bit T_LCD_RS @ BITNUM(TRISB,6);
static bit LCD_RW @ BITNUM(PORTB,5);
static bit T_LCD_RW @ BITNUM(TRISB,5);
static bit LCD_EN @ BITNUM(PORTB,4);
static bit T_LCD_EN @ BITNUM(TRISB,4);
static bit LCD_D0 @ BITNUM(PORTB,0);
static bit T_LCD_D0 @ BITNUM(TRISB,0);
static bit LCD_D1 @ BITNUM(PORTB,1);
static bit T_LCD_D1 @ BITNUM(TRISB,1);
static bit LCD_D2 @ BITNUM(PORTB,2);
static bit T_LCD_D2 @ BITNUM(TRISB,2);
static bit LCD_D3 @ BITNUM(PORTB,3);
static bit T_LCD_D3 @ BITNUM(TRISB,3);
delay.c
void
DelayMs(unsigned char cnt)
{
#if XTAL_FREQ <= 2MHZ
do {
DelayUs(996);
} while(--cnt);
#endif
#if XTAL_FREQ > 2MHZ
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
} while(--i);
} while(--cnt);
#endif
}
delay.h
#ifndef XTAL_FREQ
#define XTAL_FREQ 12MHZ /* Crystal frequency in MHz */
#endif
#define MHZ *1000L /* number of kHz in a MHz */
#define KHZ *1 /* number of kHz in a kHz */
#if XTAL_FREQ >= 12MHZ
#define DelayUs(x) { unsigned char _dcnt; \
_dcnt = (x)*((XTAL_FREQ)/(12MHZ)); \
while(--_dcnt != 0) \
continue; }
#else
#define DelayUs(x) { unsigned char _dcnt; \
_dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \
while(--_dcnt != 0) \
continue; }
#endif
extern void DelayMs(unsigned char);
ioports1.h
#ifndef BITNUM
#define BITNUM(adr, bit) ((unsigned)(&adr)*8+(bit)) //-- used for port defs
#endif
static bit LED1 @ BITNUM(PORTD,7); //-- LED
static bit T_LED1 @ BITNUM(TRISD,7);
static bit LED2 @ BITNUM(PORTD,3); //-- LED
static bit T_LED2 @ BITNUM(TRISD,3);
static bit LED3 @ BITNUM(PORTD,2); //-- LED
static bit T_LED3 @ BITNUM(TRISD,2);
please if any one can guide me my problem