running smt160

Thread Starter

BorealDude

Joined May 20, 2013
9
Hi there,
in a project i want use 4 smt160 sensors and get the average temperature in a place.
i used a 4:1 MUX and one ex_int0 and TIMER1.(with atmega16)
it starts well but after couple of cycles the program sticks and does not get the data and hangs.and i've to reset it.
can any one detect the problem?
this is the program:

Rich (BB code):
/////////////////////////////////////////////////////////////////////

#include <mega16.h>

#include <alcd.h>

#include <delay.h>

#include <stdio.h>



bit  control_1=0;
bit  control_2=0;
bit  control_3=0;
bit  control_4=0;



float tup_1=0.0 ;
float tup_2=0.0 ;
float tup_3=0.0 ;
float tup_4=0.0 ;



float tdn_1=0.0;
float tdn_2=0.0;
float tdn_3=0.0;
float tdn_4=0.0;



float temperature_1  ;
float temperature_2  ;
float temperature_3  ;
float temperature_4  ;

float temp_ave;





/////////////////////////////////////////  DISPLAY   ///////////////////////////

void display()
{
char ave [16];

lcd_clear();

sprintf(ave,"Ave=%.1f",temp_ave);
lcd_puts(ave);

delay_ms(700);
      
}

 
///////////////////////////////////////////////////////////////////////////
interrupt [EXT_INT0] void ext_int0_isr(void)

{

TCCR1B=0x00;
   

 //#######################  first sensor  ###############
      
if (PORTB==0)
 {    


     
      if (control_1)
      {

      
      tdn_1=TCNT1;
      TCNT1=0;
      
      
      MCUCR=0x02; 
      
      
      
      control_1=0;
      
      
        
           }
      
      
    else
      
     {  
     tup_1 =TCNT1;                                                  
     TCNT1=0;
     MCUCR=0x03;
     
     
     control_1=1;
     }
    
     
  }   
     
 //#######################  second sensor  ###############
    
 
else if (PORTB==1) 
{
  
     
      if (control_2)
      {

      
      tdn_2=TCNT1;
      TCNT1=0;
      
      
      MCUCR=0x02; 
      
      
      
      control_2=0;
      
     
    
           }
      
      
    else
      
     {  
     tup_2=TCNT1;                                                  
     TCNT1=0;
     MCUCR=0x03;
     
     
     control_2=1;
     }
     
     
     
   }
      
    
 //#######################  third sensor  ###############
 
      
else if (PORTB==2)
{
      if (control_3)
      {

      
      tdn_3=TCNT1;
      TCNT1=0;
      
      
      MCUCR=0x02; 
      
      
      
      control_3=0;
      
           
  
           }
      
      
    else
      
     {  
     tup_3=TCNT1;                                                  
     TCNT1=0;
     MCUCR=0x03;
     
     
     control_3=1;
     }  
     
     
}     
    
 
      
 //#######################  forth sensor  ###############
 

       
else if (PORTB==3)

{



      if (control_4)
      {

      
      tdn_4=TCNT1;
      TCNT1=0;
      
      
      MCUCR=0x02; 
      
      
      
      control_4=0;   
         
        
           }
      
      
    else
      
     {  
     tup_4=TCNT1;                                                  
     TCNT1=0;
     MCUCR=0x03;
     
     
     control_4=1;
     }  
     
     
}

////////////////end///////////////
   
      TCCR1B=0x02;
      
}

//////@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//////////

void main (void)

{

float dutyCycle_1=0.0  ;
float dutyCycle_2=0.0  ;
float dutyCycle_3=0.0  ;
float dutyCycle_4=0.0  ;


 

TCCR1A=0x00;
TCCR1B=0x02;
TCNT1H=0x00;
TCNT1L=0x00;

GICR|=0x40;     
MCUCR=0x02;
MCUCSR=0x00;
GIFR=0x40;  


DDRB=0x03;
PORTB=0x00;


lcd_init(16);

#asm("sei")

while (1)

      {    
       
      
      
      dutyCycle_1 = (tup_1/(tup_1+tdn_1)); 
      dutyCycle_1 +=0.018;
      temperature_1 =(dutyCycle_1-0.32);
      temperature_1 =temperature_1/0.0047;
      
      
      
      dutyCycle_2 = (tup_2/(tup_2+tdn_2)); 
      dutyCycle_2 +=0.018;
      temperature_2 =(dutyCycle_2-0.32);
      temperature_2 =temperature_2/0.0047;
                      
      
      
      
      dutyCycle_3 = (tup_3/(tup_3+tdn_3)); 
      dutyCycle_3 +=0.018;
      temperature_3 =(dutyCycle_3-0.32);
      temperature_3 =temperature_3/0.0047;
      
      
      
      dutyCycle_4 = (tup_4/(tup_4+tdn_4)); 
      dutyCycle_4 +=0.018;
      temperature_4 =(dutyCycle_4-0.32);
      temperature_4 =temperature_4/0.0047;  
      
      
      temp_ave=(temperature_1+temperature_2+temperature_3+temperature_4)/4;   
      
      
      
      
       if   ((control_1==0) && (control_2==0)   && (control_3==0)   && (control_4==0)) 
        
      {                     
      
         if ( PORTB<=2)
         {
          PORTB =PORTB+1;
         }
          
         
         else
        { 
           display();

           PORTB =0;   
       }  
          
       
       
          
      }
      
     
      
     } 
}
 
Last edited by a moderator:
Top