This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

issue in reading ADC values and display on 7 segment display

Other Parts Discussed in Thread: LM35

Hello !! Am facing a problem while reading temperature sensor(LM35) values from ADC, My issue is Am getting values like 32.90,25.49... etc.., and I need to display those values using two 7 segment display, how can I display those(4 digit) values on two 7 segment displays. my code is

while(1)

      {

 unsigned int i;

 temp[temp_pos++]=adc_read(); // reading average of 8 values

/* Read from adc channel 0 */

 if(temp_pos==8)

  temp_pos=0;

  temp_avg1=0;

 for(i=0;i<8;i++)

  temp_avg1+=temp[i];

  temp_avg1>>=3;

// divide by 8 to get avg of 8 values

  temp_avg=temp_avg1;

tempC = (3.3*temp_avg*100)/1024.0;

segment1(tempC/10); // display 1st digit on segment1

    _delay_cycles(2500000);

// delay 0.25 sec

   segment2(tempC%10); // display 2nd digit on segment2

  _delay_cycles(2500000);

// delay 0.25 sec

  }

 

what changes should I make to get only 2 digit number from ADC.

  • You want to get rid of the fractional part of the temperature?

    First, let tempC be an integer variable instead of a floating point.

    Second, writing a floating point number to an integer variable will discard the fraction (ie: round down), if you want to round the fraction to the closest integer value, first add 0.5 to the number: 0.4 + 0.5 = 0.9, rounded down is 0. 0.5 + 0.5 = 1.0 rounded down is 1.

    tempC = (3.3*temp_avg*100)/1024.0 + 0.5;

    tempC = temp_avg*3.3*100/1024.0 + 0.5;

    Using the latter form may cause the compiler to precalculate the value of 3.3*100/1024, thus reducing the whole operation to a single division.

**Attention** This is a public forum