Dear Readers: Welcome to The Engineering Projects


Tuesday, 1 February 2011

Heat Control System - Programming Code

Page Views:

Copyright © 2009. All rights reserved by “TheEngineeringProjects.com
Complete Programming Code in C :

#include <p18f4520.h>//points to the device library so the compiler can interpret registers
#include <portb.h>
#include <usart.h>
#include <ADC.h>
#include <stdlib.h>
#include <stdio.h>
#include <delays.h>

//device configuration
#pragma config  WDT = OFF       //disable watchdog timer
#pragma config  OSC = INTIO67   //setup internal Osc @ 31kHz
#pragma config MCLRE = ON                       //MCLR pin enabled (normal)
#pragma config PBADEN = OFF    //makes PortB digital i/o
#pragma config LVP = OFF       //disables low voltage programming (mode doesn't need PGM)

//assign pins to variables (for our sanity)
//digital outputs:
#define pump1   PORTDbits.RD0 //inside
#define pump2   PORTDbits.RD1 //outside
#define valve1  PORTBbits.RB5 //Zone 1 V1
#define valve2  PORTBbits.RB4 //Zone 1 V2
#define valve3  PORTBbits.RB3 //Zone 1 V3
#define valve4  PORTBbits.RB2 //Zone 2 V1
#define valve5  PORTBbits.RB1 //Zone 2 V2
#define valve6  PORTDbits.RD7 //Zone 3 V1
#define valve7  PORTDbits.RD6 //Zone 3 V2
#define valve8  PORTDbits.RD5 //Zone 4 V1
#define valve9  PORTCbits.RC5 //Zone 5 V1
#define valve10 PORTCbits.RC4 //Zone 5 V2

//inputs:
#define tcouple1    PORTAbits.RA0  //AN0  //analog      -Outside input sensor
#define tcouple2    PORTAbits.RA1  //AN1  //analog      -Outside output sensor
#define tcouple3    PORTAbits.RA2  //AN2  //analog      -Inside input sensor
#define tcouple4    PORTAbits.RA3  //AN3  //analog      -Inside output sensor
#define tcouple5    PORTAbits.RA5  //AN4  //analog      -Attic Sensor
#define flow_meter  PORTAbits.RA4  //T0CKI  //counter   -Safety to prevent pump from burning up
#define tstat1      PORTCbits.RC3  //digital            -Zone1 demand
#define tstat2      PORTCbits.RC2  //digital            -Zone2 demand
#define tstat3      PORTCbits.RC1  //digital            -Zone3 demand
#define tstat4      PORTCbits.RC0  //digital            -Zone4 demand
#define air_sw      PORTDbits.RD3  //spare switches     -Zone5 demand
#define press_sw    PORTBbits.RB0  //int0               -Safety for pipe blowout/leak
#define sw6         PORTDbits.RD2  //spare switches     -spare



//words easier to see than curly braces
#define begin {
#define end }

//instantiation of all our functions
void initialize(void);               //initial power up settings
void short_delay (void);             //for Valve/Pump switching
void long_delay (void);              //for loops to warm up
void sys_idle(void);                 //when no demand we can rest
void shutdown(void);                 //gracefully shut down system
void sampleADC(void);                //record current tcouple values
void InterruptHandlerHigh (void);    //process Pressure Switch interrupt



//our variables:
unsigned char attic_demand           ;//calculation for preventing condensation
char Zdemand                ;//1=A zone requests heat; 0=No zones request heat
unsigned char SystemOn      ;//1=Currently heating, 0=new heating
unsigned char tstat_sel     ;//used to define which tstat to sample
unsigned int tstat_num[6]   ;//most recent sample temp of each tcouple ([0] not used)
int oldflowcount            ;//last flow count to compare against

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// High priority interrupt vector

#pragma code InterruptVectorHigh = 0x08
void
InterruptVectorHigh (void)
begin

  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
end

// High priority interrupt routine

#pragma code
#pragma interrupt InterruptHandlerHigh


void
InterruptHandlerHigh ()
begin


      shutdown();

      putrsUSART( "ERROR: Pressure lost! System shut down.\n\r" );

      INTCONbits.INT0IF = 0;            //clear interrupt flag

      while(1)//stick in this loop until system is reset
      begin

      putrsUSART( "ERROR: Pressure lost! Shutting Down system.\n\r" );
        //recommend putting in a buzzer here to notify houshold of failure

      end//while(1)
//    end//if(INT0IF)
end//interrupthandlerHigh

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------


void main (void)
begin

//initialize the system to a known state
initialize();

putrsUSART("System Initialized.\n\r\n\r");

  while (1)
    begin

