HMC5883L kompas

Rasprava o PIC mikrokontrolerima, PIC projekti i drugo vezano za PIC-eve...

Moderators: pedja089, stojke369, [eDo], trax

Post Reply
febo26
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 207
Joined: 12-08-2008, 19:58

HMC5883L kompas

Post by febo26 »

Stigao mi ovaj modul s Ebaya i uspio sam naci primjer za njega ali je za BASICSTAMP 2.

U biti primjer radi na PIC18f452 chipu s 20mhz kristalom.
Mogu ocitati raw vrijednosti s njega ali nemogu izracunati kut.
Dio koda koji racuna kut ne funkcionira i nije mi uopce jasan o cemu se tu radi pa ako neko ima volje da baci malo oko na to i pojasni stvar.

Evo koda:

Code: Select all

' ============================================================================
' HMC5883L_Demo.bs2 - This is a Demo program for the HMC5883L Compass
' Module (PN: 29133). It displays the compass heading north, east, south and west
' clockwise from north.
'
' Author.... (C) John A. Williams
'
' {$STAMP BS2p}
' {$PBASIC 2.5}
' ============================================================================
     define OSC 20

'KONFIGURACIJA LCD DISPLEJA 4X20

        DEFINE  LCD_DREG PORTB
       	DEFINE  LCD_DBIT 0
    	DEFINE  LCD_BITS 4
    	DEFINE  LCD_RSREG PORTB
    	DEFINE  LCD_RSBIT 4
    	DEFINE  LCD_EREG PORTB
    	DEFINE  LCD_EBIT  5
    	DEFINE  LCD_LINES 2
      	DEFINE  LCD_COMMANDUS 2000
    	DEFINE  LCD_DATAUS 40
 'SET UP PORTS
TRISA = %00111111    'port a je cijeli ulazni
TRISB = %00000000
TRISC = %11111111
TRISD = %00000000
TRISE = %1111
ADCON1 = 7	   ' PORTA i PORTE su digitalni  ISKLJUCI KOMPARATORE I ANALOGNE ULAZE
' -----[ Pins/Constants/Variables ]-------------------------------------------
SDA            var      PORTC.4                   'P0 transceives to/from SDA
SCL            VAR      PORTC.3                   'P1 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
SCALEFACTOR    CON     4081 'SCALEFACTOR = 65536 * (255/4095) = 4081
SCALEOFFSET    CON     2048 'The output range of the HMC5883L is -2048 to 2047; therefore to get a 0 (LOWRANGE)to 4095 range
                            'to 4095 (HIGHRANGE) range 2048 must be added to the sensor value.
ATNFACTOR      CON     127  'the ATN function input range is -127 TO 127. In order to get the positive scaled range of
                            '0 to 255 into the range of -127 to 127, 127 must be subtracted.
LOWRANGE       CON     0    'see SCALEOFFSET
HIGHRANGE      CON     4095 'see SCALEOFFSET

X              VAR      Word 'x sensor measured value
Y              VAR      Word 'y sensor measured value
Z              VAR      Word 'z sensor measured value
brads          VAR      Word 'angle in brads
degr           VAR      Word 'angle in degrees
azimuth        VAR      Word 'value calculated using X ATN -Y
  degr=0
 PAUSE 500
  LCDOUT $FE,$01'BRISI DISPLAY
  LCDOUT $FE,$01,"PROBA"
  PAUSE 500
  LCDOUT $FE,$01'BRISI DISPLAY
' -----[ Main Routine ]-------------------------------------------------------

PAUSE 10                                     ' Give sensor needed power up time

  I2CWRITE SDA,SCL,WRITE_DATA,MODE, [cont_Mode]  ' Send continuous output command

main:
    LCDOUT $FE,$01'BRISI DISPLAY
  GOSUB GetRawReading                       ' Get RAW Compass reading

  'RAW X,Y,Z data...
 
  'lCDOUT $FE,$80,   "X= " ,SDEC x
  'LCDOUT $FE,$80+7,"Y= ", SDEC y
 ' LCDOUT $FE,$C0,   "Z= ", SDEC z        ' Print angle


  'Calculate Azimuth

  GOSUB GetAzimuth

  brads = azimuth     'get angle
  degr = brads */ 361 'convert to degrees
 
  LCDOUT $FE,$80,"Degree " , dec degr
 
  pause 500
goto main

' -----[ Subroutines ]--------------------------------------------------------

GetRawReading:                                 'Compass module subroutine


    PAUSE 400                                  'Wait for new data to become available


    I2CREAD SDA,SCL, READ_DATA, X_MSB, [X.HIGHBYTE]  'Read the data starting at x_msb 3
    I2CREAD  SDA,SCL, READ_DATA ,X_LSB, [X.LOWBYTE]

   I2CREAD  SDA,SCL, READ_DATA, Z_MSB, [Z.HIGHBYTE]
   I2CREAD SDA,SCL, READ_DATA ,Z_LSB, [Z.LOWBYTE]
   
    I2CREAD SDA,SCL, READ_DATA, Y_MSB, [Y.HIGHBYTE]
    I2CREAD  SDA,SCL, READ_DATA ,Y_LSB, [Y.LOWBYTE]

RETURN

GetAzimuth:
'The output range of the HMC5883L is -2048 to 2047 and the input range of the ATN function
'is -127 to 127.  I used the method to scale the output to the input discribed in Parallax's Smart
'Sensors Student Guide v1 starting on page 76.
'SCALEFACTOR = 65536 * (255/4095) = 4081
'SCALEOFFSET = 2048
'ATNFACTOR = 127
'LOWRANGE = 0
'HIGRANGE = 4095 (2048 + 2047 = 4095)

    x = x + SCALEOFFSET
   ' x = (x MIN LOWRANGE MAX HIGHRANGE) ** SCALEFACTOR - ATNFACTOR

    y = y + SCALEOFFSET
   ' y = (y MIN LOWRANGE MAX HIGHRANGE) ** SCALEFACTOR - ATNFACTOR


    azimuth = x ATN -y
   LCDOUT $FE,$c0,"azimuth " ,  DEC azimuth
 
RETURN

kad u labeli get azimuth izbacim ova dva reda onda dobijem neke kuteve kako okrecem modul ali nisu ni priblizno tocni.
o cemu se radi u ta dva reda ili jos nesto fali u njime neznam

x = (x MIN LOWRANGE MAX HIGHRANGE) ** SCALEFACTOR - ATNFACTOR


y = (y MIN LOWRANGE MAX HIGHRANGE) ** SCALEFACTOR - ATNFACTOR
Post Reply