I2C on my 18f452.....please help
Hi evan,
Put back the old:
to get rid of the blocks, press keys and see if your receive them. Also note the following suggested changes:
Put back the old:
Code: Select all
do {
kp = getKey();
} while (!kp)
printChar(kp);
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
Design and Development
Matrix Orbital
-
- LCD!
- Posts: 14
- Joined: Mon May 01, 2006 10:47 am
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.
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.
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:
I hope this helps a bit in understanding keypad buffer.
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:
is yes.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 hope this helps a bit in understanding keypad buffer.
Raquel Malinis
Design and Development
Matrix Orbital
Design and Development
Matrix Orbital
-
- LCD!
- Posts: 14
- Joined: Mon May 01, 2006 10:47 am
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.
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
Design and Development
Matrix Orbital
-
- LCD!
- Posts: 14
- Joined: Mon May 01, 2006 10:47 am
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
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
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:
how about putting a check for 0x00, because this will be the value transmitted when there is no key pressed.
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;
Raquel Malinis
Design and Development
Matrix Orbital
Design and Development
Matrix Orbital
-
- LCD!
- Posts: 14
- Joined: Mon May 01, 2006 10:47 am
-
- LCD!
- Posts: 14
- Joined: Mon May 01, 2006 10:47 am
-
- LCD!
- Posts: 14
- Joined: Mon May 01, 2006 10:47 am