//****test area*******************

//****test area*******************

sampleADC();
attic_demand = ( air_sw && ( tstat_num[5] < 50 ) );

  Zdemand = (tstat1 | tstat2 | tstat3 | tstat4 | attic_demand);

short_delay();

if(Zdemand==0)//no zones demand heat
  begin

      //check domestic hot water******************************//*
                                                              //*
        if( tstat_num[2] < 100 ) //domestic water is cold     //*
          begin                                               //*
                                                              //*
            pump2 = 0;//turn on pump2                         //*
                                                              //*
          end                                                 //*
        if( tstat_num[2] > 115 ) //domestic water is hot      //*
          begin                                               //*
                                                              //*
            pump2 = 1;//turn off pump2                        //*
                                                              //*
          end                                                 //*
      //end domestic hot water section************************//*


  putrsUSART( "No Demand found.\n\r" );

    shutdown();//ensure everything is off
    sys_idle();

  end
else//service the zones
  begin

//******************************************************************************
//******************************************************************************
        if(tstat1)
          begin

          putrsUSART( "\n\rZone 1 requested heat.\n\r" );

          oldflowcount = TMR0L;//samples counter for later comparison

                valve1 = 0;//open Valve 1
                putrsUSART( "Zone 1 Valve 1 opened.\n\r" );

                short_delay();//wait for valve to open

                valve2 = 1;//turn off all the other valves
                valve3 = 1;
                valve4 = 1;
                valve5 = 1;
                valve6 = 1;
                valve7 = 1;
                valve8 = 1;
                valve9 = 1;
                valve10 = 1;

                if(pump1)//inside pump is off
                      begin

                        pump1 = 0;
                        putrsUSART( "Pump 1 turned on.\n\r" );

                      end

                    if(pump2)//outside pump is off
                      begin
                        pump2 = 0;
                        putrsUSART( "Pump 2 turned on.\n\r" );
                      end

      if (oldflowcount == TMR0L)//blockage detected
        begin
            shutdown();
            putrsUSART( "ERROR: Blockage detected! Shutting Down system.\n\r" );
            while(1){};
        end;

                long_delay();//let loop warm up

                sampleADC();

                while( (tstat_num[4] < 110 ) )//wait for loop to warm up
                  begin
                      long_delay();
                      sampleADC();
                  end


            if(!valve1)//valve 1 is on
              begin

                oldflowcount = TMR0L;//samples counter for later comparison

                valve2 = 0;//open Valve 2
                putrsUSART( "Zone 1 Valve 2 opened.\n\r" );

                short_delay();//wait for valve to open

                valve1 = 1;//turn off all the other valves
                valve3 = 1;
                valve4 = 1;
                valve5 = 1;
                valve6 = 1;
                valve7 = 1;
                valve8 = 1;
                valve9 = 1;
                valve10 = 1;

              end

      if (oldflowcount == TMR0L)//blockage detected
        begin
            shutdown();
            putrsUSART( "ERROR: Blockage detected! Shutting Down system.\n\r" );
            while(1){};
        end;


            long_delay();//let loop warm up
            sampleADC();

                while( (tstat_num[4] < 110 ) )//wait for loop to warm up
                  begin
                      long_delay();
                      sampleADC();
                  end


            if(!valve2)//valve 2 is currently on
              begin

                oldflowcount = TMR0L;//samples counter for later comparison

                valve3 = 0;//open Valve 3
                putrsUSART( "Zone 1 Valve 3 opened.\n\r" );

                short_delay();//wait for valve to open

                valve1 = 1;//turn off all the other valves
                valve2 = 1;
                valve4 = 1;
                valve5 = 1;
                valve6 = 1;
                valve7 = 1;
                valve8 = 1;
                valve9 = 1;
                valve10 = 1;

              end

      if (oldflowcount == TMR0L)//blockage detected
        begin
            shutdown();
            putrsUSART( "ERROR: Blockage detected! Shutting Down system.\n\r" );
            while(1){};
        end;


            long_delay();//let loop warm up
            sampleADC();

                while( (tstat_num[4] < 110 ) )//wait for loop to warm up
                  begin
                      long_delay();
                      sampleADC();
                  end


            putrsUSART( "Zone 1 has been serviced\n\r" );

          end//Zone 1 has been serviced

