View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
ADC - The hard way |
Posted: Fri Jun 08, 2012 7:12 am |
|
|
Hi,
After my initial post related to proper configuration of the internal OSC with my old compiler:
http://www.ccsinfo.com/forum/viewtopic.php?t=48366
I'm now faced with additional problems due to the compiler not fully supporting my PIC... thankfully as explained on the referenced thread i learned how to go around that.
My problem is that the ADC setup functions do not work at all.
Not even the channel select functions.
now, the datasheet says:
Quote: | 1) Configure analog/digital I/O (ANSEL)
2) Configure voltage reference (ADCON1)
3) Select A/D input channel (ADCON0)
4) Select A/D conversion clock (ADCON0)
5) Turn on A/D module (ADCON0)
6) Wait for A/D conversion to complete
7) Read A/D Result register pair |
I've done(in the appropriate locations):
Code: |
#byte ANSEL = 0x9B
#byte ADCON0 = 0x1F
#byte ADCON1 = 0x9F
|
Selected my ADC ports:
Code: |
ANSEL=0x03; //Ports AN0 and AN1
|
configured Vrefs and ADC clock
Code: |
ADCON1=0x08; // Right justified, no div2, Vdd/Vss
|
I'm good till here...
My question is for ADCON0, I can do all these 3 in one go:
Quote: |
3) Select A/D input channel (ADCON0)
4) Select A/D conversion clock (ADCON0)
5) Turn on A/D module (ADCON0)
|
Must I do them in steps? Does it matter?
This:
Code: |
ADCON0=0x40; //Fosc/16 - Chan 0 - No Go - ADC OFF
ADCON0=0x41; //Fosc/16 - Chan 0 - No Go - ADC ON
ADCON0=0x45; //Fosc/16 - Chan 0 - GO - ADC ON
Delay_us(20); //conversion time delay
ADC_value=Read_ADC();
|
Vs. This:
Code: | ADCON0=0x45; //Fosc/16 - Chan 0 - GO - ADC ON
Delay_us(20); //conversion time delay
ADC_value=Read_ADC();
|
Is there any difference in setting all the bits in different steps than just writing all of them together?
Should there be any delay between the bit writing steps?
Is _location_ of my ADC conversion delay correct?
Then, when switching channels after executing the above code,
can I just do:
Code: | ADCON0=0x4D; //Fosc/16 - Chan 1 - GO - ADC ON
Delay_us(20); //conversion time delay
ADC_value=Read_ADC();
|
Or must I go through the whole setup again turning the ADC of, switching channels etc... ADC on again... etc...
Its not that my code is not working... it is.. i can read my adc, i wrote my own channel switch function etc. I just want to make sure I'm doing things correctly and not just forcing my way into a working, yet wrong setup.
Thanks! _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Jun 08, 2012 8:19 am |
|
|
The microchip manual DS33023A.pdf does an in-depth discussion of the ADC system.
It includes:-
(1) Advising not to set GO/DONE in the same instruction as turning on the ADC module. Quote: | Note: The GO/DONE bit should NOT be set in the same instruction that turns on the A/D, due to the required acquisition time requirement. |
(2) Suggesting that you can select your next channel to convert soon after setting GO/DONE.
Quote: | Question 2: After starting an A/D conversion may I change the input channel (for my next conversion)?
Answer 2: After the holding capacitor is disconnected from the input channel, typically 100 ns after the GO bit is set, the input channel may be changed.
|
Mike |
|
![](templates/subSilver/images/spacer.gif) |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Fri Jun 08, 2012 12:24 pm |
|
|
Ah! The plot thickens!
thanks! ill make sure to read that manual!
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
![](templates/subSilver/images/spacer.gif) |
|