Autor Tema: AYUDA CON MIKROC E I2C  (Leído 1870 veces)

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

Desconectado lutgaru

  • PIC10
  • *
  • Mensajes: 5
AYUDA CON MIKROC E I2C
« en: 26 de Noviembre de 2016, 21:45:24 »
necesito ayuda con implementar el siguiente codigo, el codigo lee un sensor el gy-85 para ser exactos del cual segui la siguiente librería como ejemplo http://libstock.mikroe.com/projects/view/1075/gy85-9dof-imu,o que pasa es que al implementar pwm ya no funciona, el programa se traba en la inicializacion del sensor que usa i2c como base, tal ves sea un error obvio o tal ves sea el compilador que en mi caso es mikroc 6.6.3. espero que me puedan ayudar de verdad.

el pic que estoy usando es el pic16f1827 con 4 modulos pwm, ademas con un rejor de 32mhz interno. les adjunto el código.

Código: [Seleccionar]
#define ACCEL_ADDRESS  0xA6 // 0x53 = 0xA6 / 2
#define GYRO_ADDRESS_Wr   0xD0// 0x68 = 0xD0 / 2
#define GYRO_ADDRESS_Rd   0xD1
#define HMC5883L_READ_ADDR       0x3D
#define HMC5883L_WRITE_ADDR      0x3C
/***************zone accelerometre************/
#define _X_LOW         0x32
#define _X_HIG         0x33
#define _Y_LOW         0x34
#define _Y_HIG         0x35
#define _Z_LOW         0x36
#define _Z_HIG         0x37
/*************magneto*********************/
#define Config_Reg_A             0x00
#define Config_Reg_B             0x01
#define Mode_Reg                 0x02
#define X_MSB_Reg                0x03
/***************************/

extern volatile int accel[3];
extern volatile int magnetom[3];
extern volatile int gyro[3];

unsigned short duty1=0,duty2=0,duty3=0,duty4=0; //Valores de las señales PWM
char  control;

//============================================================================
// Project: GY-85 9DOF IMU
volatile int accel[3];
volatile int magnetom[3];
volatile int gyro[3];

  unsigned short accel_Read_Byte(unsigned short address_sensor,unsigned short register_address) {
  unsigned short Data_Read = 0;

  I2C1_Start();
  I2C1_Wr(address_sensor);
  I2C1_Wr(register_address);

  I2C1_Start();
  I2C1_Wr(address_sensor+1);
  Data_Read = I2C1_Rd(0);
  I2C1_Stop();

  return Data_Read;
}


void Acc_Init(){
    I2C1_Start();
    I2C1_Wr(ACCEL_ADDRESS);
    I2C1_Wr(0X2D);// Power register
    I2C1_Wr(0X00); // standbay mode
    I2C1_Stop();

    Delay_ms(5);

    I2C1_Start();
    I2C1_Wr(ACCEL_ADDRESS);
    I2C1_Wr(0X31);// Data format register
    I2C1_Wr(0X08); // Set to full resolution
    I2C1_Stop();

    Delay_ms(5);

    I2C1_Start();
    I2C1_Wr(ACCEL_ADDRESS);
    I2C1_Wr(0X2C);// Data format register
    I2C1_Wr(0X09); // Set to full resolution
    I2C1_Stop();

    Delay_ms(5);
    I2C1_Start();
    I2C1_Wr(ACCEL_ADDRESS);
    I2C1_Wr(0X2D);// Power register
    I2C1_Wr(0X08); // measure mode
    I2C1_Stop();
               }

