Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help with the code to measure himidity,...

Status
Not open for further replies.

Osawa_Odessa

Banned
Full Member level 3
Joined
Dec 31, 2012
Messages
168
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Visit site
Activity points
0
Hi, I need your help again! I am having a hard time with the code to measure temperature, humidity and control a fan.
Code:
#include <16f877a.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES RESERVED                 //Used to set the reserved FUSE bits

#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3) 
#use delay(clock=20000000)
#include <lcd.c>


#bit set_ra5=0x85.5
#bit RC0=0x87.0
#bit RC3=0x87.3
#byte trisc=0x07
#byte trisa=0x85
#byte trisb=0x86
#byte trise=0x89
#byte porte=0x09
#byte portb=0x06
#byte portc=0x07
#byte porta=0x05

//khai bao bien cho quet led
int8 maled[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};

//khai bao bien dem
int8 count1=0,count2, x;

//bien doc nhiet do
int16 t,t1;

// 1 so bien kiem tra dieu kien
int1 kt1=0,kt2=0;
  //kt6 kiem tra nut ok dc nhan
      /*kt1 la bien 
      kt2 la bien de chon led ben trai hay ben phai cua led doi sang
       */
unsigned int16 frequency=0;
unsigned int8  enable_display=0;
unsigned int8  count;
//cac ham dung trong chuong trin
unsigned int8 HS1101_GetHumi(unsigned int8 frequency);
const unsigned int16 HS1101_Table[101]={
                         8109,8090,8070,8051,8033,8015,7997,7979,7961,7944,
                         7927,7910,7894,7878,7862,7846,7830,7815,7799,7784, 
                         7769,7755,7740,7726,7711,7697,7683,7669,7655,7641,
                         7628,7614,7600,7587,7574,7560,7547,7534,7521,7507,
                         7494,7481,7468,7455,7442,7429,7416,7403,7390,7377,
                         7364,7350,7337,7324,7311,7298,7284,7271,7257,7244,
                         7230,7216,7203,7189,7175,7161,7147,7132,7118,7103,
                         7089,7074,7059,7045,7029,7014,6999,6984,6968,6952,
                         6936,6920,6904,6888,6872,6855,6838,6821,6804,6787,
                         6770,6752,6735,6717,6699,6680,6662,6644,6625,6606,
                         6587};
                         
 unsigned int8  HS1101_GetHumi(unsigned int8 frequency)
{
   unsigned int8 j;
   for(j=0;j<101;j++)
   {
      if((frequency+25)>HS1101_Table[j])return j;
   }
   return 100;
}                        
#int_timer0
void ngat_timer0()
{ 
    count1++;                     //bien dem cho update thoi gian,nhiet do
    kt2++; 
   // QUET LED 7 THANH
   if(kt2==1)                    //0.0131072s
   {    
     //RC0=1;                     //
    output_c(0x01);
    portb=maled[t/10];
    
   }                             //
   else                          //
   {   
      //RC1=1;                     //
      output_c(0x08);            //
      portb=maled[t%10];
     
   }
   //update thoi gian,nhiet do
   if(count1>=50)       //0.66 giay update 1 lan
   {
               
      t=read_adc();              //update nhiet do
      t=t*100/204;
      t1=read_adc();
      t1=t1*100/204;
 
      count1=0;
      kt1=1;
   }  
 [B]  disable_interrupts(GLOBAL);
   set_timer0(5);
   count++;
   if(count==78)
   { 
      frequency=get_timer1();
      set_timer1(0);
      count=0;
   enable_display   =1;
    
   }
   enable_interrupts(GLOBAL);
   }[/B]

