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.

[SOLVED] Problem with ADC @ PIC16F877A

Status
Not open for further replies.

Mithun_K_Das

Advanced Member level 3
Joined
Apr 24, 2010
Messages
899
Helped
24
Reputation
48
Reaction score
26
Trophy points
1,318
Location
Dhaka, Bangladesh, Bangladesh
Activity points
8,254
I'm using PIC for more than last 4yrs. But I didn't fetched any problem like this before.

Problem # 1: The ADC of PIC16F877A is calculating sometimes like 8bit, sometimes like 10bit. I don't know why. Even same program showing different result. Is this the problem of China Product?

Problem # 2: If I use One Digital input and others Analog, then the PIC16F877A will measure/calculate either the Analog one or the Digital one. Can't calculate both although both measurement have different time of measurement. Have 3000ms gap between two measurement.


Can anyone suggest me why this happen?
 

Are you changing the ADC configuration frequently in the code?

Problem # 2: If I use One Digital input and others Analog, then the PIC16F877A will measure/calculate either the Analog one or the Digital one. Can't calculate both although both measurement have different time of measurement. Have 3000ms gap between two measurement.
You want to convert the Digital signal with ADC???

post your code, please
 

I think you know how to use the pin as Digital I/O. and to measure the value. Better read DHT11 datasheet first. Then you will understand how to get Digital data. and what I'm doing here.... Just measuring another Analog voltage using the other pin of the PORTA of PIC16F877A.

- - - Updated - - -

And taking the data coming from DHT11.
 

hi,

I think the acquisition time is high..
try to reduce the resistance value you are using between sensor and port.. probably below 2kohms.

correct me if i m rong..
thanks
 

hi Mithun_K_Das

Are you using DHT11, the temperature & humidity sensor?
So you are processing one data with the PICs' analog port and another data with the DHT11?
If i get it correctly, it has a bidirectional single wire communication. Sometimes, even clock synchronized bidirectional serial communication protocols give you a headache if not properly synchronized, I had the same problem once with a bridge transducer.
May be you are missing some bits of the received data for being wrongly shifted.
 

Sudden loss of precision may be due to the left/right shifting of the ADC result being set incorrectly. Try applying a gradually increasing voltage (from a potentiometer for example) and see if it gives a linear count then after 255 suddenly jumps to a much different figure. If it does, try checking the justification of the high ADC result.

Brian.
 

hi Mithun_K_Das

Are you using DHT11, the temperature & humidity sensor?
So you are processing one data with the PICs' analog port and another data with the DHT11?
If i get it correctly, it has a bidirectional single wire communication. Sometimes, even clock synchronized bidirectional serial communication protocols give you a headache if not properly synchronized, I had the same problem once with a bridge transducer.
May be you are missing some bits of the received data for being wrongly shifted.



Yes, you are right. I'm using DHT11 for humidity sensing and thermistor to measure temperature as DHT11 is not suitable for temperature in this case.

But the problem is DHT11 is providing data and I'm getting it properly. On the other hand the other pin is used as Analog input to get the analog signal from thermistor. Problem is here. Its not getting the data properly. Showing the wrong value always.

Again if I run only Analog part or the DHT11 part its showing right result. Can you tell me what is the error? or what to do to get both data sequentially?

- - - Updated - - -

Sudden loss of precision may be due to the left/right shifting of the ADC result being set incorrectly. Try applying a gradually increasing voltage (from a potentiometer for example) and see if it gives a linear count then after 255 suddenly jumps to a much different figure. If it does, try checking the justification of the high ADC result.

Brian.


Actually not this case. The problem is posted before.

- - - Updated - - -

Sudden loss of precision may be due to the left/right shifting of the ADC result being set incorrectly. Try applying a gradually increasing voltage (from a potentiometer for example) and see if it gives a linear count then after 255 suddenly jumps to a much different figure. If it does, try checking the justification of the high ADC result.

Brian.


Actually not this case. The problem is posted before.
 

