| View previous topic :: View next topic |
| Author |
Message |
skoberlink
Joined: 27 May 2010 Posts: 52
|
|
Posted: Tue Jul 27, 2010 8:22 am |
|
|
I'm certainly willing to give the divisor a try but it seems to me that you pick the internal ADC clock or you pick a divisor, not both. Am I misunderstanding that?
Is the internal ADC clock separate from the INTRC clock that is used for processing everything? I had originally used the internal ADC clock assuming that I had decided that by using the #INTRC_IO fuse. However, if the internal ADC clock is separate, then the divisor would work. I'll give it a try but a little clarification here would be great if you can. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Tue Jul 27, 2010 9:28 am |
|
|
You have multiple clocks.
There is the internal RC clock to feed the processor.
This is available via divisors to feed the ADC.
There is a separate RC clock to feed the ADC. This is asynchronous to the main processor clock, and has no divisor options.
Problem is that everything in a processor generates noise. If you run the ADC, from a divided copy of the master clock, it'll be running _synchronously_ to the master clock, so the same number of processor cycles will take place for each ADC reading, and you will get the same noise component from this. If however you use the separate ADC clock, you _will_ get 'beat' effects between the two frequencies, giving variable processor noise on the samples. This is why the data sheet _specifically_ tells you _not_ to use the internal RC ADC clock for processor frequencies above 1MHz.
The 'best' option, is to use the internal RC ADC clock, and stop the processor (using sleep), while the conversion is performed.
The 'next' option is to use the divided processor clock.
The 'worst' option is to use the internal RC clock while the processor is left running. This is what you are doing....
Best Wishes |
|
 |
skoberlink
Joined: 27 May 2010 Posts: 52
|
|
Posted: Tue Jul 27, 2010 10:02 am |
|
|
| Ttelmah wrote: |
The 'best' option, is to use the internal RC ADC clock, and stop the processor (using sleep), while the conversion is performed.
The 'next' option is to use the divided processor clock.
The 'worst' option is to use the internal RC clock while the processor is left running. This is what you are doing....
Best Wishes |
Hmm...ok this makes more sense. So would it be a good idea for me to sleep the processor during the conversion? Are there cons to this? I don't have any interrupts to fire there so all it's doing is the conversion. Like I said, I'm relatively new to this so I'm not very clear on how some things work. |
|
 |
Jerson
Joined: 31 Jul 2009 Posts: 134 Location: Bombay, India
|
|
Posted: Tue Jul 27, 2010 11:47 am |
|
|
| SherpaDoug wrote: | Double check your grounding.
|
I second that. You must be facing a situation of a ground loop where the moving motor raises the ground into the ADC leading to a bad conversion result. the other question that you haven't been asked perhaps/not answered is the hysteresis around the set point that decides which direction the motor will move. This hysteresis combined with the ground loop can cause myriads of problems for you.
How can you confirm this? Take a wire jumper - one end connects to the PIC ground pin, the other end goes directly to the power supply ground point. If it is a ground loop, 99% it should get ok with this treatment. _________________ Regards
Jerson Fernandes |
|
 |
skoberlink
Joined: 27 May 2010 Posts: 52
|
|
Posted: Wed Jul 28, 2010 11:16 am |
|
|
| Jerson wrote: | | SherpaDoug wrote: | Double check your grounding.
|
I second that. You must be facing a situation of a ground loop where the moving motor raises the ground into the ADC leading to a bad conversion result. the other question that you haven't been asked perhaps/not answered is the hysteresis around the set point that decides which direction the motor will move. This hysteresis combined with the ground loop can cause myriads of problems for you.
How can you confirm this? Take a wire jumper - one end connects to the PIC ground pin, the other end goes directly to the power supply ground point. If it is a ground loop, 99% it should get ok with this treatment. |
I tried the jumper and it didn't seem to affect anything so I'm guessing that's not the issue. I don't know the hysteresis on that point, but it's not an issue for my other methods of motor control. The motor can also be controlled digitally with two switches and it doesn't seem to have any issue stopping there.
I've also tried the sleep method now but it just goes to sleep and never comes out. I have the interrupts enabled and I also have global disabled. If I'm not mistaken this means the conversion will wake it from sleep but it will not go to an interrupt handler. Here's the current version of my code.
| Code: | unsigned long analog_in(unsigned char channel){
disable_interrupts(GLOBAL);
enable_interrupts(INT_AD);
unsigned long result = 0;
channel &= 0b00000111; // truncate channel to 3 bits
ADCON1 = 0x80;
ADCON0 = 0b11000000; //clear channel
ADCON0 |= (channel<<3); // apply new channel select
ADON = 1; //turn it on
delay_us(100); //let it settle
ADGO = 1; //initiate conversion
sleep();
result = (ADRESH<<8);
result |= ADRESL;
enable_interrupts(GLOBAL);
return result; // return 10 bit result
} |
The only thing that's changed is I added an enable for INT_AD and the sleep line instead of waiting for ADGO to go low. Why does it hang on the sleep function?
EDIT: I should say I'm assuming that it's hanging on the sleep function since that's the only thing that changed which might hang. |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 28, 2010 12:16 pm |
|
|
I didn't look closely at your code because you're not using CCS functions,
but I noticed that you're enabling Global interrupts.
You don't need to do that. Download the 16F88 data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/30487c.pdf
Look at the diagram of the interrupt enable logic:
| Quote: | FIGURE 15-7: INTERRUPT LOGIC
|
Notice how the Wake-up from Sleep signal occurs before the gate
for Global interrupts. You don't need to enable Global interrupts (GIE).
Also notice that the gate that controls most of the peripheral interrupts
is controlled by PEIE. That's the Peripheral Interrupt enable. So all
you need to do is to enable Peripheral interrupts to allow wakeup from
sleep. See this post. It shows how to do it:
http://www.ccsinfo.com/forum/viewtopic.php?t=28158&start=8 |
|
 |
