Shop OBEX P1 Docs P2 Docs Learn Events
propellor c and compass sensor — Parallax Forums

propellor c and compass sensor

Patrick222122Patrick222122 Posts: 30
edited 2013-11-10 06:32 in Propeller 1
It me again. This time I am having trouble setting up the code for a Parallax Compass Module 3-Axis HMC5883L in c. The trouble is not with the premade library functions for it but rather I cannot seem to create the i2c connection the sensor needs. I have tried multiple time using the code in both the simple tools and I2c libraries to no success. How do I open I2c bus? Please code example's if possible.

Comments

  • jazzedjazzed Posts: 11,803
    edited 2013-11-09 15:04
    It me again. This time I am having trouble setting up the code for a Parallax Compass Module 3-Axis HMC5883L in c. The trouble is not with the premade library functions for it but rather I cannot seem to create the i2c connection the sensor needs. I have tried multiple time using the code in both the simple tools and I2c libraries to no success. How do I open I2c bus? Please code example's if possible.

    So, you've tried this code with the compass connected to P2/P3 and it doesn't work ?
    /*
      Test Compass HMC5883L.c
      
      Test compass with SCL connected to P3 and SDA connected to P2. Display
      measurement results in SimpleIDE Terminal.
      
     learn.parallax.com/propeller-c-simple-devices/sense-direction-compass-hmc5883l
    */
    
    
    #include "simpletools.h"                      // Include simpletools header
    #include "compass3d.h"                        // Include compass3d header
    
    
    int main()                                    // Main function
    {
      int x, y, z;                                // Declare x, y, & z axis variables
      
      i2c *bus = i2c_newbus(3, 2, 0);             // New I2C bus SCL=P3, SDA=P2
      compass_init(bus);                          // Initialize compass on bus.
    
    
      while(1)                                    // Repeat indefinitely
      {
        compass_read(bus, &x, &y, &z);            // Compass vals -> variables
        print("%cx=%d, y=%d, z=%d%c\n",           // Display compass variables
               HOME, x, y, z, CLREOL);
    
    
        float fy = (float) y;                     // Ints to floats
        float fx = (float) x;
        float heading = atan2(fy, fx) * 180.0/PI; // Calculate heading with floats
        if(heading < 0.0)                         // Convert -180...+180 to 0...360
          heading = (360.0 + heading);
        print("heading = %.2f%c\n",               // Display heading
                            heading, CLREOL);       
    
    
        pause(500);                               // Wait 1/2 second
      }
    }
    
    

    From the Learn/Simple Library repository.
  • Patrick222122Patrick222122 Posts: 30
    edited 2013-11-10 06:32
    Thanks that fixed the problem
Sign In or Register to comment.