PDA

View Full Version : ADC on PIC12F510



SOTASOTA
- 19th December 2011, 22:58
I do not know the commands (nor where to find them) on making the ADC active on a PIC12F510.
I need to make GPIO.0 (Pin 7) an ADC input.
On the code below I found referencs to ADC and tried them. But no luck.
Here's the code so far:

'Initialize variable
#CONFIG
__config _IntRC_OSC & _WDT_ON & _MCLRE_OFF & _CP_OFF & _IOSCFS_OFF
#ENDCONFIG
DEFINE OSC 4
DEFINE ADC_BITS 10 ' 10 bit A/D Conversion
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50 ' 50 uS A/D sample time
OPTION_REG.5=0 'Set GPIO.2 to I/O

ADCON0.6 = 0
ADCON0.7 = 1
CM1CON0.3 = 0
ADCON0 = %00000001 'Set AN0 active

adval var word

low GPIO.1
low GPIO.2
low GPIO.3
low GPIO.4
low GPIO.5

mainloop:
ADCIN 0, adval
GPIO.1 = 1
GPIO.2 = 0
pause 127
GPIO.1 = 0
GPIO.2 = 1
pause 128
if adval<115 then 'if low battery
GPIO.4 = 1
endif
if adval>150 then 'if low battery
GPIO.4 = 0
endif
goto mainloop
return
end

Charles Linquis
- 20th December 2011, 06:18
For one thing, in the sequence

ADCON0.6 = 0
ADCON0.7 = 1
CM1CON0.3 = 0
ADCON0 = %00000001 'Set AN0 active


You first set ADCON0 bit 7 and then clear it two instructions later.

Try

CM1CON0.3 = 0
ADCON0 = %10000001

Instead

SOTASOTA
- 21st December 2011, 14:36
Thanks Charles. I would really like to understand these commands as I fly by the seat of my pants in this coding regarding ADC. How can I learn what the code does and is there a reference to any of this?

For example, I was under the impression that ADCON0.7 = 1 sets the ADC to right (or left) justifed?

For ADCON0 = % 1000001, does this set ADC7 and ADC0 to active??

Other chips use ANSEL??

I wish there was a simply written (in lay terms) on what each command does exactly, and examples of each.

For CM1CON0.3 = 0, I used this only because I saw someone else put it in their code to turn off comparators. Where do you find all this? I have no idea what CM1CON0.3 = 0 actually does, and if for example I use CM1CON0.1 = 0, what would that do if anything?

The good news is I have gotten the 12F510 to read with ADCIN 0, adval. So, my code is working. However, I don;t exactly know why.

Thanks for your assistance Charles!

HenrikOlsson
- 21st December 2011, 15:46
Hi,
ADCON0 and CM1CON0 etc are not commands they are registers (memory in the PIC) which you use to control the ADC and the comparator in this particular case. When you use the dot notation, ie. ADCON0.7 = 1 you set bit 7 of ADCON0. When you THEN do ADCON0 = %0000001 you overwrite anything in ADCON with this new value thus clearing bit 7 that you just set.

The datasheet for the 12F510 (http://ww1.microchip.com/downloads/en/DeviceDoc/41268D.pdf), intimidating as it may seem, IS you friend. If you open it up and look at the comparator section you'll find what each bit in the CM1CON0 register does. Bit3, ie CM1CON0.3 happens to enable/disable the comparator, clearing that bit disables the comparator.

If you do a search for CM1CON0 in the datasheet you'll find table 4-1 on page 17. This shows the power up state of the registers and if you look at the CM1CON0 register you'll see that it's %11111111 on power up (all bits are set) which means that the comparator is enabled. Therefor CM1CON0.3=0 is needed in order to disable it.

Same thing for the ADC, look at the datasheet. It'll tell you what each bit in the register(s) does. For "small" devices like the 12F510 there may only be one or two registers controlling the ADC while on larger chip there are several. Always use the datasheet for the particular device you're working with.


/Henrik.

SOTASOTA
- 22nd December 2011, 00:36
Interesting. OK, making headway. So, I am looking at Table 4-1 on page 19.
For ADCON0 bit 0 turns the ADON? So if I put ADCON0.0=1 that I would expect ADON to enable?
Does this turn on all the ADC inputs? I see in the table ANS1, ANS0, ADCS1, ADCS0, CHS1, CHS0, GO/DONE, and finally ADON.
What troubles me is what do all those mean? Is all I need to configure the ADON in order to turn ON the ADC?

Thanks Henrik...making progress here.

HenrikOlsson
- 22nd December 2011, 07:10
Hi,
It depends if you're using ADCIN or manually "driving" the ADC module.

* You need to set the appropriate pins to analog which on the 12F510 is done thru bits 6 and 7 of ADCON0.

* Bits 4 and 5 selects from where the ADC gets it clock. When using ADCIN you can DEFINE ADC_CLOCK 3 which basically sets those two bits to 11, selecting INTOSC/4.

* Bits 2 and 3 selects WHICH of the analog channels that actually gets connected to the ADC. There's only ONE converter, each channel is multiplexed to that single converter by selecting it thru these bits. If using ADCIN it handles these bits for you based on the channel number you specify in the command. Ie ADCIN 1, ADVal will set these bits to 01 selecting channel 1.

* After selecting a channel you should wait a certain amount of time for the capacitor in the sample and hold circuit to charge. When using ADCIN this time can be specified thru DEFINE ADC_SAMPLEUS

* Bit 1 is the "start button" for the conversion. You set it to start a conversion and the ADC clears it again when the conversion is complete and result is available. When using ADCIN it handles this for you.

* ADCON0.0 is the "mains switch" for the ADC module. It needs to be set in order for the ADC to work but simply setting it does NOT actually perform a conversion. The actual conversion is initiated thru bit 1.

Read the complete ADC section in the datasheet - don't just look at the register. It explains it pretty well and this ADC is basically as simple as they get in a PIC so it's a great start for learning. It does not, however, tell you what the PBP ADCIN command handles for you and what it doesn't but understanding how it works makes it a lot easier to debug when it doesn't work...

/Henrik.

PS. On the 12F510 the ADC is 8 bits so your DEFINE ADC_BITS 10 won't work properly.

SOTASOTA
- 22nd December 2011, 14:02
Thank-you!!! That makes a whole-lotta-sense now Henrik!
It is finally "registering" with me. (pun intended)
I can see how PBP commands do much of the grunt work in getting the ADC paramteres to funtion correctly.
Yes, once I changed to 8-bit ADC, my code is now working flawlessly. I can now code to a deeper degree with this understanding.
Again...THANKS!!!!