View Full Version : A/D converter PIC16F1827
dovegroup
- 15th December 2017, 16:53
i use this code but is not working
DEFINE ADC_BITS 10
DEFINE ADC_SAMPLEUS 50
'DEFINE ADC_CLOCK 3
ADCON0 = %00000001
ADCON1 = %01100011
ANSELA = %00000111
TRISA = %00000111
How to enable three channels (AN0,AN1,AN2) A/D converter PIC16F1827
any help please
HenrikOlsson
- 15th December 2017, 17:46
You've selected the FVR as reference for the ADC but I see no code that configures the FVR (it's disabled on POR).
That's how far I got with what's shown.
/Henrik.
mpgmike
- 15th December 2017, 17:53
ADCON1 = %01100011
For your ADC Clock to = 3, ADCON1 must be %0011....
You have the ADC Module looking at the FVR for a VREF+ (bits <2-0> of ADCON1). Nowhere in your listing do you mention setting up the FVRCON register so your ADC has its positive reference turned on. You might want to change ADCON1 to %00110000 and use Vdd for your VREF+.
You can use the ADCIN command, ADCIN 0, VAR and PBP will automatically adjust ADCON0 to AN0 for you, or you can work it manually:
ADCON0 = %00000011
DO
LOOP WHILE ADCON0.1 = 1. ;The GO bit
VAR = ADRESH
This gives you an 8-bit ADC value. For AN1, ADCON0 = %00000111, for AN2, ADCON0 = %00001011, and so forth.
dovegroup
- 19th December 2017, 08:50
mpgmike Thank you very much
Can you tell me how I will get a 10-bit price in the baty variable
my programm
baty var word
arxh:
ADCON0 = %00000011
DO
LOOP WHILE ADCON0.1 = 1
adcin 0,baty
if baty > 1050 then
gosub OK
endif
if baty < 1000 then
gosub noOK
endif
goto arxh
Ioannis
- 19th December 2017, 09:46
Look from page 142 and after the details of the AD converter. It always makes a 10 bit conversion. It is up to you if you want 8 or 10 bits result.
Anyway, having ADFM=1, the result is Right justified and stored in the ADRESH, ADRESL.
So get your result in the baty variable as:
baty.HIGHBYTE=ADRESH
baty.LOWBYTE=ADRESL
or
baty.BYTE1=ADRESH
baty.BYTE0=ADRESL
In any case for the variable handling (e.g. baty.BYTE1, etc) look at the PICBASIC reference manual.
Ioannis
HenrikOlsson
- 19th December 2017, 10:29
You don't have to first do a "manual conversion" (via the GO/DONE bit) and then do a ADCIN (which basically does the same thing for you).
If you're going to use ADCIN then add a DEFINE ADC_BITS 10. If you're going to use the "manual method" just do what Ioannis showed and get rid of the ADCIN command.
And once you DO have your 10-bit resuly this won't work because baty will never be >1050 since the maximum result returned by the 10 bit ADC is 1023:
if baty > 1050 then
gosub OK
endif
/Henrik.
mpgmike
- 19th December 2017, 11:31
There are some 12-bit ADC Modules in a few of the PICs if you want better resolution. Referencing the ADMF = 1, that would be ADCON1.7 = 1. Let us know how you make out.
dovegroup
- 19th December 2017, 12:50
Thank you all for the help. Excuse me .I am a novice to PICBASIC
Ioannis
- 21st December 2017, 08:47
That's what we are here for. Don't worry being novice. We all are, one way or another!
Henrik caught the over 1023 point that I missed. Be careful on this as the 10 bit might be not enough for what your are after.
But you can always scale the measurements. Tell us more to help.
Ioannis
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.