Go Down

Topic: How to use AD6 & AD7 in "Arduino Mini" (Read 1 time) previous topic - next topic

fallen

Hi ! "ATmega8/168 AI" have two more AD pin(AD6 & AD7) , Arduino use "ATmega8 PI" , so only have 6 AD pin(0-5) .

Now I see "Arduino Mini" use "ATmega168 AI" ,and have the AD6 & AD7 , I want to make sure that could I use " analogRead(6) ;" to use AD6 ?

thank you !

kuuk

#1
Sep 16, 2006, 10:50 am Last Edit: Sep 16, 2006, 11:12 am by leKuk Reason: 1
it didn't work for me.
i have an arduino mini from ars electronica. with arduino005 only 5 analog inputs work (AD0 -AD4). can anyone confirm this?

is this a software or hardware thing?


//kuk

edit:
i was wrong. six inputs work. but AD6 und AD7 don't.

mellis

Yes, the software doesn't yet support the extra analog inputs.  But take a look at ARDUINO/lib/targets/arduino/wiring.c and pins_arduino.c, you might be able to add the support easily.  

fallen

Hi! I know M168 have 5 PWM(D/A) , and I don't know how to add the other 2 PWM in :
Code: [Select]
int analog_out_pin_to_timer_array[NUM_DIGITAL_PINS] = {
     NOT_ON_TIMER,
     NOT_ON_TIMER,
     NOT_ON_TIMER,
     // on the ATmega168, digital pin 3 has pwm
#if defined(__AVR_ATmega168__)
     TIMER2B,
#else
     NOT_ON_TIMER,
#endif
     NOT_ON_TIMER,
     NOT_ON_TIMER,
     NOT_ON_TIMER,
     NOT_ON_TIMER,
     NOT_ON_TIMER,
     TIMER1A,
     TIMER1B,
#if defined(__AVR_ATmega168__)
     TIMER2A,
#else
     TIMER2,
#endif
     NOT_ON_TIMER,
     NOT_ON_TIMER,
};


If Arduino 06 can add these new AD and DA to soft , it will be better!

fallen


kuuk

Quote
Yes, the software doesn't yet support the extra analog inputs.  But take a look at ARDUINO/lib/targets/arduino/wiring.c and pins_arduino.c, you might be able to add the support easily.  


i must admit, i already had one look to see if could find something. didn't think it would be too easy. but i promise to try with a little more time in the next days.

fallen

kuk[ch65306]
I think mellis is right , AD6 and AD7 are the number of  "CH" , it is easy to add them ,and make them work  ;) . but I don't have "Arduino Mini" , so I can't test it , I will buy some M168 next week to make it sure.
the code:
Code: [Select]
int analogRead(int pin)
{
     unsigned int low, high, ch = pin;

     // the low 4 bits of ADMUX select the ADC channel
     ADMUX = (ADMUX & (unsigned int) 0xf0) | (ch & (unsigned int) 0x0f);

     // without a delay, we seem to read from the wrong channel
     delay(1);

     // start the conversion
     sbi(ADCSRA, ADSC);

     // ADSC is cleared when the conversion finishes
     while (bit_is_set(ADCSRA, ADSC));

     // we have to read ADCL first; doing so locks both ADCL
     // and ADCH until ADCH is read.  reading ADCL second would
     // cause the results of each conversion to be discarded,
     // as ADCL and ADCH would be locked when it completed.
     low = ADCL;
     high = ADCH;

     // combine the two bytes
     return (high << 8) | low;
}



kuuk

well,

i'm doing a little trial & error with no success so far.
do i understand it right that those files "wiring.c" and "pins_arduino.c" from the lib/targets/arduino folder are the EXACT files that are used when my program gets compiled?
do i have to restart the arduino IDE or are these files read each time when i start the compiling process?

so far, i tried my luck mostly in pins_arduino.c, as wiring.c seems to be written quite dynamical. but my programm seems unaffected. i can't even disable one of the existing analog ins :-/

@fallen
did you change someting in the code that you posted?

mellis

I'm pretty sure that wiring.c and pins_arduino.c get recompiled every time you compile/verify or upload your sketch, but I can't swear to it.  What did you try to disable an existing analog input?

fallen

kuk:
Yes , I change "ch = analogInPinToBit(pin) " to "ch = pin" , in "analogInPinToBit(pin)" , there are only return 0~5 , now you can use 0~7,  but I don't have "Arduino Mini" , so I can't test it , if it can't work , I so sorry about it.

kuuk

@fallen:
i see,

but don't i rather have to adapt the array in arduino_pins.c ?

Code: [Select]


pin_t analog_in_pin_to_port_array[NUM_ANALOG_IN_PINS] = {
     { PC, 0 },
     { PC, 1 },
     { PC, 2 },
     { PC, 3 },
     { PC, 4 },
     { PC, 5 },
};


i think this array maps the numbered pins to the right inputs on the atmega. i tried adding 2 lines
     { PC, 6 },
     { PC, 7 },
but that didn't work. (shouldn't it be ADC instead of PC anyway?) but even after i deleted some of the existing pairs, my program still worked with the six existing analog ins. i don't have my board here at the moment so i cannot work on it right now.

//kuk



kuuk

so shame on me,

i've always been editing the wrong files. i had downloaded arduino 005 twice, so it couldn't work.

ha, but now it does. you just need to edit "arduino_pins.c".

in line 53 change
Code: [Select]
#define NUM_ANALOG_IN_PINS 6 to
Code: [Select]
#define NUM_ANALOG_IN_PINS 8

then in
Code: [Select]

pin_t analog_in_pin_to_port_array[NUM_ANALOG_IN_PINS] = {
     { PC, 0 },
     { PC, 1 },
     { PC, 2 },
     { PC, 3 },
     { PC, 4 },
     { PC, 5 },
     { PC, 6 },
     { PC, 7 },
};


add the last two lines as shown here.

everything works fine for my needs. i don't need exact analog values though, so those two extra ins might still be a little different from the others.

thank you mellis and fallen for encouraging me to try this :-D

best, kuk

Go Up