Beispiel #1
0
	unsigned int Complete(void)
	{
		unsigned int result = AdcCountsToBatteryVoltage(AdcConversion<input, channel>::ReadADC());
		BATTERY_SENSE_DISABLE();
		if (QueryCalibrationValid())
			result += GetBatteryCalibrationValue();
		Average.AddSample(result);
		return result;
	}
Beispiel #2
0
/* 80 us
 * 
 * conversion time = 13 * ADC12DIV *  1/FreqAdcClock
 * 13 * 8 * 1/5e6 = 20.8 us
 */
void BatterySenseCycle(void)
{ 
  static unsigned char Index = 0;

  xSemaphoreTake(AdcMutex, portMAX_DELAY);
  
  BATTERY_SENSE_ENABLE();
  ENABLE_REFERENCE();

  /* low_bat_en assertion to bat_sense valid is ~100 ns */
  /* Start battery sense conversion */
  AdcCheck();
  CLEAR_START_ADDR();
  ADC12CTL1 |= ADC12CSTARTADD_1;
  ENABLE_ADC();

  WaitForAdcBusy();

  /* Convert the ADC count for the battery input into a voltage 
   * ADC12MEM1: Counts Battery Voltage in ADC counts
   * Result: Battery voltage in millivolts */
  unsigned int Value = (unsigned int)(CONVERSION_FACTOR_BATTERY * (double)ADC12MEM1);

  if (ValidCalibration()) Value += GetBatteryCalibrationValue();

  /* smoothing algorithm: cut extreme values (gap > 20) */
  unsigned char Prev = (Index == 0) ? MAX_SAMPLES - 1: Index - 1;
  if (Sample[BATTERY][Prev])
  {
    int Gap = Value - Sample[BATTERY][Prev];
    if (Charging())
    {
      if (Gap > GAP_BIG) Gap = GAP_BIG;
      else if (Gap < GAP_SMALL_NEGATIVE) Gap = GAP_SMALL_NEGATIVE;
    }
    else
    {
      if (Gap > GAP_SMALL) Gap = GAP_SMALL;
      else if (Gap < GAP_BIG_NEGATIVE) Gap = GAP_BIG_NEGATIVE;
    }
    
    Sample[BATTERY][Index] = Sample[BATTERY][Prev] + Gap;
  }
  else Sample[BATTERY][Index] = Value;

  if (++Index >= MAX_SAMPLES) Index = 0;
  
  BATTERY_SENSE_DISABLE();
  EndAdcCycle(); //xSemaphoreGive()
}