Read a Honeywell HMC6352 magnetic compass using I2C

Download pre-release library modules and new examples to use with Astrobe for Cortex-M3. Forum members can also upload their own source code examples.

Read a Honeywell HMC6352 magnetic compass using I2C

Postby cfbsoftware » Sun Jun 17, 2012 6:39 am

Code: Select all
MODULE Compass;
(* ===================================================================== 
   Example Cortex-M3 Oberon I2C Digital Compass Program 

   Description:
     Displays the magnetic compass bearings (0 - 359 deg) every second
   
   Target:
     LPC17xx systems with a Honeywell HMC6352 digital compass
     connected to the I2C bus

   Tested with:
     SparkFun Compass Module SEN-07915 +
     Embedded Artists LPCXpresso LPC1769 Board
     
   Refs:
     NXP LPC13xx User Manual UM10375
     Honeywell HMC6352 Datasheet
     Oberon for Cortex-M3 Microcontrollers
   
   (c) 2010-2012 CFB Software   
   http://www.astrobe.com 
   
   ===================================================================== *)

IMPORT MCU, I2C, SYSTEM, Main, Out, Timer;

PROCEDURE Progress(msg: ARRAY OF CHAR; status: INTEGER);
BEGIN
  Out.String(msg);
  IF status = I2C.OK THEN
    Out.String(" OK")
  ELSE
    Out.String(" Error ");
    Out.Int(status, 0)
  END;
  Out.Ln
END Progress;


PROCEDURE Run;
CONST
  I2CBus = 1;
  freq = 100000;
  (* Ref: HMC6352 *)
  compassAddr = 021H;
VAR
  command: CHAR;
  reading: ARRAY 2 OF SYSTEM.BYTE;
  status, degrees: INTEGER;
BEGIN
  I2C.Init(I2CBus, freq);
  status := I2C.OK;
  Progress("Compass", status);
  command := "A";
  WHILE TRUE DO
    status := I2C.ReadBytes(compassAddr, command, 1, reading, 0, 2);
    IF status = I2C.OK THEN
      (* Reading is 16-bits msb:lsb *)
      degrees := (ORD(reading[0]) * 256 + ORD(reading[1])) DIV 10;
      Out.Int(degrees, 0); Out.String(" degrees"); Out.Ln()
    ELSE
      Progress("Read", status)
    END;
    Timer.MSecDelay(1000)
  END
END Run;


BEGIN
  Run()
END Compass.


compass.gif
You do not have the required permissions to view the files attached to this post.
cfbsoftware
Site Admin
 
Posts: 309
Joined: Fri Dec 31, 2010 12:30 pm

Return to Cortex-M3

cron