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 result jumps between two numbers after averaging
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
languer



Joined: 09 Jan 2004
Posts: 144
Location: USA

View user's profile Send private message

PostPosted: Tue Apr 02, 2013 10:00 pm     Reply with quote

Quote:
better option would be to use a quadrature encoder

great idea ... like use the right tool for the job

Code:
use the 1024 counts from a 10 bit ADC to create 1000 different frequencies

I'll beg to differ on this. I will argue that this is entirely possible. With proper signal processing (read: dithering, oversampling, and averaging/decimating) this is within the realm. This is done quite frequently on sigma-delta ADCs (lookup ADI for examples; a simplified note from AVR here). However, having the same potentiometer position give you the same ADC value (i.e. the same frequency), now that is very unlikely.

All that said, a quadrature encoder, now that sounds like the right answer.
Mike Walne



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

View user's profile Send private message

PostPosted: Wed Apr 03, 2013 2:42 am     Reply with quote

OK. I was stretching a point. I suggested either fewer frequencies or more resolution.
I am aware that he could increase resolution by dithering etc. with his current PIC's 10bit ADC.
That still leaves the problem of how to deal with being on the edge.

Here's one way you could achieve moving the ADC input around by ~1count or 0.1%.
Does it without the complication of OPAs etc.
Code:
                               5V
                                |
                                >
                                >  4R7
                                >
                   4k7          |
             -----WWWWW---------|
            |                   |
            |                   >
      PWM --|              4k7  > <----to ADC input
            |              pot  >
            |                   |
             -----WWWWW---------|
                   4k7          |
                                >
                                >  4R7
                                >
                                |
                               0V

When PWM is low it pulls the upper end of the pot down by ~1 part per 1000.
And leaves lower end more or less as is.
When PWM is high it pulls the lower end of the pot up by ~1 part per 1000
And leaves upper end more or less as is.

Having said all that, we still don't really know exactly what he's trying to do.
I have to agree, encoder looks like a good idea.
Maybe just two buttons would do the job, one each for sharp & flat.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Wed Apr 03, 2013 3:13 am     Reply with quote

and (of course), if you look at how some mice 'shut down' when not moved, you have a change in behaviour, where is if the movement is less than some figure for a few seconds, it stops returning new readings, and switches to sampling at a much lower frequency. Then requires quite a large movement to trigger the restart, with two readings (say) 1/4 second apart, differing by more than perhaps 20 counts, to switch back to 'full' operation, and start returning movements to the computer. Behaviour like this would then give the 'stable reading' required.

Best Wishes
SherpaDoug



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

View user's profile Send private message

PostPosted: Wed Apr 03, 2013 5:48 am     Reply with quote

Could you go to a different method of setting a value such as:

0 to 20% - scan down in frequency quickly
21 to 40% - scan down slowly
41 - 60% - stay on current frequency
61 - 80% - scan up slowly
81 - 100% - scan up quickly
_________________
The search for better is endless. Instead simply find very good and get the job done.
notbad



Joined: 10 Jan 2013
Posts: 68

View user's profile Send private message

PostPosted: Thu Apr 04, 2013 1:03 am     Reply with quote

Thanks guys
The problem is solved. Here is the code that I wrote:
Code:
int16 val;
int16 temp;
int32 AdcSum;
int8  n=0;
int8  LSByte;
void main()
{
setup_adc_ports(sAN0);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);
while(1)
{
delay_ms(1);
AdcSum+=read_adc();
if(++n==0)
 {
 LSByte=AdcSum;
 temp= AdcSum>>8;
 AdcSum=0;
 int16 diff=abs((signed int16)val-temp);
 if (diff>1){
   printf("%Lu-%u\n\r", temp,LSByte );
   val=temp;}
 else if (diff==1){
  if ((temp>val && LSByte>150) || (temp<val && LSByte<100) ) {
   printf("%Lu-%u\n\r", temp,LSByte );
   val=temp;}}
}//avg
}//while
}//main

The problem was that after oversampling I "threw out" the extra bits. Now I use them to implement a threshold. If the major value is more than one count away it’s accepted. If it is -+1 then we look at the less significant bits. If they are far enough then the value is updated.

Thanks again for your attention
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