News:

;) This forum is the property of Proton software developers

Main Menu

Yet another I2C Query

Started by streborc, Today at 05:41 PM

Previous topic - Next topic

streborc

I am currently porting code to Positron8 that I originally composed in Pic Basic Pro.  The application includes writing to an I2C LCD.  A portion of the program includes a very simple loop counter where I send the loop count to the LCD.  PBP requires the loop counter variable be converted to ASCII using its ArrayWrite command.  So (with y as the loop counter and y_array as the ASCII translation) the simple set of instructions is:

y = y + 1                               
ARRAYWRITE y_array, [DEC2 y]           
I2CWRITE SDA_LCD, SCK_LCD, LCDA, [CHDR, $A0, DHDR, "SYNC#:", STR y_array\2," "]       

The ArrayWrite command converts the variable y, which could be 73 for instance, into ASCII representations of 7 (0x37) and 3 (0x33) and the I2CWrite command sends the characters "7" and "3" to the LCD with "STR y_array\2".

Positron's I2COut command context is:

I2COut SDA_Pin, SCL_Pin, Control, {Address}, [Output Data]

"Output Data" is described in the manual as a list of variables, constants expressions and modifiers that informs I2COut how to format outgoing data.  I2COut can transmit individual or repeating bytes, convert values into decimal, hexadecimal or binary text representations, or transmit strings of bytes from variable arrays.

I've edited the PBP command to:

I2COut SDA_LCD, SCK_LCD, LCDA, [CHDR, $A0, DHDR, "SYNC#:", Dec2 y," "]

thinking that "convert values into decimal" means that my y value of 73 is going to appear on the LCD as "73".  Of course, it doesn't compile and the error message tells me Dec2 isn't allowed in the I2COut command.  As a check, I can substitute the hex values $37 and $33 in place of "Dec2 y", and the ASCII characters "73" do appear.

Obviously I can take y's value, parse it into two hex values and do a lookup to ASCII, but what is it I'm misinterpreting in the I2COut's description "convert values into decimal, hexadecimal or binary text"?  Starting with a y value of 73, how is a conversion done and what is the conversion result?

streborc

In answering my own inquiry, I offer the following:

Dim y_array as String*2

y = y + 1
y_array = Str$(Dec2 y)
I2COut SDA_LCD, SCK_LCD, LCDA, [CHDR, $A0, DHDR, "SYNC#:", Str y_array\2," "]

This produces the increasing count on the LCD in the same manner as with PBP.  Nevertheless, I welcome comments on the sanity of this solution.

top204

With String variables, a null terminates it, so if transmiting or sending a String variable, it will automatically terminate. So there is no need for the Str modifier, or the amount of elements from it.

For example:
    Dim MyString As String * 10
    Dim Bytein As Byte = 23

    MyString = Str$(Dec2, Bytein)           ' Convert the integer into a 2 digit ASCII value in MyString
    I2COut SDA_LCD_Pin, SCK_LCD_Pin, LCDA, [CHDR, $A0, DHDR, "SYNC#:", MyString]

MyString will be sent, until the null is reached within it. Which is after the 2 digit ASCII decimal. i.e. "23", 0

If the relevant assembler code is examined (Press the F2 button), it shows that it is sending the contents of MyString:

    lfsr 0,MyString              <------- Place the address of MyString into FSR0L\H
_pblb__2
    movf INDF0,W,0               <------- Read an element of MyString into WREG
    rcall i2cout__no_stop
    movf POSTINC0,W,0            <------- Read an element of MyString into WREG with auto address increment
    bnz _pblb__2                 <--------- Continue sending until a 0 value is found. i.e Its null
    rcall i2cout__with_stop

Regards
Les

RGV250

Hi,
Why do you need to send the value as a string, is this something the display need?
Surely you can just send Dec y, you do not need the 2 as that is for decimal places and you are sending an integer.
Also INC y is neater than y = y + 1

Regards,
Bob

top204

Bob... The Dec, Hex, Bin, Str$ etc, modifiers are not supported in the I2C commands, because of the complication of having to send a Stop with the last byte.

So converting it to a String is the easiest. Or using, or adapting, one of the I2C LCD code listings that are on the forum's Wiki, that I created to replace the compiler's Print, Cls, and Cursor commands. Or the many others, that the good users of this forum have created over the years.

LCD Text Wiki Section

Regards
Les