LCD03 on I2C; problem receiving data from keyboard with I2C

I2C, Serial, Wireless etc.

Moderator: chris

LCD03 on I2C; problem receiving data from keyboard with I2C

Postby hvneck » Sun Mar 08, 2009 11:54 pm

Hello,

I've purchased 2 LCD03 displays. They both work great when displaying data with I2C.
When i try to receive data from the keyboard however, i have to get 2 bytes, 1 from register 1 and 1 from register 2
They also (almost) present the right bits, eg. when pressing '5' the fifth bit is set of the low byte; perfectly correct!!!
So far so good, but...
On both bytes i receive (the high byte on reg 2 and the low byte on reg 1) the Most Significant Bit is set: "10000000" and "10000000".
This is even the case when there is no key pressed at all.
This way the '8'-key gets lost because this IS the MSB of the low byte which is (in my opinion incorrectly) always set.

Am i doing something wrong? or is there another explanation or workaround?

I'm using Crownhill Proton+ PicBasic with an 16f628; i've terminated the I2C bus with two 2k2 resistors.
The keyboard is the one you sell on the website.
Both displays give exactly the same results......

Code: Select all
DEVICE 16F628A
CONFIG INTRC_OSC_NOCLKOUT, MCLRE_ON, LVP_OFF, WDT_OFF, PWRTE_ON, CP_OFF, BODEN_OFF
;DECLARE optimiser_level = 6

ALL_DIGITAL TRUE                        ; All inputs digital
CLEAR
DELAYMS 500

DIM KeyLow  AS BYTE
DIM Integer  AS BYTE
DIM IntegerStr[9] AS BYTE               ; Create a byte array to hold converted value, and NULL terminator     

SYMBOL I2Caddr = $C6                    ; Targetaddress of the display
SYMBOL SDA = PORTB.2                    ; Alias the SDA (Data) line
SYMBOL SCL = PORTB.1                    ; Alias the SSL (Clock) line

WHILE 1 = 1
  ; Just the low byte; the high byte has exactly the same problem
  I2CIN SDA, SCL, I2Caddr, 1, [KeyLow]
  ; BUSIN I2Caddr,2,[KeyLow]            ; !!!!! tried this as well !!!!!!
  Integer = KeyLow
  STRN IntegerStr = STR$(BIN Integer )  ; Convert the Integer to a STRING     
  I2COUT SDA, SCL, I2Caddr, 0, [ 3, 4, 1,  STR IntegerStr \9]   ; Cursor to line 3 pos 1 and print result
WEND

END



I'm really hoping you have an answer to this!!!

Many thanks in advance,

Hans van Neck
hvneck
 
Posts: 3
Joined: Sun Mar 08, 2009 10:59 pm

Re: LCD03 on I2C; problem receiving data from keyboard with I2C

Postby chris » Mon Mar 09, 2009 11:03 am

Hi Hans,

This sounds like the Proton+ PicBasic compiler you are using is not fully implementing the I2C protocol correctly, specifically the bus hold feature. When data bytes are clocked in or out of a slave module the master is supposed to check the clock line state at the end of each byte to see if it has been released by the slave, the clock line being held low by the slave indicates it is still processing.
Therefore the master tries to clock data out of the slave before it is ready and clocks a 1 at the start of the byte before the slave has got round to initialising the buffer contents.
User avatar
chris
 
Posts: 172
Joined: Wed Nov 08, 2006 3:13 pm
Location: Norfolk, England

Re: LCD03 on I2C; problem receiving data from keyboard with I2C

Postby hvneck » Mon Mar 09, 2009 7:10 pm

Chris,

Thank you very much, sounds plausible!
I'm going to try and make some assembly-routines for reading the I2C bus then.

Writing to the display works really perfectly anyway!!!!!

When i've got things working, i'll let you know. But i'm afraid this will not be until after the weekend.

Thanks again,

Hans
hvneck
 
Posts: 3
Joined: Sun Mar 08, 2009 10:59 pm

Re: LCD03 on I2C; problem receiving data from keyboard with I2C

Postby hvneck » Sat Mar 21, 2009 9:11 pm

Hi Chris,

I've got it working allright!

The lowlevel i2c routines from Proton PicBasic do the job very well.

Code: Select all
SYMBOL LCD03_I2Caddr = $C6              ; Targetaddress of the display

DIM KeyValue     AS WORD

ReadKeypad:
  DIM KeyLow   AS BYTE
  DIM KeyHigh  AS BYTE
  ; This can only be done the lowlevel manner. I2CIN is officialy not supported for
  ; software i2c (which is the only possibility on a 16F628; although while it seems
  ; to work with I2CIN, there is a bug; the highest byte read will always be '1'
 
  ; read keyboard low byte
  BSTART                                ' Send a START condition
  BUSOUT LCD03_I2Caddr                  ' Send i2c write address of display
  BUSOUT 1                              ' Send register to read from (low byte)
  BRESTART                              ' Send a RESTART condition
  BUSOUT LCD03_I2Caddr + 1              ' Send i2c read address of display
  BUSIN KeyLow                          ' Load byte received
  BSTOP                                 ' Send a STOP condition
  ; read keyboard high byte
  BSTART                                ' Send a START condition
  BUSOUT LCD03_I2Caddr                  ' Send i2c write address of display
  BUSOUT 2                              ' Send register to read from (high byte)
  BRESTART                              ' Send a RESTART condition
  BUSOUT LCD03_I2Caddr + 1              ' Send i2c read address of display
  BUSIN KeyHigh                         ' Load byte received
  BSTOP                                 ' Send a STOP condition
  ; combine the two bytes to a word
  KeyValue = (KeyHigh << 8) | KeyLow
RETURN


I've got a snippet for MicroC as well:
Code: Select all
unsigned short lcdReadLowByte(void) {
  unsigned short readByte = 0;
  Soft_I2C_Start();
  Soft_I2C_Write(0xC6);      // I2C write address of the LCD03 display
  Soft_I2C_Write(1);         // register 1 (read low byte) of display
  Soft_I2C_Start();          // I2C restart
  Soft_I2C_Write(0xC7);      // I2C read address of the LCD03 display
  readByte = Soft_I2C_Read(0);
  Soft_I2C_Stop();
  return readByte;
}


If you have any comments on this code, i would be pleased to know.
I can send you the complete PicBasic demo program if you like. Then you can put it in the Example page of your website if you want.

Thanks for the support!

With kind regards,

Hans
hvneck
 
Posts: 3
Joined: Sun Mar 08, 2009 10:59 pm


Return to Communication devices

Who is online

Users browsing this forum: TurnitinBot [Bot] and 1 guest