This code is designed to read and display the ambient...

September 3, 2025 at 05:05 PM

#include <xc.h> #include "config.h" // ========================== // Definitions // ========================== #define DIGIT1 LATCbits.LATC0 // First digit select #define DIGIT2 LATCbits.LATC1 // Second digit select #define STORE_BTN PORTDbits.RD0 // Store button #define READ_BTN PORTDbits.RD1 // Read button // 7-seg digit patterns (common cathode) const unsigned char SSD_digits[10] = { 0x3F, // 0 0x06, // 1 0x5B, // 2 0x4F, // 3 0x66, // 4 0x6D, // 5 0x7D, // 6 0x07, // 7 0x7F, // 8 0x6F // 9 }; // ========================== // ADC Functions // ========================== unsigned int readADC(void) { ADCON0bits.GO = 1; // Start conversion while(ADCON0bits.GO); // Wait until done return ((ADRESH << 8) + ADRESL); // Return 10-bit result } int readTemperature(void) { unsigned int adc_val = readADC(); // LM35: 10mV/°C ; with Vref=5V and 10-bit ADC: 1 step ≈ 4.88mV return (int)(adc_val * 0.488); // Convert to °C } // ========================== // SSD Functions // ========================== void sendToSSD(int digit) { LATB = SSD_digits[digit]; // Send to segments } void displayNumber(int num) { int tens = (num / 10) % 10; int ones = num % 10; // Show tens DIGIT1 = 1; sendToSSD(tens); __delay_ms(5); DIGIT1 = 0; // Show ones DIGIT2 = 1; sendToSSD(ones); __delay_ms(5); DIGIT2 = 0; } // ========================== // Main // ========================== void main(void) { // I/O setup TRISA = 0x01; // RA0 = analog input TRISB = 0x00; // PORTB = SSD segments TRISC = 0x00; // RC0, RC1 = digit select TRISD = 0xFF; // RD0, RD1 = buttons (inputs) // ADC setup ADCON1 = 0x0E; // AN0 analog, others digital ADCON2 = 0xA9; // Right justified, 8TAD, Fosc/8 ADCON0 = 0x01; // Enable ADC, channel 0 int temp = 0; int last = 0; while(1) { // Read temperature and show it temp = readTemperature(); displayNumber(temp); // Store button if(STORE_BTN == 0) { last = temp; // Save current temperature __delay_ms(300); // Debounce delay } // Read button if(READ_BTN == 0) { // Show stored value instead of live temperature for(int i=0; i<200; i++) { displayNumber(last); } __delay_ms(300); } } } // ========================== // SD Card Placeholders // ========================== // Later we add SPI + FAT FS here void saveToSD(int t) { /* TODO */ } int readFromSD(void) { return 0; }

This code is designed to read and display the ambient temperature from a sensor (likely an LM35 analog sensor), show it on a 7-segment display, and support storing and recalling the last temperature reading using push buttons. Below is a breakdown of its functionality:

1. Hardware Setup

  • Analog Temperature Sensor Connection:
    • Analog temperature data is read via RA0 (analog input, AN0 pin) using the PIC microcontroller's ADC (Analog-to-Digital Converter).
  • 7-Segment Display (SSD):
    • A 2-digit 7-segment display is connected to PORTC for digit selection (DIGIT1, DIGIT2) and PORTB to control segment LEDs through predefined digit patterns (SSD_digits).
  • Buttons:
    • Two buttons are used:
      • STORE_BTN (connected to RD0) saves the current temperature reading.
      • READ_BTN (connected to RD1) recalls and displays the last stored value.

2. ADC (Analog-to-Digital Conversion):

  • The readADC function configures and starts ADC conversion to read temperature data from the sensor.
  • Conversion is based on the LM35 characteristics (10 mV/°C):
    • LM35 output is scaled to degrees Celsius using the (adc_val * 0.488) formula to compute temperature in integer form.

3. 7-Segment Display Control:

  • Two digits of the display are alternately activated (using DIGIT1 and DIGIT2) to display values by multiplexing. The function:
    • displayNumber(int num) splits the input into tens and ones digits and sends the corresponding segment patterns (SSD_digits) to PORTB for each digit.
    • A brief delay (__delay_ms) ensures both digits are shown in sequence quickly enough to appear simultaneous to the human eye.

4. Button Handling:

  • Button presses are read from STORE_BTN and READ_BTN (inputs on RD0 and RD1).
    • If STORE_BTN is pressed, the current temperature is saved in the last variable.
    • If READ_BTN is pressed, the stored temperature (last) is displayed on the SSD for a fixed duration (via repeated calls to displayNumber()).

5. Main Loop:

  • Continuously:
    • Reads live temperature and displays it on the 7-segment display.
    • Checks for button presses to allow storing or recalling temperatures.
    • Implements simple debounce logic with a short delay after each button press (__delay_ms(300)).

6. SD Card Placeholder:

  • The saveToSD() and readFromSD() functions represent planned functionality for storing temperature data on an SD card (e.g., via SPI). These functions are placeholders and are not yet implemented.

Summary:

The purpose of this code is to measure temperature with an LM35 sensor using the PIC microcontroller, display it dynamically on a 2-digit 7-segment display, and provide functionality to store and recall the last measured temperature value using external buttons. Additionally, it includes provisions for future extensions with SD card storage.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node