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

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

Reference voltage for A/D inputs

Postby echase » Thu Jun 28, 2007 11:25 pm

The 16F876 datasheet says the A/D inputs have a reference voltage input that is software selectable to be Vdd, Vss, RA2 or RA3. Where in Flowcode can I set it to select one of these references? If not selectable what is the default reference configuration used by Flowcode?

My temp sensors will give 1.4V max and my supply rail is 2.7V (using low voltage version of PIC.) If the reference is the supply rail then I am wasting half the resolution. Can I easily change the reference to say 1.5V without using an external 1.5V reference (I don’t have a spare input on RA2 or RA3 free for an external reference as temp sensors are connected there.)

User avatar
Steve
Matrix Staff
Posts: 3071
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 387 times
Contact:

Postby Steve » Fri Jun 29, 2007 9:07 am

[NOTE: I wrote all of this without realising you cannot use RA3 as the Vref+ pin, but it's all still relevant to anyone who has Vref+ free. I will give you another answer in another post]

The ADC component uses the Vdd line as the voltage reference. The code that defines this behaviour is in the FCD file for the chip. This is the relevant entry in the 16F876.fcd file:

In the "Code" section:

Code: Select all

ADCCapture="char ta, cnt;\nadcon1 = 0x00;\nta = trisa;\ntrisa = trisa | 0x2F;\nadcon0 = 0x81 | (%a << 3);\ncnt =0;\nwhile (cnt <220) cnt++;\nadcon0 = adcon0 | 0x04;\nwhile (adcon0 & 0x04) ;\ntrisa = ta;\nadcon1 = 0x07;\nadcon0 = 0x80;\n"


Each "\n" represents a "newline" character, so this produces the following code:

Code: Select all

char ta, cnt;
adcon1 = 0x00;
ta = trisa;
trisa = trisa | 0x2F;
adcon0 = 0x81 | (%a << 3);
cnt =0;
while (cnt <220) cnt++;
adcon0 = adcon0 | 0x04;
while (adcon0 & 0x04) ;
trisa = ta;
adcon1 = 0x07;
adcon0 = 0x80;


where %a represents the channel selected.

To use an external Vref+, you need to change the initial value of the adcon1 register to 0x01 (see the device datasheet). So here's what I suggest:

1) Copy the existing 16F876.FCD file and call it "16F876_Vref.FCD".

2) Edit the "ADCCapture" line of this new file so that adcon1 is set to 0x01 at the beginning, i.e.

Code: Select all

ADCCapture="char ta, cnt;\nadcon1 = 0x01;\nta = trisa;\ntrisa = trisa | 0x2F;\nadcon0 = 0x81 | (%a << 3);\ncnt =0;\nwhile (cnt <220) cnt++;\nadcon0 = adcon0 | 0x04;\nwhile (adcon0 & 0x04) ;\ntrisa = ta;\nadcon1 = 0x07;\nadcon0 = 0x80;\n"


3) Add a line at the end of the [Device] section that defines the chip:

Code: Select all

ChipName=16F876


Then you should be able to use the "16F876_Vref" as the target in place of the "16F876". You will need to connect 1.5V to your RA3 pin because this is now used as the Vref+ pin.

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

Postby Benj » Fri Jun 29, 2007 9:08 am

Hello

Yes you are correct in assuming that the reference voltage will be the same as the supply rail VDD. If you do not have a spare reference pin then im afraid you will have to leave the reference voltage as it is. However using 10 bits of accuracy means that you will still get a range of more then 0 - 512.

Hope this helps.

User avatar
Steve
Matrix Staff
Posts: 3071
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 387 times
Contact:

Postby Steve » Fri Jun 29, 2007 9:10 am

If you don't have RA3 free, you have 2 choices:

1) Use a chip with more ADC lines and free up the RA3 pin (e.g. the 16F886 is pin-for-pin compatible with the 876, but has additional ADC inputs on portB)

2) Add extra circuitry to amplify the signal

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

Postby echase » Fri Jun 29, 2007 9:27 am

I was using a Byte variable so presumably am only getting 7 bit resolution (half the range on 8 bits). But if I change this to an Integer variable will it increase to 9 bit resolution (half of 10 bits)? That would be accurate enough in my application.

You ought to add to the help file the fact that voltage accuracy is only as good as the accuracy of the supply rails. If for instance someone powered the PIC direct off a battery the accuracy would be poor.

I could spare a pin for a reference but only if I add a chip to multiplex 2 or more analogue sensors to one PIC input, which is inconvenient.

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