skoberlink
Joined: 27 May 2010 Posts: 52
|
|
Posted: Thu Jul 29, 2010 8:00 am |
|
|
I don't have a lot of time right now but I wanted to point something out in my code. I'll come back to this later today.
For that analog_in function, I actually disable global interrupts, enable INT_AD, perform the read and then enable global interrupts. This is to avoid leaving the function while conversion is happening.
So should I enable peripherals along with INT_AD but leave globals disabled? |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 29, 2010 10:52 am |
|
|
| Follow the example code that I linked to. It doesn't enable Global interrupts. |
|
 |
skoberlink
Joined: 27 May 2010 Posts: 52
|
|
Posted: Thu Jul 29, 2010 10:55 am |
|
|
| I actually got the sleep mode working. I'm still getting the twitching but it's better and acting a little differently now. I don't think the read is the problem anymore. Now I'm still seeing twitching but it's more common and more significant when I'm at a smaller position (i.e. smaller voltage). This leads me to believe that something in my step calculation is having a problem. If you see any problems with this let me know but I think the ADC is finally as good as I can do. Thanks for all the help guys! |
|
 |
Gabriel
Joined: 03 Aug 2009 Posts: 1085 Location: Panama
|
|
Posted: Mon Aug 09, 2010 8:00 am |
|
|
I had a similar problem with a water presure sensor.
AD values where always a few steps off.... basically i had the same issue.
i used a low pass filter and olimpic averaging simultaneously and the problem disappeared..... completely.
it was a SLOWER but i still got 18Hz resolution / freq response. (forgot the actual term)....( 18 measurements per second comming from RS232 port)
lo pass filter can be implemented as follows:
| Code: |
Filtered_Value = (Old_Value+Old_value+Old_value+ New_Value)/4;
Filtered_value=Old_Value;
"use filtered value for what ever you wanted it for"
|
after that i ALSO implemented an Olimpic averaging using 10 filtered values..... i used 10 values cause 10 - high value - low value resulted in
8values... thus my division was faster.
my problem was gone....
it Will make your system slower.
on your low pass filter notice its a power of 2 division.... keep this for speed.... simpler division... only requires shifting... obviously keep the number of items you are dividing as a power of 2.
i was using VCC as my Vref as well... i had good filter caps on my supply
and on my vcc and gnd pins.
Hope this helps...
gabriel _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
 |
skoberlink
Joined: 27 May 2010 Posts: 52
|
|
Posted: Mon Aug 09, 2010 8:14 am |
|
|
Wow I really appreciate the info here. This sounds very promising. Could you explain your code a little better for the low-pass filter though please? It's been a while since I refreshed my memory on numerical methods. It seems like you're never creating a new value because you set filtered_value = old_value. Also do you use this in conjunction with olympic averaging or is it just one or the other?
I'll do some more looking myself of course. You've offered some excellent suggestions, thanks! |
|
 |
Gabriel
Joined: 03 Aug 2009 Posts: 1085 Location: Panama
|
|
Posted: Mon Aug 09, 2010 8:52 am |
|
|
| Code: |
Filtered_Value = (Old_Value+Old_value+Old_value+ [b]New_Value[/b])/4;
Filtered_value=Old_Value;
"use filtered value for what ever you wanted it for"
|
New value = latest adc value
old value = last filtered value
Filtered value = the average of old value x3 and 1 new value.
your output will depend heavily on the old values.
your new value will have a small impact on your filtered value
basic concept being (4+4+4+3)/4 = 3.75 -- although your new value is 3.... answer is closer to 4 than 3....
next round asumming 3 is the target value will be
(3.75+3.75+3.75+3)/4=3.56
next:
(3.56+3.56+3.56+3)/4=3.42
and so on until:
(3+3+3+3)/4=3
just like a regular low pass RC filter.
Another way to look at it:
| Code: |
Filtered_Value = ((Old_Value * 3)+ [b]New_Value[/b])/4;
Filtered_value=Old_Value;
"use filtered value for what ever you wanted it for"
|
hope that helps
will try to post code example soon.
gabriel _________________ CCS PCM 5.078 & CCS PCH 5.093
Last edited by Gabriel on Mon Aug 09, 2010 9:00 am; edited 1 time in total |
|
 |
Gabriel
Joined: 03 Aug 2009 Posts: 1085 Location: Panama
|
|
Posted: Mon Aug 09, 2010 8:55 am |
|
|
Forgot to mention.... I use both low pass and olympic averaging.
I believe I feed my low pass filter with values from the olympic average..
I have to look at my code. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
 |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Aug 09, 2010 1:49 pm |
|
|
Are you sure you don't mean:
| Code: |
Old_Value = Filtered_value
|
_________________ The search for better is endless. Instead simply find very good and get the job done. |
|
 |
Gabriel
Joined: 03 Aug 2009 Posts: 1085 Location: Panama
|
|
Posted: Mon Aug 09, 2010 2:14 pm |
|
|
@ SherpaDoug
| Quote: | Are you sure you don't mean:
Code:
Old_Value = Filtered_value
|
Totally right..... my mistake.
thanks for the correction....
gabriel _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
 |
|