//******************************************************************************
//******************************************************************************
        if(tstat2)
          begin

          putrsUSART( "\n\rZone 2 requested heat.\n\r" );

                oldflowcount = TMR0L;//samples counter for later comparison

                valve4 = 0;//open Valve 4
                putrsUSART( "Zone 2 Valve 1 opened.\n\r" );

                short_delay();//wait for valve to open

                valve1 = 1;//turn off all the other valves
                valve2 = 1;
                valve3 = 1;
                valve5 = 1;
                valve6 = 1;
                valve7 = 1;
                valve8 = 1;
                valve9 = 1;
                valve10 = 1;

                    if(pump1)//inside pump is off
                      begin

                        pump1 = 0;
                        putrsUSART( "Pump 1 turned on.\n\r" );
                      end

                    if(pump2)//outside pump is off
                      begin
                        pump2 = 0;
                        putrsUSART( "Pump 2 turned on.\n\r" );
                      end

      if (oldflowcount == TMR0L)//blockage detected
        begin
            shutdown();
            putrsUSART( "ERROR: Blockage detected! Shutting Down system.\n\r" );
            while(1){};
        end;


            long_delay();//let loop warm up
            sampleADC();

                while( (tstat_num[4] < 110 ) )//wait for loop to warm up
                  begin
                      long_delay();
                      sampleADC();
                  end

            if(!valve4)//valve 4 is currently on
              begin

                oldflowcount = TMR0L;//samples counter for later comparison

                valve5 = 0;//open Valve 2
                putrsUSART( "Zone 2 Valve 2 opened.\n\r" );

                short_delay();//wait for valve to open

                valve1 = 1;//turn off all the other valves
                valve2 = 1;
                valve3 = 1;
                valve4 = 1;
                valve6 = 1;
                valve7 = 1;
                valve8 = 1;
                valve9 = 1;
                valve10 = 1;

              end

      if (oldflowcount == TMR0L)//blockage detected
        begin
            shutdown();
            putrsUSART( "ERROR: Blockage detected! Shutting Down system.\n\r" );
            while(1){};
        end;


            long_delay();//let loop warm up
            sampleADC();

                while( (tstat_num[4] < 110 ) )//wait for loop to warm up
                  begin
                      long_delay();
                      sampleADC();
                  end

            putrsUSART( "Zone 2 has been serviced\n\r" );

           end//Zone 2 has been serviced

//******************************************************************************
//******************************************************************************
        if(tstat3)
          begin

          putrsUSART( "\n\rZone 3 requested heat.\n\r" );

                oldflowcount = TMR0L;//samples counter for later comparison

                valve6 = 0;//open Valve 1
                putrsUSART( "Zone 3 Valve 1 opened.\n\r" );

                short_delay();//wait for valve to open

                valve1 = 1;//turn off all the other valves
                valve2 = 1;
                valve3 = 1;
                valve4 = 1;
                valve5 = 1;
                valve7 = 1;
                valve8 = 1;
                valve9 = 1;
                valve10 = 1;

                    if(pump1)//inside pump is off
                      begin

                        pump1 = 0;
                        putrsUSART( "Pump 1 turned on.\n\r" );
                      end

                    if(pump2)//outside pump is off
                      begin
                        pump2 = 0;
                        putrsUSART( "Pump 2 turned on.\n\r" );
                      end

      if (oldflowcount == TMR0L)//blockage detected
        begin
            shutdown();
            putrsUSART( "ERROR: Blockage detected! Shutting Down system.\n\r" );
            while(1){};
        end;


            long_delay();//let loop warm up
            sampleADC();

                while( (tstat_num[4] < 110 ) )//wait for loop to warm up
                  begin
                      long_delay();
                      sampleADC();
                  end

            if(!valve6)//valve 6 is currently on
              begin

                oldflowcount = TMR0L;//samples counter for later comparison

                valve7 = 0;//open Valve 2
                putrsUSART( "Zone 3 Valve 2 opened.\n\r" );

                short_delay();//wait for valve to open

                valve1 = 1;//turn off all the other valves
                valve2 = 1;
                valve3 = 1;
                valve4 = 1;
                valve5 = 1;
                valve6 = 1;
                valve8 = 1;
                valve9 = 1;
                valve10 = 1;

              end

      if (oldflowcount == TMR0L)//blockage detected
        begin
            shutdown();
            putrsUSART( "ERROR: Blockage detected! Shutting Down system.\n\r" );
            while(1){};
        end;


            long_delay();//let loop warm up
            sampleADC();

                while( (tstat_num[4] < 110 ) )//wait for loop to warm up
                  begin
                      long_delay();
                      sampleADC();
                  end

            putrsUSART( "Zone 3 has been serviced\n\r" );

          end//Zone 3 has been serviced

