BS2p driving I2C display problem



  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited February 14 Vote Up0Vote Down
    Notice for characters it is sending the data out to LCDD (LCD Data Register) whereas commands are being sent out to LCDI (LCD Instruction Register). This is using 8-bit parallel mode on a Hitachi display, however the point is how the main code handles things, which I forgot to include. If you notice, the HL register pair is the address reference, so before I call PRINT I do something like this:

    KDATA is a label and .byte is the equivalent of the BS2 DATA statement. The $00 is a null terminator. The $0C clears the screen, while the $07 beeps a piezo speaker. When I call (GOSUB) to KNIGHT, it loads the HL register pair with the address of the data at KDATA and then starts printing one character at a time until it hits the $00.

    You can make a subroutine to handle all this in PBASIC so that for a string you need only pass the address of the DATA block to it and the subroutine will send all the characters until it hits the NULL (0). With planning it could handle special characters as my routine did.
    Chris Savage | Engineering Tech | Main Office: (916) 624-8333 | Direct to Tech Support: (888) 997-8267 | Website | Twitter | Google+
  • kwinn wrote: »
    john_s wrote: »
    One more thing - will I be able to send "Hello world" as a single string when using I2COUT or do I have to brake it and send char after character instead?

    You can send a single string like "Hello world" using the string formatter. See page 232 of the "BASIC Stamp Syntax and Reference Manual Version 2.2"

    Actually in this case that won't work. If you create a subroutine to handle to individual writes, you can also create a string subroutine to take a zero-delimited string and send it one character at a time calling the data subroutine.

    Ah yes, forgot that this was going through the I2C to parallel interface to get to the LCD.
    Thanks for catching my mistake.
    In science there is no authority. There is only experiment.
    Life is unpredictable. Eat dessert first.
  • Here is a sample code I cobbled together that puts the I2C transfer operation into a subroutine. This code is by no means complete but you should be able to utilize some of it for your project. The compiler says the syntax is good, however, there may be a flaw in the logic which may crop up when uploading to your stamp. You may have to add some delay after the RETURN if the datasheet for the LCD requires it, such as after a clear display operation.
    ' PCF8574 I2C LCD test
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    sda     PIN 0
    slave   CON $27  ' or whatever your device address is
    char    VAR Byte  ' holds what you want to send
    upper   VAR char.HIGHNIB
    lower   VAR char.LOWNIB
    buffer  VAR Byte
    val     VAR buffer.HIGHNIB
    brs     VAR buffer.BIT0
    brw     VAR buffer.BIT1
    bcs     VAR buffer.BIT2
    bbl     VAR buffer.BIT3
    rs      VAR Bit   ' you set these three
    rw      VAR Bit   '
    bl      VAR Bit   '
    ' Your main program will need to place into variable 'char' the byte
    ' you want to transfer to the LCD. You also need to set up 3 additional
    ' bit variables as follows:
    ' rs = 0 for an instruction  ( clear the display, cursor movement, etc)
    ' rs = 1 for data transfer (stuff to display)
    ' rw = 0 for writing to LCD
    ' rw = 1 for reading from LCD
    ' bl = 0 for backlight OFF
    ' bl = 1 for backlight ON
    'Once these four variables have been set up then call the transfer
    ' subroutine to transfer the byte to the LCD. The subroutine will
    ' return to the following instruction after the call.
    ' do your thing here like the following...
      char = $31
      rs = 1
      rw = 0
      bl = 1
      GOSUB send2
    ' do more of your thing here
      GOTO main
      val = upper
      brs = rs
      brw = rw
      bcs = 0     ' number 0
      bbl = bl
      I2COUT sda, slave, [buffer]
      bcs = 1
      I2COUT sda, slave, [buffer]
      val = lower
      bcs = 0
      I2COUT sda, slave, [buffer]
      bcs = 1
      I2COUT sda, slave, [buffer]    ' LCD now has received the value you placed in 'char'
    Florida, between St. Petersburg and the Gulf of Mexico

    Do not look directly into laser with remaining good eye...
Sign In or Register to comment.