This code to read an analog temperature sensor, convert the reading to a digital value using the ADC, and display the temperature on a 4-digit 7-segment display. The display is updated periodically based on a timer interrupt.
1.Initialization: Setting up ports, configuring ADC, and Timer0.
2.Infinite Loop: Continuously reads analog input and updates the display.
3.ADC Conversion: Reads analog voltage from a sensor.
4.Voltage Calculation: Converts ADC value to voltage.
5.Digit Extraction: Extracts individual digits from the temperature/voltage value.
6.Display Function: Displays the digits on a 4-digit 7-segment display with slight delays.
---------------------------------------------
#include <pic.h> // Include the header file for PIC microcontroller family.
unsigned char PORTB_value[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; // Array containing values for 7-segment display.
unsigned int a2d_value,temp; // Variables for analog to digital conversion and temporary storage.
double voltage; // Variable to store voltage value.
unsigned char dig1,dig2,dig3; // Variables to store individual digits.
#define _XTAL_FREQ 4000000 // Define crystal frequency.
void dis_value(); // Function prototype for displaying value on 7-segment display.
void main() // Main function.
{
TRISB=0; // Set PORTB as output.
TRISA=0x01; // Set RA0 as input.
TRISC=0; // Set PORTC as output.
ADCON0=0B01000001; // Configure ADC: Fosc/8, RA0 input, A2D On.
ADCON1=0B10001110; // Configure ADC: Right justify, RA0 analog(Vdd/Vss ref).
OPTION=0x07; // Timer0 setup.
TMR0=0;
RC0=1; // Turn on heater.
while(1) // Infinite loop.
{
dis_value(); // Call function to display value.
if(T0IF==1){ // Check for Timer0 overflow (cap charge time).
ADGO=1; // Start A2D conversion.
while(ADGO==1){} // Wait for result ready.
a2d_value=0;
a2d_value=ADRESH;
a2d_value=a2d_value<<8;
a2d_value=a2d_value+ADRESL;
voltage=((double)a2d_value/(double)1023)*(double)500; // Calculate voltage.
temp=(unsigned int)voltage;
dig3=temp/100; // Extract hundreds digit.
temp=temp%100;
dig2=temp/10; // Extract tens digit.
dig1=temp%10; // Extract ones digit.
T0IF=0; // Clear Timer0 overflow flag.
}
}
}
void dis_value() // Function to display value on 7-segment display.
{
PORTA=0; // Turn off all digits.
PORTB=PORTB_value[dig3]; // Display hundreds digit.
RB7=1; // Enable digit.
RA2=1; // Turn on digit2.
__delay_ms(5);
RA2=0; // Turn off digit2.
PORTB=PORTB_value[dig2]; // Display tens digit.
RA3=1; // Turn on digit3.
__delay_ms(5);
RA3=0; // Turn off digit3.
PORTB=PORTB_value[dig1]; // Display ones digit.
RA5=1; // Turn on digit4.
__delay_ms(5);
}
----------------------------------------------
#define _XTAL_FREQ 4000000
This line defines the crystal frequency of the microcontroller to be 4 MHz. This information is crucial for timing-related operations in the code, such as generating delays.
unsigned char count; // 8-bit variable to store the count value
unsigned char PORTB_value[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
count: This variable is used to store the count value. It's an 8-bit unsigned integer, meaning it can hold values from 0 to 255.
PORTB_value: This array holds the hexadecimal values corresponding to the digits 0 to 9, encoded for a common-cathode 7-segment display. Each element represents the pattern required to display a specific digit on the 7-segment display.
Configuration Settings:
TRISA=0: Configures all pins of PORTA as output.
ADCON1=0x07: Configures PORTA pins as digital I/O.
TRISB=0x80: Configures RB7 pin as input and all other pins as output.
PORTB=0 and PORTA=0: Initialize PORTB and PORTA to 0.
count=0: Initialize count variable to 0.
Infinite Loop:
* The program enters an infinite loop using while(1). This loop continuously executes the code within its body.
* It checks whether the RB7 input pin is high (RB7==1). If it is, it implies that some external event has occurred, possibly a button press or some sensor activation.
* Inside the conditional block, it introduces a delay of 150 milliseconds (__delay_ms(150)). This delay might be used to debounce the input or to ensure stable readings from external sensors.
* After the delay, it increments the count variable. If count reaches 10, it's reset to 0 to restart the counting sequence.
* It then turns on digit1 (assuming it controls a multi-digit display) by setting RA1 to 1. This line assumes that the RA1 pin is used to control digit1 of a multi-digit display.
* The value corresponding to the current count is retrieved from the PORTB_value array and written to the PORTB register. This is done to display the count value on a 7-segment display.
*I nside a nested while loop, the program waits until RB7 becomes low. This loop effectively waits until the external event that triggered the count increment is over before proceeding.
* After RB7 becomes low, indicating the end of the external event, the program introduces another delay of 150 milliseconds.
* The loop then repeats, waiting for the next external event to occur.
This code is for a PIC microcontroller. It configures the pins of PORTA and PORTB, waits for RB7 (an input pin) to go high, increments a count variable, displays the count on a 7-segment display, and then waits for RB7 to go low again before repeating the process. The delays ensure proper timing and debounce input signals.

Comments
Post a Comment