Analog Input
Your Scamp computer has a multi-channel Analog to Digital Converter (ADC). The input voltage range is 0V to 3.3V.
On a Scamp1/Scamp2 pins 1, 2, 3, 5, 6, 10, 11 can be assigned as analog inputs. The input voltage range is 0V to 3.3V.
On a Scamp3 all pins except pin 8 can be assigned as analog inputs, giving you 11 channels of analog input.
The Scamp3e supports analog input on all pins, except pins 8, 10 and 11. The Scamp2e supports analog input on all pins, except pins 3, 8, 10 and 11. |
There is a bug in Scamp3/3e firmware versions v1.8 and earlier, where the reference oscillator output (REFO) is enabled for pin 9, when it shouldn't be. That is overriding all other functions associated with that pin. The following word will disable the REFO output, and all functions associated with pin 9 will work from then on.
: REFOoff Note: The datasheet for the PIC24FJ64GB202 processor indicates that RB15/AN9 (pin 12 on the Scamp1/Scamp2) is an analog input. This is an error on the datasheet, as RB15 is not available as an analog input on the PIC24FJ64GB202. See the Microchip Errata Sheet for more details. This does not affect Scamp2e, Scamp3 or Scamp3e.
Note: Pin 0 (AN2 on the PIC24) does not go down to 0 for a 0V input. A 0V input will read as a value of approx 20 or so. This is an issue with the PIC24 silicon and not your Scamp computer. I recommend using pin 0 only for analog inputs where approximate values are acceptable. For more accurate results, use other pins. |
Sampling an Analog Input
On a Scamp2, Scamp3 and Scamp3, the word adc12 will configure the ADC for 12-bit sampling, while adc10 will configure it for 10-bit sampling. (The Scamp2e supports 10 bit sampling only and the resolution can't be changed.)
The word analog will configure a pin to be an analog input. The format is pin analog. For example, to make pin 2 an analog input:
2 analog
To sample that analog input, first it must be selected as the current channel:
2 channel
Then the word sample performs the conversion and returns the result to the stack:
sample
In addition to the input channels, channel 29 will sample the negative reference voltage AVss (GND) and channel 30 will select the positive reference voltage, AVcc (3.3V).
29 channel
sample
will return 0 to the stack, while
30 channel
sample
will return 4096.
|
Note that Scamps with BSP v1.4 have a bug where channel 4 doesn't allocate properly. This is fixed in BSP v1.5 onwards. To work around the bug in BSP v1.4, simply create the word
: channel4 10 $228 ! ; |
|
This video shows you how to use analog inputs on a Scamp:
The ADC uses the system clock (Tcy) as the source for its conversion clock. On your Scamp, Tcy = 62.5 ns.
On the Scamp3/3e, the word adcclk can be used to change the number of Tcy cycles used by the ADC. adcclk takes an 8-bit value from the stack (0..255). The conversion clock is equal to Tcy multiplied by this value plus 1.
On the Scamp3/3e, the word adcclk can be used to change the number of Tcy cycles used by the ADC. adcclk takes an 8-bit value from the stack (0..255). The conversion clock is equal to Tcy multiplied by this value plus 1.
TAD = (n * TCY) + 1
For example, to make the conversion clock equal to 100 Tcy cycles:
#99 adcclk
The default setting for the conversion clock is 10, giving a conversion clock of 11 Tcy cycles (or 687.5 ns). On reset, the conversion clock resets back to 11 Tcy cycles. If you require a different setting, this should be configured in your initalization code. Note that a low value (fast conversion clock) may not give your ADC sufficient time to complete a conversion. The appropriate value depends on your application. For most simple applications, the default setting is fine.
Turning the ADC oFF and ON
The ADC module is enabled by default at reset. You can turn off the module using the word adcoff. The module can be enabled using the word adcon.
ADC Format
The word adcsgn lets you change the ADC format to a signed result, whereas adcunsgn lets you change the ADC format to an unsigned result. The word adcfrac lets you change the ADC format to a left-justified fractional result, whereas adcdec lets you change the ADC format to a right-justified decimal result.
Learn : Interfacing : Analog Input