Reference voltage for A/D inputs

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

That's a good suggestion. But at the moment (as you know from experience!), Flowcode's ADC component assumes the Vrefs are Vdd and Gnd.

We have shown ways of changing the generated code so that you can get around this limitation in hardware. BUT the simulation will still assume Vdd and Gnd.

I'll look into this and might add this feature to a future version of Flowcode, especially if I can find a way of doing it whilst maintaining Flowcode's ease-of-use.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

What I’d like to see is a knob with 0 - 6V (6V being a bit above the presumed max any PIC can take on A/D input) on it instead of the current uncalibrated one. Then I can enter say 1.5V rather than have to back calculate from 1.5/Vref = 1/4 or so. Thus I need to wind this knob around to 1/4 way. It works wherever one gets Vref from. A further and possibly unnecessary complication would be a prescaler value so if people are feeding their voltage through a voltage divider they can enter that divide ratio.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

Make sure you close the editor after changing an FCD and also restart Flowcode. Else Flowcode may not be able to read the file you edited and I found really odd things then happen! Like I got so many error messages upon compiling the code that I thought I had completly fouled up my code.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

Are you sure changing the adcon value in the 886.FCD file works like it allegedly does for the 876? I tried it and the PIC still uses Vdd as the reference.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

The 886 FCD file requires the following part of the ADCsample section to be edited.

adcon1 = 0xXX; //should be replaced with

adcon1 = 0x30; //Configures the use of +VREF and -VREF

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

Does that mean I have to connect pin 4 (Vref –) to a known reference? Actually I only wanted to connect Vref+ externally and use the internal Vss(0 volts) for the negative ref. Then I still can use pin 4 as AN2.

There is another adcon value for this option (with VCGFG=0), but I am not very good at translating the datasheet into actual hex/adcon values, sorry. So what is it?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello

Yes you are correct. To use the +VREF and VSS as -VREF you must use this.

adcon1 = 0x10; //Configures the use of +VREF and VSS

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

Accuracy is a problem.

If I set Vin to 0.8V on all inputs (wired together so definitely same 0.8V on all) with a 2.5V ref, I would expect the PIC to read EXACTLY same (+/-1 count) on all inputs. But in reality get 80, 78, 82, etc. type readings with each channel steady but not same as other channels. Why is it varying? Have added capacitors on Vref and 0.8V in case ripple is causing variability. Might be because I am using 10k input resistors which is on limit of acceptability according to datasheet and also Vref, at 2.5V, is at lower limit of range allowed. But am running at slow clock speed so plenty of time to charge up the A/D input capacitor.


As the +/- 1 count, etc. errors should only affect the 10th bit and possibly 9th bit doesn’t it mean that the 8 bits that Flowcode actually uses should not be affected by these errors?

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

I was wrong, am using 1k resistors so that can't be the problem.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

I had too much ripple on the supply rails. My scope showed 50mV ripple. Now stable readings.

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Great - thanks for letting us know.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

Interesting that supply ripple makes such a difference. I sort of assumed that as the ref+, ground (ref-) and measured voltages are all stable then the supply could be anything. But whilst it can be any steady voltage, if the reference is set to eternal ref+, it obviously does not like ripple.

tinker
Flowcode V4 User
Posts: 8
Joined: Mon Jul 02, 2007 9:35 am
Contact:

buying pics

Post by tinker »

I found this company to be very competitive on chip prices and good delivery

http://www.crownhill.co.uk/level3.php?cat=14&sec=3
Jane

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Here is another well priced company to buy microcontrollers from. However be prepared for a fairly long delivery time.

http://www.futurlec.com/ICMicrochip_PIC16.shtml

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

I see Steve has made quite a few amendments to the 16F886.FCD file in the 3.2.2 version, so I used the instructions above to modify this file to change a value to adcon1=0x10 as above. This is to get it to read an external Vref+. Unfortunately when compiling it now gives about 500 error messages, only some of which are below.

It does not give these mesages if I use the unmodified file.

Is there some other way to modify it to get the external ref to work? Or I could just use my old hacked FCD which seems to still work OK.

Also I see that there are a lot of options put into the file now for interrupts. How are these activated and what are they for?

Errors:-