As has already been said - post your code. It sounds like you are corrupting either the ADC/port setup with your code. Run it with the debugger and when the problem occurs, check all registers associated with the ports and ADC to see if they are what you expect.

Keith
 

here is the code:
Code:
sbit Data at RA1_bit;
sbit DataDir at TRISA1_bit;
char message2[] = "H:  ";
char message3[] = "H:  ";
unsigned short TOUT = 0, CheckSum, i;
unsigned int value;
unsigned char ch;
unsigned short row, position;
unsigned int ax,bx,cx,dx;
unsigned int ax1,bx1,cx1,dx1;
unsigned long button_value;
unsigned long button_value2;
unsigned long Recent_Temp_Value;
unsigned int humidity;
long tlong;
float temp;
float average;
unsigned int j,adc_rd0;
unsigned int sample[3];
unsigned int T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;

void StartSignal()
{
  DataDir = 0;     // Data port is output
  Data    = 0;
  Delay_ms(25);
  Data    = 1;
  Delay_us(30);
  DataDir = 1;     // Data port is input
}

unsigned short CheckResponse()
{
  TOUT = 0;
  TMR2 = 0;
  T2CON.TMR2ON = 1;      // start timer
  while(!Data && !TOUT);
  if (TOUT) return 0;
  else 
  {
   TMR2 = 0;
   while(Data && !TOUT);
   if (TOUT) return 0;
   else 
   {
    T2CON.TMR2ON = 0;
    return 1;
   }
  }
}

unsigned short ReadByte()
{
  unsigned short num = 0, t;
  DataDir = 1;
  for (i=0; i<8; i++)
  {
    while(!Data);
    Delay_us(40);
    if(Data) num |= 1<<(7-i);
    while(Data);
  }
  return num;
}


void interrupt()
{
  if(PIR1.TMR2IF)
  {
   TOUT = 1;
   T2CON.TMR2ON = 0; // stop timer
   PIR1.TMR2IF  = 0; // Clear TMR0 interrupt flag
  }
}


  // port initialization
  TRISB = 0b00000000;
  PORTB = 0;
  TRISA = 0b00100011;
  TRISC = 0x10; // RC4 in all out
  PORTC = 0x00;
  TRISD = 0x0C; // D2 & D3 inputs all out
  PORTD = 0x00;
  CMCON = 7;// comparator off
  ADCON1 = 0b00001110;// AN selletion
  INTCON.GIE = 1;    //Enable global interrupt
  INTCON.PEIE = 1;   //Enable peripheral interrupt
  // Configure Timer2 module
  PIE1.TMR2IE = 1;  // Enable Timer2 interrupt
  T2CON = 0;        // Prescaler 1:1, and Timer2 is off initially
  PIR1.TMR2IF =0;   // Clear TMR INT Flag bit
  TMR2 = 0;
  Lcd_Init();
  Lcd_Cmd(_Lcd_Clear);
  Lcd_Cmd(_LCD_CURSOR_OFF);


  while(1)
  {
    DHT11_Data();
    Delay_ms(500);  
    Thermal_Data();
    Delay_ms(500);
  }// while(1)

/*******************************************************************************/
/***************************** DHT11_Data **************************************/
/*******************************************************************************/
 void DHT11_Data(void)
 {
    unsigned short check;
    //PIE1.TMR2IE = 1;  // Enable Timer2 interrupt
    //ADCON1 = 0b10000111; // all digital selected
    StartSignal();
    check = CheckResponse();
    if (!check)
    {
       Lcd_Cmd(_Lcd_Clear);
       Lcd_Out(1, 1, "No response");
       Lcd_Out(2, 1, "from the sensor");
    }
    else
    {
     RH_Byte1 = ReadByte();
     RH_Byte2 = ReadByte();
     T_Byte1 = ReadByte();
     T_Byte2 = ReadByte();
     CheckSum = ReadByte();
     // Check for error in Data reception
     if (CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF))
     {
        value  = RH_Byte1;
        message2[0] = RH_Byte1/10 + 48;
        message2[1] = RH_Byte1%10 + 48;
        Lcd_Out(1,14, message2);
        Lcd_Out(1,16,"%");
     }
      else
      {
         Lcd_Cmd(_Lcd_Clear);
         Lcd_Out(1, 1, "Checksum Error!");
         Lcd_Out(2, 1, "Trying Again...");
      }
    }
    //ADCON1 = 0x00;// all analog selected
 }

