Skip to main content

Temperature Controller

This code is tested on Pic 16f876

This code essentially creates a system that continuously monitors the temperature using an ADC and adjusts a heater to maintain the temperature within a specified range. It's commonly used in applications where precise temperature control is required, such as in incubators, ovens, or climate control systems.

These comments should help you understand the functionality of the code better.


Here is the code

--------------------------------------------------------------------

#include <pic.h> // Include the PIC header file

unsigned int a2d_value; // Variable to store the ADC value

void main() {

    TRISA = 0xFF;    // Configure Port A as input

    TRISC = 0;       // Configure Port C as output


    ADCON0 = 0B01000001; // ADC configuration: Fosc/8, RA0 input, ADC On

    ADCON1 = 0B10001110; // ADC configuration: Right justify, RA0 analog (Vdd/Vss reference)


    OPTION = 0x07; // Timer0 setup

    TMR0 = 0;


    RC0 = 1; // Turn on heater


    while(1) {

        if(T0IF == 1) { // Check if Timer0 overflowed (capacitor charge time)

            ADGO = 1; // Start ADC conversion

            while(ADGO == 1) { // Wait for ADC conversion to complete

                // Do nothing while waiting

            }

            a2d_value = 0;

            a2d_value = ADRESH; // Store the high byte of ADC result

            a2d_value = a2d_value << 8;

            a2d_value = a2d_value + ADRESL; // Combine high and low bytes of ADC result

            if(RC0 == 1) { // If heater is on

                if(a2d_value > 716) { // If temperature > 70°C

                    RC0 = 0; // Turn off heater

                }

            } else { // If heater is off

                if(a2d_value < 511) { // If temperature < 50°C

                    RC0 = 1; // Turn on heater

                }

            }


            T0IF = 0; // Reset Timer0 overflow flag

        }

    }

}

--------------------------------------------------------------------

Let's break down the code step by step to understand its functionality:

#include <pic.h>

This line includes the necessary header file for the PIC microcontroller. It provides definitions and functions specific to the PIC microcontroller family, allowing the code to interact with the hardware.

unsigned int a2d_value;

This line declares an unsigned integer variable named a2d_value. This variable is used to store the result of an analog-to-digital conversion (ADC).

void main() {

This is the entry point of the program. All the executable code lies within this main function.

TRISA = 0xFF;
TRISC = 0;

These lines configure the direction of the I/O pins. TRISA sets all pins of port A as input, while TRISC sets all pins of port C as output. This is crucial for interfacing with external devices or sensors.

ADCON0 = 0B01000001;
ADCON1 = 0B10001110;

These lines configure the ADC module. They set up parameters such as clock source, analog channel selection, and reference voltage. The ADC is used to convert analog input from a temperature sensor (connected to pin RA0) into a digital value.

OPTION = 0x07;
TMR0 = 0;

These lines configure Timer0. Timer0 is often used for timing purposes in microcontroller applications. It's configured here with a specific prescaler value.

RC0 = 1;

This line initializes the heater by setting pin RC0 high. This assumes that RC0 is connected to a transistor or relay controlling the heater.

while(1) {

This initiates an infinite loop, where the main functionality of the program resides.

if(T0IF == 1) {
    // Timer0 overflowed (capacitor charge time)
    // Start ADC conversion
    // Wait for ADC conversion to complete
    // Read ADC result
    // Check temperature and control heater accordingly
    // Reset Timer0 overflow flag
}


Within the loop, the code checks if Timer0 has overflowed. When the overflow occurs, it triggers an ADC conversion to measure the temperature using the sensor connected to pin RA0. Depending on the temperature reading, the heater connected to pin RC0 is turned on or off to maintain a desired temperature range.


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...

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 sto...