Exemplo n.º 1
0
void main ()
{
  hBSP430halSERIAL i2c = hBSP430serialLookup(APP_TMP102_I2C_PERIPH_HANDLE);
  uint8_t pr = 0;

  vBSP430platformInitialize_ni();
  (void)iBSP430consoleInitialize();

  cprintf("I2C interface on %s is %p\n", xBSP430serialName(APP_TMP102_I2C_PERIPH_HANDLE), i2c);
#if BSP430_PLATFORM_PERIPHERAL_HELP
  cprintf("TMP102 I2C Pins: %s\n", xBSP430platformPeripheralHelp(APP_TMP102_I2C_PERIPH_HANDLE, BSP430_PERIPHCFG_SERIAL_I2C));
#endif /* BSP430_PLATFORM_PERIPHERAL_HELP */
  i2c = hBSP430serialOpenI2C(i2c,
                             BSP430_SERIAL_ADJUST_CTL0_INITIALIZER(UCMST),
                             0, 0);
  if (! i2c) {
    cprintf("I2C open failed.\n");
    return;
  }

  (void)iBSP430i2cSetAddresses_ni(i2c, -1, APP_TMP102_I2C_ADDRESS);

  /** Raw number is a 16 bit value.  First 12 bits represent the
   * temperature as a count of 0.0625C values.  (If the LSB is 1, then
   * an extended temperature is used and the 13th bit represents a
   * half count.)  0.625 = 5/8; shifting by 3 gets us the 13-bit
   * value; dividing by 2 accounts for the half-count in extended
   * temperature mode. */
#define TMP102_RAW_TO_dC_(raw_) (5 * ((raw_) >> 3) / 16)
#define TMP102_RAW_TO_dC(raw_) ((0 <= (int)(raw_)) ? TMP102_RAW_TO_dC_(raw_) : -TMP102_RAW_TO_dC_(-(int)(raw_)))
#define dC_TO_dF(dC_) (320 + 9 * (dC_) / 5)

#if 0
  {
    unsigned int data[] = { 0x7FF0, 0x4B00, 0x1900, 0xFFC0, 0xE700, 0xC908 };
    int i;

    for (i = 0; i < sizeof(data)/sizeof(*data); ++i) {
      int temp_dC = TMP102_RAW_TO_dC(data[i]);
      cprintf("temp 0x%04x = %d dC = %d d[degF]\n", data[i], temp_dC, dC_TO_dF(temp_dC));
    }
  }
#endif

  while (1) {
    int rc;
    uint8_t data[2];
    uint16_t raw;

    rc = iBSP430i2cTxData_ni(i2c, &pr, 1);
    if (0 > rc) {
      cprintf("I2C TX ERROR\n");
      break;
    }
    memset(data, 0, sizeof(data));
    rc = iBSP430i2cRxData_ni(i2c, data, sizeof(data));
    if (0 > rc) {
      cprintf("I2C RX ERROR\n");
      break;
    }
    raw = data[1] | (data[0] << 8);
    if (0 == pr) {
      int temp_dC = TMP102_RAW_TO_dC(raw);
      cprintf("temp 0x%04x = %d dC = %d d[degF]\n", raw, temp_dC, dC_TO_dF(temp_dC));
    } else {
      cprintf("reg %d is 0x%04x\n", pr, raw);
    }
    pr = (pr + 1) & 0x03;
    BSP430_CORE_DELAY_CYCLES(BSP430_CLOCK_NOMINAL_MCLK_HZ);
  }
}
Exemplo n.º 2
0
int getSample (sSample * sp,
               int refv,
               unsigned int inch)
{
  unsigned int divisor;
  unsigned int vref_scale = 0;

  memset(sp, 0, sizeof(*sp));
  if (0 != setSource(inch)) {
    return -1;
  }
  __delay_cycles(1000);
#if HAVE_ADC10
  divisor = 1024;
  ADC10CTL0 |= ADC10ENC | ADC10SC;
  while (ADC10CTL1 & ADC10BUSY) {
    ;
  }
  sp->adj = sp->raw = ADC10MEM0;
  ADC10CTL0 &= ~ADC10ENC;
#elif HAVE_ADC12
  divisor = 4096;
  ADC12CTL0 |= ADC12ENC + ADC12SC;
  while (ADC12CTL1 & ADC12BUSY) {
    ;
  }
  sp->adj = sp->raw = ADC12MEM0;
  ADC12CTL0 &= ~ADC12ENC;
#endif

  if (REF_1pX == refv) {
    vref_scale = REF_1pX_SCALE;
#if HAVE_REF
  } else if (REF_2p0 == refv) {
    vref_scale = 2000;
#endif /* HAVE_REF */
  } else if (REF_2p5 == refv) {
    vref_scale = 2500;
  }

  if (cal_adc) {
    unsigned long r32 = sp->raw;
    unsigned int vref_factor;
    int t30;
    int t85;

    if (REF_1pX == refv) {
#if HAVE_REF
      vref_factor = cal_ref->cal_adc_15vref_factor;
#else /* HAVE_REF */
#if defined(__MSP430_HAS_ADC10_B4__)
      vref_factor = 0x8000;
#else /* __MSP430_HAS_ADC10_B4__ */
      vref_factor = cal_adc->cal_adc_15vref_factor;
#endif /* __MSP430_HAS_ADC10_B4__ */
#endif /* HAVE_REF */
      t30 = cal_adc->cal_adc_15t30;
      t85 = cal_adc->cal_adc_15t85;
#if !defined(__MSP430_HAS_ADC10_B4__)
#if HAVE_REF
    } else if (REF_2p0 == refv) {
      vref_factor = cal_ref->cal_adc_20vref_factor;
      t30 = cal_adc->cal_adc_20t30;
      t85 = cal_adc->cal_adc_20t85;
#endif /* HAVE_REF */
    } else if (REF_2p5 == refv) {
#if HAVE_REF
      vref_factor = cal_ref->cal_adc_25vref_factor;
#else /* HAVE_REF */
      vref_factor = cal_adc->cal_adc_25vref_factor;
#endif /* HAVE_REF */
      t30 = cal_adc->cal_adc_25t30;
      t85 = cal_adc->cal_adc_25t85;
#endif /* __MSP430_HAS_ADC10_B4__ */
    } else {
      return -1;
    }

    r32 = ((r32 << 1) * vref_factor) >> 16;
    sp->adj = ((r32 << 1) * cal_adc->cal_adc_gain_factor) >> 16;
    sp->adj += cal_adc->cal_adc_offset;
    if (INCH_TEMP == inch) {
#define ADC_TO_dC(_v) (3000 + (int)((85 - 30) * ((100L * ((int)(_v) - t30)) / (long)(t85 - t30))))
      sp->cCel = ADC_TO_dC(sp->raw);
#define dC_TO_dF(_v) (3200 + (9 * (_v)) / 5)
      sp->cF = dC_TO_dF(sp->cCel);
    }
  }
  if (INCH_TEMP != inch) {
#define ADC_TO_mV(_v) ((unsigned int)(((_v) * (long)vref_scale) / divisor))
    sp->mV = ADC_TO_mV(sp->adj);
  }

  return 0;
}