/*******************************************************************************/
/***************************** Thermal_Data    *********************************/
/*******************************************************************************/
 void Thermal_Data(void)
 {

   Delay_ms(50);
   //ADCON1 = 0x00;// all analog selected
   ADCON0=0b11000001;// Channel 1 selected with internal clock speed
    for(j=0; j<3; j++)
    {
     adc_rd0 = ADC_Read(0);
     sample[j] = adc_rd0;
     Delay_ms(200);
    }
    average = 0;
    for(j=0;j<3;j++)
    {
     average += sample[j];
    }
    average /= 3;
    average = ((1023 /average )-1);
    average = 10000/average; // get the resitror value
    temp = average/10000; // R/Ro
    temp = log(temp); // temp = log(R/Ro)
    temp /= 3950; // 1/B
    temp += 1.0/(25.00 + 273.15);
    temp = 1.00/temp;
    temp = ((temp-273.15)*1.8)+32.00; // converting K to F.
    tlong = temp*100;
    Recent_Temp_Value = temp*100;
    row = 1;
    position = 3;
    Lcd_Out(1,1,"T:");
    Display_Temp();
    
    ax = EEPROM_Read(0);
    bx = EEPROM_Read(1);
    cx = EEPROM_Read(2);
    dx = EEPROM_Read(3);

    button_value = dx*1000 + cx*100 + bx*10 + ax;

    row = 2;
    position = 3;
    tlong = button_value;
    Display_Temp();
    Lcd_Out(2,1,"S:");
    //ADCON1 = 0b10000111; // all digital selected
 }
 

I Found the problem that if I use DHT11(DhT11_Data() Subprogram) the ADC can not calculate the analog signal. But if I use only the Analog Voltage measurement its ok.


Can anyone tell me why??

- - - Updated - - -

Are you changing the ADC configuration frequently in the code?


You want to convert the Digital signal with ADC???

post your code, please


You should read twitch the post before you make a comments.
 

ADC not working while DHT11 is connected on the other PIN.

Can anyone tell me why ADC is not working if I use DHT11 in the other pin of PIC16F877A ?

DHT11 is connected with RB7 of PIC16F877A. And I need to measure an analog voltage.

Note: 1. both sensors are at a separate board. And these sensors(DHT11 & Thermistor) are connected via a IDE connector.
2. The analog measurement(for Thermistor) and Digital measurement(for DHT11) have time gap in measurement period. I mean if Analog voltage is measured at 00.20 min, Digital is measured at 01.20min, then another 1.00 min gap.
3. TMR2 is used for DHT11 data calculation.


*** Can anyone tell me why this is happening?
 

Re: ADC not working while DHT11 is connected on the other PIN.

Hi,

Have used the DHT11 sensors ( though not that accurate) along side analogue thermistors and other digital sensors on the same chip without any problem.

Your timing gaps are fine and you only need a 2 second gap between reads of the DHT11

You do not say exactly what is not working in the ADC ? incorrect result or nothing at all ?

Would suspect you have somehow got your ports or even registers mixed up, have you configured them correctly.

Are both sensors boards on the same +5v rail as the Pic ?

Also would avoid using RB6 and RB7, that means you would have to isolate the sensors for programming and it will stop you using the debugger function if you have a Pickit programmer ? if so have you tried running Debugger to see whats happening ?

If still stuck show your code and circuit so with can see whats happening.
 

Re: ADC not working while DHT11 is connected on the other PIN.

Hi,

