void SendTemperature(uint8 channel, int16 adcOffset, int32 opampGainFactor) {
#define NUM_ADC_READINGS 16
    // Select the desired thermocouple input
    AMux_1_Select(channel);
    
    // Wait for op-amp to settle
    CyDelay(1);
    
    // Discard a result from ADC (probably unnecessary)
    ADC_SAR_Seq_1_IsEndConversion(ADC_SAR_Seq_1_WAIT_FOR_RESULT);
    
    // Average together a bunch of ADC readings
    // (note the hardware is already averaging 256 samples at a time)
    int32 adcReadingTotal = 0;
    uint8 i = 0;
    for (i = 0; i < NUM_ADC_READINGS; i++) {
        ADC_SAR_Seq_1_IsEndConversion(ADC_SAR_Seq_1_WAIT_FOR_RESULT);
        adcReadingTotal += ADC_SAR_Seq_1_GetResult16(0) - adcOffset;
    }
    int16 adcReading = adcReadingTotal / NUM_ADC_READINGS;
    
    // Convert to microvolts
    int32 hotJunctionVolts = ADC_SAR_Seq_1_CountsTo_uVolts(0, adcReading);
    
    // Reading was amplified by op-amp
    hotJunctionVolts = hotJunctionVolts * 100 / opampGainFactor;
    
    // Adjust for temperature at cold side of thermocouple
    int32 coldJunctionVolts = Thermocouple_1_GetVoltage(ROOM_TEMPERATURE);
    
    // Read temperature (100 * degrees C)
    int32 temperature = Thermocouple_1_GetTemperature(hotJunctionVolts + coldJunctionVolts);
    
    // Send over serial
    sprintf(
        serialBuf,
        "T%d=%ld (adc=%hd uvHot=%ld uvCold=%ld)\r\n",
        channel + 1,
        temperature,
        adcReading, hotJunctionVolts, coldJunctionVolts
    );
    UART_1_UartPutString(serialBuf);
}
Example #2
0
uint16 read_battery_voltage(){
    return (uint16)(((ADC_SAR_Seq_1_GetResult16(1) * 3.3 / 2048.0) * 10.106  - 1.3278) * 1000.0);
}