CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ADC_Value vs o\p display on 16*2 LCD
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sahu77



Joined: 08 Sep 2011
Posts: 202

View user's profile Send private message

PostPosted: Sat Jan 07, 2012 12:46 pm     Reply with quote

dyeatman wrote:
You should be using lcd_putc in a printf statement.

The CCS Example file EX_LCDTH.C shows how it should be done..

There is another example in the current manual on page 216 under printf
where i found EX-LCDTH.C ?
_________________
sahu
dyeatman



Joined: 06 Sep 2003
Posts: 1985
Location: Norman, OK

View user's profile Send private message

PostPosted: Sat Jan 07, 2012 12:49 pm     Reply with quote

in the PICC/Examples directory
_________________
Google and Forum Search are some of your best tools
sahu77



Joined: 08 Sep 2011
Posts: 202

View user's profile Send private message

PostPosted: Sun Jan 08, 2012 7:44 am     Reply with quote

i got solution .but found new problem ....it code as
Code:
/***************************************************************************
  Function getchreading
  Calculate minivolts on selected channel
 ****************************************************************************/
int16 getchreading(int channel)
{
       int32 tlong;
        int1 done; // Put local variable at beginning of code block
        int x; // Number of times to average ADC value
        int16 temp_result, adc_value;

        set_adc_channel(channel); // Select Channel

        // Average ADC value
        temp_result = 0;
        for (x=1; x<=ADC_AVERAGE_COUNT; x++)
        {
                delay_ms(ADC_DELAY); //Charge capacitor
                read_adc(ADC_START_ONLY); //Do A/D conversion

                done == adc_done();

                while(!done) {
                done = adc_done();
                }

                temp_result += read_adc(); // Read A/D register . Pin A0 in an input
                delay_ms(ADC_2TAD); // Wait 2 TADs before doing another A/D conversion
        }

        adc_value = temp_result / ADC_AVERAGE_COUNT;
       tlong = (int32)ADC_value*5000/255; //1023;
       return (int32) tlong;
        //return adc_value;
}
/***************************************************************************
  Function getchreading
  Calculate minivolts on selected channel & DISPLY ON 16*2 LCD
 ****************************************************************************/
void Process_display(void)
{
 
unsigned int16 ch_0, ch_1;

        int16 temp_result, adc_value,involts,outvolts ;
    // page 1
    ch_0 = getchreading(1);
    involts =(int16) ch_0 / 10 ; 

      lcd_gotoxy(1,1);
      printf ( LCD_PutChar,"INPUT  ""%lu ",involts);     
       lcd_gotoxy(12,1);
       printf(LCD_PutChar,"VOLT");

    ch_1 = getchreading(1);
    outvolts =(int16) ch_1 / 10 ;

      lcd_gotoxy(1,2);
      printf ( LCD_PutChar,"OUTPUT ""%lu   ",outvolts);     
      lcd_gotoxy(12,2);
      printf(LCD_PutChar,"VOLT  ");   

    delay_ms(500);
}

lcd show only outvolts value . on lcd
Not show involts value
_________________
sahu
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sun Jan 08, 2012 7:59 am     Reply with quote

ch_0 = getchreading(1);
ch_1 = getchreading(1);

It looks like you are reading the same channel both times.

Also do you realize you are waiting ADC_2TAD + ADC_DELAY between readings?
The ADC_START_ONLY and while(!done) loop are unnecessary. If you simply call read_adc() it will automatically wait until the ADC is done.
_________________
The search for better is endless. Instead simply find very good and get the job done.
sahu77



Joined: 08 Sep 2011
Posts: 202

View user's profile Send private message

PostPosted: Sun Jan 08, 2012 8:14 am     Reply with quote

SherpaDoug wrote:
ch_0 = getchreading(1);
ch_1 = getchreading(1);

It looks like you are reading the same channel both times.

Also do you realize you are waiting ADC_2TAD + ADC_DELAY between readings?
The ADC_START_ONLY and while(!done) loop are unnecessary. If you simply call read_adc() it will automatically wait until the ADC is done.


HOHO hoho sorry its my mistake.
now ok
Code:

set_adc_channel(channel); // Select Channel

        // Average ADC value
        temp_result = 0;
        for (x=1; x<=ADC_AVERAGE_COUNT; x++)
        {
                delay_ms(ADC_DELAY); //Charge capacitor
                read_adc(ADC_START_ONLY); //Do A/D conversion

                done == adc_done();

                 temp_result += read_adc(); // Read A/D register . Pin A0 in an input
                delay_ms(ADC_2TAD); // Wait 2 TADs before doing another A/D conversion
        }

Now ADC part is right ??
_________________
sahu
dyeatman



