mugur
Newbie level 4
- Joined
- Nov 21, 2012
- Messages
- 7
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,336
Hi guys. I am a beginner in c programming and I try to connect a 20x4 lcd to a pic16f877 through i2c module. The lcd is working if I connect it directly to pic. The i2c module is a cheap one from ebay, a mjkdz model like **broken link removed**. I have the code but my problem is that I can't make it work because I need to change the address. The initial address is 0xc6 and I need to change it to 0x20 or 0x27. The code is below:
I tryed to replace "SSPBUF = 0xc6;" with "SSPBUF = 0x20;" or "SSPBUF = 0x27;" but so far no luck. Do I need to change something else in code or this should be enough?
I tryed to put 4.7k pull up resistors on SDA and SCL but is not working.
Please help me. Thank you.
Code:
#include <pic.h>
#include <stdio.h>
__CONFIG(0x3b32);
void clrscn(void); // prototypes
void cursor(char pos);
void print(char *p);
void setup(void);
char s[21]; // buffer used to hold text to print
void main(void)
{
setup(); // sets up the PIC16F877 I2C port
clrscn(); // clears the LCD03 disply
cursor(26); // sets cursor to center the text on 2nd row of LCD03
sprintf(s,"Hello World"); // Traditional welcome text, printed into our buffer
print(s); // send it to the LCD03
while(1); // just stops
}
void clrscn(void)
{
SEN = 1; // send start bit
while(SEN); // and wait for it to clear
SSPIF = 0;
SSPBUF = 0xc6; // LCD02 I2C address
while(!SSPIF); // wait for interrupt
SSPIF = 0; // then clear it.
SSPBUF = 0; // address of register to write to
while(!SSPIF); //
SSPIF = 0; //
SSPBUF = 12; // clear screen
while(!SSPIF); //
SSPIF = 0; //
SSPBUF = 4; // cursor off
while(!SSPIF); //
SSPIF = 0; //
PEN = 1; // send stop bit
while(PEN); //
}
void cursor(char pos)
{
SEN = 1; // send start bit
while(SEN); // and wait for it to clear
SSPIF = 0;
SSPBUF = 0xc6; // LCD02 I2C address
while(!SSPIF); // wait for interrupt
SSPIF = 0; // then clear it.
SSPBUF = 0; // address of register to write to
while(!SSPIF); //
SSPIF = 0; //
SSPBUF = 2; // set cursor
while(!SSPIF); //
SSPIF = 0; //
SSPBUF = pos; //
while(!SSPIF); //
SSPIF = 0; //
PEN = 1; // send stop bit
while(PEN); //
}
void print(char *p)
{
SEN = 1; // send start bit
while(SEN); // and wait for it to clear
SSPIF = 0;
SSPBUF = 0xc6; // LCD02 I2C address
while(!SSPIF); // wait for interrupt
SSPIF = 0; // then clear it.
SSPBUF = 0; // address of register to write to
while(!SSPIF); //
SSPIF = 0; //
while(*p) {
SSPBUF = *p++; // write the data
while(!SSPIF); //
SSPIF = 0; //
}
PEN = 1; // send stop bit
while(PEN); //
}
void setup(void)
{
unsigned long x;
TRISC = 0xff;
PORTC = 0xff;
SSPSTAT = 0x80;
SSPCON = 0x38;
SSPCON2 = 0x00;
SSPADD = 50; // SCL = 91khz with 20Mhz Osc
for(x=0; x<60000; x++); // wait for LCD03 to initialise
}
I tryed to replace "SSPBUF = 0xc6;" with "SSPBUF = 0x20;" or "SSPBUF = 0x27;" but so far no luck. Do I need to change something else in code or this should be enough?
I tryed to put 4.7k pull up resistors on SDA and SCL but is not working.
Please help me. Thank you.
Last edited: