-
Notifications
You must be signed in to change notification settings - Fork 0
/
jm_ez_analog.spin2
134 lines (94 loc) · 5.19 KB
/
jm_ez_analog.spin2
1
'' ================================================================================================='''' File....... jm_ez_analog.spin2'' Purpose.... Simple P2 analog input using smart pin'' Author..... Jon "JonnyMac" McPhalen'' Copyright (c) 2020 Jon McPhalen'' -- see below for terms of use'' E-mail..... [email protected]'' Started....'' Updated.... 29 JUN 2020'''' ================================================================================================={ Configures pin for analog input. -- uses sample window of 8192 ticks (14 bits SINC2 sampling) -- At 200MHz samples available every 40.1 microseconds}con { fixed io pins } RX1 = 63 { I } ' programming / debug TX1 = 62 { O } SF_CS = 61 { O } ' serial flash SF_SCK = 60 { O } SF_SDO = 59 { O } SF_SDI = 58 { I }var long ap ' analog pin long orlo ' output range, low long orhi ' output range, high long callo ' calibration, ground long calhi ' calibration, vio long setup ' true when pin setuppub null() ' This is not a top-level objectpub start(pin, lo, hi) | clo, chi'' Setup pin for analog input'' -- lo and hi define user range'' * scaled from Gnd to Vio (3.3v) stop() org fltl pin ' reset smart pin wrpin ##(P_ADC | P_ADC_GIO), pin ' read ground reference wxpin #%00_1101, pin ' 8192 samples wypin #0, pin dirh pin ' enable waitx ##(8192 << 2) ' allow 4x readings rdpin clo, pin ' save ground cal level fltl pin ' reset smart pin wrpin ##(P_ADC | P_ADC_VIO), pin ' read 3.3v reference wxpin #%00_1101, pin ' 8192 samples wypin #0, pin dirh pin ' enable waitx ##(8192 << 2) ' allow 4x readings rdpin chi, pin ' save Vio (3.3v) cal level fltl pin ' reset smart pin wrpin ##(P_ADC | P_ADC_1X), pin ' read input, no scaling wxpin #%00_1101, pin ' 8192 samples wypin #0, pin dirh pin ' enable end longmove(@ap, @pin, 5) ' save setup setup := truepub stop()'' Disable analog smart pin if previously configured if (setup) pinclear(ap) ' disable smart pin pinfloat(ap) longfill(@ap, 0, 6) ' mark disabledpub read() : result'' Read and scale output'' -- simple mx+b result := (rdpin(ap)-callo) * (orhi-orlo) / (calhi-callo) ' mx result := orlo #> result+orlo <# orhi ' +b (restricted to range)pub raw() : result'' Read analog level from pin'' -- not scaled/calibrated return rdpin(ap) con { license }{{ Terms of Use: MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.}}