[rasp.] 지자기 센서(Digital Compass) HMC5883L

지자기 센서 예제

그 활용은 알아서~

 

import smbus

import time

from ctypes import c_short

 

bus = smbus.SMBus(1)

 

DEV_ADDR = 0x1E # for compass

 

# identification chip

 

id1 = bus.read_byte_data(DEV_ADDR,10)

id2 = bus.read_byte_data(DEV_ADDR,11)

id3 = bus.read_byte_data(DEV_ADDR,12)

 

print "ID 1 : ", hex(id1)

print "ID 2 : ", hex(id2)

print "ID 3 : ", hex(id3)

 

#CRA Registor Setting

bus.write_byte_data(DEV_ADDR,0,0×70)

#CRB Registor Setting

bus.write_byte_data(DEV_ADDR,1,0xA0)

#MODE Registor Setting

bus.write_byte_data(DEV_ADDR,2,0×00)

 

time.sleep(.100)

 

while 1 :

        x_axis_m = bus.read_byte_data(DEV_ADDR,3)

        x_axis_l = bus.read_byte_data(DEV_ADDR,4)

 

        z_axis_m = bus.read_byte_data(DEV_ADDR,5)

        z_axis_l = bus.read_byte_data(DEV_ADDR,6)

 

        y_axis_m = bus.read_byte_data(DEV_ADDR,7)

        y_axis_l = bus.read_byte_data(DEV_ADDR,8)

 

        bus.write_byte_data(DEV_ADDR,3,0×00)

 

        x = (c_short((x_axis_m << 8)+ x_axis_l).value)

        y = (c_short((y_axis_m << 8)+ y_axis_l).value)

        z = (c_short((z_axis_m << 8)+ z_axis_l).value)

        print "COMP : %6d %6d %6d"% (x,y,z)

        time.sleep(.01)

 

Leave a Reply