trouble in interfacing LCD to PIC16f877a

Status
Not open for further replies.

ku91l

Newbie level 6
Joined
Sep 5, 2013
Messages
11
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Visit site
Activity points
78
Hello,
Please check the following HiTech C code for 16x2 LCD interfacing.
The 1st line of LCD shows blocks.The code is taken from the Samples and only the connection bits are changed.
But,its not working.
However,if I write similar code in microC,it works.So the connections are right.
Please help.
Thank You.


Also note,the HTC code is working in Proteus ISIS.
 

Attachments

  • lcdupload.txt
    863 bytes · Views: 84

yes, the code is working properly on ISIS,and yes the connections are fine too .
two things that are confusing me
1. the ISIS dig. for Pic16f877a and the one in data sheet are showing different positions of PORTD- 0 & 1 pins
2. are the < configuration bits > and the <XTal frequency causing this trouble.
 

Post the circuit? Is your Config settings right? Have you included HS_OSC in Config? Delay is not sufficient. Change Fosc to 4 MHz and see if it works else increase delays. If 4 MHz is used change OSC to XT_OSC in Config.
 

these lines are written in the code ..
[[#define _xtal_freq 16000000;
__CONFIG (0x2F0A);]]
delay value given is [ 250 ] .. (as given in the code in the 1st pdf uploaded)
* do you want to make Xtal frequency =4mhz ?*




also please confirm that denoting first pin of PORT B by [[ RB1 ]] is right or not
i.e. #define en RB1 ??
 

the hardware lcd i am using is on a development board so the pins Vss Vcc and Vee are already connected to the required terminals internally.There's nothing i can do in it except controlling the contrast through the potentiometer .
Can you please elaborate on the freq and config points , i suppose a mistake there
 

Consider the datasheet for good pins connections;VSS,VDD and VEE.

Hope its helpfull.
 

Nothing still ..
can you tell
1. how to define first port of PORT B in MPLAB using hitech c compiler for pic16f877a
2. what should be the configuration bits set
3. the xtal frequency defined
 

i have already posted the code in the first post itself as an attachment ..
here it is [[ it displays M D E to the lcd using two functions 1. lcddata 2. lcdcmd and a simple delay function

#include<htc.h>
#define _xtal_freq 16000000;
__CONFIG (0x2F0A);

#define ldata PORTD
#define rs RB0
#define rw RB1
#define en RB2

void MSDelay(unsigned int itime);
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);

void main()
{
TRISD = 0x00;
TRISB = 0x00;
en=0;

MSDelay(250);
lcdcmd(0x38);
MSDelay(250);
lcdcmd(0x0E);
MSDelay(150);
lcdcmd(0x01);
MSDelay(250);
lcdcmd(0x06);
MSDelay(250);
lcdcmd(0x86);
MSDelay(250);
lcddata('M');
MSDelay(250);
lcddata('D');
MSDelay(250);
lcddata('E');
}

void lcdcmd(unsigned char value)
{
ldata = value;

rs=0;
rw=0;
en=1;
MSDelay(100);
en=0;
MSDelay(100);
}

void lcddata(unsigned char value)
{
ldata = value;
rs=1;
rw=0;
en=1;
MSDelay(100);
en=0;
MSDelay(100);
}

void MSDelay(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<72;j++)
{}
}
 

Try this with 4 MHz clock. Turn off code optimization. maybe optimization is removing the delay code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<htc.h>
 
#define _xtal_freq 4000000;
 
    __CONFIG(FOSC_XT & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON 
            & LVP_OFF & DEBUG_OFF);
 
#define ldata PORTD
#define rs RB0
#define rw RB1
#define en RB2
 
void MSDelay(unsigned int itime);
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
 
void main()
{
    TRISD = 0x00;
    TRISB = 0x00;
    
 
    MSDelay(250);
    lcdcmd(0x38);
    MSDelay(250);
    lcdcmd(0x0E);
    MSDelay(15);
    lcdcmd(0x01);
    MSDelay(25);
    lcdcmd(0x06);
    MSDelay(25);
    lcdcmd(0x86);
    MSDelay(25);
    lcddata('M');
    MSDelay(25);
    lcddata('D');
    MSDelay(250);
    lcddata('E');
 
    while(1);
 
}
 
void lcdcmd(unsigned char value)
{
    ldata = value;
 
    rs=0;
    rw=0;
    en=1;
    MSDelay(1);
    en=0;
 
}
 
void lcddata(unsigned char value)
{
    ldata = value;
    rs=1;
    rw=0;
    en=1;
    MSDelay(1);
    en=0;
}
 
void MSDelay(unsigned int itime)
{ 
    unsigned int i,j;
 
    for(i=0;i<itime;i++)
        for(j=0;j<72;j++)
    
}

 

HOW exactly do i turn off code optimisation ?



 

the minimum allowed value is [ 1 ] for optimization.
still not working. as you are having the code can you check if it is working or not.
 

It is working in Proteus. I don't have PIC16F877A with me now. Use 4 MHz Crystal or increase the delay and see. Try to use Hi-Tech C delay library routine for the delays.
 

Attachments

  • lcd8bit.png
    89.1 KB · Views: 77

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…