thermo.c

C2C-plus code that uses a 16F84 to read a thermocouple via a Maxim's Max6674. (by Ryan Dumouchel).



/* The following code for a 16F84, will read a thermocouple
via the Maxim Max6674 IC.  Thermocouples allow much faster
time response and are suited for process control.  The 
max6674 provides 10 bit resoloution from 0 to 127.875 degC,  The spi6674 code bit
bangs the thermocouple reading and stores them in spihi (MSB)
and 3 bits in spilo (lsb).  The results are sent to LCD using
a 74ls595 shift register.

Code turns on output, solenoid valve (sv), when it goes above
T1 and shuts it of when it drops below T2.  These setpoints (t1, t2)
are stored in EEPROM and can be changed by the user.

There are three state of operation. Temp_monitor displays the
temp from thermocouple.  By pressing button A & button B 
simultaneously the user can move to state changeT1.  The user increases T1 by pressing button A and decrease
T1 by pressing button B.  By pressing A & B again the user can move
to state changeT2 which is similar to changeT1.  By pressing A & B again the user
completes the loop back to temp_monitor.  

Author: R. Dumouchel Mar 24 2005
*/

#include <system.h>
#include "lcd3.h"
#include "spi6674.h"
#include "eeprom.h"

#define temp_monitor    0  // These are the 3 states of the state machine  
#define changeT1        1   
#define changeT2        2   

#define T1_lob    0x00   // EEPROM Address of low byte for Temp1
#define T1_hib    0x01   // EEPROM Address of hi byte for Temp1
#define T2_lob    0x02
#define T2_hib    0x03

#define buta      3     // Define bit on porta where button a will be read
#define butb      4

char prev_porta, now_porta;
char sv_state, sv_state_prev;
char mode=temp_monitor;
char T1_lo;
char T1_hi;
char T2_lo, T2_hi;
//char changeTlo, changeThi;

void temp1_dispsetup(void);
void MonitorTemp(void);
void changeT(char hi, char lo, char modea);
void change_displayT(char T);

asm
{
  __CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON
}


main()
{
disable_interrupt( GIE );
//clear_bit( STATUS, RP0 );

set_bit(trisa, buta);          // Set RA3 to Input Button A  +5v = open
set_bit(trisa, butb);          // Set RA4 to Input Button B  +5v = open
clear_bit(trisb, 4);           // Output for solenoid on RB4

EEPROM_WRITE(T1_hib, 17);  //Write initial Temp data to T1
EEPROM_WRITE(T1_lob, 6);   //Take out after

EEPROM_WRITE(T2_hib, 15);  //Write initial Temp data to T1
EEPROM_WRITE(T2_lob, 6);

sv_state = 0; // Initialize state as being off
//sv_state_prev = 0; //

T1_hi = EEPROM_READ(T1_hib);  //Read value of T1 from EEPROM
T1_lo = EEPROM_READ(T1_lob);  //Solenoid will switch on if Thermocouple reads higher than this value

T2_hi = EEPROM_READ(T2_hib);  //Read value of T2 from EEPROM
T2_lo = EEPROM_READ(T2_lob);  //Solenoid will switch off if Thermocouple reads higher than this value


LcdSetup();
SPI_Setup();
//MonitorTemp();     //Get temperature
temp1_dispsetup(); //Set up Main Screen for LCD

 while (1)
{
  prev_porta = now_porta;
  now_porta = porta;


// Code below monitors button A and button B 

  switch(mode)
  {
    case temp_monitor:
      if ((prev_porta ^ now_porta & prev_porta) == 00011000b)
        {
        mode = changeT1;
        change_displayT(changeT1);
        }
       else
        {
         MonitorTemp();  //Get thermocouple reading from Max6674 
        }
       break;

    case changeT1:
       if ((prev_porta ^ now_porta & prev_porta) == 00011000b)
         {
          mode = changeT2;
          change_displayT(changeT2);

          EEPROM_WRITE(T1_hib, T1_hi);
          EEPROM_WRITE(T1_lob, T1_lo);
         }

       else
         {
          //change_T1();
          changeT(T1_hi, T1_lo, mode);

         }

      case changeT2:
       if ((prev_porta ^ now_porta & prev_porta) == 00011000b)
         {
          mode = temp_monitor;
          temp1_dispsetup();

          EEPROM_WRITE(T2_hib, T2_hi);
          EEPROM_WRITE(T2_lob, T2_lo);
         }

       else
         {
         // change_T2();
         changeT(T2_hi, T2_lo, mode);
         }

      break;
  }


}


}



void temp1_dispsetup(void)  //Set up Main Screen of LCD
{

   FuncMode();
   LCD4Bit(clear_lcd);
   DatMode();
   LcdWrite("TE:       ");
   LCD4Bit(11011111b); //degree symbol
   LCD4Bit('C');
   FuncMode();    //function mode
   LCD4Bit(line2);
   DatMode();

   LcdWrite("H:");
   LcdInt(T1_hi);
   LCD4Bit('.');
   LcdInt(T1_lo*125);

   LcdWrite(" L:");
   LcdInt(T2_hi);
   LCD4Bit('.');
   LcdInt(T2_lo*125);
   LcdWrite(" SV:");

}

void MonitorTemp(void)
{
  /* sv_state is the state the sv should be in.  If themocouple readings
  spihi & spilo is greater than T1 we want the solenoid to turn on.
  If less than T2 we want the sv to turn off */

  SPI_Read();
 if (spihi > T1_hi) sv_state = 1;
 //if (spihi == T1_hi) & (spilo > T1_lo) sv_state = 1;
 if (spihi < T2_hi) sv_state = 0;
 //if (spihi == T2_hi) & (spilo < T2_lo) sv_state = 0;      

 if (sv_state == 1)  set_bit(portb, 4);
 if (sv_state == 0)  clear_bit(portb, 4);



  LcdSetPos(4);
  DatMode();
  LcdInt(spihi);
  LCD4Bit('.');
  LcdInt(spilo*125);

}

void changeT(char hi, char lo, char modea)
{
FuncMode();
  LCD4Bit(line2);
  DatMode();
  LcdInt(hi);
  LCD4Bit('.');
  LcdInt(lo*125);
  clear_bit(STATUS, RP0 );

  if (prev_porta ^ now_porta & prev_porta & 00001000b) //Increase T1 or T2 depending on mode
   {
     if (lo < 7) lo ++;
     else
      {
       lo = 0;
       hi ++;
      }
   }

   if (prev_porta ^ now_porta & prev_porta & 00010000b) //Decrease T1 or T2 depending on mode
     if (lo > 0) lo --;
     else
      {
       //t1_Sw_l = 0;
       lo=7;
       hi --;
      }
if (modea == changeT1)
 {
  T1_hi = hi;
  T1_lo = lo;
 }

if (modea == changeT2)
 {
  T2_hi = hi;
  T2_lo = lo;
 }

}




void change_displayT(char T)
{
   FuncMode();
   LCD4Bit(clear_lcd);
   DatMode();
   if (T == changeT1)LcdWrite("Chg T1:");
   if (T == changeT2)LcdWrite("Chg T2:");



 }



http://www.sourceboost.com/home.html

Copyright © 2002-2006 SourceBoost Technologies