Skip to main content

Voltmeter

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

Popular posts from this blog

ANALOGUE to DIGITAL (A/D) Converter in PIC16F87X Series

ANALOGUE to DIGITAL (A/D) Converter What is a Sample & Sample rate? A "sample" is a single measurement of amplitude. Sample rate is simply the number of samples (or measurements of amplitude) taken per second. Sampling Intervals Quantization The samples are assigned a binary number approximating their sampled value. Quantizing divides up the sampled voltage range into 2n-1 quantizing intervals, where “n” is the number of bits per sample (the sampling resolution). For example, an 8-bit system can identify 28 (256) discrete sampled signal values (255 quantizing intervals). The amplitude of such a signal can occupy the entire quantizing range. A/D Reference Voltages Successive Approximation A/D Example 1 Example 2  Example 3 A/D Converter Modules in PIC16F87X series 28-pin devices has 5 modules. (AN0-AN4)         PIC16F873        PIC16F876 4...

Other PicBasic commands 01

W e shall n o w brief l y look at the remaining PicBasic commands in alphabetical order w hich are useful during the pr o g ram d e v elopment. More details about these commands can be obtained from the PicBasic manual. EEP R OM EEP R OM Location, (constant, constant,….., constant) Thi s comman d store s constant s i n consecut i v e b yte s i n on-chi p EEP R O M memo r y . Th e command on l y w ork s wit h th e PI C microcontroller s tha t h a v e EEP R OM , suc h a s th e PIC16F84 , PIC16F877, etc . Locatio n i s optional , an d i f omitte d th e f irs t EEP R O M locatio n i s assumed . Constant s ca n be numeri c constant s o r strin g constants . String s ar e store d a s consecut i v e b yte s o f ASCI I v alues . An e xampl e i s g i v e n bel o w . EEP R OM 3, (5, 2, 8)        ‘ Store 5 in location 3, ‘ 2 in location 4, and 8 in ‘ location 5 END END St...