Postby Benj » Fri Jun 29, 2007 5:05 pm

Hello

Yes if you read the signal back as an INT then halving the range of the ADC would give a 9 Bit reading.

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

Postby echase » Fri Jun 29, 2007 6:03 pm

A 16F886 is a good device but very difficult to buy, even though allegedly cheaper than a 876, and not well supported by some software/programmers. E.g. it’s not mentioned as supported by your MCU Multiprogrammer, although I am not using one

User avatar
Steve
Matrix Staff
Posts: 3071
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 387 times
Contact:

Postby Steve » Mon Jul 02, 2007 9:07 am

We do support it with Flowcode and our programmer (although we have probably forgotten to update the documentation!).

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

Postby echase » Tue Jul 03, 2007 9:30 am

I found that, unlike RS Componenets, Farnell stock the 886 but only do about 2 variants of it as opposed to about 20 for the 876. I am using the very low voltage version of 876 and Farnell don't sell the equivalent in 886.

User avatar
Steve
Matrix Staff
Posts: 3071
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 387 times
Contact:

Postby Steve » Tue Jul 03, 2007 9:37 am

Have you tried Microchip Direct?

Alternatively, you could try requesting a sample direct from Microchip. These often take weeks to come, but this may be another option for you.

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

Postby echase » Tue Jul 03, 2007 10:38 am

I'd forgotten about that route but still can’t get a LF version. Not sure it’s manufactured. The standard 886 is a staggeringly low price.

If I got from Microchip a PICkit 2 serial programmer and software (DV164120) and PICkit 2 28-Pin Demo Board (DM164120-3), it would give me a USB connected programmer and a development board with 886 where I can build my circuit and programme the chip in situ.

Using code from Flowcode could I use their software/hardware to program the code direct into the chip with no translation, header files etc?

Saves me having to swap the chip over to my Velleman board for programming. See http://matrixmultimedia.com/mmforums/vi ... t=velleman and http://matrixmultimedia.com/mmforums/vi ... =vellerman

User avatar
Steve
Matrix Staff
Posts: 3071
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 387 times
Contact:

Postby Steve » Tue Jul 03, 2007 11:17 am

Flowcode works with PICkit2 without too many problems. The only issue is that the programmer software does not have a command-line interface, so cannot work directly from Flowcode.

However, it has a mode which will automatically reprogram a chip when the HEX has changed. I have made a small batch file which uses this feature to allow PICkit2 and Flowcode to work seamlessly.

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

Postby echase » Wed Jul 04, 2007 8:17 pm

The 886 has eleven A/D inputs bizarrely named AN0 to 13 with a gap in the numbering in the middle and not connected to the package pins in order. Flowcode lists them as ADC0 to 10. Can I assume that AN9 = ADC6 and AN13 = ADC10?

If I assign say pin RB3/AN9 to both an ADC input and a LED output does Flowcode manage the code to do both functions?

User avatar
Steve
Matrix Staff
Posts: 3071
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 387 times
Contact:

Postby Steve » Thu Jul 05, 2007 10:14 am

There is an error in that FCD file at the moment which does not allow you to use the AN11 to AN13 on this device.

The changes required to the FCD file are as follows:

Code: Select all

[Device]
Pins=28
Ports=5
ADCPins=14
    .
    .
    .

[ADCPins]
ADC0=2
ADC1=3
ADC2=4
ADC3=5
ADC4=7
ADC5=0
ADC6=0
ADC7=0
ADC8=23
ADC9=24
ADC10=22
ADC11=25
ADC12=21
ADC13=26


Changing the FCD file in this way will allow you to access these higher ADC channels.

There is a small side-effect. In Flowcode, you will be able to connect an ADC component to channels ADC5 to ADC7 - but the code will default to channel ADC0 (i.e. pin A0) when downloaded to the PICmicro.

A future version of Flowcode will hopefully fix this by not allowing connection to these unused "middle" channels.

I've also noticed another minor problem... the display of the ADC channel on the ADC component is wrong when the channel is above 9.

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

Postby echase » Thu Jul 05, 2007 1:15 pm

How does this double use work in practice? If say I set the pin to turn an LED on for a long time what happens when the ADC is read on same pin?

Presumably the LED will go off briefly whilst the pin is switched to its other function. But as it takes a fraction of a second to read an analogue input the off time will be irrelevant

Suitable diode/resistor protection is needed to prevent the circuits interacting, e.g. the analogue voltage from turning the LED on.

User avatar
Steve
Matrix Staff
Posts: 3071
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 387 times
Contact:

Postby Steve » Thu Jul 05, 2007 1:20 pm

