PDA

View Full Version : ADC resolution for 16F616



pescador
- 26th April 2018, 01:45
I'm using a 16F616 analog input with a ADCIN routine. My resolution is 5V / 256 = about .19 mv. I'm using a voltage divider for the input with .1% tolerance resistors. Is there a better way to improve my accuracy? It seems that my code is not reacting to .19mv steps, more like 100 or 200 mv at best. I tried 10 bit resolution but never got that to work - pretty sure my code was wrong.


DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 10

pescador
- 26th April 2018, 02:17
Note: - I'm not doing anything as far as CCP1CON, CM1CON0,CM2CON0, CM2CON1, ADCON1.

Yes I'm new...

pescador
- 26th April 2018, 02:43
I've been reading other posts - so I tried this to see if light would turn green - fail


DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

ADCON1=%10000010

OUTPUT PORTA.1 ' FAULT LED
OUTPUT PORTA.0 ' TIMING LED

ANSEL = PORTA.2 ' Analog in setup for AN2, pin 11

adval var word ' holds analog value
GREEN VAR PORTA.0 ' TIMING LED
RED VAR PORTA.1 ' FAULT LED

low green
low red

main:

ADCIN 2, ADVAL ' read analog coming into pin 11 and store variable in adval
if adval < 500 then
high green
endif

goto main

pescador
- 26th April 2018, 03:02
Its working - but crazy numbers now - 32k instead of 1024 uggg...

pescador
- 26th April 2018, 03:23
I dont know what I'm doing.. I dont know what to set ADCON1 to.

pescador
- 26th April 2018, 03:36
I'm using Analog port 2 on 16F616 - 4Mhz.. VDD as reference. Any help would be great. I didn't have to set the ADCON registers for 8 bit. Why is 10 bit different? Was I lucky?

mpgmike
- 26th April 2018, 05:39
First; when you look at a Special Function Register in the Data Sheet, there are letters & numbers above each Bit in the Register. For example, ADCON0 bit 7 (denoted as ADCON0.7) has "R/W-0" above it. This means you can read that bit (Bit_Var = ADCON0.7), you can write to that bit (ADCON0.7 = 1), and the default value is "0" (R/W-0). ADCON1 is just your A/D Conversion Clock selector, which is handled with your "DEFINE ADC_CLOCK 3" statement. Get rid of your "ADCON1 = %10000010" statement. First, it is handled with the DEFINE ADC_CLOCK 3", secondly, you have bits 7 & 2 set which are not used and default to 0.

Starting on page 73 (Section 9.0) of the PIC16F610/16HV10_PIC16F616/16HV616 Data Sheet (which you can download from Microchip.com) the ADC Function is covered. Read that section to get a better idea of what you're asking the PIC to do for you. On page 78 (Register 9-1) is the ADCON0 Register. Look at the ADFM bit (bit 7 on the left). It determines if the result is Left or Right Justified. To better understand what that means, look at Registers 9-3, 9-4, 9-5, and 9-6 on page 80. If you wanted an 8-bit result you would Justify Left (ADCON0.7 = 0) and simply read ADRESH. If you want a 10-bit result, Justify Right (ADCON0.7 = 1) and read ADRESL into the LOWBYTE of your variable, and ADRESH into the HIGHBYTE. This isn't covered in the PBP3 Reference Manual, and you don't need to know it to use the ADC feature with PBP3. However, it helps to at least understand what's under the hood.

Next, look at Section 4, starting on page 33 of the Data Sheet. It covers I/O Ports. Register 4-1 is your PORTA Register; 1 = HIGH, 0 = LOW. Next is your TRISA Register, which stands for TRIState. Bits 7-6 are shaded grey as this PIC only has PORTA <5:0>, there is no PORTA.6 or 7. Next, if the bit is clear (0) the port is configured as an Output. If the bit is set (1) it is configured as an input. An easy way to remember this is a number 1 looks like the letter I for Input. The number 0 looks like the letter O for Output. Register 4-3 is your ANSELA Register, which is used to denote Input ports as Analog Inputs. A "1" makes it analog. You must configure the port as an Input in the TRISA Register for the ANSELA Register to work. Setting an ANSELA bit on a pin set up as an output in the TRISA Register can cause unpredictable results (on some PICs it causes a current overload). And now for one that I think may be causing you issues, the WPUA Register (Register 4-4 on page 35). Remember the number above the Bit in the Register denotes the default value. The Weak Pull Up Enable is defaulted to ON. Unless you turn it off with the WPUA Register, it could be causing you...well, what you described. "WPUA = 0" is a quick fix.

Take a weekend to read through the Data Sheet. It's a lot to absorb, but your answers are always in there. Although I've never worked with the PIC16F616, it looks like a pretty good one. I hope this helps.

pescador
- 26th April 2018, 13:17
Your answer is appreciated, you took awhile to answer and you're right - read. Most people would just reply with - hey idiot - read the docs for petes sake.
I've read the docs about 25 times, but sometimes this 65 year old brain is a bit slow.

Now - I didn't start w page 73 on this topic, I started with 74. If I would have slowed down and read pg 73 I might have understood better. So - again thanks for taking the time to explain. I actually got it to work by "massaging the ADCON0 register but need to read your reply again and figure out why it worked.

Would you be willing to write code for me? - for a price of course.. email [email protected] - my name is Rich. I could use a good programmer for my side business. (hope this is ok to post)

pescador
- 26th April 2018, 13:40
My ADCON0 register looks like this 10010011. Now I have to figure out why it worked.

starting from left

1 = Right justified, needed for 10 bit
0 = reference is VDD
0010 = using AN2
1 = not sure..
1 = ADC is enabled.


INPUT PORTA.2
ANSEL - PORTA.2
WPUA = ?

mpgmike
- 26th April 2018, 14:29
With PBP3 you won't need to even list ADCON0 unless you change your resolution. PBP3 has to address the ADCON0 Register to select the appropriate AD Channel, and the Justify Bit. Try not listing ADCON0 or ADCON1 at all and see if it still works.

Ioannis
- 27th April 2018, 07:44
Everything you need to setup the registers is really in the pdf of your chip. Please be careful selecting new chips as Microchip may issue a Preliminary pdf file having a lot of errors. Also keep your eye on the errata files that Microchip publishes on the chip's page.

Now, you asked for more precision on your first post.

Most chips have 8 or 10 bits at the low end 8-bit chips. If you can spare some time on your chip and your application, then you may use the Oversampling routine published here: http://dt.picbasic.co.uk/CODEX/AnalogueOverSampling

You may reach 16-bits results given you will sacrifice 50-100ms, depending on the chip. I thing 12 or 14 bits are good enough with maximum 6.2ms delay in your program (less if you have a fast MCU).

With the routine mentioned, you do not need to filter your results for noise or invalid sampling.

Ioannis