PIC16F877-LM20&LM35-Configuration&Initialization

by Marwen Maghrebi
#include <xc.h>
#include <stdio.h>
#include <math.h>
#include "uart.h"

// Configuration bits
#pragma config FOSC = HS        // High-Speed Oscillator
#pragma config WDTE = OFF       // Watchdog Timer disabled
#pragma config PWRTE = ON       // Power-up Timer enabled
#pragma config BOREN = ON       // Brown-out Reset enabled
#pragma config LVP = OFF        // Low Voltage Programming disabled
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection disabled
#pragma config WRT = OFF        // Flash Program Memory Write disabled
#pragma config CP = OFF         // Flash Program Memory Code Protection disabled

#define _XTAL_FREQ 8000000     // 8MHz Crystal
#define VREF 5.0f              // Reference voltage

// Function prototypes
void Initialize_ADC(void);
unsigned int Read_ADC(unsigned char channel);
float Convert_To_Temperature_LM20(unsigned int adc_value);
float Convert_To_Temperature_LM35(unsigned int adc_value);
void Display_Temperature(const char* sensor_name, float temperature);

// Initialize ADC for multiple channels
void Initialize_ADC(void) {
    TRISA = 0x03;     // Set RA0 and RA1 as input (binary: 0000 0011)
    ADCON1 = 0x8E;    // Configure AN0 and AN1 as analog, others digital
    ADCON0 = 0x41;    // ADC ON, Channel 0, Fosc/16
    
    __delay_ms(10);   // Wait for ADC to stabilize
}

// Read ADC value from specified channel
unsigned int Read_ADC(unsigned char channel) {
    ADCON0bits.CHS = channel;    // Select ADC channel
    __delay_ms(2);               // Wait for channel to set up
    ADCON0bits.GO = 1;           // Start conversion
    while(ADCON0bits.GO_nDONE);  // Wait for conversion to complete
    return (ADRESH << 8) + ADRESL;
}
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