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

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

Moderators: Henry, Mods

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

Post by Raquel »

Hi evan,

I have a quick answer about the solid square (though I haven't looked at your code). A solid square very much likely means a 0x00. You are probably sending a printChar(0x00);

I'll look at the code now.
Raquel Malinis
Design and Development
Matrix Orbital

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

Post by Raquel »

Hi evan,

Put back the old:

Code: Select all

do {
 kp = getKey();
} while (!kp)
printChar(kp);
to get rid of the blocks, press keys and see if your receive them. Also note the following suggested changes:

Code: Select all

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

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

keyPressed = SSPBUF;

SSPCON2bits.ACKDT = 1;

SSPCON2bits.ACKEN = 1;
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(keyPressed);
}
Raquel Malinis
Design and Development
Matrix Orbital

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

Post by evanwright »

Raquel,

Just a quick question.

What is the poll keypad command? Is it not necessary for the way I am trying to read the keypad?

I wont be able to try out your suggestions untill later but I will let you know if that helps.

Also, I reallized that the block means there is probably nothing in the buffer so it just returns 0x00 but if I press a key shouldnt that go into the buffer and be read by the next getKey?

I am so confused, however I think I am very close. Thankyou.

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

Post by Raquel »

Hi evan,

The poll keypad command is for RS232/TTL communication. This feature is offered to hosts that do not want the module to automatically send the keypresses. These hosts needs to send the Turn Auto Transmit off command. And for these hosts to retrieve the keypresses, they send the poll keypad command.

With I2C, the keys are of course polled (master has to initiate the communication). It is not ncessary for the host to tell the module to turn auto transmit off because trasnmitting the key does not take the key out of the buffer. When the module realizes that the master is addressing it with a read (the last bit is 1, this is why 0x5D), it automatically transmits the oldest key in the buffer, then when it is once again addressed, it transmits the next key. When there is no key or if the buffer is empty, it transmits 0x00. And so the answer to your question:
Also, I reallized that the block means there is probably nothing in the buffer so it just returns 0x00 but if I press a key shouldnt that go into the buffer and be read by the next getKey?
is yes.

I hope this helps a bit in understanding keypad buffer.
Raquel Malinis
Design and Development
Matrix Orbital

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

Post by evanwright »

Hey Raquel,
I tried out your code suggestions and Im still not getting the expected results. Do you have any more ideas?

Do you have any simular examples that might help me?

Thanks,
-evan

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

Post by Raquel »

Hi evan,

Can you tell me how far in the code you get? Do you still see blocks displayed on the module? Any of the keys you press get transmitted? If you can, please insert debug code to see which part of the code is actually reached and stepped through, and which is not reached.

I'll try to find an example source code that you can try as well.
Raquel Malinis
Design and Development
Matrix Orbital

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

Post by evanwright »

This is my latest code:

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


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

printChar('T');
printChar('e');
printChar('s');
printChar('t');
printChar(':');

printChar('n');

loop:
kp = getKey();
if(kp == 0xff){
goto loop;
}
printChar(kp);
goto loop;

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

} //end main program



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.ACKDT = 1;

SSPCON2bits.ACKEN = 1;
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(keyPressed);
}


Note: If I uncomment your do while statement the following results are the same. If I just assign a value to kp instead of kp = getKey(); that value will be continuously displayed across the screen.

For example:

kp = 'k';

test:nkkkkkkkkkkkkkk
kkkkkkkkkkkkkkkkkkk
kkkkkkkkkkkkkkkkkkk
kkkkkkkkkkkkkkkkkkk

otherwise:

kp = getKey();


test:n

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

Post by Raquel »

Hi evan,

Please note that getKey() has SSPCON2bits.RCEN = 1 at the beginning of the function, this is not in the code that I suggested to try.

For this loop:

Code: Select all

loop:
kp = getKey();
if(kp == 0xff){
goto loop;
}
printChar(kp);
goto loop; 
how about putting a check for 0x00, because this will be the value transmitted when there is no key pressed.
Raquel Malinis
Design and Development
Matrix Orbital

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

Post by evanwright »

I believe that either I am incorrectly reading the lcd and always reading the value 0xff. Or for some reason the only values in the keypad buffer are 0xff.

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

Post by Raquel »

Hi evan,

I hope that you caught the my last post. I think you posted at the same time. You probably get 0xFF because of wrong time of reading SSPBUF.
Raquel Malinis
Design and Development
Matrix Orbital

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

Post by evanwright »

Hey Raquel,
I dont mean to sound cheesy but, you are my hero!

Thankyou so much. It is working perfectly now.

You are a very smart guy.

Thanks again,

-evan

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

Post by Raquel »

Hi evan,

I am glad that I've been of help.
And btw, I am a gal!
Raquel Malinis
Design and Development
Matrix Orbital

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

Post by evanwright »

Please excuse my ignorance.

But, you are a very smart girl!

Thanks again,
-evan

Post Reply