int16_t temp_get(temp_unit_t unit) { int16_t res; int16_t temp; /* Return if temp sensor driver not initialized */ if (temp_initialized == false) { return EOF; } /* Power up sensor */ TEMP_PORT |= (1 << TEMP_BIT_PWR); /* Init ADC and measure */ adc_init(ADC_CHAN_ADC4, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); adc_conversion_start(); while ((res = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){ ; } #if MEASURE_ADC2 /* Measure external voltage supply, routed to ADC2 through a 470K/100K divider*/ /* AVCC is 3.3 volts if using external supply, else Vbat which will be lower */ /* Convert result to millivolts assuming AVCC is 3.3 volts, on battery it will be lower! */ adc_init(ADC_CHAN_ADC2, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); adc_conversion_start(); while ((ADC2_reading = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){ ; } ADC2_reading = (ADC2_reading*((470+100)*3300UL))/(100*1024UL); #endif adc_deinit(); /* Re-init the adc for buttons. */ key_init(); /* Power down sensor */ TEMP_PORT &= ~(1 << TEMP_BIT_PWR); /* Get corresponding temperature from table */ if (unit == TEMP_UNIT_CELCIUS) { temp = find_temp(res, temp_table_celcius, sizeof(temp_table_celcius)/sizeof(int)) + TEMP_ZERO_OFFSET_CELCIUS; } else /*unit == TEMP_UNIT_FAHRENHEIT*/{ temp = find_temp(res, temp_table_fahrenheit, sizeof(temp_table_fahrenheit)/sizeof(int)) + TEMP_ZERO_OFFSET_FAHRENHEIT; } return temp; }
key_state_t key_state_get(void) { key_state_t ret = KEY_NO_KEY; int16_t res; adc_init(ADC_CHAN_ADC1, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); adc_conversion_start(); while ((res = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){;} adc_deinit(); if (res>0x0370) { ret = KEY_NO_KEY; } else if (res>0x0280) { ret = KEY_DOWN; } else if (res>0x0180) { ret = KEY_LEFT; } else if (res>0x00C0) { ret = KEY_RIGHT; } else { ret = KEY_UP; } if (!(ENTER_PORT & (1<<ENTER_PIN))) { ret |= KEY_ENTER; } return ret; }
/** \brief Read current voltage * \return EOF on error */ double voltage_get() { int16_t result; /* Init ADC and measure */ adc_init(ADC_CHAN_ADC30, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); adc_conversion_start(); while ((result = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){ ; } adc_deinit(); return 1.1 * (1024.0 / (double)result); }
static int16_t supply_voltage_read(void) { int16_t adc_res; // Read band gap voltage with Avcc as ref. Use result to calulate Vcc adc_init(ADC_CHAN_VBG, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); // wait for band gap voltage to stabilize delay_us(150); // read ADC adc_conversion_start(); while ((adc_res = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){;} adc_deinit(); // Return battery voltage in mV return (int16_t)((1100L*1024L)/(long)adc_res); }
/** \brief Read current light's raw value * \return EOF on error */ int16_t sensor_light_get_raw() { int16_t result; if (!light_initialized) { light_init(); } /* Power up sensor */ LIGHT_PORT |= (1 << LIGHT_BIT); /* Init ADC and measure */ adc_init(ADC_CHAN_ADC4, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_128); adc_conversion_start(); while ((result = adc_result_get(ADC_ADJ_RIGHT)) == EOF ){ ; } adc_deinit(); /* Power down sensor */ LIGHT_PORT &= ~(1 << LIGHT_BIT); return result; }