int main(){ LSM9DS1_LowLevel_Init(); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOE, &GPIO_InitStructure); GPIO_SetBits(GPIOE, GPIO_Pin_3); printf("PE3 %d\n", GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3)); uint8_t byte_write = 96; //119 Hz and 2g range LSM9DS1_Write(&byte_write, LSM9DS1_CTRL_REG6_XL, 1); printf("Sent %d on the bus\n", byte_write); uint8_t byte_read = 0; LSM9DS1_Read(&byte_read, LSM9DS1_CTRL_REG6_XL, 1); printf("Received %d on the bus\n", byte_read); byte_write = 56; //enable all axes LSM9DS1_Write(&byte_write, LSM9DS1_CTRL_REG5_XL, 1); printf("Sent %d on the bus\n", byte_write); byte_read = 0; LSM9DS1_Read(&byte_read, LSM9DS1_CTRL_REG5_XL, 1); printf("Received %d on the bus\n", byte_read); int32_t accelerometer_out[3]; LSM9DS1_ReadACC(accelerometer_out); printf("%d %d %d\n", accelerometer_out[0],accelerometer_out[1],accelerometer_out[2]); // osKernelInitialize (); // initialize CMSIS-RTOS // display_mode = 0; // angle_to_draw = SHOW_ROLL; // // // initialize peripherals here // initialize_ADC_Temp(); // init_accelerometer(); // init_interrupts(); // init_7_segment(); // init_TIM3(); // init_TIM4(); // init_TIM5(); // EXTI_GenerateSWInterrupt(EXTI_Line0); // // // create 'thread' functions that start executing, // // example: tid_name = osThreadCreate (osThread(name), NULL); // temperature_reader_thread = osThreadCreate(osThread(temperature_reader),NULL); // display_refresher_thread = osThreadCreate(osThread(display_refresher),NULL); // accelerometer_reader_thread = osThreadCreate(osThread(accelerometer_reader),NULL); // keypad_detector_thread = osThreadCreate(osThread(keypad_detector),NULL); // // osKernelStart(); // start thread execution }
/** * @brief Get the accelerometer readings from the LSM9DS1, fills the parameter buffer with the X, Y, and Z accelerations in milli-gs. * @param 3 32bit int buffer to store data * @retval None */ void LSM9DS1_Read_XL(int32_t* out) { uint8_t buffer[6]; uint8_t crtl, i; //Read the raw accelerometer registers LSM9DS1_Read(&crtl, LSM9DS1_CTRL_REG6_XL, 1); LSM9DS1_Read(buffer, LSM9DS1_OUT_X_H_XL, 1); LSM9DS1_Read(buffer+1, LSM9DS1_OUT_X_L_XL, 1); LSM9DS1_Read(buffer+2, LSM9DS1_OUT_Y_H_XL, 1); LSM9DS1_Read(buffer+3, LSM9DS1_OUT_Y_L_XL, 1); LSM9DS1_Read(buffer+4, LSM9DS1_OUT_Z_H_XL, 1); LSM9DS1_Read(buffer+5, LSM9DS1_OUT_Z_L_XL, 1); //Apply the appropriate sensitivity factors depending on the scale switch(crtl & 0x18) { case XL_SCALE_2G: for(i = 0; i < 6; i+=2){ *out =(int32_t)(XL_SENSITIVITY_2G * (int16_t)(buffer[i+1]+(buffer[i]<<8))); out++; } break; case XL_SCALE_16G: for(i = 0; i < 6; i+=2){ *out =(int32_t)(XL_SENSITIVITY_16G * (int16_t)(buffer[i+1]+(buffer[i]<<8))); out++; } break; case XL_SCALE_4G: for(i = 0; i < 6; i+=2){ *out =(int32_t)(XL_SENSITIVITY_4G * (int16_t)(buffer[i+1]+(buffer[i]<<8))); out++; } break; case XL_SCALE_8G: for(i = 0; i < 6; i+=2){ *out =(int32_t)(XL_SENSITIVITY_8G * (int16_t)(buffer[i+1]+(buffer[i]<<8))); out++; } break; default: break; } }
void accelerometer_read_raw(void) { uint8_t Buffer[6]; #ifdef LSM9DS1 for (uint8_t i = 0; i < 6; i++) { LSM9DS1_Read(Buffer + i, LSM9DS1_OUT_X_ADDR + i, 1); } #else LIS3DSH_Read(&Buffer[0], LIS3DSH_OUT_X_L, 1); LIS3DSH_Read(&Buffer[1], LIS3DSH_OUT_X_H, 1); LIS3DSH_Read(&Buffer[2], LIS3DSH_OUT_Y_L, 1); LIS3DSH_Read(&Buffer[3], LIS3DSH_OUT_Y_H, 1); LIS3DSH_Read(&Buffer[4], LIS3DSH_OUT_Z_L, 1); LIS3DSH_Read(&Buffer[5], LIS3DSH_OUT_Z_H, 1); #endif x_raw = Buffer[0] + (int16_t)(Buffer[1] << 8); y_raw = Buffer[2] + (int16_t)(Buffer[3] << 8); z_raw = Buffer[4] + (int16_t)(Buffer[5] << 8); }
/** * @brief Get the gyroscope readings from the LSM9DS1, fills the parameter buffer with X, Y and Z readings in millidegrees per second * @param 3 32bit int buffer to store data * @retval None */ void LSM9DS1_Read_G(int32_t* out){ uint8_t buffer[6]; uint8_t crtl, i; //Read all the raw register values LSM9DS1_Read(&crtl, LSM9DS1_CTRL_REG1_G, 1); LSM9DS1_Read(buffer, LSM9DS1_OUT_X_H_G, 1); LSM9DS1_Read(buffer+1, LSM9DS1_OUT_X_L_G, 1); LSM9DS1_Read(buffer+2, LSM9DS1_OUT_Y_H_G, 1); LSM9DS1_Read(buffer+3, LSM9DS1_OUT_Y_L_G, 1); LSM9DS1_Read(buffer+4, LSM9DS1_OUT_Z_H_G, 1); LSM9DS1_Read(buffer+5, LSM9DS1_OUT_Z_L_G, 1); //Fill the parameter buffer with the appropriately scaled values //(concatenating the low and high bytes into a single value) switch(crtl & 0x18) { case G_SCALE_245_DPS: for(i = 0; i < 6; i+=2){ *out =(int32_t)(G_SENSITIVITY_245_DPS * (int16_t)(buffer[i+1]+(buffer[i]<<8))); out++; } break; case G_SCALE_500_DPS: for(i = 0; i < 6; i+=2){ *out =(int32_t)(G_SENSITIVITY_500_DPS * (int16_t)(buffer[i+1]+(buffer[i]<<8))); out++; } break; case G_SCALE_2000_DPS: for(i = 0; i < 6; i+=2){ *out =(int32_t)(G_SENSITIVITY_2000_DPS * (int16_t)(buffer[i+1]+(buffer[i]<<8))); out++; } break; default: break; } }
/** * @brief Read LSM9DS1 output register, and calculate the acceleration * ACC[mg]=SENSITIVITY* (out_h*256+out_l)/16 (12 bit rappresentation) * @param s16 buffer to store data * @retval None */ void LSM9DS1_ReadACC(float* out) { uint8_t buffer[6]; uint8_t ctrl = 0x00, i = 0x00; //uint8_t offsetX, offsetY, offsetZ; int16_t aggregateResult = 0; LSM9DS1_Read(&ctrl, LSM9DS1_CTRL_REG6, 1); LSM9DS1_Read(&buffer[0], LSM9DS1_ACC_OUT_X_L, 1); LSM9DS1_Read(&buffer[1], LSM9DS1_ACC_OUT_X_H, 1); LSM9DS1_Read(&buffer[2], LSM9DS1_ACC_OUT_Y_L, 1); LSM9DS1_Read(&buffer[3], LSM9DS1_ACC_OUT_Y_H, 1); LSM9DS1_Read(&buffer[4], LSM9DS1_ACC_OUT_Z_L, 1); LSM9DS1_Read(&buffer[5], LSM9DS1_ACC_OUT_Z_H, 1); ctrl = (ctrl & 0x18 ) >> 3; debug_acc++; switch(ctrl) { /* FS bits = 000 ==> Sensitivity typical value = 0.061 milligals/digit*/ case 0x00: for(i=0; i<0x06; i=i+2) { aggregateResult = (int32_t)(buffer[i] | buffer[i+1] << 8); *out =(float)(LSM9DS1_ACC_SENSITIVITY_2G * (float)aggregateResult); out++; } break; /* FS bit = 001 ==> Sensitivity typical value = 0.122 milligals/digit*/ case 0x01: for(i=0; i<0x06; i=i+2) { aggregateResult = (int32_t)(buffer[i] | buffer[i+1] << 8); *out =(float)(LSM9DS1_ACC_SENSITIVITY_16G * (float)aggregateResult); out++; } break; /* FS bit = 010 ==> Sensitivity typical value = 0.183 milligals/digit*/ case 0x02: for(i=0; i<0x06; i=i+2) { aggregateResult = (int32_t)(buffer[i] | buffer[i+1] << 8); *out =(float)(LSM9DS1_ACC_SENSITIVITY_4G * (float)aggregateResult); out++; } break; /* FS bit = 011 ==> Sensitivity typical value = 0.244 milligals/digit*/ case 0x03: for(i=0; i<0x06; i=i+2) { aggregateResult = (int32_t)(buffer[i] | buffer[i+1] << 8); *out =(float)(LSM9DS1_ACC_SENSITIVITY_8G * (float)aggregateResult); out++; } break; default: break; } }