'------------------------------------------------------------------------------- ' starter.bas ' this is an example for readining a Devantech Compass ' all output will be on com2 ' There is also an LED on portB.0 that will light if the ' direction = north +/- 5 degrees '------------------------------------------------------------------------------- $crystal = 14745600 $regfile = "m128def.dat" $baud1 = 19200 '$sim 'config serial buffer Config Serialout1 = Buffered , Size = 40 Config Serialin1 = Buffered , Size = 40 'com 2 standard conection Config Com2 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0 'config I2C lines for mavric Config Sda = Portd.1 Config Scl = Portd.0 'this function is used to read the compass value 'declare Functons Declare Function I2cread8_w(byval I2cnode As Byte , Byval Loc As Byte) As Word ' Startup int code I2cinit 'start the I2C buss Open "com2:" For Random As #2 'open come port Enable Interrupts 'enable interupts Config Portb = Output Portb = 0 'set all low 'varables Dim Heading As Word 'varable to hold compass value Dim Readable_heading As Single Const Compass = 192 'make address of compass 'start main program Do 'small delay of 500mS Waitms 500 Heading = I2cread8_w(compass , 2) 'read the heading reading from node 192 and locations 2 & 3 ' sence the heading is returned as a word a reading of 90 would come back as "0900" ' to make it human readable you can copy the heading heading to "readable_heading" and devide it by 10 Readable_heading = Heading 'copy first Readable_heading = Readable_heading / 10 'devide after the varable is stored in a single ' a bit of math on the reading to see if it is with in +/- 5 degrees of north Print #2 , "Heading = " + Str(readable_heading) + Chr(10) + Chr(13) If Heading > 3550 Or Heading < 50 Then Portb.0 = 1 ' within 5 degrees of north then led ON Print #2 , "LED on" + Chr(10) + Chr(13) Else Portb.0 = 0 ' greater then 5 degrees then led OFF Print #2 , "LED off" + Chr(10) + Chr(13) End If Print #2 , "" + Chr(10) + Chr(13) 'blank print to seperate readings Loop 'end main program 'subs '------------------------------ I2C I2C I2C I2C I2C I2C I2C I2C I2C I2C I2C '8x16 Function I2cread8_w(byval I2cnode As Byte , Loc As Byte) As Word Local Lsb As Byte Local Msb As Byte I2cstart I2cwbyte I2cnode I2cwbyte Loc I2cstart Incr I2cnode I2cwbyte I2cnode I2crbyte Msb , Ack I2crbyte Lsb , Nack I2cstop I2cread8_w = Makeint(lsb , Msb) End Function