// Reads x, y and z accelerometer registers
void Acc_Read(){
      unsigned short buffer[6];
      buffer[0]=accel_Read_Byte(ACCEL_ADDRESS,_X_LOW);  // low_x
      buffer[1]=accel_Read_Byte(ACCEL_ADDRESS,_X_HIG); //  high_x

      buffer[2]=accel_Read_Byte(ACCEL_ADDRESS,_Y_LOW);  // low_y
      buffer[3]=accel_Read_Byte(ACCEL_ADDRESS,_Y_HIG); //  high_y

      buffer[4]=accel_Read_Byte(ACCEL_ADDRESS,_Z_LOW);  // low_y
      buffer[5]=accel_Read_Byte(ACCEL_ADDRESS,_Z_HIG); //  high_y

    accel[0] = (((int) buffer[1]) << 8) | buffer[0];  // X axis
    accel[1] = (((int) buffer[3]) << 8) | buffer[2];  // Y axis
    accel[2] = (((int) buffer[5]) << 8) | buffer[4];  // Z axis
               }
  //============================================//
 /***********HMC5883L******************/
 void HMC5883L_write(unsigned char reg_address, unsigned char value)
{
   I2C1_Start();
   I2C1_Wr(HMC5883L_WRITE_ADDR);
   I2C1_Wr(reg_address);
   I2C1_Wr(value);
   I2C1_Stop();
}
 /*void Magneto_Init()
{
    unsigned char value = 0;
   HMC5883L_write(Config_Reg_A, 0b00011000);
   HMC5883L_write(Config_Reg_B, 0xA0);
   HMC5883L_write(Mode_Reg, 0x00);
}*/

/*void Magneto_Read(){
         unsigned short buffer[6];
    I2C1_Start();
    I2C1_Wr(HMC5883L_WRITE_ADDR);
    I2C1_Wr(X_MSB_Reg);
    I2C1_Start();
    I2C1_Wr(HMC5883L_READ_ADDR);
                  buffer[0]=I2C1_Rd(1);
                  buffer[1]=I2C1_Rd(1);
                  buffer[2]=I2C1_Rd(1);
                  buffer[3]=I2C1_Rd(1);
                  buffer[4]=I2C1_Rd(1);
                  buffer[5]=I2C1_Rd(0);
                   I2C1_Stop();

    magnetom[1] = 1 * ((((int) buffer[0]) << 8) | buffer[1]);  // X axis (internal sensor -y axis)
    magnetom[0] = 1 * ((((int) buffer[4]) << 8) | buffer[5]);  // Y axis (internal sensor -x axis)
    magnetom[2] = 1 * ((((int) buffer[2]) << 8) | buffer[3]);  // Z axis (internal sensor -z axis)

                   }*/

/**********ITG3025**************/
void Gyro_Init(){
    // Power up reset defaults
    I2C1_Start();
    I2C1_Wr(GYRO_ADDRESS_Wr);
    I2C1_Wr(0X3E);
    I2C1_Wr(0X80);
    I2C1_Stop();

   // Set sample rato to 50Hz
    I2C1_Start();
    I2C1_Wr(GYRO_ADDRESS_Wr);
    I2C1_Wr(0X15);
    I2C1_Wr(0X0A); //SMPLRT_DIV = 10 (50Hz)
    I2C1_Stop();

  // Select full-scale range of the gyro sensors
  // Set LP filter bandwidth to 42Hz
    I2C1_Start();
    I2C1_Wr(GYRO_ADDRESS_Wr);
    I2C1_Wr(0X16);
    I2C1_Wr(0X1B);
    I2C1_Stop();

    // Set clock to PLL with z gyro reference
    I2C1_Start();
    I2C1_Wr(GYRO_ADDRESS_Wr);
    I2C1_Wr(0X3E);
    I2C1_Wr(0X03);
    I2C1_Stop();


                  }

 // Reads x, y and z
 void Gyro_Read(){
                   unsigned short buffer[6];
   I2C1_Start();
   I2C1_Wr(GYRO_ADDRESS_Wr);
   I2C1_Wr(0X1D);
   I2C1_Start();
   I2C1_Wr(GYRO_ADDRESS_Rd);

                  buffer[0]=I2C1_Rd(1);
                  buffer[1]=I2C1_Rd(1);
                  buffer[2]=I2C1_Rd(1);
                  buffer[3]=I2C1_Rd(1);
                  buffer[4]=I2C1_Rd(1);
                  buffer[5]=I2C1_Rd(0);
                  I2C1_Stop();


    gyro[0] = 1 * ((((int) buffer[0]) << 8) | buffer[1]);    // X axis (internal sensor -y axis)
    gyro[1] = 1 * ((((int) buffer[2]) << 8) | buffer[3]);    // Y axis (internal sensor -x axis)
    gyro[2] = 1 * ((((int) buffer[4]) << 8) | buffer[5]);    // Z axis (internal sensor -z axis)
                 }

  void UART1_Printf(int value){
   char txt[7];
   IntToStr(value, txt);
   UART1_Write_Text(txt);
                              }