Joined: 06 Sep 2003
Posts: 1985
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Jan 08, 2012 8:29 am     Reply with quote

One note about your printf statements,
printf ( LCD_PutChar,"OUTPUT ""%lu ",outvolts);

You don't need two sets of quotes, put the string and format specifier all in the same set:
printf ( LCD_PutChar,"OUTPUT %lu ",outvolts);

The % tells the compiler the next char(s) are a format specifier
_________________
Google and Forum Search are some of your best tools
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Sun Jan 08, 2012 8:37 am     Reply with quote

It is still far more complex than it needs to be.
The delay to acquire is a few _usec_, not mSec, and is the same as the delay needed between successive samples. All you need, is:
Code:

        set_adc_channel(channel); // Select Channel

        // Average ADC value
        temp_result = 0;
        for (x=1; x<=ADC_AVERAGE_COUNT; x++) {
                delay_us(ADC_DELAY); //Charge capacitor
                temp_result+=read_adc(); // Read ADC
        }

In your code, you are actually starting the ADC twice, throwing away the first reading, and then not allowing time to re-acquire between these.....

This will take ADC_AVERAGE_COUNT readings, and sum them, _but_ this is not what you need to do.

You need:

1) To add some form of detection of the AC input zero crossing, or a physical sampling _time_ that corresponds to an _exact number of cycles_.
2) To be adding the _squares_ of the ADC value, minus the zero point value, not just summing the readings.

SherpaDoug has already told you this.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

ac RMS Hardware & Software suggestions
PostPosted: Tue Jan 10, 2012 6:51 am     Reply with quote

Here are two suggestions for coupling +/- 25V pk-pk ac to a PIC ADC input.

Code:


Circuit (a)

    Large C
      ||    22k5
AC ---||---/\/\/\------------------  PIC Input
      ||+           |     
                    |         
            5k0     |    5k0 
+5v -------/\/\/\-------/\/\/\-----
                                  |
                                 --- 0V Ground
                                  -



Circuit (b)

          22k5
AC ------/\/\/\------------------  PIC Input
                  |     
                  |         
          4k5     |    5k625 
+5v -----/\/\/\-------/\/\/\-----
                                |
                               --- 0V Ground
                                -



Circuit a is simplest to understand and set up.

The two 5k0's bias the PIC input to its midpoint.
The dc impedance seen by the PIC is 2k5 as per data sheet.
The attenuation from ac to PIC input is 10:1.
When ac is at +25V, PIC input is 5V.
When ac is at -25V, PIC input is 0v.

The coupling capacitor introduces an error depending on C's value.
To reduce this error to 0.1% (i.e. 1 bit of 10) C has to be > 3mF (i.e. 3000uF).
That gives a time constant of several seconds.
The settling time is thus unreasonably long.
You could get away with a smaller cap and correct for the error in software.
Either way you get current into the PIC protection diodes during the settling (NOT good).

Circuit b avoids the cost of the capacitor and its timing problem.

With the values shown.

PIC is biased at mid point.
The dc impedance seen by the PIC is 2k25 i.e. better than 2k5 as per data sheet.
The attenuation from ac to PIC input is 10:1.
When ac is at +25V, PIC input is 5V.
When ac is at -25V, PIC input is 0v.

To calculate the RMS value of the ac input you have several options.

You can assume that the input is truly biased at its midpoint. Using 1% resistors the error here could be 2%.
On the other hand you could take an average of the ADC values and treat that as the bias value.

Here are two methods for calculating the ac RMS value. Both require you to take averages over several cycles as outlined by SherpaDoug.

Code:



Method 1)

For each ADC value:-

 Subtract the bias value (ac zero point value).
 Square the result.
 Add square to a Sum_of_Squares total.

When enough samples are taken calculate:-

 Mean_Square = Sum_of_Squares / No_of_Samples.
         RMS = Square root of Mean_Square.



Method 2)

For each ADC value:-

 Add value to a Sum_of_Values total.
 Square ADC value.
 Add square to a Sum_of_Squares total.

When enough samples are taken calculate:-

        Mean =  Sum_of_Values / No_of_Samples.
 Mean_Square = ( Sum_of_Squares / No_of_Samples ) -  Mean ^ 2
         RMS = Square root of Mean_Square as before.
 


With method 1 you either assume that the bias value is 50% of full scale, or measure it first, which then makes the method a two pass process.

With method 2 you don't have to make any prior assumptions about the bias value. The bias value falls out at the end, avoiding the subtraction on every value and possibly incurring a rounding off error. All processing up to the final calculation stage is done as integer arithmetic on the raw 10 bit ADC values.

For either method I'm assuming that you convert to engineering units after calculating an RMS from the ADC values.

Mike
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group