Peripheral myCompass As CompassA @ 0 ' Set the module to be operated as 0. Dim g_bCalEndEvent As Byte ' Store the variable for determining the completeness of calibration. Dim g_iFX As Integer ' Save the x-axis magnetic field intensity. Dim g_iFY As Integer ' Save the y-axis magnetic field intensity. Dim g_wAngle As Word ' Save the directional angle measurement. Dim g_iDevAngle As Integer ' Save the deviation angle. Sub Main() ' Main program g_bCalEndEvent = 0 ' Clear the variable for determining the completeness of calibration. Debug CLS Debug "Module calibration starts. Please horizontally rotate the module at least 360 degrees. ", CR myCompass.Calibration(2) ' Set the calibration duration to be 20 seconds. Do Loop Until g_bCalEndEvent=1 ' Exit after the calibration completion event is detected. Debug "Calibration completes.", CR myCompass.SetRefreshFreq(4) ' Set the refresh frequency. myCompass.SaveAngle(0, 0) ' Save zero degrees in the 0th angle storage block. myCompass.SetDevAngleLimit(45) ' Set the deviation angle limit to be 45 degrees. myCompass.SetDevAngleNum(0) ' Set the angle stored in the 0th storage block to be the base direction. myCompass.EnableRefreshEvent() ' Enable the measurement refresh event. myCompass.EnableDevAngleLimitEvent() ' Enable the deviation angle limit event. Do ' Infinite loop Loop End Sub Event myCompass.FieldRefreshEvent() ' Refresh event myCompass.GetXYField(g_iFX, g_iFY) ' Read the x- and y-axis magnetic field intensities myCompass.GetAngle(g_wAngle) ' Read the current directional angle with respect to the north Debug CSRXY(1, 5), "Current directional angle: ", %DEC3 g_wAngle, CR Debug CSRXY(1, 6), "The x-axis magnetic field intensity: ", %DEC6 g_iFX, CR Debug CSRXY(1, 7), "The y-axis magnetic field intensity: ", %DEC6 g_iFY, CR End Event Event myCompass.DevAngleLimitEvent() ' Deviation angle limit exceeding event myCompass.GetDevAngle(0, g_iDevAngle) ' Read the deviation angle Debug CSRXY(1, 10), "The deviation angle: ", %DEC4 g_iDevAngle, CR End Event Event myCompass.CalEndEvent() ' Calibration completion event g_bCalEndEvent = 1 End Event