void printf_result(){
UART1_Write_Text("X_acc:");UART1_Printf(accel[0]); UART1_Write_Text("  ");
UART1_Write_Text("Y_acc:");UART1_Printf(accel[1]); UART1_Write_Text("  ");
UART1_Write_Text("Z_acc:");UART1_Printf(accel[2]); UART1_Write_Text("  ");
UART1_Write_Text("\r\n");

UART1_Write_Text("X_mag:");UART1_Printf(magnetom[0]); UART1_Write_Text("  ");
UART1_Write_Text("Y_mag:");UART1_Printf(magnetom[1]); UART1_Write_Text("  ");
UART1_Write_Text("Z_mag:");UART1_Printf(magnetom[2]); UART1_Write_Text("  ");
UART1_Write_Text("\r\n");

/*UART1_Write_Text("X_gyro:");UART1_Printf(gyro[0]); UART1_Write_Text("  ");
UART1_Write_Text("Y_gyro:");UART1_Printf(gyro[1]); UART1_Write_Text("  ");
UART1_Write_Text("Z_gyro:");UART1_Printf(gyro[2]); UART1_Write_Text("  ");
UART1_Write_Text("\r\n");*/
                               }

void GY_85_Init(){
Acc_Init();
//Magneto_Init();
Gyro_Init();
                 }

void GY_85_Read(){
Acc_Read();
//Magneto_Read();
Gyro_Read();
                 }

void main() {
      OSCCON=0b11110000;
      ANSELA=0x00;
      ANSELB=0x00;
      TRISA=0;
      PORTA=0;
      TRISB=0;
      PORTB=0;

      UART1_Init(9600);
      TRISB2_bit = 1;     // only change if pin remap done
      TRISB5_bit = 0;     // only change if pin remap done
      TXCKSEL_bit=1;
      RXDTSEL_bit=1;
      Delay_ms(100);
      UART1_Write_Text("uart init\r\n");

      PWM1_Init(5000);
      PWM2_Init(5000);
      PWM3_Init(5000);
      PWM4_Init(5000);
      Delay_ms(100);
      CCP1SEL_bit=1;
      CCP2SEL_bit=1;
      TRISB2_bit = 1;     // only change if pin remap done
      TRISB5_bit = 0;     // only change if pin remap done
      UART1_Write_Text("pwm init\r\n");

      PWM1_Start();
      PWM2_Start();
      PWM3_Start();
      PWM4_Start();
      Delay_ms(100);
      duty1 = 4;
      duty2 = 4;
      duty3 = 4;
      duty4 = 4 ;

       PWM1_Set_Duty(duty1);

       PWM2_Set_Duty(duty2);

       PWM3_Set_Duty(duty3);

       PWM4_Set_Duty(duty4);

      UART1_Write_Text("pwm start\r\n");

      I2C1_Init(1000000);
      Delay_ms(100);
      GY_85_Init(); // Init GY-85 sensor
      Delay_ms(100);
      UART1_Write_Text("GY-85 Start\r\n");

      while(1){
      GY_85_Read(); // Read GY-85 sensor
      printf_result();// Send the data to the USART
      Delay_ms(100);
      control = UART1_Read();
         if(control == 'a'){
             UART_Write_Text("PWM+\n\r");
             Delay_ms(100);
             duty1++;                    //Impulso de 0,8 msg de pwm0 posición 0º
             duty2++;                      //Impulso de 0,8 msg de pwm1 posición 0º
             duty3++;                      //Impulso de 0,8 msg de pwm2 posición 0º
             duty4++;                      //Impulso de 0,8 msg de pwm3 posición 0º

         }
         if(control == 'b'){
             UART_Write_Text("PWM-\n\r");
             Delay_ms(100);
             duty1--;                    //Impulso de 0,8 msg de pwm0 posición 0º
             duty2--;                      //Impulso de 0,8 msg de pwm1 posición 0º
             duty3--;                      //Impulso de 0,8 msg de pwm2 posición 0º
             duty4--;                      //Impulso de 0,8 msg de pwm3 posición 0º

         }

         }
            }
« Última modificación: 26 de Noviembre de 2016, 23:13:19 por lutgaru »