/** function loads calibration table from EEPROM, validate it and if OK uses it */
static void touch_LoadCalibration(void)
{
  I2CSPM_Init_TypeDef i2cInit = I2CSPM_INIT_DEFAULT;
  uint32_t         temp, checksum;
  int              count;
  MATRIX           new_matrix;

#if !defined( BSP_STK )
  BSP_PeripheralAccess(BSP_I2C, true);
#endif

  /* Initialize I2C driver, using standard rate. Devices on DK itself */
  /* supports fast mode, but in case some slower devices are added on */
  /* prototype board, we use standard mode. */
  I2CSPM_Init(&i2cInit);
  count  = EEPROM_Read(I2C0, EEPROM_DVK_ADDR, CALIBRATION_EEPROM_OFFSET, (uint8_t*) &temp, sizeof(temp));
  count += EEPROM_Read(I2C0, EEPROM_DVK_ADDR, CALIBRATION_EEPROM_OFFSET + 4, (uint8_t*) &new_matrix, sizeof(new_matrix));
  if (count == sizeof(new_matrix) + 4)
  {
    if (temp == CALIBRATION_MAGIC_NUMBER)
    {
      checksum = touch_CountChecksum(temp, (uint32_t*) &new_matrix, sizeof(new_matrix) / 4);
      count    = EEPROM_Read(I2C0, EEPROM_DVK_ADDR, CALIBRATION_EEPROM_OFFSET + 4 + sizeof(new_matrix), (uint8_t*) &temp, sizeof(temp));
      if (temp == checksum)
      {                                      /* looks like calibration table is valid */
        ADC_IntDisable(ADC0, ADC_IF_SINGLE); /* we need to disable ADC interrupt to avoid current_pos structure update for a while */
        memcpy(&calibrationMatrix, &new_matrix, sizeof(calibrationMatrix));
        ADC_IntEnable(ADC0, ADC_IF_SINGLE);
      }
    }
  }
}
Exemplo n.º 2
0
/**************************************************************************//**
 * @brief  Main function
 *****************************************************************************/
int main(void)
{
  int i;
  uint8_t data[7];
  SENSOR_DATA_TypeDef axis_converted_avg[33];
  char lcd_data[20];
  /* ADXL345 I2C driver config */
  ADXL345Handle.port = I2C1;
  ADXL345Handle.sclPort = ADXL345_I2C_SCL_PORT;
  ADXL345Handle.sclPin = ADXL345_I2C_SCL_PIN;
  ADXL345Handle.sdaPort = ADXL345_I2C_SDA_PORT;
  ADXL345Handle.sdaPin = ADXL345_I2C_SDA_PIN;
  ADXL345Handle.portLocation = ADXL345_I2C_PORT_LOC;
  /* Chip errata */
  CHIP_Init();

  /* If first word of user data page is non-zero, enable eA Profiler trace */
  BSP_TraceProfilerSetup();

  /* Enable two leds to show we're alive */
  BSP_LedsInit();
  BSP_LedSet(0);
  BSP_LedSet(1);

  /* Setup SysTick Timer for 1 msec interrupts  */
  if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) while (1) ;

  /* Enable LCD without voltage boost */
  SegmentLCD_Init(false);
  /* Initialize I2C drivers */
  I2CSPM_Init(&ADXL345Handle);

	ADXL345_INIT(ADXL345Handle.port);
	SegmentLCD_Write("ADXL345");
	Delay(500);
	/* Read Device ID */
	SegmentLCD_Write("DEV_ID");
	Delay(500);
	ADXL345_Read_Reg(ADXL345Handle.port, DEVICEID_REG_ADDR,data,1);
	SegmentLCD_LowerHex(data[0]);
	Delay(500);
	/* Read FIFO CTL */
	SegmentLCD_Write("AXISDATA");
	Delay(500);
	/* Infinite loop with test pattern. */
   while(1)
   {
	   // axis_converted_avg.X =  0;
	   // axis_converted_avg.Y =  0;
	   // axis_converted_avg.Z =  0;
	   ADXL345_Read_Reg(ADXL345Handle.port, 0x30,data,1);
	   if(data[0]&0x02)
	   {
		   
		 ADXL345_READ_FIFO(axis_converted_avg);
		    // sprintf(lcd_data,"X:%d",(int)axis_converted_avg.X/i);
		    // SegmentLCD_Write(lcd_data);
			// Delay(500);
			// sprintf(lcd_data,"Y:%d",(int)axis_converted_avg.Y/i);
		    // SegmentLCD_Write(lcd_data);
			// Delay(500);
			// sprintf(lcd_data,"Z:%d",(int)axis_converted_avg.Z/i);
		    // SegmentLCD_Write(lcd_data);
			// Delay(500);  
		 for(i=0;i<33;i++)
		 {
			ADXL345_STEPCOUNT(axis_converted_avg[i]); 
		 }
		 

	   }
	  SegmentLCD_LowerNumber(STEP_COUNT);
	// SegmentLCD_LowerNumber(ADXL345_DATA_CONVERT(axis_data[0]));
	 Delay(500);        
	// SegmentLCD_LowerNumber(ADXL345_DATA_CONVERT(axis_data[1]));
	// Delay(500);          
	// SegmentLCD_LowerNumber(ADXL345_DATA_CONVERT(axis_data[2]));
	// Delay(500);      	
	// ADXL345_Read_Reg(ADXL345Handle.port, 0x1e,data,1);
	// SegmentLCD_LowerHex(data[0]);
	// Delay(500);   
	// ADXL345_Read_Reg(ADXL345Handle.port, 0x1f,data,1);
	// SegmentLCD_LowerHex(data[0]);
	// Delay(500);  
	// ADXL345_Read_Reg(ADXL345Handle.port, 0x20,data,1);
	// SegmentLCD_LowerHex(data[0]);
	// Delay(500);  

	// SegmentLCD_LowerHex(axis_data[1]<< 8 + axis_data[0]);
	// Delay(500);
	// SegmentLCD_LowerHex(axis_data[3]<< 8 + axis_data[2]);
	// Delay(500);
	// SegmentLCD_LowerHex(axis_data[5]<< 8 + axis_data[4]);
	// Delay(1000);
  }


}