LM35 sensor data to an LCD in 8-bit mode using a PIC microcontroller

 To send the LM35 sensor data to an LCD in 8-bit mode using a PIC microcontroller, you can directly access the control and data registers of the LCD (typically using the PORT registers for data and control signals). Below is a simple example code for this, assuming you're using a PIC microcontroller and the 16x2 LCD in 8-bit mode.

Assumptions:

  • LM35 sensor is connected to an analog pin (e.g., AN0).
  • LCD is connected to the microcontroller's PORTD for data and PORTC for control signals.
  • PIC16F series is being used, but it can be adapted for other PICs as well.

Simple Code:

#include <xc.h>
#define _XTAL_FREQ 4000000  // Set your oscillator frequency (e.g., 4 MHz)

// Function to initialize LCD
void LCD_Init(void) {
    // Set LCD control pins as output
    TRISC = 0x00;  // PORTC as output for control signals (RS, RW, EN)
    TRISD = 0x00;  // PORTD as output for data

    // Send 8-bit command to LCD
    LCD_Command(0x38); // Function set: 8-bit, 2 lines, 5x7 font
    LCD_Command(0x0C); // Display ON, Cursor OFF
    LCD_Command(0x06); // Entry mode set: increment, no shift
    LCD_Command(0x01); // Clear display
    __delay_ms(2);     // Delay to allow the LCD to clear
}

// Function to send command to LCD
void LCD_Command(unsigned char cmd) {
    PORTCbits.RC0 = 0; // RS = 0 for command mode
    PORTCbits.RC1 = 0; // RW = 0 for write operation
    PORTCbits.RC2 = 1; // EN = 1 to latch the command
    PORTD = cmd;       // Send the 8-bit command to PORTD
    __delay_ms(1);     // Delay for the command to execute
    PORTCbits.RC2 = 0; // EN = 0 to finish the operation
}

// Function to send data to LCD
void LCD_Data(unsigned char data) {
    PORTCbits.RC0 = 1; // RS = 1 for data mode
    PORTCbits.RC1 = 0; // RW = 0 for write operation
    PORTCbits.RC2 = 1; // EN = 1 to latch the data
    PORTD = data;      // Send the 8-bit data to PORTD
    __delay_ms(1);     // Delay for data to appear on screen
    PORTCbits.RC2 = 0; // EN = 0 to finish the operation
}

// Function to display a string on the LCD
void LCD_String(char* str) {
    while(*str) {
        LCD_Data(*str);
        str++;
    }
}

// Function to read analog data from LM35 (connected to AN0)
unsigned int Read_LM35(void) {
    unsigned int value;
    ADCON0bits.CHS = 0x00;  // Select channel AN0
    ADCON0bits.GO = 1;      // Start conversion
    while(ADCON0bits.GO);    // Wait for conversion to finish
    value = (ADRESH << 8) + ADRESL; // Combine the 8-bit result
    return value;
}

void main(void) {
    unsigned int lm35_value;
    char display_value[10];

    // Set the ADCON0 and ADCON1 registers to initialize the ADC
    ADCON1 = 0x06; // Configure pins for analog input
    ADCON0 = 0x01; // Turn on ADC and select AN0

    // Initialize LCD
    LCD_Init();
    
    // Display message
    LCD_String("LM35 Value: ");
    
    while(1) {
        lm35_value = Read_LM35(); // Read LM35 value
        sprintf(display_value, "%u", lm35_value); // Convert to string
        LCD_Command(0xC0); // Move cursor to second line
        LCD_String(display_value); // Display value on LCD
        __delay_ms(500); // Delay before updating the reading
    }
}

Explanation:

  1. LCD Initialization:

    • The LCD_Init() function initializes the LCD in 8-bit mode, sets up the control pins (RS, RW, EN), and sends the necessary commands to configure the LCD.
  2. Reading LM35 Data:

    • The Read_LM35() function reads the analog data from the LM35 sensor connected to AN0 (analog pin 0). The ADC result is read using the ADRESH and ADRESL registers.
  3. Displaying on LCD:

    • The LCD_Data() function sends the actual data (the LM35 reading) to the LCD, while LCD_Command() sends control commands like clearing the display or setting the cursor position.
    • The LCD_String() function is used to display a string of characters on the LCD.
  4. Main Loop:

    • Inside the main loop, the LM35 data is read periodically, converted to a string, and displayed on the second line of the LCD.

Key Points:

  • The code reads the analog data from the LM35 sensor and sends it to the LCD in 8-bit mode by writing directly to the PORTD register for data and the PORTC register for control.
  • The ADCON0 and ADCON1 registers are used to configure the ADC module.
  • This code assumes that the LCD is connected in 8-bit mode, which means the data pins (D0 to D7) are directly connected to PORTD and the control pins (RS, RW, EN) are connected to PORTC.

This is a basic example of using a PIC microcontroller to read an analog sensor (like the LM35) and display the data on an LCD. You can adapt this code to your specific hardware setup and expand it as needed.

Comments

Popular posts from this blog

PIC16F877A - UART Registers

Introduction to SPI Communication on the PIC16F877A

Introduction to UART Communication