I2C on my 18f452.....please help

LK/ELK/VK/PK/OK/MX/GLK/EGLK/GVK/GLT Series

Moderators: Henry, Mods

evanwright
LCD!
Posts: 14
Joined: Mon May 01, 2006 10:47 am

I2C on my 18f452.....please help

Post by evanwright »

I just picked up a lk204-25 lcd and I am using a microchip 18f452 pic. I have tried countless different things to get this lcd to work. However, nothing seems to have any effect on the display.

I am using a 4MHz crystal and 10k pull up resistors on the SDA and SCL lines. I have also tried 4.7k.

Anyways, I also viewed both the SDA and SCL lines on a ossilloscope and there is absolutely no activity.

Code is written using Microchips c18 c compiler. Please help me?



/*****************************************/
/* Included Files */
/*****************************************/
#include <P18F452.H> //
#include <stdio.h> //
#include <delays.h> //
#include <i2c.h> //
#include <sw_i2c.h> //
#include <p18cxxx.h> //


/******************************************/
/* Function Prototypes */
/******************************************/
void clrscn(void); //
void cursor(char pos); //
void print(char *p); //
void setup(void); //
/******************************************/

char s[21]; //buffer used to hold text

void main(void) //main program begins here
{
setup();
clrscn();
cursor(26);
sprintf(s,"Hello World");
print(s);

while(1);

} //end main program

void clrscn(void)
{

SSPCON2bits.SEN = 1; //send start bit
while(SSPCON2bits.SEN); //wait for it to clear

PIR1bits.SSPIF = 0;
SSPBUF = 0xb8; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0xfe; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0x58; //clear screen
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0x4b; //turn cursor off
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPCON2bits.PEN = 1; //send stop bit
while(SSPCON2bits.PEN);
}

void cursor(char pos)
{
SSPCON2bits.SEN = 1; //send start bit
while(SSPCON2bits.SEN); //wait for it to clear

PIR1bits.SSPIF = 0;
SSPBUF = 0xb8; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0xfe; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 47; //set cursor command
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it
SSPBUF = pos; //cursor location
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPCON2bits.PEN = 1; //send stop bit
while(SSPCON2bits.PEN);
}

void print(char *p)
{
SSPCON2bits.SEN = 1; //send start bit
while(SSPCON2bits.SEN); //wait for it to clear

PIR1bits.SSPIF = 0;
SSPBUF = 0xb8; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0xfe; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

while(*p) {
SSPBUF = *p++; //write the data
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it
}

SSPCON2bits.PEN = 1; //send stop bit
while(SSPCON2bits.PEN);
}

void setup(void)
{
unsigned long x;


DDRCbits.RC3 = 1;
DDRCbits.RC4 = 1;
TRISC = 0xff;
PORTC = 0xff;

SSPSTAT = 0x80;
SSPCON1 = 0x38;
SSPCON2 = 0x00;
SSPADD = 0x09; //(4MHz/100kHz)/4 - 1 = 9

for(x = 0; x < 60000; x++); //wait for lcd to initiate
}

Raquel
Matrix Orbital
Matrix Orbital
Posts: 805
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Post by Raquel »

Hi evan,

Thank you for posting on the forum.

You should be able to see activity on the SDA and SCL pins on their own, so start with no display connected. Also, I see that you addressed the slave with 0xB8, a reminder to change the slave address on the module when you do connect it back.

I'll still be reading some more on the data sheet and see if your code is violating anything but it looks good so far.
Last edited by Raquel on Mon May 01, 2006 1:50 pm, edited 1 time in total.
Raquel Malinis
Design and Development
Matrix Orbital

evanwright
LCD!
Posts: 14
Joined: Mon May 01, 2006 10:47 am

Post by evanwright »

Thanks for replying. I disconnected the lcd and still no activity on either sda or scl lines. Also, if the default jumpers(J1 and J2 installed) gave you an I2C address of 0x5c or 1011100 then to write to the lcd you would send 10111000 or b8 right? Maybe I am confused about the whole 7bit addressing with the eight bit being either 0:write or 1:read.

I appreciate any help I can get.

-evan

Raquel
Matrix Orbital
Matrix Orbital
Posts: 805
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Post by Raquel »

Hi evan,

You are right, the default address of the LK204-25 with jumpers 1 and 2 connected is 0x5C. Matrix Orbital does not fully follow the 7-bit addressing implemented by Philips, but it is actually a lot simpler. 0x5C is the default write address, and that 0x5D (0x5C + 1) is the read address. 0x5C should be the address you put in SSPBUF when writting to the module.