Have used the DHT11 sensors ( though not that accurate) along side analogue thermistors and other digital sensors on the same chip without any problem.

Your timing gaps are fine and you only need a 2 second gap between reads of the DHT11

You do not say exactly what is not working in the ADC ? incorrect result or nothing at all ?

Would suspect you have somehow got your ports or even registers mixed up, have you configured them correctly.

Are both sensors boards on the same +5v rail as the Pic ?

Also would avoid using RB6 and RB7, that means you would have to isolate the sensors for programming and it will stop you using the debugger function if you have a Pickit programmer ? if so have you tried running Debugger to see whats happening ?

If still stuck show your code and circuit so with can see whats happening.

Thanks for your reply first. Yes, the ADC is showing wrong result. and its varying with time without any voltage change at the pin. And the both sensors are connected at the same +5V source.

I don't know why you are suggesting to avoid RB6 & RB7. I'm using QL programmer. I didn't see the debugger result yet. Actually I'm still confused. Because if I run only thermistor, its ok. If I run only DHT11, its ok. But if I run both its not ok.

Still don't know where is the problem.
 

Hi Mithun

Is the ADC configuration not changed/corrupted during ADC conversion. why don't you implement ADC_Read() by your self. I think you know that every conversion is initiated by setting bit 2 (GO/DONE) of ADCON0.
 
Last edited:

Hi,
Can u give the exact pin connections on PIC. At what pins u have connected the thermistor and the humidity sensor? And tell the nature of t signal fed to PIC by each sensors...
 

Hi,
Can u give the exact pin connections on PIC. At what pins u have connected the thermistor and the humidity sensor? And tell the nature of t signal fed to PIC by each sensors...

See the program posted before, hope you'll understand.

- - - Updated - - -

& see post #14.
 

Re: ADC not working while DHT11 is connected on the other PIN.

Thanks for your reply first. Yes, the ADC is showing wrong result. and its varying with time without any voltage change at the pin. And the both sensors are connected at the same +5V source.

I don't know why you are suggesting to avoid RB6 & RB7. I'm using QL programmer. I didn't see the debugger result yet. Actually I'm still confused. Because if I run only thermistor, its ok. If I run only DHT11, its ok. But if I run both its not ok.

Still don't know where is the problem.


Hi,
The point about RB6 and RB7 only applies if your are using an In Circuit Serial Pprogrammer.

Again if you are using a hardware debugger then RB6 and RB7 must be dedicated to the Debugger.

Software apart, I would suspect you have a +5v problem in that the adc or dht are taking too much current when both are connected.
Without seeing your circuit diagram it is hard to say what the cause is.

Are you using Vref to power the thermistor or +5v ?
 

Re: ADC not working while DHT11 is connected on the other PIN.

Hi,
The point about RB6 and RB7 only applies if your are using an In Circuit Serial Pprogrammer.

Again if you are using a hardware debugger then RB6 and RB7 must be dedicated to the Debugger.

Software apart, I would suspect you have a +5v problem in that the adc or dht are taking too much current when both are connected.
Without seeing your circuit diagram it is hard to say what the cause is.

Are you using Vref to power the thermistor or +5v ?

I don't know how can you say these?
* RB7 is just using as a simple Digital input. DHT11 is connected in this pin.
* no special function is used in these RB6 & RB7 of PIC16F877A.
* the +5V is coming from a voltage regulator and the regulator can supply up to 300mA where DHT11 is consuming only 0.3mA and the thermistor is consuming 0.25mA only. So I don't think its a problem of the supply. and rest of the circuit will consume maximum 50mA.
* Vref == VDD.

****** Most important note: Both sensors are connected and powered up all the time. And if I use just the Thermistor calculation, its fully fully ok. And if I use just DHT11 calculation its also fully fully ok. But if I use both calculation, Its not ok. Only DHT11 data is ok and showing right result and only the thermistor measurement result is not ok. That means only the ADC is not working properly.

I think the main problem is now clear to all.
 
Last edited by a moderator:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top