#int_timer2
void dk_dongco()
{
   if((0<=t1)&&(t1<40)) set_pwm1_duty(0);
   else if(t1>=60) set_pwm1_duty(624);
   else if( (t1<60)&&(t1>=40)) set_pwm1_duty((t1-40)*15+312);
   set_timer2(0);
} 
void main() 
{   
   unsigned int8 humi;
   //khoi tao lcd
   lcd_init();
  
   //khoi tao cac port
   set_ra5=1;
   //trise=0b111;
   set_tris_e(0b111);
   //porte=0x00;
   output_e(0);
   //trisb=0b10000000;
   set_tris_b(0b10000000);
   //portb=0x00;
   output_b(0);
   //trisc=0b11100000;
   set_tris_c(0b11100010);
   //trisc=0b11100000;
   
   //output_c(0);
   
   //setup adc
   setup_adc_ports(AN0);
   setup_adc(adc_clock_internal);
   set_adc_channel(PIN_A0);
   delay_us(10);
   
   //setup timer0
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
   set_timer0(5);
   enable_interrupts(int_timer0);
   
   //setup timer1
  // enable_interrupts(INT_TIMER1);            
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); 
   set_timer1(0);
   enable_interrupts(INT_TIMER1); 
 
   //setup timer2
   //enable_interrupts(int_timer2);
   setup_ccp1(ccp_pwm);
   setup_timer_2(t2_div_by_16,155,1);
   //dk_dongco();
   set_timer2(0);//vi da set trong ham ngat rui.
   enable_interrupts(int_timer2);
   
   //ngat toan cuc
   enable_interrupts(global);
   
   while(true)
   {
  [B] 
  if(enable_display)
   {
     printf(lcd_putc,"%u",32);
    humi= HS1101_GetHumi(frequency);
     enable_display=0; 
   printf(lcd_putc,"%u",humi);
   } [/B]
  
   }
}
The problem located in two bold parts in the code. In the timer0 interrupt , I can check and make sure that "enable_display" really has value 1 but the code in if in main doesn't happen.
disable_interrupts(GLOBAL);
set_timer0(5);
count++;
if(count==78)
{
frequency=get_timer1();
set_timer1(0);
count=0;
enable_display =1;

}
enable_interrupts(GLOBAL);
}
Code:
 if(enable_display)
   {
     printf(lcd_putc,"%u",32);
    humi= HS1101_GetHumi(frequency);
     enable_display=0; 
   printf(lcd_putc,"%u",humi);
   }
When I remove enable_interrupts(GLOBAL) in timer0 interrupt, the if code can be done but I can remove " enable_interrupts(GLOBAL)".
 
Last edited:

To be more specific,
I used Timer 0 for setting time scan LED for temperature and used Timer0 and Timer 1 to measure Frequency. Timer2 for PWM.

- - - Updated - - -

You mean humi is not displayed on LCD? Make humi a gloabal variable.

The problem is that all instructions in if doesn't not occur even thouth enable_display =1.

- - - Updated - - -

Here Timer0 is set upRTCC_INTERNAL|RTCC_DIV_256 and set_timer0(5) therefore it will have a period is 0.0131072s(overflow).
Code:
disable_interrupts(GLOBAL);
set_timer0(5);
count++;
if(count==78)
{ 
frequency=get_timer1();
set_timer1(0);
count=0;
enable_display =1;

}
enable_interrupts(GLOBAL);
}
IF count = 78 => the time elapsed is 78 x 0.0131072s = 1s.
Therefore, in order to measure frequency I need to count the number of periods in the time 1s, frequency=get_timer1();.

- - - Updated - - -

Timer2 is currently used for PWM. Could you tell me if I can use it for both PWM and led scan? I think it is better for using Timer0 only for Frequency not include led scaning.
 

Where is the isr for timer1 and ccp?

Timer1 is used in capture mode. The Frequency from NE555 is connected to the timer through CCP2 of pic.
Timer 2 is for PWM:
#int_timer2
void dk_dongco()
{
if((0<=t1)&&(t1<40)) set_pwm1_duty(0);
else if(t1>=60) set_pwm1_duty(624);
else if( (t1<60)&&(t1>=40)) set_pwm1_duty((t1-40)*15+312);
set_timer2(0);
}
I think what you asked is here:
disable_interrupts(GLOBAL);
set_timer0(5);
count++;
if(count==78)
{
frequency=get_timer1();
set_timer1(0);
count=0;
enable_display =1;

}
enable_interrupts(GLOBAL);
}

- - - Updated - - -

Opps, the zip file I still not connected to NE555. I used pulse generator in Proteus to create frequency for simulation.
 

In the second piece of code disable_interrupts(GLOBAL); disables all interrupts including timer0, timer1, ccp. During that time even timer0 and timer1 interrupts will not occur. Is that what you want to do?
 

In the second piece of code disable_interrupts(GLOBAL); disables all interrupts including timer0, timer1, ccp. During that time even timer0 and timer1 interrupts will not occur. Is that what you want to do?