The pull-up you mentioned earlier, are they on your board (there are modules that have pads for pull-up for the SDA and SCL)? You may have the pull-ups on your board or the module, but not both. But for this test we are doing (SDA and SCL) the pull-ups should be on your board, so as to pull the signals up.

Are SCL and SDA at non-changing low level or high? I see that you properly make these two lines input and this follows the recommendations on the PIC data sheet. Is it possible that these lines are tied somewhere? A short to some other lines maybe?
Raquel Malinis
Design and Development
Matrix Orbital

evanwright
LCD!
Posts: 14
Joined: Mon May 01, 2006 10:47 am

Post by evanwright »

Okay, so I understand now.

Anyways, this pic does have internal pull up resistors but I have them dissabled. I have a 10k pull up resistors on my board for both SDA and SCL.

Both lines are staying high.

Raquel
Matrix Orbital
Matrix Orbital
Posts: 805
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Post by Raquel »

Hi evan,

This is just one thing I want to suggest, all the actions on the I2C transaction will result to the same flag being set, so instead of checking for:

SSPCON2bits.SEN = 1; //send start bit
while(SSPCON2bits.SEN); //wait for it to clear

or

SSPCON2bits.PEN = 1; //send stop bit
while(SSPCON2bits.PEN);

do the same while(!PIR1bits.SSPIF); //wait for interrupt
that you do on when writing the SSPBUF.
Raquel Malinis
Design and Development
Matrix Orbital

evanwright
LCD!
Posts: 14
Joined: Mon May 01, 2006 10:47 am

Post by evanwright »

Well, we are definately getting somewhere now. For the first time ever I have seen something on the lcd besides the start up screen. However, instead of "Hello" like the program is designed to output I just get a "K" in the top left corner.

You were right about the while loops though. Thankyou very much.

Any idea why I got a "K". On reset I get the same thing each time.

-evan

Raquel
Matrix Orbital
Matrix Orbital
Posts: 805
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Post by Raquel »

Hi evan,

It's good to hear that we are moving forward. 'K' is 0x4B, and I see that in clrscrn() you have 0x4B loaded in the SSPBUF without the command prefix 0xFE, this is why you see the 'K'. You should load in 0xFE first then 0x4B.

I want to remind you to address the slave with 0x5C. We should have seen 'Hello' since we saw 'K'.

Please let me know how it turns out.
Raquel Malinis
Design and Development
Matrix Orbital

evanwright
LCD!
Posts: 14
Joined: Mon May 01, 2006 10:47 am

Post by evanwright »

Oh, I see. So each command you send has to be proceded by a 0xfe.

Oh, and I finally got it to display Hello. It turns out that one big problem was the following code at the end of each function. I was forgetting to clear the PIR1bits.SSPIF bit.

SSPCON2bits.PEN = 1; //send stop bit
while(!PIR1bits.SSPIF); //wait for interrupt

instead:

SSPCON2bits.PEN = 1; //send stop bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

I do have one more question though. I cant seem to figure out how to set the cursor position. Could you give me an example using my code how I would center the word hello. I spent a lot of time trying to figure it out last night but had no success. Maybe you can see what is wrong with my function: void cursor(char pos);


Anyways, I really appreciate all your help. You have made a huge difference. Thank you!

Raquel
Matrix Orbital
Matrix Orbital
Posts: 805
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Post by Raquel »

Hi evan,

Since you have a 20 column display, and the word 'Hello' is 5 characters, it'll be best to put the cursor at column 8. So before sending 'Hello' send command 0xFE, 0x47, 0x08, 0x01. <0x08> is the column, <0x01> is the row (change this to 0x02 if you want the text in the second row, 0x03 for the third row, so on).
Raquel Malinis
Design and Development
Matrix Orbital

evanwright
LCD!
Posts: 14
Joined: Mon May 01, 2006 10:47 am

Post by evanwright »

Hey Raquel,
So I have finally mastered writing all the commands to the lcd thanks to you.

But I havent been successful with reading the keypad yet. I have a 4x4 keypad wired to the lcd correctly. I have included the code I am using. Everyonce in a while I will get one key pressed to display but for the most part it just isnt working.

Can you please take a look at my code and let me know what you think. I wasnt really sure what mode to put the lcd in.

What I want it to do is when I call the function getKey() I want it to loop in that function until a key is pressed and then return the character to the function call.

