readadc10 misfunction ? am i wrong ?

jyb

Senior Member
hi all
i am probabily wrong but i meet a problem using readadc10 command
what i would is using symbol named "canaladc" to give (e.g) A.2 or A.1
and symbol named "potentiometre" for word variable
like this " readadc10 canaladc , potentiometre" it works for potentiometre
but not for canaladc ....can't find why

above the entire prog (it is a cell from a bigger one)
it runs correctly if i replace "canaladc" by A.1 or A.2


#picaxe 40X2
symbol switches = b0 'lecture d'etat des switches pot et vitesse
symbol direction = b1 'contient le n° de port/bit de direction
symbol pulse = b2 'contient le n° de port/bit de marche moteur
symbol canaladc = b3
symbol potentiometre = w3 'contient la valeur lue du potar a lire
symbol delai = w5 'contient la valeur lue du delai entre train
symbol speedswitch = bit0
symbol potswitch1 = bit2 'switch sur axe pot1
symbol potswitch2 = bit3 'switch sur axe pot2
'marche arret ok 2 vitesses ok arret obligatoire pour changer de vitesse
'bit0 ne declanche pas d'interrupt 770 rpm
init:
sertxd ("cnc 31/775/775 rpm 2pot separes +delai")
setfreq em64
adcsetup = %0000000000000110
pulse = C.2
direction = C.6
delai = 1
canaladc = A.1
debut:
low A.7 switches = pinsD 'lecture etat switches
setint %00001100,%00001100,D 'armes les interruptions
if speedswitch = 0 then let delai = 5200 else let delai = 1 endif 'changement de vitesse
if potswitch1 = 0 and potswitch2 = 1 then low direction goto droite endif 'test sens de rotation droite
if potswitch1 = 1 and potswitch2 = 0 then high direction goto gauche endif 'test sens de rotation gauche
goto debut 'butoir de boucle

gauche:
readadc10 canaladc , potentiometre'lis valeur potar1
pulsout pulse ,delai pauseus potentiometre'envoies impulsion au moteur
goto gauche 'et recommences

droite:
readadc10 canaladc , potentiometre'lis valeur potar2
pulsout pulse ,delai pauseus potentiometre'envoies impulsion au moteur
goto droite 'et recommences

interrupt:
reset 'reprends au debut
 
hi all

#picaxe 40X2
symbol switches = b0 'lecture d'etat des switches pot et vitesse
symbol direction = b1 'contient le n° de port/bit de direction
symbol pulse = b2 'contient le n° de port/bit de marche moteur
symbol canaladc = b3 <--Replace with A.1
symbol potentiometre = w3 'contient la valeur lue du potar a lire
symbol delai = w5 'contient la valeur lue du delai entre train
symbol speedswitch = bit0
symbol potswitch1 = bit2 'switch sur axe pot1
symbol potswitch2 = bit3 'switch sur axe pot2
'marche arret ok 2 vitesses ok arret obligatoire pour changer de vitesse
'bit0 ne declanche pas d'interrupt 770 rpm
init:
sertxd ("cnc 31/775/775 rpm 2pot separes +delai")
setfreq em64
adcsetup = %0000000000000110
pulse = C.2
direction = C.6
delai = 1
canaladc = A.1 <--Remove
debut:
low A.7 switches = pinsD 'lecture etat switches
setint %00001100,%00001100,D 'armes les interruptions
if speedswitch = 0 then let delai = 5200 else let delai = 1 endif 'changement de vitesse
if potswitch1 = 0 and potswitch2 = 1 then low direction goto droite endif 'test sens de rotation droite
if potswitch1 = 1 and potswitch2 = 0 then high direction goto gauche endif 'test sens de rotation gauche
goto debut 'butoir de boucle

gauche:
readadc10 canaladc , potentiometre'lis valeur potar1
pulsout pulse ,delai pauseus potentiometre'envoies impulsion au moteur
goto gauche 'et recommences

