PIC16F877-LM20&LM35-TemperatureCalculation&Display

by Marwen Maghrebi
// Convert ADC value to temperature for LM20
float Convert_To_Temperature_LM20(unsigned int adc_value) {
    float Vo = ((float)adc_value * VREF) / 1023.0f;
    float temp = -1481.96f + sqrtf(2.1962e6f + ((1.8639f - Vo) / (3.88e-6f)));
    return temp;
}

// Convert ADC value to temperature for LM35
float Convert_To_Temperature_LM35(unsigned int adc_value) {
    float Vo = ((float)adc_value * VREF) / 1023.0f;
    float temp = Vo * 100.0f;  // LM35 outputs 10mV per °C, so multiply by 100
    return temp;
}

// Display temperature function implementation
void Display_Temperature(const char* sensor_name, float temperature) {
    char buffer[50];
    sprintf(buffer, "%s Temperature: %.2f°C\n\r", sensor_name, temperature);
    UART_Write_Text(buffer);
}

// Main function
void main(void) {
    unsigned int adc_value_LM20, adc_value_LM35;
    float temperature_LM20, temperature_LM35;
    
    Initialize_ADC();
    UART_TX_Init();
    
    while(1) {
        // Read temperature from LM20 (connected to AN0)
        adc_value_LM20 = Read_ADC(0);  // Channel 0 for LM20
        temperature_LM20 = Convert_To_Temperature_LM20(adc_value_LM20);
        Display_Temperature("LM20", temperature_LM20);
        
        // Read temperature from LM35 (connected to AN1)
        adc_value_LM35 = Read_ADC(1);  // Channel 1 for LM35
        temperature_LM35 = Convert_To_Temperature_LM35(adc_value_LM35);
        Display_Temperature("LM35", temperature_LM35);
        
        UART_Write_Text("----------------------------\n\r");

        __delay_ms(1000);  // Wait before next reading
    }
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00