Any ideas?

Thanks

-evan

/******************************************/
/* Function Prototypes */
/******************************************/
void clrscn(void); //
void setup(void); //
void printChar(unsigned char x);
unsigned char getKey(void);

/******************************************/


void main(void) //main program begins here
{

setup();
clrscn(); //setup the lcd

while(1){
printChar(getKey); //print all keys pressed to the lcd
}

while(1);

} //end main program

void clrscn(void)
{
SSPCON2bits.SEN = 1; //send start bit
while(!PIR1bits.SSPIF); //wait for it to clear
PIR1bits.SSPIF = 0;

SSPBUF = 0x5c; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0xfe; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0x58; //clear screen
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0xfe; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0x60; //auto repeat mode off for keypad
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0xfe; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0x4f; //auto transmit keypresses off
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPCON2bits.PEN = 1; //send stop bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it
return;
}

void printChar(unsigned char x)
{
SSPCON2bits.SEN = 1; //send start bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0;

SSPBUF = 0x5c; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0xfe; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = x; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPCON2bits.PEN = 1; //send stop bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it
return;
}

unsigned char getKey(void)
{
unsigned char keyPressed = 0x00;
SSPCON2bits.SEN = 1; //send start bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0;

SSPBUF = 0x5d; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

poll:
keyPressed = SSPBUF;
if(keyPressed == 0){
SSPCON1bits.CKP = 1;
SSPBUF = 0x5d; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it
goto poll;
}

SSPCON2bits.PEN = 1; //send stop bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

return(keyPressed);
}

void setup(void)
{
unsigned long x;

DDRCbits.RC3 = 1;
DDRCbits.RC4 = 1;
TRISC = 0xff;
PORTC = 0xff;

SSPSTAT = 0x80;
SSPCON1 = 0x38;
SSPCON2 = 0x00;
SSPADD = 0x09; //(4MHz/100kHz)/4 - 1 = 9

for(x = 0; x < 60000; x++); //wait for lcd to initiate
}

Raquel
Matrix Orbital
Matrix Orbital
Posts: 805
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Post by Raquel »

Hi evan,

I went back to the PIC data sheet and found that you have to configure the I2C port to receive data. You do this by setting SSPCON2<3>, please see waveform Fig. 15-22 on page 157 of the PIC18F452 data sheet.

I see that in getKey(), you set SSPCON1.CKP, I am not sure why, but I believe that CKP is not used in master mode. I see that you also re-send the address, you are probably trying to do a re-start. May I suggest to do the poll within a higher while loop, ie:

while (1) {
do{
kp = getKey();
} while (!kp)

printChar(kp);
}

and finish each read transaction with a NAK and a STOP. Ie. do not read more than 1 byte; no reason for only doing one byte, but let's start with a simple read.

Please let me know how it turns out.
Raquel Malinis
Design and Development
Matrix Orbital

evanwright
LCD!
Posts: 14
Joined: Mon May 01, 2006 10:47 am

Post by evanwright »

Hey Raquel,

Well, I havent had any more luck, however maybe the changes i made were not correct. Here is my updated code. Any ideas? (Did you notice the keypad commands I have in clrscn(). Maybe those are wrong)

-evan

void main(void) //main program begins here
{

unsigned char kp;
clrscn();

cursor(0x98);


while(1){
do{
kp = getKey();
}
while(!kp);
printChar(kp);
}

while(1);

while(1);

} //end main program

void clrscn(void)
{
SSPCON2bits.SEN = 1; //send start bit
while(!PIR1bits.SSPIF); //wait for it to clear
PIR1bits.SSPIF = 0;

SSPBUF = 0x5c; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0xfe; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0x58; //clear screen
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0xfe; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0x60; //auto repeat mode off for keypad
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0xfe; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0x4f; //auto transmit keypresses off
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPCON2bits.PEN = 1; //send stop bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it
return;
}

void printChar(unsigned char x)
{
SSPCON2bits.SEN = 1; //send start bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0;

SSPBUF = 0x5c; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0xfe; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = x; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPCON2bits.PEN = 1; //send stop bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it
return;
}

unsigned char getKey(void)
{
unsigned char keyPressed = 0x00;
SSPCON2bits.RCEN = 1; //enable receive mode for 12c

SSPCON2bits.SEN = 1; //send start bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0;

SSPBUF = 0x5d; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

keyPressed = SSPBUF;

SSPCON2bits.PEN = 1; //send stop bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPCON2bits.RCEN = 0; //dissable receive mode for 12c
return(keyPressed);
}

