pastebin

Paste #cGO -- näytä pelkkänä tekstinä -- uusi tämän pohjalta

Värjäys: Tyyli: ensimmäinen rivinumero: Tabin korvaus:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int16_t interpolate_i16(int16_t low_point_value, int16_t high_point_value, uint8_t range, int16_t difference){          //Note: limited ranges! Built for purpose. NOTE: difference parameter changed from int8_t to int16_t because of overflowing.
    return ( low_point_value + ( ( (high_point_value - low_point_value) * difference) / range ) );
}

//NOTE: ATTINY44A
#define INTERNAL_THERMOMETER_M40C_ADC_VALUE         230
#define INTERNAL_THERMOMETER_25C_ADC_VALUE          300
#define INTERNAL_THERMOMETER_85C_ADC_VALUE          370

#define INTERNAL_THERMOMETER_M40C_TEMPERATURE       -40
#define INTERNAL_THERMOMETER_25C_TEMPERATURE        25
#define INTERNAL_THERMOMETER_85C_TEMPERATURE        85



int8_t measure_convert_return_internal_temperature_celsius_x1(){                //Function to measure convert and return temperature in celsius x1 format using internal diode thermometer. NOTE: ususable without EEPROM stored calibration to offset result.
    adc_set_refs_internal_1v1_bandgap();                                        //Switch reference selection to Internal 1V1 bandgap
    adc_ad_channel_switch(AD_CHANNEL_INTERNAL_THERMOMETER);                     //Switch AD channel to internal thermometer
    _delay_ms(1);                                                               //1ms settling time required before conversion!
    uint16_t adc_value = adc_read_ad_channel_ui16_noise_reduction();            //Get ADC value from function that does ADC noise reduction during conversion
    adc_set_refs_vcc();                                                         //Switch reference selection back to VCC. Internal 1.1V bandgap reference is mainly used here
    
    if(adc_value<INTERNAL_THERMOMETER_25C_ADC_VALUE){                           //If we are interpolating -40C to 25C range
        return (int8_t)((interpolate_i16(INTERNAL_THERMOMETER_M40C_TEMPERATURE, INTERNAL_THERMOMETER_25C_TEMPERATURE, (uint8_t)(INTERNAL_THERMOMETER_25C_ADC_VALUE - INTERNAL_THERMOMETER_M40C_ADC_VALUE),(adc_value - INTERNAL_THERMOMETER_M40C_ADC_VALUE))) + global_temperature_offset_celsius_x1);     //Interpolate temperature in range -40C - 25C
    }
    else{                                                                       //Else we are interpolating 25C - 85C range
        return (int8_t)(interpolate_i16(INTERNAL_THERMOMETER_25C_TEMPERATURE, INTERNAL_THERMOMETER_85C_TEMPERATURE, (uint8_t)(INTERNAL_THERMOMETER_85C_ADC_VALUE - INTERNAL_THERMOMETER_25C_ADC_VALUE),(adc_value - INTERNAL_THERMOMETER_25C_ADC_VALUE)) + global_temperature_offset_celsius_x1);        //Interpolate temperature in range 25C - 85C
    }
}