HMC5883L Digital Compass with Pic 16f877A CCS C Code

HMC5883L 3 eksenli pusula sensörü dünyanın manyetik alanından faydalanarak manyetik yön bilgisi veren i2c ile haberleşebilinen bir sensördür.

Driver

// HMC5883 required Registers

#define W_DATA   0x3C     //'Used to perform a Write operation
#define R_DATA   0x3D     //'Used to perform a Read operation
#define CON_A    0x00
#define CON_B    0x01     //'Send continuous MeVARurement mode.
#define MOD_R    0x02     //'Read/Write Register, Selects the operating mode. Default = Single meVARurement
#define X_MSB    0x03     //'Read Register, Output of X MSB 8-bit value.
#define X_LSB    0x04     //'Read Register, Output of X LSB 8-bit value.
#define Z_MSB    0x05     //'Read Register, Output of Z MSB 8-bit value.
#define Z_LSB    0x06     //'Read Register, Output of Z LSB 8-bit value.
#define Y_MSB    0x07     //'Read Register, Output of Y MSB 8-bit value.
#define Y_LSB    0x08     //'Read Register, Output of Y LSB 8-bit value.

void hmc5883_write(int add, int data)
{
         i2c_start();
         i2c_write(W_DATA);
         i2c_write(add);
         i2c_write(data);
         i2c_stop();
 
}
      
int16 hmc5883_read(int add){
         int retval;
         i2c_start();
         i2c_write(W_DATA);
         i2c_write(add);
         i2c_start();
         i2c_write(R_DATA);
         retval=i2c_read(0);
         i2c_stop();
         return retval;
}
 
void hmc5883_init(){
         hmc5883_write(MOD_R,  0x00);
         delay_ms(100);
         hmc5883_write(CON_A,  0x10);
         delay_ms(100);
         hmc5883_write(CON_B,  0x20);
         delay_ms(100);
}

Örnek uygulama programı

#include <16f877A.h>
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES HS                       //Dont change
#use    delay(clock=20MHz)  
#use I2C(master, sda=PIN_d0, scl=PIN_d1, slow)  //You can change, according your board ports
#include "EXLCD.C"  //You can change, according your board ports
#include "HMC5883.C"
#include "math.h"
int8 M_data[6]; //6 units 8 bit Measured datas
int16 Xm=0,Ym=0,Zm=0; //16 bit X,Y,Z variables

void main()
{
delay_ms(2);
lcd_init();
hmc5883_init();    
   
printf(lcd_putc,"\f");
lcd_gotoxy(1,1);
printf(lcd_putc,"    HMC5883L    ");
lcd_gotoxy(1,2);
printf(lcd_putc,"Digital Compass ");
delay_ms(1000);
printf(lcd_putc,"\f");

   while(TRUE)
   {
   M_data[0]=hmc5883_read(0x04); //Read X (LSB)
   M_data[1]=hmc5883_read(0x03); //Read X (MSB)
   M_data[2]=hmc5883_read(0x08); //Read Y (LSB)
   M_data[3]=hmc5883_read(0x07); //Read Y (MSB)
   M_data[4]=hmc5883_read(0x06); //Read Z (LSB)
   M_data[5]=hmc5883_read(0x05); //Read Z (MSB)

   Xm=make16(M_data[1],M_data[0]);
   Ym=make16(M_data[3],M_data[2]);
   Zm=make16(M_data[5],M_data[4]);

   float Heading = atan2((signed int16)Ym,(signed int16)Xm)* 180 / pi + 180;
       
   lcd_gotoxy(1,1);
   printf(lcd_putc,"X=%ld  ",Xm);
   lcd_gotoxy(9,1);
   printf(lcd_putc,"Y=%ld  ",Ym);
   lcd_gotoxy(1,2);
   printf(lcd_putc,"Z=%ld  ",Zm);
   lcd_gotoxy(9,2);
   printf(lcd_putc,"H=%f  ",Heading);
   delay_ms(100);

   }
}

 

Bu yazı CCS C Sürücü kategorisine gönderilmiş. Kalıcı bağlantıyı yer imlerinize ekleyin.