0
\$\begingroup\$

I have multiple temperature sensor connected to microcontroller PIC16F887. How can I assign single PIN to a variable? Is it possible?

unsigned char  Check, T_byte1, T_byte2, RH_byte1, RH_byte2, Ch1;
unsigned Temp, RH, Sum;
unsigned int Temperature, Humidity;
unsigned char TH;

void SelectSensor(int sensor){
    if(sensor == 1)
        TH = RA0;
    else if(sensor == 2)
        TH = RA1;
    else
        TH = RA2;
}

void StartSignal() 
{
    TH = 0;
    __delay_ms(18);
    TH = 1;
   __delay_us(30); 
} 

void CheckResponse() 
{ 
    Check = 0;
    __delay_us(40); 
    if (TH == 0) 
    { 
        __delay_us(80);//80 
        if (TH == 1){    
            Check = 1;
        }
        __delay_us(40);
    } 
}

///////////////////////////////////////////////////////////////////////////////////////////////////////

char ReadData(){
     char i=0, j;
     for(j = 0; j < 8; j++){
         while(!TH );               //Wait until TH1 goes HIGH
         __delay_us(30);
         if(TH == 0)
             i&= ~(1<<(7 - j));     //Clear bit (7-b)
         else {
            i|= (1 << (7 - j));     //Set bit (7-b)
            while(TH1);             //Wait until TH1 goes LOW
        }  
    }
    return i;
}

void calculateSignal(){
    StartSignal();
    CheckResponse();
    if(Check == 1){
        RH_byte1 = ReadData();
        RH_byte2 = ReadData();
        T_byte1 = ReadData();
        T_byte2 = ReadData();
        Sum = ReadData();
        if(Sum == ((RH_byte1+RH_byte2+T_byte1+T_byte2) & 0XFF)){
            Temp = T_byte1;
            Temp = (Temp << 8) | T_byte2;
            RH = RH_byte1;
            RH = (RH << 8) | RH_byte2;
            if (Temp > 0X8000){
                Temp = Temp & 0X7FFF; 
            }
            Temperature = Temp;
            Humidity = RH;
        }
        else{
            //lcd_clear();
            //printf("ceksum error");
        }
    }
    else {
        //printf("no response");;
    }
}

int getTemp(){
    return Temperature;
}

int getHumid(){
    return Humidity;
}
\$\endgroup\$
5
  • \$\begingroup\$ Why are you trying to do that? You should be setting the ADC mux. \$\endgroup\$
    – Matt Young
    Mar 24, 2017 at 1:15
  • \$\begingroup\$ So that i can reuse TH for different PIN. \$\endgroup\$
    – Tuss
    Mar 24, 2017 at 1:17
  • \$\begingroup\$ I can't make sense of what you're trying to accomplish. What is TH? What is it for? \$\endgroup\$
    – Matt Young
    Mar 24, 2017 at 1:19
  • \$\begingroup\$ How do you expect the micro to know which sensor it's reading from? \$\endgroup\$
    – DerStrom8
    Mar 24, 2017 at 1:20
  • \$\begingroup\$ All sensors(DHT22) are same. By selecting sensor 1,2 or 3, I can reuse same code to get temperature and humidity. I am expecting micro knows which sensor by referencing to different pin in PORTA. \$\endgroup\$
    – Tuss
    Mar 24, 2017 at 1:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.