This example demonstrates how to configure a P2 I/O pin to sample and capture an analog input, and display the raw values using DEBUG.
' Program reads an ADC and displays raw count CON _clkfreq = 100_000_000 'set clock frequency _pin = 0 'do conversion on this pin PUB go() | i pinstart(_pin, p_adc_1x | p_adc, 13, 0) 'init ADC pin repeat 'print sample set every 100ms i := rdpin(_pin) debug(sdec_(i), dly(100))
The PINSTART method is used to configure the I/O pin, in this case for the ADC mode, which is described in the Propeller 2 Spin2 Documentation:
PINSTART(PinField, Mode, Xval, Yval)
The arguments used in the method are:
pinstart(_pin, p_adc_1x | p_adc, 13, 0) 'init ADC pin
The pipe symbol (|) is a logical OR operator and merges the low-level 1x magnification ADC pin configuration with the ADC smart pin mode. The 13 selects 14-bit sampling. The 0 is only a filler for the unused Yval parameter.
RDPIN command obtains the ADC sample value and puts it into the variable
i := rdpin(_pin)
which is output to a DEBUG display with a 100 ms delay between values:
The values are raw numbers which are proportional to the voltage. To display millivolts you need to record the I/O pin’s values using the ADC’s internal switches to GND and VIO to calculate a scale factor and offset.