//******************************************************************************
//******************************************************************************
        if(tstat4)
          begin

          putrsUSART( "\n\rZone 4 requested heat.\n\r" );

                oldflowcount = TMR0L;//samples counter for later comparison

                valve9 = 0;//open Valve 1
                putrsUSART( "Zone 4 Valve 1 opened.\n\r" );

                short_delay();//wait for valve to open

                valve1 = 1;//turn off all the other valves
                valve2 = 1;
                valve3 = 1;
                valve4 = 1;
                valve5 = 1;
                valve6 = 1;
                valve7 = 1;
                valve8 = 1;
                valve10 = 1;

                    if(pump1)//inside pump is off
                      begin
                        pump1 = 0;
                        putrsUSART( "Pump 1 turned on.\n\r" );
                      end

                   if(pump2)//outside pump is off
                      begin
                        pump2 = 0;
                        putrsUSART( "Pump 2 turned on.\n\r" );
                      end
//              end

      if (oldflowcount == TMR0L)//blockage detected
        begin
            shutdown();
            putrsUSART( "ERROR: Blockage detected! Shutting Down system.\n\r" );
            while(1){};
        end;


            long_delay();//let loop warm up
            sampleADC();

                while( (tstat_num[4] < 110 ) )//wait for loop to warm up
                  begin
                      long_delay();
                      sampleADC();
                  end


            if(!valve9)//valve 8 is currently on
              begin

                oldflowcount = TMR0L;//samples counter for later comparison

                valve10 = 0;//open Valve 2
                putrsUSART( "Zone 3 Valve 2 opened.\n\r" );

                short_delay();//wait for valve to open

                valve1 = 1;//turn off all the other valves
                valve2 = 1;
                valve3 = 1;
                valve4 = 1;
                valve5 = 1;
                valve6 = 1;
                valve7 = 1;
                valve8 = 1;
                valve9 = 1;

              end

      if (oldflowcount == TMR0L)//blockage detected
        begin
            shutdown();
            putrsUSART( "ERROR: Blockage detected! Shutting Down system.\n\r" );
            while(1){};
        end;


            long_delay();//let loop warm up
            sampleADC();

                while( (tstat_num[4] < 110 ) )//wait for loop to warm up
                  begin
                      long_delay();
                      sampleADC();
                  end

            putrsUSART( "Zone 4 has been serviced\n\r" );

          end//Zone 4 has been serviced

//******************************************************************************
//******************************************************************************
        if( attic_demand )//this loop prevents condensation in attic
          begin

          putrsUSART( "\n\rZone 5 requested heat.\n\r" );

                oldflowcount = TMR0L;//samples counter for later comparison

                valve8 = 0;//open Valve 1
                putrsUSART( "Zone 5 Valve 1 opened.\n\r" );

                short_delay();//wait for valve to open

                valve1 = 1;//turn off all the other valves
                valve2 = 1;
                valve3 = 1;
                valve4 = 1;
                valve5 = 1;
                valve6 = 1;
                valve7 = 1;
                valve9 = 1;
                valve10 = 1;

                    if(pump1)//inside pump is off
                      begin
                        pump1 = 0;
                        putrsUSART( "Pump 1 turned on.\n\r" );
                      end

                   if(pump2)//outside pump is off
                      begin
                        pump2 = 0;
                        putrsUSART( "Pump 2 turned on.\n\r" );
                      end

      if (oldflowcount == TMR0L)//blockage detected
        begin
            shutdown();
            putrsUSART( "ERROR: Blockage detected! Shutting Down system.\n\r" );
            while(1){};
        end;


               long_delay();
               sampleADC();

               putrsUSART( "Zone 5 has been serviced\n\r" );

          end//Zone 5 has been serviced

  end//if (Zdemand)

    end//while(1)
end//main


//initialize all the stuff for power up
void initialize(void)
begin

    printf(stdout,"%dSystem Initializing\n\r\n\r");

   OSCCONbits.IRCF0 = 1;
   OSCCONbits.IRCF1 = 1;
   OSCCONbits.IRCF2 = 1; //8Mhz internal oscillator

   while(!OSCCONbits.IOFS); //wait for oscillator to stabilize

// RCSTAbits.SPEN = 1;




//setup for INT0 (Pressure Switch)
INTCONbits.GIE = 1; //enables all global interrupts
INTCONbits.INT0IE = 1; //enables INT0 from external source
INTCON2bits.INTEDG0 = 0; //interrupt on rising edge of INT0 pressure switch

//setup Flow Meter (Timer0)
T0CON = 0xB8;
INTCONbits.TMR0IE = 0; //disable interrupt when ovflow

//setup port directions
TRISA = 0xFF;//port direction
TRISB = 0xC1;//port direction
TRISC = 0xCF;//port direction
TRISD = 0x1C;//port direction


