ADC1 on 20X2 confusion

pmulvey

New Member
On the 20X2 pin 18 is B.0 / ADC1.

The following piece of code was tried with the results shown:

symbol Voltage = b.0

readadc Voltage, b1

;readadc Voltage, b1
^
;Error: ADC channel 0 does not exist!


;This line passes the syntax check
readadc b.0, b1

Whats happening?
 

westaust55

Moderator
This has arisen and been discussed before. You need to use a channel number.

The pin identified b.0 is a predefined symbol with the value 0
The ADC channel No are from 1.

If you put B.0 in directly the PE works it out for you but as a variable you need to have a value 1, 2, 3, etc as the channel number.

Edit:
Also as it is an X2 part do not forget the ADCSETUP as mentioned in the READADC section of manual2
 
Last edited:

inglewoodpete

Senior Member
The following code is (hacked) from a 20X2 program that I am currently developing. If you use similar syntax, the code should work for you too.

Code:
#PICAXE 20X2
'   Port B: I/O 
Symbol iZAxis0         = 1      'Leg 18 B.0 ADC1
Symbol oLCD            = B.1    'Leg 17 B.1 ADC2  Output to serial LCD
'ymbol <spare>         = B.2    'Leg 16 B.2 ADC4  
'ymbol <spare>         = B.3    'Leg 15 B.3 ADC5 
Symbol iXAxis0         = 6      'Leg 14 B.4 ADC6
'
Symbol wZVal         = w13      'b26,27
Symbol wXVal         = w14      'b28,29
'
Init:'Initialise ADC Inputs
     '           5432109876543210 ADC Channel
     ADCSetup = %0000000001000010        'Enable only ADC channels 1 & 6 (Pins B.0 & B.4)
'
Main:ReadADC10 iZAxis0, wZVal            'Read the accelerometer Z-Axis value
     ReadADC10 iXAxis0, wXVal            'Read the accelerometer X-Axis value
 

hippy

Ex-Staff (retired)
When the port.pin is used inline in the command the compiler automagically sorts out which actual ADC channel to use. It's only when a symbol is used to specify the channel number of the READADC does the issue arise.

For example, for the 20X2 the ADC channel for pin B.0 ( leg 18 ) is ADC1, so these two are the same ...

ReadAdc 1, b0

Symbol POT_ADC = 1
ReadAdc POT_ADC, b0

The compiler also converts "ReadAdc B.0" to "ReadAdc 1" but this will not happen for the following case -

Symbol POT_ADC = B.0
ReadAdc POT_ADC, b0

The "B.0" has a pin numbering value of "0" so this ends up as "ReadAdc 0" not the "ReadAdc 1" desired. There is no ADC0 so the compiler generates an error.
 
Top