Reference voltage for A/D inputs
Reference voltage for A/D inputs
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.)
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.)
- Steve
- Matrix Staff
- Posts: 3431
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
[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:
Each "\n" represents a "newline" character, so this produces the following code:
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.
3) Add a line at the end of the [Device] section that defines the chip:
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.
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"
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;
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"
Code: Select all
ChipName=16F876
- 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:
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
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.
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.
- 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:
Hello
Yes if you read the signal back as an INT then halving the range of the ADC would give a 9 Bit reading.
Yes if you read the signal back as an INT then halving the range of the ADC would give a 9 Bit reading.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
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
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
- Steve
- Matrix Staff
- Posts: 3431
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
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.
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.
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?
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?
- Steve
- Matrix Staff
- Posts: 3431
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
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:
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.
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
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.
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.
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.
- Steve
- Matrix Staff
- Posts: 3431
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
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.
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.
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.
- 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:
Hello
Let us know how you get on with them.
Let us know how you get on with them.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
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.
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.
1) When a file is copied and renamed how do you know whether it reads the old or new .FCD file?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".
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.
- 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:
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Steve
- Matrix Staff
- Posts: 3431
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
There are some optional parts to the FCD file that may be of help:
In the [Device] section, you can put these in:
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:
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.
In the [Device] section, you can put these in:
Code: Select all
ChipName=16F88
ConfigOverride=1
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 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.
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.