Simple Analog Input

Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on print
Table of Contents
Description

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.

The RDPIN command obtains the ADC sample value and puts it into the variable i:

i := rdpin(_pin)

which is output to a DEBUG display with a 100 ms delay between values:

debug(sdec_(i), dly(100))

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. 

Source Code
Programming Language
Tools and Operating System
Document Author
Source Code Author
Table of Contents
0 0 vote
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jose Rullan

Thank you! I was in fact looking into how to configure a pin for analog input. Thanks for this. I hope there would be one Quick Bytes for each feature or keyword or concept. Great initiative!

2
0
Would love your thoughts, please comment.x
()
x
New Parallax Website!

We have a brand new website to bring you all the latest and greatest Parallax has to offer. Not to worry, our old site is still available and can be accessed at https://www1.parallax.com!