I can't give an exact answer here, other than "it depends on the chip"...

The code in some FCD files handles this better, but in a lot of them the output state of a pin is not saved during the "ADCcapture" code. This means that when the pin is changed back to a digital output, it may not retain the original output value.

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

Postby echase » Mon Jul 09, 2007 12:04 pm

After some problems registering with Microchip Direct they have posted me free samples of 16F886 and some analogue interface chips I need. Ironically I was happy to pay for these but they only sell most of these types to business accounts, whereas samples are sent to anyone with a company email address.

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

Postby echase » Fri Jul 13, 2007 9:36 am

Have just received these sample 18F886s, a reference voltage IC, etc from Microchip. Took less than 1 week from order. I was lucky though as they had all 4 item in stock. Many are on backorder only.

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

Postby Benj » Fri Jul 13, 2007 10:34 am

Hello

Let us know how you get on with them.

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

Postby echase » Mon Jul 16, 2007 9:28 am

I tried my 886 after patching the 886.FCD file to correct the A/D inputs and changing the Vref as above. I added an external 2.5V Vref chip.

The circuit did not seem to behave as well as with the 876, with the LCD showing different things to the 876 version. Some of this is down to things in my post “Why hardware behaves different to Flowcode simulation?” I however wonder if the 886.FCD and PPP calculation of config words (there are 2 on the 886) is as properly debugged as the 876 versions as there are some residual issues I can’t explain. To be fair though I have not had much time yet to sort the bugs.

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

Postby echase » Tue Jul 17, 2007 9:08 am

Copy the existing 16F876.FCD file and call it "16F876_Vref.FCD".


Then you should be able to use the "16F876_Vref" as the target in place of the "16F876".


1) When a file is copied and renamed how do you know whether it reads the old or new .FCD file?
2) I used Wordpad to edit the file. It saved it with a .TXT extension. Do I need to amend this to an .FCD extension?
3) When an .FCD is changed does both the Flowcode simulation and the compiled code use this file to apply the changes?
4) Does the above change to adcon work with 886 too? I looked in the 886.FCD and it has almost identical code around the adcon statement.

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

Postby Benj » Tue Jul 17, 2007 11:21 am

Hello

1) All files with extension FCD will be read into Flowcode. Therefore if you rename the file "16F876_Vref.FCD" then "16F876_Vref" will be the name of the target device.

2) Yes it will need the .FCD file extension to work.

3) Yes the simulation and compiled code use the FCD file.

4) I think that the 876 and the 886 are part of the same family so the edit should work for both target processors.

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

Postby echase » Tue Jul 17, 2007 12:59 pm

So if there are 2 files for say the 886, one with original name and one different how does Flowcode know which applies? Do the line ChipName=16F876 define the one to use?

User avatar
Steve
Matrix Staff
Posts: 3071
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 387 times
Contact:

Postby Steve » Tue Jul 17, 2007 1:01 pm

There are some optional parts to the FCD file that may be of help:

In the [Device] section, you can put these in:

Code: Select all

ChipName=16F88
ConfigOverride=1


The first defines the processor name (if this entry is missing, the name of the FCD file is used to identify the chip used). If the second is used, then the configuration data for the chip is fixed and should be in a separate configuration section as follows:

Code: Select all

[Config]
0=0x3FBA
1=0x3FFC
2=0xFFFF
3=0xFFFF
4=0xFFFF
5=0xFFFF
6=0xFFFF
7=0xFFFF
8=0xFFFF
9=0xFFFF
10=0xFFFF
11=0xFFFF
12=0xFFFF
13=0xFFFF


I'm not 100% sure, but I think that all 14 places need a value - even if only 1 or 2 (as in this case) are used by the device.

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

Postby echase » Wed Jul 18, 2007 10:04 am

I have modified that adcon entry in the FCD file, took off the .txt extension and by renaming the file I see that when I pick the chip type both 886 and my revised 888_Vref are listed.

I have added an A/D component to simulate the external 2.5V reference to AD3 input. Problem is that when I run the simulation changing the slider on AD3 makes no difference to the readings on the other AD inputs. I kind of expect that as the sliders are not inputting a simulated voltage by just a BYTE number between 0 and 255 which is not related to any Vref. Is this a function of the way the simulator works?


Would be useful for future if one could enter a simulated voltage, like say 1.2V on pin 6. Then set parameters for chip voltage and Vref which the simulator uses to calculate the BYTE corresponding to input, e.g. number =1.2/Vref or 1.2 / PIC voltage depending on adcon value.