Autor Tema: DsPIC30F + HMC5883l  (Leído 1399 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado KnightG

  • PIC10
  • *
  • Mensajes: 1
DsPIC30F + HMC5883l
« en: 22 de Septiembre de 2015, 23:16:20 »
Buen dia, soy nuevo en este foro y me gustaria pedir su ayuda, estoy usando un compass digital hmc5883l con un dspic30f en CCS.
El problema que tengo es que al momento de viisualizar los datos para verificar que el angulo lo envie de 0 a 360° solo se queda entre 100 y 180, y no se si se deba a que interfiere mi transmisor RF para comunicacion serial, algun elemento de mi placa o la configuracion misma de la libreria del HMC58883 ya que uso una variacion de la proporcionada por CCS.

#ifndef _HMC5883L_h
#define _HMC5883L_h

#define HMC5883L_ADD 0x1E
#define HMC5883L_W   0x3C
#define HMC5883L_R   0x3D

#define HMC5883L_CRA 0x00        //Configuration Register A
#define HMC5883L_CRB 0x01       //Configuration Register B
#define HMC5883L_MR  0x02      //Mode Register

#define HMC5883L_MSB_X  0x03  //outputs
#define HMC5883L_LSB_X  0x04
#define HMC5883L_MSB_Y  0x07
#define HMC5883L_LSB_Y  0x08
#define HMC5883L_MSB_Z  0x05
#define HMC5883L_LSB_Z  0x06

#define HMC5883L_SR  0x09    //Status Register
#define HMC5883L_IRA 0x0A   //Identification Register A
#define HMC5883L_IRB 0x0B  //Identification Register B
#define HMC5883L_IRC 0x0C //Identification Register C

#define HMC5883L_IRVA 0x48   //Identification Register Value A
#define HMC5883L_IRVB 0x34  //Identification Value Register B
#define HMC5883L_IRVC 0x33 //Identification Value Register C

#define declinacion 0.0866 //para la ciudad de mexico
#define escala 0.92

signed int8 X_ALTO,X_BAJO,Y_ALTO,Y_BAJO,Z_ALTO,Z_BAJO;
signed int16 EJEX,EJEY,EJEZ;
float ANGLE;



signed int8 lee_registros(HMC5883L_REGISTER)
{
 signed int8 dato=0;
 
  i2c_start();       //S

  i2c_write(HMC5883L_W);   //direccion + write
  i2c_write(HMC5883L_REGISTER);  //RA put the register address
 
  i2c_start();       //S

  i2c_write(HMC5883L_R);  //direccion + read
  dato=i2c_read(0);  //leer + nack
 
  i2c_stop();
 
  return dato;
}

void escribe_registros(int8 HMC5883L_REGISTER, int8 value)
{
 i2c_start();
 
 i2c_write(HMC5883L_W);
 i2c_write(HMC5883L_REGISTER);
 i2c_write(value);
 
 i2c_stop();
}

void inicializa()
{
  escribe_registros(HMC5883L_CRA, 0x10);     //default
 
  //CRA7//CRA6//CRA5//CRA4  // CRA3 // CRA2 // CRA1 //CRA0 //
  // 0 // 0  // 0    //  1  //  0  // 0     // 0    // 0  //
  //R // NUMERO DE   // VELOCIDAD DE DATOS  //  MEDICION //
      //  MUESTRAS   //     DE SALIDA      //    NORMAL //
      //PROMEDIO 1-8 //      15 Hz        //
 
  escribe_registros(HMC5883L_CRB, 0x20);    //default
 
  //CRB7//CRB6//CRB5// CRB4  // CRB3 // CRB2 // CRB1 //CRB0 //
  // 0 // 0  // 1   //  0  //  0  // 0     // 0    // 0  //
  //GANANCIA CONFIG // 
  // +- 1.3 Ga 0.92 //   
       
 
  escribe_registros(HMC5883L_MR, 0x00);      //01=single measurement
                                            //00=continuos measurement
}

void leer_ejes()
{
   X_ALTO=lee_registros(HMC5883L_MSB_X);
   X_BAJO=lee_registros(HMC5883L_LSB_X);
   EJEX=make16(X_ALTO,X_BAJO);
   
   Y_ALTO=lee_registros(HMC5883L_MSB_Y);
   Y_BAJO=lee_registros(HMC5883L_LSB_Y);
   EJEY=make16(Y_ALTO,Y_BAJO);
   
   Z_ALTO=lee_registros(HMC5883L_MSB_Z);
   Z_BAJO=lee_registros(HMC5883L_LSB_Z);
   EJEZ=make16(Z_ALTO,Z_BAJO);
}

void scale()
{
   EJEX*=escala;
   EJEY*=escala;
   EJEZ*=escala;
}

float angulo(signed int16 Y, signed int16 X)
{
   ANGLE= atan2(Y,X);
   ANGLE+=declinacion;
   ANGLE *= (180.0 / PI);
   return ANGLE;
}

saludos