//USART setup
     OpenUSART(USART_TX_INT_OFF &         //USART config
     USART_RX_INT_OFF &
     USART_ADDEN_OFF &
     USART_ASYNCH_MODE &
     USART_EIGHT_BIT &
     USART_CONT_RX &
      USART_BRGH_LOW, 12);           //9600 baud rate

//initial value of internal params
SystemOn      = 0;

shutdown();


end

//simple delay tasks for testing (uses ticks not seconds)
void short_delay (void)//short time for valve transitions
begin

Delay1KTCYx(1000);

end//shortdelay

void long_delay (void)//long time to heat up loop
begin

Delay10KTCYx(100000);

end//longdelay


//gracefully turn the system off
void shutdown(void)
begin

        pump1     = 1;
//        pump2     = 1;//domestic water will continue to run

        short_delay();//short delay to wait for pumps to stop

        valve1    = 1;//close all valves
        valve2    = 1;
        valve3    = 1;
        valve4    = 1;
        valve5    = 1;
        valve6    = 1;
        valve7    = 1;
        valve8    = 1;
        valve9    = 1;
        valve10   = 1;

end

void sys_idle(void)
begin

      long_delay();
      long_delay();
      long_delay();
      long_delay();
      long_delay();
      long_delay();

end//sys_idle


//sample tstats
void sampleADC(void)
begin

unsigned int adc_result     ;//temporary assignment from adc
char i;

OpenADC(ADC_FOSC_RC & ADC_RIGHT_JUST & ADC_12_TAD,ADC_CH0 & ADC_INT_OFF, 0); //open adc port for reading
ADCON1 =0x09; //set VREF+ to VDD and VREF- to GND (VSS)

    for (i = 1; i < 6; i++)
    begin

        switch(i)
        begin

          case 1:
              SetChanADC(ADC_CH0); //tstat1

              Delay10TCYx(5);

              ConvertADC(); //perform ADC conversion
              while(BusyADC()); //wait for result

              adc_result = ReadADC(); //get ADC result

              Delay10TCYx(5);;

              break;

          case 2:
              SetChanADC(ADC_CH1); //tstat2

              Delay10TCYx(5);

              ConvertADC(); //perform ADC conversion
              while(BusyADC()); //wait for result

              adc_result = ReadADC(); //get ADC result

              Delay10TCYx(5);

              break;

          case 3:
              SetChanADC(ADC_CH2); //tstat3

              Delay10TCYx(5);

              ConvertADC(); //perform ADC conversion
              while(BusyADC()); //wait for result

              adc_result = ReadADC(); //get ADC result

              Delay10TCYx(5);

              break;

          case 4:
              SetChanADC(ADC_CH3); //tstat4

              Delay10TCYx(5);

              ConvertADC(); //perform ADC conversion
              while(BusyADC()); //wait for result

              adc_result = ReadADC(); //get ADC result

              Delay10TCYx(5);

              break;

          case 5:
              SetChanADC(ADC_CH4); //tstat5

              Delay10TCYx(5);

              ConvertADC(); //perform ADC conversion
              while(BusyADC()); //wait for result

              adc_result = ReadADC(); //get ADC result

              Delay10TCYx(5);
              break;

           default:
//              SetChanADC(ADC_CH0); //tstat1
              break;
          end//case


if(i<5)//water system tcouples
begin
  //adc_result = 0-1000; Temp range is 60-180degF
  tstat_num[i] = ( (adc_result/8) + 60);//scale value
end
else//attic tcouple
begin
  //adc_result = 0-1000; Temp range is 0-100degF
  tstat_num[i] = ( (adc_result/10));//scale value
end

              fprintf(stdout, "Tcouple #%#i", i);
              fprintf(stdout, ": %#i", tstat_num[i]);
              fprintf(stdout, " DegF\n\r");

    end//for loop

CloseADC();//done using it

end

If you don't want to get yourself into Serious Technical Trouble while doing your programming OR technical projects then just sit back and relax and let us do the Job for you at a fairly reasonable cost. Submit your project details by Clicking Here »

About the Author

I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry.

In 61 people's circles

Subscribe To Get FREE Tutorials!

0 comments:

Confused? Feel free to ask

Your feedback is always appreciated. I will try to reply to your queries as soon as time allows.
Note:-
Please do not spam Spam comments will be deleted immediately upon my review.

Regards,
Admin

Post a Comment

 

Recent Posts

Join Me On Facebook

3000+ Followers

Followers

Recent Comments

Follow Me On Twitter

1112+ Followers