magnetometer with 28x2

Code:
'''    mag_quad_1_28x2_2.bas
''''   prgm simulates printing out 
''''    sample readings from  compass
''''  chip HMC5883L
''''    convert 2s complement display
#picaxe  28x2
#com /dev/tty.usbserial-00001004
#terminal 9600

symbol x_lin =w0
symbol x_lin_lsb = b0
symbol x_lin_msb = b1

symbol y_lin =w1
symbol y_lin_lsb = b2
symbol y_lin_msb = b3

symbol z_lin =w2
symbol z_lin_lsb = b4
symbol z_lin_msb = b5

symbol  loop_m = b23
symbol loc_pp = b24
symbol pass_x = b17
symbol angl_1  = w3
symbol end_angl = w4
symbol quot_x = w5
symbol m = b18
symbol p = b19
symbol  x_sign  = b20
symbol  y_sign = b21
symbol quad_val = b22
symbol x_tmp = w6
symbol y_tmp = w7

'''loc_pp = 100     '''''   INIT  POKE  LOC
pass_x = 1
m = 0
p = 1
'''******************************
serout c.0 ,n2400,(254,1)
pause 500


serout c.0, n2400, ( " HELLO   WORLD" )    
    wait 3

'''''do 
'''''loop
hi2csetup i2cmaster ,$3c, i2cslow, i2cbyte   '''' INIT  I2C
pause 500

hi2cout $00,($18)
pause 500
hi2cout $01, ($00)
pause 500

''''****************************************
main:
 for loop_m = 1 to 4
serout c.0, n2400, (254,1)
pause 500
serout c.0,n2400,("loop = ",#loop_m)
wait 2

hi2cout $02,($01)    ''''  request single measurement
pause 500
hi2cin $03,(x_lin_msb,x_lin_lsb,z_lin_msb,z_lin_lsb,y_lin_msb,y_lin_lsb)
pause 500
         
          if x_lin_msb > 127 then
    '''  number is negative
    x_sign = m
    x_lin = x_lin xor 0xffff
    inc x_lin
    serout c.0 ,n2400,(254,1)
pause 500


serout c.0, n2400, ( " x  =  -",#x_lin)    
    pause 500
    
    
else 
    x_sign = p
    serout c.0 ,n2400,(254,1)
    pause 500
    serout c.0, n2400, ( " x  = +",#x_lin)    
    pause 500
    
    endif

wait 1
if y_lin_msb > 127 then
    '''  number is negative
    y_sign = m
    y_lin = y_lin xor 0xffff
    inc y_lin
    serout c.0 ,n2400,(254,192)
pause 500

serout c.0, n2400, ( " y  =  -",#y_lin)    
    pause 500
    

else 
    y_sign = p
    serout c.0 ,n2400,(254,192)
    pause 500
    serout c.0, n2400, ( "  y  = +",#y_lin)    
    pause 500
    

endif
wait 1
inc pass_x
serout c.0 ,n2400,(254,1)
    pause 500
    serout c.0, n2400, ( " end_pass ",#pass_x)    
    pause 500
    wait 2
serout c.0,n2400,(254,192)
pause 500
serout c.0,n2400,("quad = ",#x_sign,#y_sign)
wait 2


next
'''  pass_x = 4
'''   x and y positive
'''  I can use WORDS x_lin and y_lin
'''   for computations

pause 500

''' start processing x_lin and y_lin
'''************************
wait  1
''''  ***********************
''''   CALCULATE   QUAD_VAL
''''**************************
if x_sign = p then
    if y_sign = p then
        quad_val = 1
    else
        quad_val = 4
    endif
endif
if x_sign = m then
    if y_sign = m then
        quad_val = 3
    else 
        quad_val = 2
    endif
endif
'''' ****************************        
if x_lin > y_lin then
    y_tmp =y_lin
    y_tmp = y_tmp * 100
    quot_x = y_tmp/ x_lin
angl_1 = atan quot_x
pause 500
else 
    x_tmp = x_lin
    x_tmp = x_tmp  * 100
    quot_x  = x_tmp / y_lin
    angl_1 = atan quot_x
    pause 500
endif
    ''serout c.0,n2400,(254,1)
    ''pause 500
    '''serout c.0,n2400,("angle is ",#angl_1)
    ''''pause 500
    ''''   ***************
    ''''  DISPLAY FULL  ANGLE
    '''*************************
      select case quad_val
      case   1
          if y_lin > x_lin  then
              
              angl_1 = 90 - angl_1
          endif
      case  2
          if x_lin > y_lin then 
          angl_1 = 90 - angl_1 
          endif 
          angl_1  = angl_1  + 90
      case  3
          if y_lin > x_lin then
              angl_1 = 90 - angl_1
          endif
          angl_1 = angl_1 + 180
      case 4
          if x_lin > y_lin then
              angl_1 = 90 - angl_1
          endif
          angl_1 = angl_1 +270
      end select
      
      wait  3
      
    serout c.0 ,n2400,(254,1)
    pause 500
    serout c.0, n2400, ( " angle is  ", #angl_1)    
    pause 500
    

stop
beep_2:
high c.1
pause 500
low c.1
pause 500
high c.1
pause 500
low c.1
pause 500

return
''''''''  *********
beep_1:
high c.1
pause 500
low c.1
pause 500
return
 
Last edited by a moderator:

westaust55

Moderator
HMC5883L 3-axis compass

For those not familiar with the HMC5883L, it is a 3-axis compass module.

@larryatheist,
Well done and thanks for posting - for furture reference by all.

For your future reference, the 2-compliment calculation (albeit the same as what I was using many years earlier),
can be reduced from:
Code:
    x_lin = x_lin xor 0xffff
    inc x_lin_adj
to:
Code:
    x_lin = -x_lin
which also happens to save ~4 bytes at each instance
 
Top