C:\Documents and Settings\\886v9.c(236:2): error: invalid operand 'trisc'
C:\Documents and Settings\\886v9.c(236: : error: failed to generate expression
C:\Documents and Settings\\886v9.c(238:2): error: unknown identifier 'portc'
C:\Documents and Settings\\886v9.c(238:2): error: invalid operand 'portc'
C:\Documents and Settings\\886v9.c(238: : error: failed to generate expression
C:\Documents and Settings\\886v9.c(238:2): error: error in the body of 'if' expression
C:\Documents and Settings\\886v9.c(485:2): error: unknown identifier 'adcon0'
C:\Documents and Settings\\886v9.c(485:2): error: invalid operand 'adcon0'
C:\Documents and Settings\\886v9.c(485: : error: failed to generate expression
C:\Documents and Settings\\886v9.c(511:2): error: unknown identifier 'trisb'
C:\Documents and Settings\\886v9.c(511:2): error: invalid operand 'trisb'
C:\Documents and Settings\\886v9.c(511: : error: failed to generate expression
C:\Documents and Settings\\886v9.c(512:24): error: unknown identifier 'portb'
C:\Documents and Settings\\886v9.c(512:24): error: invalid operand 'portb'
C:\Documents and Settings\\886v9.c(512:30): error: failed to generate expression
C:\Documents and Settings\\886v9.c(512:30): error: invalid operand '& '
C:\Documents and Settings\\886v9.c(512:22): error: failed to generate expression
C:\Documents and Settings\\886v9.c(1774:2): error: unknown identifier 'trisb'
C:\Documents and Settings\\886v9.c(1774:2): error: invalid operand 'trisb'
C:\Documents and Settings\\886v9.c(1774: : error: failed to generate expression
C:\Documents and Settings\\886v9.c(1776:2): error: unknown identifier 'portb'
C:\Documents and Settings\\886v9.c(1776:2): error: invalid operand 'portb'
C:\Documents and Settings\\886v9.c(1776: : error: failed to generate expression
C:\Documents and Settings\\886v9.c(1776:2): error: error in the body of 'if' expression
C:\Documents and Settings\\886v9.c(1783:2): error: unknown identifier 'trisc'
C:\Documents and Settings\\886v9.c(1783:2): error: invalid operand 'trisc'
C:\Documents and Settings\\886v9.c(1783: : error: failed to generate expression
C:\Documents and Settings\\886v9.c(1785:2): error: unknown identifier 'portc'
C:\Documents and Settings\\886v9.c(1785:2): error: invalid operand 'portc'

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello Echase

Can you send me your FCF file and your new modded FCD file that generated all of the errors.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

Will send tonight. Have only changed a 0 to a 1 so change was not big, unless I somehow corrupted the file when saving it. But I edited it and saved it twice as a check.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello Echase

You need to add the line

ChipName=16F886

to the [device] section of the FCD.

When the FCD is not specifically called the Chip name then the compiler must know which chip you are accessing so that it can look up the addresses of the function registers.

I also noted from your warning messages that the stack is going over the recommended hardware buffer. BoostC can cope with this and create its own software stack but it is always better to keep the stack size as low as possible. Eg call a function from the main and then return to the main and call another function if you can.

Finally if you are having problems with code being re-entered when using interrupts then see if you can try to move all of the code into the main loop o f the program. Then you can use the interrupts to set variables which can then be passed into the main program and used to control the LCD etc.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Post by echase »

Many thanks.

What is the answer to my previous question:-

Also I see that there are a lot of options put into the file now for interrupts. How are these activated and what are they for?

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Re: Reference voltage for A/D inputs

Post by echase »

I now need to make this change of Vref to read an external positive ref on AN3 for an 18F2520 device. ADCON1 is initially set to 0F in the fcd file. If I change this to 1F will it work?

ADCON1 has a slightly different function compared with the 886 device in that the lowest nibble is now used to set the port to digital I/O or A/D input. Seems a bit strange that the fcd file, by setting 0F initially, sets all the inputs to digital in a section of code about A/D. Should it not be setting them all to A/D?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Reference voltage for A/D inputs

Post by Benj »

Hello

I have editied the fcd file for you to include the +Vref. You are correct that the adcon1 value should be changed to 0x10 to enable the +Vref and also the analogue inputs.

The line of code that sets the adcon1 register to 0x0f is actually converting all I/O to digital on startup so you can use the digital I/O functionality.

Code: Select all

Initialise="adcon1 = 0x0F;\n"
Here is the part of the FCD that I have edited.

Code: Select all

ADCCapture="//set up ADC conversion\nchar old_tris, cnt;\n#define MX_ADC_SAMP_TIME 40\nadcon2 = 0x03;\n\n//find appropriate bit\n#if (%a == 0)\n  #define MX_ADC_TRIS_REG  trisa\n  #define MX_ADC_TRIS_MSK  0x01\n  adcon1 = 0x1E;\n#endif\n#if (%a == 1)\n  #define MX_ADC_TRIS_REG  trisa\n  #define MX_ADC_TRIS_MSK  0x02\n  adcon1 = 0x1D;\n#endif\n#if (%a == 2)\n  #define MX_ADC_TRIS_REG  trisa\n  #define MX_ADC_TRIS_MSK  0x04\n  adcon1 = 0x1C;\n#endif\n#if (%a == 3)\n  #define MX_ADC_TRIS_REG  trisa\n  #define MX_ADC_TRIS_MSK  0x08\n  adcon1 = 0x1B;\n#endif\n#if (%a == 4)\n  #define MX_ADC_TRIS_REG  trisa\n  #define MX_ADC_TRIS_MSK  0x20\n  adcon1 = 0x1A;\n#endif\n#if (%a == 8)\n  #define MX_ADC_TRIS_REG  trisb\n  #define MX_ADC_TRIS_MSK  0x04\n  adcon1 = 0x16;\n#endif\n#if (%a == 9)\n  #define MX_ADC_TRIS_REG  trisb\n  #define MX_ADC_TRIS_MSK  0x08\n  adcon1 = 0x15;\n#endif\n#if (%a == 10)\n  #define MX_ADC_TRIS_REG  trisb\n  #define MX_ADC_TRIS_MSK  0x02\n  adcon1 = 0x14;\n#endif\n#if (%a == 11)\n  #define MX_ADC_TRIS_REG  trisb\n  #define MX_ADC_TRIS_MSK  0x10\n  adcon1 = 0x13;\n#endif\n#if (%a == 12)\n  #define MX_ADC_TRIS_REG  trisb\n  #define MX_ADC_TRIS_MSK  0x01\n  adcon1 = 0x12;\n#endif\n\n//sanity check\n#ifndef MX_ADC_TRIS_REG\n  #pragma error ADC conversion code error - please contact technical support\n#endif\n \n//store old tris value, and set the i/o pin as an input\nold_tris = MX_ADC_TRIS_REG;\nMX_ADC_TRIS_REG = MX_ADC_TRIS_REG | MX_ADC_TRIS_MSK;\n \n//turn ADC on\nadcon0 = 0x01 | (%a << 2);\n \n//wait the acquisition time\ncnt = 0;\nwhile (cnt < MX_ADC_SAMP_TIME) cnt++;\n \n//begin conversion and wait until it has finished\nadcon0 = adcon0 | 0x02;\nwhile (adcon0 & 0x02);\n \n//restore old tris value, and reset adc registers\nMX_ADC_TRIS_REG = old_tris;\nadcon1 = 0x1f;\nadcon0 = 0x00;\n \n#undef MX_ADC_TRIS_REG\n#undef MX_ADC_TRIS_MSK\n#undef MX_ADC_SAMP_TIME\n"
Attachments
18F2520_VREF+.fcd
(7.49 KiB) Downloaded 420 times

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Re: Reference voltage for A/D inputs

Post by echase »

You need to add the line
ChipName=18F2520 to that revised file

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Reference voltage for A/D inputs

Post by Benj »

Right you are well spotted.

Here is the ammended file.
Attachments
18F2520_VREF+.fcd
(7.51 KiB) Downloaded 445 times

User avatar
tanlipseong
Posts: 31
Joined: Thu Aug 27, 2009 3:11 pm
Location: Malaysia
Contact:

Re: Reference voltage for A/D inputs

Post by tanlipseong »

Benj,

16F887 datasheet says that there is a "Fixed voltage reference (0.6V)" in the device for Comparators
and ADC Module.

how can i set it as Vref for ADC using Flowcode 3 to get more resolution (for 0V~0.5V range ADC application).

Please help

thanks in advance.

Tan

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Reference voltage for A/D inputs

Post by Benj »

Hello,

I have had a quick look through the datasheet and this is what it says about the 0.6V fixed reference.
A simplified circuit for an analog input is shown in Figure 8-6. Since the analog input pins share their connection with a digital input, they have reverse biased ESD protection diodes to VDD and VSS. The analog input, therefore, must be between VSS and VDD. If the input voltage deviates from this range by more than 0.6V in either direction, one of the diodes is forward biased and a latch-up may occur.
The comparitor has access to this reference voltage so you can tell if an input is under or over that voltage. However I don't think that the ADC +VREF voltage can run down that low. I have seen someone trying previously to run the +Vref pin at 1V and not being able to get it to work. The lowest voltage you can use for +Vref is 2.2 to 2.7V as listed on page 257 of the device datasheet.

Flowcode v4 allows you direct access to the +Vref functionality using the component properties. The ADC routines in Flowcode v3 are contained within the chip FCD file located in the "Flowcode v3/FCD" folder

Post Reply