Thermostat code not working

General discussion on mikroC.
Post Reply
Author
Message
ureche
Posts: 6
Joined: 20 Oct 2014 17:34

Thermostat code not working

#1 Post by ureche » 20 Oct 2014 18:01

Hello everybody, i need a little help with my code, i am working on a thermostat project for a printer fuser. I'm using PIC16F688, with internal oscillator, and disabled mclr. The temperature is displayed ok. The problem is with the buttons. If i try to use the code as it is bellow the buttons are not working, but if i comment the up and down button or if i erase the logarithm from the formula, the buttons are working, my thought is towards the ram of the pic. But when i compile the code it gives this message:

Used RAM (bytes): 83 (35%) Free RAM (bytes): 157 (65%) Used RAM (bytes): 83 (35%) Free RAM (bytes): 157 (65%)

So there is 65% free ram..

I would appreciate your help,
Thanks.

Code: Select all

float Vout;
float Vdd = 5.00;
int maxTemp = 200;
int maxHist = 50;
unsigned int RS2 = 200000;
unsigned int RS1 = 200000;
float Rth;
float temperature;
float Tn = 25 + 273.15;
unsigned int Bth = 3950;
const float ADC_resolution = 0.0048828; // ADC resolution in volts = 5 / 1024
unsigned int adc_value, test_temp, cnt;
unsigned short mask(unsigned short num);
unsigned short digit_no, digit100, digit10, digit1, digit, i;
unsigned short set, but1, but2, but3, tol, ttol, his, se, stem, tstem, ch1, ch2;
unsigned long adc_test;

#define SHIFT_DATA     PORTC.F1         // 74HC595 Data
#define SHIFT_LATCH    PORTC.F2         // 74HC595 Latch
#define SHIFT_CLOCK    PORTC.F0         // 74HC595 Clock

unsigned short mask(unsigned short num) {
   switch (num) {
     case 0 : return 0xC0;
     case 1 : return 0xF9;
     case 2 : return 0xA4;
     case 3 : return 0xB0;
     case 4 : return 0x99;
     case 5 : return 0x92;
     case 6 : return 0x82;
     case 7 : return 0xF8;
     case 8 : return 0x80;
     case 9 : return 0x90;
   }
}

void latch_595() {
   SHIFT_LATCH = 1;
   asm nop;
   SHIFT_LATCH = 0;
}

void shift_out_HC595(char one_digit) {
   char x;
   for (x=0; x<=7; ++x) {
      if (one_digit & 0x80) SHIFT_DATA = 1;
      else SHIFT_DATA = 0;
      one_digit <<= 1;
      SHIFT_CLOCK = 1;
      asm nop;
      SHIFT_CLOCK = 0;
   }
}

void interrupt() {
  if (TMR0IF_bit == 1) {
      if (digit_no==0) {
          PORTC.F3 = 0;
          PORTC.F4 = 0;
          PORTC.F5 = 0;                 // Turn off both displays
          shift_out_HC595(digit1);      // Set mask for displaying ones on PORTD
          latch_595();
          PORTC.F3 = 1;                 // Turn on display for ones (LSD)
          digit_no = 1;
      } else if (digit_no==1) {
          PORTC.F3 = 0;
          PORTC.F4 = 0;
          PORTC.F5 = 0;                 // Turn off both displays
          shift_out_HC595(digit10);     // Set mask for displaying tens on PORTD
          latch_595();
          PORTC.F4 = 1;                 // Turn on display for tens (MSD)
          digit_no = 2;
      } else {
          PORTC.F3 = 0;
          PORTC.F4 = 0;
          PORTC.F5 = 0;                 // Turn off both displays
          shift_out_HC595(digit100);    // Set mask for displaying tens on PORTD
          latch_595();
          PORTC.F5 = 1;                 // Turn on display for tens (MSD)
          digit_no = 0;
      }
      TMR0 = 0;                      // Reset counter TMRO
      INTCON = 0x20;                 // Bit T0IF=0, T0IE=1
    }
}

void Display_Temperature(unsigned int ftemp) {

   digit = ftemp % 10u;
   digit1 = mask(digit);  // Prepare mask for displaying ones

   if (ftemp/10) {
      digit = (char)(ftemp / 10u) % 10u;
      digit10 = mask(digit); // Prepare mask for displaying tens
   } else {
      digit10 = 0xFF;
   }
   if (ftemp/100) {
      digit = (char)(ftemp / 100u) % 10u;
      digit100 = mask(digit); // Prepare mask for displaying tens
   } else {
      digit100 = 0xFF;
   }
}