Raquel
Matrix Orbital
Matrix Orbital
Posts: 805
Joined: Thu Aug 19, 2004 3:37 pm
Location: MO Office

Post by Raquel »

Hi evan,

I notice that you have a cursor(0x98) command, I am not sure what that is for.
Also just wondering about the extra while(1)'s in main, compiler must have warned you about that. The commands that you have within clrscrn() look good. How about adding a debug code right after clearing the screen, by sending the letter 'A' to the module?, just to see how far you actually get on the code.

Also, I do not see the code following the waveform that I mentioned to you last time (Fig. 15-22 on page 157).

By the way, I did not notice this earlier but printChar should not have 0xFE sent to the SSPBUF, just send the character. 0xFE is only necessary when sending commands to the module.

At this point, do you see any keys at all?
Raquel Malinis
Design and Development
Matrix Orbital

evanwright
LCD!
Posts: 14
Joined: Mon May 01, 2006 10:47 am

Post by evanwright »

Hey Raquel,

Well, I have looked at the waveforms over and over and I have finally come up with the following code. It seems to be reading the lcd however it only prints a solid square on the screen. I think I may have the reading sequence correct however maybe my logic on how to read from the lcd buffer is wrong.

I hope you can see what is wrong.

-evan


#include <P18F452.H> //
#include <stdio.h> //
#include <delays.h> //
#include <adc.h> //
#include <i2c.h> //
#include <sw_i2c.h> //
#include <p18cxxx.h>


/***********************************************************************************************************************/
/* FUNCTION PROTOTYPES */
/***********************************************************************************************************************/
void setup(void); //
void command(unsigned char com); // //
void printChar(unsigned char x); //
unsigned char getKey(void); //

/***********************************************************************************************************************/

void main(void) //main program begins here
{
//LCD AND KEYPAD COMMANDS
unsigned char clrscn = 0x58, cursorHome = 0x48, clearKeyBuf = 0x45, wideVertBar = 0x76,
backLightOff = 0x46, cursorBlinkOn = 0x53, pollKeypad = 0x26, narrowVertBar = 0x73,
lineWrapOn = 0x43, cursorBlinkOff = 0x54, repeatModeOff = 0x60, horizontalBar = 0x68,
lineWrapOff = 0x44, cursorLeft = 0x4c, transmitKeyOn = 0x41, largeDigits = 0x6e,
scrollOn = 0x51, cursorRight = 0x4d, transmitkeyOff = 0x4f,
scrollOff = 0x52, cursorOn = 0x4a,
cursorOff = 0x4b;

setup();
command(clrscn);
command(repeatModeOff);
command(transmitkeyOff);

while(1){
printChar(getKey());
}

while(1);

} //end main program

void setup(void)
{
unsigned long x;

DDRCbits.RC3 = 1;
DDRCbits.RC4 = 1;
TRISC = 0xff;
PORTC = 0xff;

SSPSTAT = 0x80;
SSPCON1 = 0x38;
SSPCON2 = 0x00;
SSPADD = 0x09; //(4MHz/100kHz)/4 - 1 = 9

for(x = 0; x < 60000; x++); //wait for lcd to initiate
}

void command(unsigned char com)
{
SSPCON2bits.SEN = 1; //send start bit
while(!PIR1bits.SSPIF); //wait for it to clear
PIR1bits.SSPIF = 0;

SSPBUF = 0x5c; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = 0xfe; //address of register to write to
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = com; //clear screen
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPCON2bits.PEN = 1; //send stop bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it
return;
}


void printChar(unsigned char x)
{
SSPCON2bits.SEN = 1; //send start bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0;

SSPBUF = 0x5c; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPBUF = x; //write the data
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPCON2bits.PEN = 1; //send stop bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it
return;
}

unsigned char getKey(void)
{
unsigned char keyPressed = 0x00;
SSPCON2bits.RCEN = 1;

SSPCON2bits.SEN = 1; //send start bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0;

SSPBUF = 0x5d; //i2c address
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPCON2bits.RCEN = 1;

while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

keyPressed = SSPBUF;

SSPCON2bits.ACKEN = 1;

while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

SSPCON2bits.ACKDT = 1;

SSPCON2bits.PEN = 1; //send stop bit
while(!PIR1bits.SSPIF); //wait for interrupt
PIR1bits.SSPIF = 0; //then clear it

return(keyPressed);
}

Post Reply