andybarrett1
- 3rd January 2015, 23:07
Hi .. Thank you for reading.
I have problems with this code... I have stolen it from elswhere to just read degrees on LCD.
As is it is, displaying Radians and Degrees.
By question is more of query... I am really not sure if my means of deriving the Degrees is correct.
Would welcome any help / advice. ALso as I spin the compass module it apears to count backwards?
'************************************************* ***************
'* Name : COMPASS_818.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 24/08/2014 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
' LCD should be connected as follows:
' LCD PIC
' DB4 PortA.0
' DB5 PortA.1
' DB6 PortA.2
' DB7 PortA.3
' RS PortA.4 (add 4.7K (10k)pullup resistor to 5 volts)
' E PortB.3
' RW Ground
' Vdd 5 volts
' Vss Ground
' Vo 5K potentiometer (or ground)
' DB0-3 No connect
adcon1 = 7 ' Turns Analogs off
DEFINE OSC 12 'Set oscillator in MHz
'OSCCON = $70
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 6
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 7
' -----[ Pins/Constants/Variables ]-------------------------------------------
SDA var portb.1 'transceives to/from SDA
SCL var portb.4 'sends clock pulses
WRITE_DATA CON $3C 'Used to perform a Write operation
READ_DATA CON $3D 'Used to perform a Read operation
MODE CON $02 'Read/Write Register, Selects the operating mode. Default = Single measurement
'Send $3C $02 $00 on power up to change to continuous measurement mode.
X_MSB CON $03 'Read Register, Output of X MSB 8-bit value.
X_LSB CON $04 'Read Register, Output of X LSB 8-bit value.
Z_MSB CON $05 'Read Register, Output of Z MSB 8-bit value.
Z_LSB CON $06 'Read Register, Output of Z LSB 8-bit value.
Y_MSB CON $07 'Read Register, Output of Y MSB 8-bit value.
Y_LSB CON $08 'Read Register, Output of Y LSB 8-bit value.
Cont_Mode CON $0
X VAR byte 'x sensor measured value
Y VAR byte 'y sensor measured value
Z VAR byte 'z sensor measured value
READX VAR WORD
READY VAR WORD
READZ VAR WORD
brads VAR Word 'angle in brads
degr VAR Word 'angle in degrees
Radians VAR Word 'value calculated using X ATN -Y
' -----[ Main Routine ]-------------------------------------------------------
PAUSE 500 ' Give sensor needed power up time
I2Cwrite SDA,SCL,WRITE_DATA, $00, [$70] '(8-average, 15 Hz default, normal measurement)
I2Cwrite SDA,SCL,WRITE_DATA, $01, [$70] 'gain 8 setting
I2CWRITE SDA,SCL,WRITE_DATA,MODE,[Cont_Mode] ' Send continuous output command
DO
GOSUB GetRawReading ' Get RAW Compass reading
'RAW READX,READY,READZ data...
'Lcdout $fe,1 ' Clear LCD screen
'Lcdout "X=",SDEC READX," Y=",SDEC READY
'Lcdout $fe, $C0, "Z=",SDEC READZ
'Calculate Radians
GOSUB GetRadians
'Calculated Heading in Degrees
brads = Radians 'get angle
degr = brads */360 'convert to degrees
Lcdout $fe, 1 ' Clear LCD screen
Lcdout $fe,$80,"Radians =",SDEC Radians
Lcdout $fe,$C0,"Degrees =",SDEC degr
GOSUB GetHeading
LOOP
' -----[ Subroutines ]--------------------------------------------------------
GetRawReading: 'Compass module subroutine
PAUSE 400 'Wait for new data to become available
I2CREAD SDA,SCL,READ_DATA, X_MSB,[READX.HIGHBYTE] 'Read the data starting at x_msb 3
I2CREAD SDA,SCL,READ_DATA ,X_LSB,[READX.lowBYTE]
I2CREAD SDA,SCL,READ_DATA, Z_MSB,[READZ.HIGHBYTE]
I2CREAD SDA,SCL,READ_DATA ,Z_LSB,[READZ.lowBYTE]
I2CREAD SDA,SCL,READ_DATA, Y_MSB,[READY.HIGHBYTE]
I2CREAD SDA,SCL,READ_DATA ,Y_LSB,[READY.lowBYTE]
RETURN
GetRadians:
'The output range of the HMC5883L is -2048 to 2047 and the input range of the ATN function
'is -127 to 127.
x =readx >>4
y =ready >>4
z =readz >>4
'Lcdout $fe, 1 ' Clear LCD screen
'Lcdout "X=",SDEC READX ," Y=",SDEC READY
'Lcdout $fe, $C0, "Z=",SDEC readZ
Radians = x ATN -y
RETURN
end
I have problems with this code... I have stolen it from elswhere to just read degrees on LCD.
As is it is, displaying Radians and Degrees.
By question is more of query... I am really not sure if my means of deriving the Degrees is correct.
Would welcome any help / advice. ALso as I spin the compass module it apears to count backwards?
'************************************************* ***************
'* Name : COMPASS_818.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 24/08/2014 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
' LCD should be connected as follows:
' LCD PIC
' DB4 PortA.0
' DB5 PortA.1
' DB6 PortA.2
' DB7 PortA.3
' RS PortA.4 (add 4.7K (10k)pullup resistor to 5 volts)
' E PortB.3
' RW Ground
' Vdd 5 volts
' Vss Ground
' Vo 5K potentiometer (or ground)
' DB0-3 No connect
adcon1 = 7 ' Turns Analogs off
DEFINE OSC 12 'Set oscillator in MHz
'OSCCON = $70
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 6
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 7
' -----[ Pins/Constants/Variables ]-------------------------------------------
SDA var portb.1 'transceives to/from SDA
SCL var portb.4 'sends clock pulses
WRITE_DATA CON $3C 'Used to perform a Write operation
READ_DATA CON $3D 'Used to perform a Read operation
MODE CON $02 'Read/Write Register, Selects the operating mode. Default = Single measurement
'Send $3C $02 $00 on power up to change to continuous measurement mode.
X_MSB CON $03 'Read Register, Output of X MSB 8-bit value.
X_LSB CON $04 'Read Register, Output of X LSB 8-bit value.
Z_MSB CON $05 'Read Register, Output of Z MSB 8-bit value.
Z_LSB CON $06 'Read Register, Output of Z LSB 8-bit value.
Y_MSB CON $07 'Read Register, Output of Y MSB 8-bit value.
Y_LSB CON $08 'Read Register, Output of Y LSB 8-bit value.
Cont_Mode CON $0
X VAR byte 'x sensor measured value
Y VAR byte 'y sensor measured value
Z VAR byte 'z sensor measured value
READX VAR WORD
READY VAR WORD
READZ VAR WORD
brads VAR Word 'angle in brads
degr VAR Word 'angle in degrees
Radians VAR Word 'value calculated using X ATN -Y
' -----[ Main Routine ]-------------------------------------------------------
PAUSE 500 ' Give sensor needed power up time
I2Cwrite SDA,SCL,WRITE_DATA, $00, [$70] '(8-average, 15 Hz default, normal measurement)
I2Cwrite SDA,SCL,WRITE_DATA, $01, [$70] 'gain 8 setting
I2CWRITE SDA,SCL,WRITE_DATA,MODE,[Cont_Mode] ' Send continuous output command
DO
GOSUB GetRawReading ' Get RAW Compass reading
'RAW READX,READY,READZ data...
'Lcdout $fe,1 ' Clear LCD screen
'Lcdout "X=",SDEC READX," Y=",SDEC READY
'Lcdout $fe, $C0, "Z=",SDEC READZ
'Calculate Radians
GOSUB GetRadians
'Calculated Heading in Degrees
brads = Radians 'get angle
degr = brads */360 'convert to degrees
Lcdout $fe, 1 ' Clear LCD screen
Lcdout $fe,$80,"Radians =",SDEC Radians
Lcdout $fe,$C0,"Degrees =",SDEC degr
GOSUB GetHeading
LOOP
' -----[ Subroutines ]--------------------------------------------------------
GetRawReading: 'Compass module subroutine
PAUSE 400 'Wait for new data to become available
I2CREAD SDA,SCL,READ_DATA, X_MSB,[READX.HIGHBYTE] 'Read the data starting at x_msb 3
I2CREAD SDA,SCL,READ_DATA ,X_LSB,[READX.lowBYTE]
I2CREAD SDA,SCL,READ_DATA, Z_MSB,[READZ.HIGHBYTE]
I2CREAD SDA,SCL,READ_DATA ,Z_LSB,[READZ.lowBYTE]
I2CREAD SDA,SCL,READ_DATA, Y_MSB,[READY.HIGHBYTE]
I2CREAD SDA,SCL,READ_DATA ,Y_LSB,[READY.lowBYTE]
RETURN
GetRadians:
'The output range of the HMC5883L is -2048 to 2047 and the input range of the ATN function
'is -127 to 127.
x =readx >>4
y =ready >>4
z =readz >>4
'Lcdout $fe, 1 ' Clear LCD screen
'Lcdout "X=",SDEC READX ," Y=",SDEC READY
'Lcdout $fe, $C0, "Z=",SDEC readZ
Radians = x ATN -y
RETURN
end