This part is really confusing me. I have found two codes in net, one for temperature and fan control and the other is for humidity. The part bellow is from humidity code:
Code:
disable_interrupts(GLOBAL);
set_timer0(5);
count++;
if(count==78)
{ 
frequency=get_timer1();
set_timer1(0);
count=0;
enable_display =1;

}
enable_interrupts(GLOBAL);
}
I am confused why we need to disable interrupt in this time. Here is the code for humidity;
Code:
#include "main.h"
#include "var.h"
#include "lcd16x2/lcd16x2.c"
#include "hs1101/hs1101.c"
#INT_RTCC
void  ngat_timer0(void) 
{
   disable_interrupts(GLOBAL);
   set_timer0(5);
   count++;
   if(count==100)
   {
      frequency=get_timer1();
      set_timer1(0);
      count=0;
      enable_display=1;
   }
   enable_interrupts(GLOBAL);   
}

void main()
{
   uint8_t str[20],humi,x;
   //DDRB=0xfe;
   DDRD=0x00;
   DDRE=0x00;
   LCD_Init();
   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_BIT);  
   sprintf(str,"inhKien.Vn");
   LCD_Puts(str);
  // sprintf(str,"BanLinhKien.Vn");
  // LCD_Puts(str);
   delay_ms(40);
   set_timer0(5);
   set_timer1(0);
   enable_interrupts(INT_TIMER0);   
   enable_interrupts(GLOBAL);
 //  sprintf(str,"inhKien.Vn");
  // LCD_Puts(str);
while(TRUE)
   {
  //   sprintf(str,"inhKien.Vn");
     if(enable_display)
     {
     x=frequency/100;
     
   sprintf(str,"Do Am:%3u",x);
   LCD_Puts(str);
   //  humi=HS1101_GetHumi(frequency);
  //   enable_display=0;
   //  sprintf(str,"Do Am:%3u",humi);
     LCD_Gotoxy(0,1);
     LCD_Puts(str);
     LCD_PutChar('%');
     }
   }
}

- - - Updated - - -

Now I will try to describe the code.
I use LM35 sensor for measuring temperature. The temperature is displayed by two seven segment leds.
To measure humidity I use NE555 operating in astable mode. The output of it is a Frequency and is connected to pin CCP2 pic 16F877A.
The temperature is used to control a Fan. When the temperature increase, the speed of fan also increase.
In the code:
Timer0:
- LED scan
- measuring Frequency
Timer1:
I don't know it exactly but I think it also used to measure Frequency.
Timer2:
PWM for changing fan speed
 

To be honest, using up to three timers is too difficult for me to manage what is happen now.
It seems so. Presently you enabled three timer interrupts but have only two interrupt functions defined. That's the final reason why the enable_display code gets never executed.
 

n the second piece of code disable_interrupts(GLOBAL); disables all interrupts including timer0, timer1, ccp. During that time even timer0 and timer1 interrupts will not occur. Is that what you want to do?
And this is also my intention.During measure frequency, I think it is better to disable all interrupt and will enable it again after geting Frequency value. But it is also where the problem come from.

- - - Updated - - -

It seems so. Presently you enabled three timer interrupts but have only two interrupt functions defined.

I think Timer1 is Input capture timer and it is used in Timer0 interrupt: frequency=get_timer1();

- - - Updated - - -

Code:
disable_interrupts(GLOBAL);
set_timer0(5);
count++;
if(count==78)
{ 
frequency=get_timer1();
set_timer1(0);
count=0;
enable_display =1;

}
enable_interrupts(GLOBAL);
}
In this code at the time count = 78 => the time elapsed = 1s. Therefore the value of Timer1 is also the frequency of signal.

- - - Updated - - -

Code:
disable_interrupts(GLOBAL);
set_timer0(5);
count++;
if(count==78)
{ 
frequency=get_timer1();
set_timer1(0);
count=0;
enable_display =1;

}
enable_interrupts(GLOBAL);
}
When I remove both "disable_interrupts(GLOBAL)" and "enable_interrupts(GLOBAL)" the part of code bellow in main doesn't occur despite enable_display =1
Code:
if(enable_display)
   {
     printf(lcd_putc,"%u",32);
    humi= HS1101_GetHumi(frequency);
     enable_display=0; 
   printf(lcd_putc,"%u",humi);
   }
But when I remove only enable_interrupts(GLOBAL), the peice of code above is done and I get 3200 in display.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top