Analgue Interrupt

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
Docara
Posts: 315
Joined: Sun Jun 23, 2013 1:29 pm
Has thanked: 28 times
Been thanked: 61 times
Contact:

Analgue Interrupt

Post by Docara »

Hi

Has an analgoue interrupt been sorted out yet - I remeber there was talk of it about a year ago?

Thanks
Matt

Docara
Posts: 315
Joined: Sun Jun 23, 2013 1:29 pm
Has thanked: 28 times
Been thanked: 61 times
Contact:

Re: Analgue Interrupt

Post by Docara »

Found this on the net - It compiles without errors but how would you use it in FC (and I don't mean dropping it into a C icon).

// Testing interrupt-based analog reading
// ATMega328p

// Note, many macro values are defined in <avr/io.h> and
// <avr/interrupts.h>, which are included automatically by
// the Arduino interface

// High when a value is ready to be read
volatile int readFlag;

// Value to store analog result
volatile int analogVal;


// Initialization
void setup(){

// clear ADLAR in ADMUX (0x7C) to right-adjust the result
// ADCL will contain lower 8 bits, ADCH upper 2 (in last two bits)
ADMUX &= B11011111;

// Set REFS1..0 in ADMUX (0x7C) to change reference voltage to the
// proper source (01)
ADMUX |= B01000000;

// Clear MUX3..0 in ADMUX (0x7C) in preparation for setting the analog
// input
ADMUX &= B11110000;

// Set MUX3..0 in ADMUX (0x7C) to read from AD8 (Internal temp)
// Do not set above 15! You will overrun other parts of ADMUX. A full
// list of possible inputs is available in Table 24-4 of the ATMega328
// datasheet
ADMUX |= 8;
// ADMUX |= B00001000; // Binary equivalent

// Set ADEN in ADCSRA (0x7A) to enable the ADC.
// Note, this instruction takes 12 ADC clocks to execute
ADCSRA |= B10000000;

// Set ADATE in ADCSRA (0x7A) to enable auto-triggering.
ADCSRA |= B00100000;

// Clear ADTS2..0 in ADCSRB (0x7B) to set trigger mode to free running.
// This means that as soon as an ADC has finished, the next will be
// immediately started.
ADCSRB &= B11111000;

// Set the Prescaler to 128 (16000KHz/128 = 125KHz)
// Above 200KHz 10-bit results are not reliable.
ADCSRA |= B00000111;

// Set ADIE in ADCSRA (0x7A) to enable the ADC interrupt.
// Without this, the internal interrupt will not trigger.
ADCSRA |= B00001000;

// Enable global interrupts
// AVR macro included in <avr/interrupts.h>, which the Arduino IDE
// supplies by default.
sei();

// Kick off the first ADC
readFlag = 0;
// Set ADSC in ADCSRA (0x7A) to start the ADC conversion
ADCSRA |=B01000000;
}


// Processor loop
void loop(){

// Check to see if the value has been updated
if (readFlag == 1){

// Perform whatever updating needed

readFlag = 0;
}

// Whatever else you would normally have running in loop().

}


// Interrupt service routine for the ADC completion
ISR(ADC_vect){

// Done reading
readFlag = 1;

// Must read low first
analogVal = ADCL | (ADCH << 8);

// Not needed because free-running mode is enabled.
// Set ADSC in ADCSRA (0x7A) to start another ADC conversion
// ADCSRA |= B01000000;
}

Post Reply