Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : MM2: Cfunction to read reference voltage
Author | Message | ||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8605 |
I think Geoff is correct to remove the pin(0) syntax as it seems to be unreliable on the MX170. Also, on the MX470 it doesn't work at all (this is in the chip errata documentation). However, for those of you who want it, here is a Cfunction to do it for you The usage should be obvious from the example code but ask any question as required. The CFunction code reads the reference voltage 9 times at 1msec intervals and throws away the first reading which often seems to be erroneous. It then averages the other 8 readings. To make things even easier it also returns the scale factor to apply to an ADC reading to correct it for VCC option default none option explicit dim integer adccount dim float measuredref,scalefactor const adcpin=23 setpin adcpin,ain do adccount=readref(scalefactor) 'read the reference voltage measuredref=adccount/1024.0*3.3 print measuredref, scalefactor print pin(adcpin)*scalefactor ' prints corrected voltage pause 1000 LOOP end ' ' reads the internal reference voltage ' ' function returns the ADC count for the reference voltage ' also the float parameter is updated with the scale factor to use for correcting any ADC readings ' i.e. scalefactor * voltage read = true voltage ' CFunction readref 00000000 27bdffc8 3c029d00 8c420004 34038013 afa40038 3c04bf81 ac83a000 afbe0030 afb7002c afb60028 afb50024 afb40020 afb3001c afb20018 afb00010 240403e8 afbf0034 0040f809 afb10014 3c021000 3c03bf88 ac621064 3c04bf88 34028000 3c03bf81 ac801068 24140001 00008021 ac629004 3c13bf88 3c1e000e 3c17ffff 34168f00 3c121000 3c151000 3c04bf81 3c02bf88 ac9e9040 3c03bf88 ac406118 3c04bf81 3c02bf81 ac606108 ac979050 3c03bf81 ac569020 24020003 ac629010 3c04bf81 240200e4 ac829000 3c03bf81 34028000 ac629008 8e621030 00521024 1040fffd 3c04bf81 8c829070 02028021 24020001 12820030 3c029d00 3c119d00 8e220004 3c03bf88 ac751034 0040f809 240403e8 2a820009 1440ffdc 26940001 3c021000 3c04bf88 ac821064 3c02bf88 8e320064 3c03bf81 ac401068 3c04bf81 34028000 ac629004 ac80a000 3c08453a 35082e8c 01009821 0010a7c3 8e220080 02002021 0040f809 02802821 00402821 0240f809 02602021 8fa30038 8fbf0034 ac620000 8fbe0030 001010c3 02801821 8fb7002c 8fb60028 8fb50024 8fb40020 8fb3001c 8fb20018 8fb10014 8fb00010 03e00008 27bd0038 8c420004 3c04bf88 ac951034 0040f809 240403e8 00008021 1000ffae 26940001 End CFunction |
||||
Chris Roper Senior Member Joined: 19/05/2015 Location: South AfricaPosts: 280 |
That looks excellent matherp, Thanks, I will give it a try. Cheers Chris http://caroper.blogspot.com/ |
||||
viscomjim Guru Joined: 08/01/2014 Location: United StatesPosts: 925 |
Excellent work Matherp. There seems to be no limits to what can be added using Cfunctions. Thats a good thing especially in situations like this. |
||||
G8JCF Guru Joined: 15/05/2014 Location: United KingdomPosts: 676 |
The first draft of V2.0 of the CFunctions tutorial together with V2.0 of CFuncgen itself are available for download from http://www.cfuncgen.dyndns.org/ V2.0 of the tutorials document includes a new chapter "C for MMBasic programmers" which tries to help MMBasic programmers get into C from a CFunction writing perspective. Peter The only Konstant is Change |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8605 |
Here is the CFunction code for reading the reference voltage. Basically this is cribbed from Geoff's source but the interesting bit is that I have been able to use the Microchip hardware library functions without any issues #define _SUPPRESS_PLIB_WARNING #include <plib.h> #define FAM #include "APIDefs.h" long long readref(float *a){ int anChan,i; long val=0; CM1CON = 0b1000000000010011; uSec(1000); // turn on the internal voltage reference and let it settle CloseADC10(); for(i=0;i<=8;i++){ anChan = ADC_CH0_POS_SAMPLEA_AN14; SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | anChan ); OpenADC10( ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON , \ ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_1| ADC_ALT_BUF_ON | ADC_ALT_INPUT_ON , \ ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15 , \ 0 /* anInput */ , \ SKIP_SCAN_ALL \ ); EnableADC10(); // Enable the ADC while ( ! mAD1GetIntFlag()) ; val+= ReadADC10(0); // read the result of the conversion if(i==0)val=0; //scrap the first reading mAD1ClearIntFlag(); uSec(1000); } CloseADC10(); // turn off the ADC so that it does not add to the power drain (particularly in sleep) CM1CON = 0; // turn off the internal voltage reference (in case pin = 0) *a=FDiv(M_ANY(0x453A2E8C),IntToFloat(val)); //0x453A2E8C = 1.2 * 8192 / 3.3 return val>>3; //divide by 8 } |
||||
MolsonB Regular Member Joined: 25/01/2015 Location: CanadaPosts: 48 |
With scalefactor as a float, I am getting the error. Changed it to a integer, and this error. I'm on 4.6B, not sure if that matters? MkII 44pin - v5.0 ColorMax 2 - v4.5 |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8605 |
This code only works on 4.7 (only use beta 8 until Geoff releases something later than b11). The normal pin(0) still works in 4.6b |
||||
MolsonB Regular Member Joined: 25/01/2015 Location: CanadaPosts: 48 |
Oh ok that makes sense. I was waiting to upgrade to 4.7 when it comes officially released. I'll play around with pin(0) until then. MkII 44pin - v5.0 ColorMax 2 - v4.5 |
||||
Print this page |