'''''''''''''''''''''''''''''''''''''''''''''''''''''
'            CMPS03 I2C with serial LCD03           '
'              example in PicBasic PRO              '
'            Reads a rotation as 0 - 255            ' 
'              By James Henderson 2009              '
'''''''''''''''''''''''''''''''''''''''''''''''''''''

DEFINE OSC 8                                        ' 8mhz oscillator
DEFINE I2C_SLOW 1
DEFINE I2C_HOLD 1
DEFINE CHAR_PACING 102                              ' 1 stop bit time to make 2 for LCD03 

ver VAR BYTE                                        ' To store software revision
w0 VAR word                                         ' to store bearing data
b0 VAR w0.byte0                                     ' b0 is the low byte of word w0
b1 VAR w0.byte1                                     ' b1 is the high byte of word w0
b2 var byte                                         ' Byte for storing decimal point of bearing

OUTPUT PORTC.1                                      ' RC1 tristate to output for lcd03
     
SEROUT PORTC.1,2,[12,4,1]                           ' Clear screen and hide cursor of LCD03
pause 1000

loop:
    I2CREAD PORTC.4,PORTC.3,$C0,0,[ver]             ' Read version number from CMPS03
    SEROUT PORTC.1,2,[1,"Software v: ",#ver,13]     ' Put it on the screen
    pause 100
    I2CREAD PORTC.4,PORTC.3,$C0,2,[b1,b0]           ' Get the bearing as a value 0 - 3600
    b2 = w0//10                                     ' Get the remainder of range this gives us our one decimal place of the range value
    w0 = w0/10                                      ' Convert w0 into 0 - 360 range data
    SEROUT PORTC.1,2,["bearing: ",#w0,".",#b2,"   "]' Display bearing data to the screen
goto loop     