droite:
readadc10 canaladc , potentiometre'lis valeur potar2
pulsout pulse ,delai pauseus potentiometre'envoies impulsion au moteur
goto droite 'et recommences

interrupt:
reset 'reprends au debut
Se bold markings.
 

hippy

Ex-Staff (retired)
This is likely expected behaviour. READADC uses analogue channel numbers, usually 0, 1, 2 etc. In earlier PICAXE the analogue channel numbers matched the pin numbers, but in later PICAXE not all do.

For example, on 40X2, A.1-A.7 are ADC channels 0-7, but B.0-B.5 are ADC channels 12, 10, 8, 9, 11, 13.

To read ADC channel 10 then 8 you can use either of -

ReadAdc 10, b0
ReadAdc 8, b1

ReadAdc B.1, b0
ReadAdc B.2, b1

Internally, pin names are actually given numeric values, eg B.1=1 and B.2=2 so the compiler converts to what ADC channel is meant when a pin name is specified within a READADC command -

ReadAdc B.1, b0 -->
ReadAdc Convert(B.1), b0 -->
ReadAdc Convert(1), b0 -->
ReadAdc 10, b0

When a pin name is assigned to a variable then the number is assigned without conversion, so

Let b13 = B.1 -->
Let b13 = 1

When a variable name is used in a READADC command the conversion done for pin name earlier is not performed -

Let b13 = B.1 : ReadAdc b13, b0 -->
Let b13 = 1 : ReadAdc 1, b0

So you end up reading ADC channel 1, not ADC channel 10 which is on B.1.

The solution is, to reference an ADC channel using a variable, assign the ADC channel number rather than the pin name -

Let b13 = 10
ReadAdc b13, b0

not

Let b13 = B.1
ReadAdc b13, b0

The same also applies with Symbol definitions using pin names rather than ADC channels. As above, the Symbol definitions should be -

Symbol myInputPot = 10
ReadAdc myInputPot, b0

not

Symbol myInputPot = B.1
ReadAdc myInputPot, b0
 
Last edited:

jyb

Senior Member
i read your explanation i apply it works !

This is likely expected behaviour. READADC uses analogue channel numbers, usually 0, 1, 2 etc. In earlier PICAXE the analogue channel numbers matched the pin numbers, but in later PICAXE not all do.

For example, on 40X2, A.1-A.7 are ADC channels 0-7, but B.0-B.5 are ADC channels 12, 10, 8, 9, 11, 13.

To read ADC channel 8 then 9 you can use either of -

ReadAdc 8, b0
ReadAdc 9, b1

ReadAdc B.1, b0
ReadAdc B.2, b1

Internally, pin names are actually given numeric values, eg B.1=1 and B.2=2 so the compiler converts to what ADC channel is meant when a pin name is specified within a READADC command -

ReadAdc B.1, b0 -->
ReadAdc Convert(B.1), b0 -->
ReadAdc Convert(1), b0 -->
ReadAdc 9, b0

When a pin name is assigned to a variable then the number is assigned without converstion, so

Let b13 = B.1 -->
Let b13 = 1

When a variable name is used in a READADC command the conversion done for pin name earlier is not performed -

Let b13 = B.1 : ReadAdc b13, b0 -->
Let b13 = 1 : ReadAdc 1, b0

So you end up reading ADC channel 1, not ADC channel 9 which is on B.1.

The solution is, to reference an ADC channel using a variable, assign the ADC channel number rather than the pin name -

Let b13 = 9
ReadAdc b13, b0

not

Let b13 = B.1
ReadAdc b13, b0
this a complete and limpid explanation as i like
thanks a lot for your efficient help
 

hippy

Ex-Staff (retired)
this a complete and limpid explanation as i like
thanks a lot for your efficient help
Thanks. I made a mistake in my original post on ADC channel numbers - had 8/9 for B.1/B.2 which should be 10/8 for B.1/B2 - which I have now corrected, but the principle remains the same.
 
Top