PDA

View Full Version : Simple ADC problem


onklen
04-20-07, 07:30 AM
I'm trying to create a function which should make a moving average of some values from the adc channel. But I would like to be able to call the function and sending the start address of the buffer and which adc channel it should take new values from. As seen in my example below it's pretty simple to send the start address of the buffer, but I'm pretty lost in how to send the adc channel.

: ADC-NEW DUP 21 1 DO I
+ DUP 1 - SWAP @ SWAP ! DUP
LOOP
DROP ADC9 ANALOGIN SWAP 20 + ! ;

So this example takes the new data and shift it through the 21 word buffer and the last word should be the new data from channel ADC9. But is there anyway to avoid writing ADC9 in the function and instead putting something on the stack and making Isomax understand that it is channel 9 it should take the new data from.
The reason is that I need to get new data from a lot adc channels and would like to re-use the function.

Rolf

nmitech
04-20-07, 11:00 AM
: ADC-NEW DUP 21 1 DO I
+ DUP 1 - SWAP @ SWAP ! DUP
LOOP
DROP ADC9 ANALOGIN SWAP 20 + ! ;

So this example takes the new data and shift it through the 21 word buffer and the last word should be the new data from channel ADC9. But is there anyway to avoid writing ADC9 in the function and instead putting something on the stack and making Isomax understand that it is channel 9 it should take the new data from.
The reason is that I need to get new data from a lot adc channels and would like to re-use the function.

Here is one way to do it,

: CHNL0 ADC0 ;
: CHNL1 ADC1 ;
: CHNL2 ADC2 ;


:CASE ADCX ( char --- )
( 0 ) CHNL0
( 1 ) CHNL1
( 2 ) CHNL2
;

: ADC-NEW ( "char" "address" --- )
DUP 21 1 DO I
+ DUP 1 - SWAP @ SWAP ! DUP
LOOP
DROP ADCX ANALOGIN SWAP 20 + ! ;


i.e.,
1 300 ADC-NEW
where,
300 is the buffer starting address
1 ADCX returns ADC1

Note: There is no space between colon and CASE ":CASE"

Dave
04-20-07, 12:05 PM
Another possible solution is to use the PIN designation for ADC9 (or whatever ADC pin is needed).

Using 72 PIN is shown as equivalent to addressing ADC9 on page 30 of the IO Programming document (http://www.newmicros.com/store/product_manual/IOProgramming.pdf) Explanation of the PIN addressing starts on page 27.

: ADC-NEW DUP 21 1 DO I
+ DUP 1 - SWAP @ SWAP ! DUP
LOOP
DROP ADC9 ANALOGIN SWAP 20 + ! ;

could become

: ADC-NEW DUP 21 1 DO I
+ DUP 1 - SWAP @ SWAP ! DUP
LOOP
DROP 72 PIN ANALOGIN SWAP 20 + ! ;

with 72 to be changed according to what ADC pin was to be read. Perhaps something like :

VARIABLE ADC-PIN

72 ADC-PIN ! ( set for ADC9 reading

could be used beforehand to set which could be used as :

: ADC-NEW DUP 21 1 DO I
+ DUP 1 - SWAP @ SWAP ! DUP
LOOP
DROP ADC-PIN @ PIN ANALOGIN SWAP 20 + ! ;

onklen
04-23-07, 02:08 AM
Thx for the replies. It is of course two obvious methods to solve the problem when you see them.