void SetTemp_Menu(unsigned int fstem) {
  if (se == 0) {
    digit1 = 0xFF;
    digit10 = 0x06;
    digit100 = 0x92;
    delay_ms(200);
    se = 1;
  }
  Display_Temperature(stem);
}

void SetHist_Menu(unsigned int ftol) {
  if (his == 0) {
    digit1 = 0x12;
    digit10 = 0xCF;
    digit100 = 0x89;
    delay_ms(200);
    his = 1;
  }
  Display_Temperature(tol);
}

void main() {

    OPTION_REG = 0x80;             // Set timer TMR0
    TMR0 = 0;
    INTCON = 0xA0;                 // Disable interrupt PEIE,INTE,RBIE,T0IE
    ANSEL = 0b00000001;
    TRISA = 0b00011111;            // PORTA All Inputs, Except RA5
    PORTA = 0b00100000;
    TRISC = 0b00000000;            // set PORTC to be output
    PORTC = 0b00111000;
    CMCON0 = 0x07;                 // Disbale comparators

    ch2=ch1=0;
    but1,but2,but3,set=0;
    his,se=0;

    stem = 145;
    tol = 5;
    
    while(1){
        /*
        // for stability take more adc samples
        adc_test = 0;
        for (cnt=0; cnt <= 7; cnt++) {
            adc_test = adc_test + ADC_Read(0);
            delay_ms(3);
        }
        adc_value = adc_test >> 3;
        */
        adc_value = ADC_Read(0);
        Vout = adc_value * ADC_resolution;
        Rth = ((RS2 * Vdd) / Vout) - RS2;
        temperature = (Bth*Tn) / (Bth + log(Rth / RS2) * Tn);
        temperature = temperature - 273.15;
        test_temp = temperature;

        switch(set) {
          case 0: Display_Temperature(test_temp); break;
          case 1: SetTemp_Menu(stem); break;
          case 2: SetHist_Menu(tol); break;
        }
        
        if(test_temp >= 1 && test_temp <= stem - tol) {
          PORTA.F5=1;
        }
        if(test_temp >= stem || test_temp <= 0) {
          PORTA.F5=0;
        }
        
        if (PORTA.F3==0) but1 = 1;                   // If set button is pressed
        if (but1==1 && PORTA.F3==1) {
                but1 = 0;
                set++;
                if (set==1) {
                    tstem=stem;
                    ttol=tol;
                }
                if (set==2) {
                    se = 0;
                }
                if (set>=3) {
                    set=0;
                    his=0;
                }
        }

        if (PORTA.F1==0) but3 = 1;                   // If up button is pressed
        if (but3==1 && PORTA.F1==1) {
                but3 = 0;

                if (set == 1) {
                        stem = stem + tol;
                        if(stem>maxTemp) stem=1;
                } else if (set == 2) {
                        tol++;
                        if(tol>maxHist) tol=1;
                }

        }

        if (PORTA.F2==0) but2 = 1;                   // If down button is pressed
        if (but2==1 && PORTA.F2==1) {
                but2 = 0;
                if (set == 1) {
                        stem = stem - tol;
                        if(stem<=0) stem=maxTemp;
                } else if (set == 2) {
                        tol--;
                        if(tol<=0) tol=maxHist;
                }
        }

    }
}
The schematic is a bit messy...
Image

ureche
Posts: 6
Joined: 20 Oct 2014 17:34

Re: Thermostat code not working

#2 Post by ureche » 23 Oct 2014 08:18

Hi, the same thing happens with other uC, i tried with pic16f877a, which has more ram than pic16f688, the buttons are working if i delete the log function. Also tried with an lcd instead of 7 segments, and as long as the log function is in the equation the lcd screen is pulsating and no value is displayed. As soon as i remove the log, a value is displayed and buttons are working, the value is not correct.

Any suggestions?

Thanks.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Thermostat code not working

#3 Post by filip » 24 Oct 2014 13:30

Hi,

Which compiler version are you using ?
Have you tried this on the real hardware ?

Regards,
Filip.

ureche
Posts: 6
Joined: 20 Oct 2014 17:34

Re: Thermostat code not working

#4 Post by ureche » 24 Oct 2014 20:13

Hi, i'm using v5.6.1. I tried the circuit on a breadboard, i thought this happens because proteus, but on live hardware the code acts the same.

Today i got an pic16f1825 which has 1k ram, same thing happens, if i remove the logarithm everything works. As an alternative i'm trying to use lookup table interpolation but i don't know if it has the same accuracy.

I would like to use the Beta coefficient equation with logarithm, but i can't figure this out.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Thermostat code not working

#5 Post by filip » 27 Oct 2014 13:14

Hi,

Please, can you update to the version 6.0.0 and see if this helps ?

Regards,
Filip.

Post Reply

Return to “mikroC General”