Temperature sensor with PIC16F877a using Analog to Digital convertion.

 Temperature sensor with PIC16F877a using Analog to Digital convertion.


#include <xc.h>

#include "config.h"


// Define oscillator frequency

#define _XTAL_FREQ 4000000


// Define LCD control pins

#define RS PORTDbits.RD0

#define RW PORTDbits.RD1

#define EN PORTDbits.RD2


// Global variables for ADC and temperature calculation

unsigned int a, b, c, d, e, f;

unsigned int temp, adc;

float temperature, adc1;


// Function to send a character to the LCD

void chr(unsigned char b) {

    PORTC = b;  // Send character to PORTC

    RS = 1;     // Set RS pin high for data

    RW = 0;     // Set RW pin low for write mode

    EN = 1;     // Enable LCD

    __delay_ms(100); // Delay for LCD operation

    EN = 0;     // Disable LCD

}


// Function to send command to LCD

void lcd_comand(unsigned char a) {

    PORTC = a;  // Send command to PORTC

    RS = 0;     // Set RS pin low for command

    RW = 0;     // Set RW pin low for write mode

    EN = 1;     // Enable LCD

    __delay_ms(100); // Delay for LCD operation

    EN = 0;     // Disable LCD

}


// Function to send string to LCD

void str(const unsigned char *d, unsigned char n) {

    unsigned char o;

    for (o = 0; o < n; o++) {

        chr(d[o]); // Send each character in the string to the LCD

    }

}


// Function to initialize the LCD

void lcd_init() {

    lcd_comand(0x38); // Initialize LCD in 2 lines, 5x8 font mode

    lcd_comand(0x06); // Set cursor to increment mode

    lcd_comand(0x0C); // Display on, cursor off, blink off

    lcd_comand(0x01); // Clear display

}


void main(void) {

    TRISD = 0;  // Set PORTD as output for LCD control

    TRISC = 0;  // Set PORTC as output for LCD data

    ADIE = 1;   // Enable ADC interrupt

    PEIE = 1;   // Enable peripheral interrupts

    GIE = 1;    // Enable global interrupts


    PORTC = PORTD = 0x00; // Initialize PORTC and PORTD

    lcd_init(); // Initialize LCD

    lcd_comand(0x80); // Set cursor to line 1

    str("sensor:", 7); // Display "sensor:" on LCD


    ADCON0 = 0x41; // Configure ADC control register 0

    ADCON1 = 0x8E; // Configure ADC control register 1


    while (1) {

        ADCON0 = ADCON0 | (0x04); // Start ADC conversion

        adc1 = adc / 2.046;       // Convert ADC value to voltage

        temperature = adc1 * 100; // Convert voltage to temperature

        lcd_comand(0xc0);         // Set cursor to line 2

        // Extract digits of temperature for display

        a = (int)temperature / 10;

        b = (int)temperature % 10;

        c = a % 10;

        d = a / 10;

        e = d % 10;

        f = d / 10;

        // Display temperature on LCD

        chr(f + 0x30); // Display hundreds digit

        chr(e + 0x30); // Display tens digit

        chr(".");      // Display decimal point

        chr(c + 0x30); // Display ones digit

        chr(b + 0x30); // Display tenths digit

    }

      

    return;

}


// Interrupt service routine for ADC conversion

void __interrupt() ISR(void) {

    if (ADIF) {                // Check if ADC interrupt flag is set

        adc = (ADRESH << 8);   // Read higher 8 bits of ADC result

        adc = adc + ADRESL;    // Read lower 2 bits of ADC result

        ADIF = 0;              // Clear ADC interrupt flag

    }

}


Comments

Popular posts from this blog

Digital clock " DS1307 RTC" with PIC16F877A microcontroller and I2C

Smart Elevator simulation with arduino uno and Proteus