External Links

Creative Science Centre

 

Library_adc

// #include "http://www.byvac.com//mBlib/flb/Library/2016/lib_adc.bas"

ADC

Note: Unlike rookie 1 and 2, this does not use automatic conversion and so the adc_get is different, see below.

MX170 Devices
PORT    PIC    Channel
RA0      AN0      0
RA1      AN1      1
RB0      AN2      2
RB1      AN3      3
RB2      AN4      4
RB3      AN5      5
RC0      AN6      6   ** SMD Device 44 pin only
RC1      AN7      7   **
RC2      AN8      8   **
RB12    AN12     12
RB13    AN11     11
RB14    AN10     10
RB15    AN9      9

MX370 devices as follows
Chan, pin, Port
AN0  16  RB0
AN1  15  RB1
AN2  14  RB2
AN4  13  RB3
AN5  12  RB4
AN6  11  RB5
AN7  17  RB6
AN8  18  RB7
AN9  21  RB8
AN10 22  RB9
AN11 23  RB10
AN12 24  RB11
AN13 27  RB12
AN14 28  RB13
AN15 29  RB14
AN16 30  RB15
AN17 4   RG6
AN18 5   RG7
AN19 6   RG8
AN20 8   RG9
AN21 62  RE1
AN22 64  RE4
AN23 1   RE5
AN24 49  RD1
AN25 50  RD2
AN26 51  RD3
AN27 3   RE7

Initialise the ADC system for the particular channel, the channel number is given in the table as a range as above

adc_init(chan)

Example adc_init(6) // this will set RB12 to be an adc input

This only needs to be called once.

Gets the results of the adc channel
x = adc_get(channel,sample,timeout)

The above will get the results of an ADC conversion. The conversion begins when the function is called and will start with the sampling time. This is an arbitrary value, 1,000,000 will give about 1 second. After the sample period a conversion will take place. This is internally times but if the channel has not been initialised it can get stuck and so a timeout is specified. If it does get stuck the value returned will be 0 after the timeout period. The timeout of course must give the internal timer chance to do the conversion, 1000 should be adequate, of course the function returns as soon as the conversion is complete so a larger value will only take effect on a 'stuck' conversion.

x=adc_get(1,100,1000) Example for getting channel 1