CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

HMC1022 (GY-26) UART Compass Library

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
sshahryiar



Joined: 05 May 2010
Posts: 94
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

HMC1022 (GY-26) UART Compass Library
PostPosted: Fri Jul 17, 2015 12:01 pm     Reply with quote

HMC1022.h


Code:

#define Default_I2C_Address 0xE0                                                                 
#define Get_Angular_Measurement 0x31
#define Start_Calibration 0xC0 
#define End_Calibration 0xC1                     
#define Set_Magnetic_Declination_High_Byte 0x03   
#define Set_Magnetic_Declination_Low_Byte    0x04
                                                                                                                                                             
#define no_of_data_bytes_returned 0x08
                                                   
#define calibration_LED pin_C1


#use RS232(UART1, baud = 9600, stop = 1, parity = N, timeout = 1000)
                                                                       

float read_heading();     
void calibrate_compass(); 
void factory_reset();
void set_I2C_address(unsigned char i2c_address);
void set_declination_angle(unsigned long angle);



HMC1022.c

Code:

#include "HMC1022.h"
                                                         
     
float read_heading()
{                         
     unsigned char s = 0; 
     unsigned long CRC = 0x00;
     register float heading = 0;
     
     unsigned char data_bytes[no_of_data_bytes_returned] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
                                                           
     putc(Get_Angular_Measurement); 
               
     for(s = 0; s < no_of_data_bytes_returned; s++)
     {               
        data_bytes[s] = getc();
        if(s < 7)   
        {
            CRC += data_bytes[s];
        }
     }                                                       
     
     CRC = (CRC & 0xFF);
     
     if(CRC == data_bytes[7])
     {                                     
        for(s = 2; s < 7; s++)                       
        {                                       
           data_bytes[s] -= 0x30;               
        }
       
        heading = (data_bytes[2] * 100.0);
       heading += (data_bytes[3] * 10.0);                       
       heading += (data_bytes[4] * 1.0);                 
       heading += (data_bytes[6] * 0.1); 
     }     
     else
     {
        heading = -1;
     }
                                             
     return heading;
}


void calibrate_compass()
{
   unsigned char s = 0x00;   

   putc(Start_Calibration);
   
   for(s = 0; s < 60; s++)
   {
       output_high(calibration_LED);   
      delay_ms(100);
      output_low(calibration_LED);
      delay_ms(900);
   } 
   
   for(s = 0; s < 60; s++)   
   {
       output_high(calibration_LED);   
      delay_ms(400);             
      output_low(calibration_LED);
      delay_ms(600);
   }             
   
   putc(End_Calibration);
}


void factory_reset()
{
   putc(0xA0);
   putc(0xAA);
   putc(0xA5); 
   putc(0xC5);
}   

                                               
void set_I2C_address(unsigned char i2c_address) 
{         
   putc(0xA0);
   putc(0xAA);
   putc(0xA5); 
   putc(i2c_address);
}   


void set_declination_angle(unsigned long angle)
{
   unsigned long hb = 0;
   unsigned char lb = 0;
   
   lb = (angle & 0x00FF);   
                            
   hb = (angle & 0xFF00);
   hb >>= 8; 
              
   putc(Set_Magnetic_Declination_High_Byte);   
   putc(hb);
   
   putc(Set_Magnetic_Declination_Low_Byte);
   putc(lb);
}

_________________
https://www.facebook.com/MicroArena

SShahryiar
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group