attachInterrupt() + prescaler?

Hello friends!

After investigating for some time on the net, I do not get a library that allows me to create a prescaler for an external interrupt (as PIC).

Arduino UNO may not support this function?

Thanks.

I do not get a library that allows me to create a prescaler for an external interrupt (as PIC).

A prescaler refers to how often a timer will fire an event, based on clock ticks.

Timers and timer-based events are NOT external interrupts.

If you explained what you were trying to do, we might (no guarantees) be able to help.

Thanks for answering. :slight_smile:

I need to make a prescaler for an external interrupt.

PIC have a prescaler for interrupt by clock pulse, and external interrupts.

There are also these prescaler for Arduino UNO?

betances:
Thanks for answering. :slight_smile:

I need to make a prescaler for an external interrupt.

PIC have a prescaler for interrupt by clock pulse, and external interrupts.

There are also these prescaler for Arduino UNO?

I think Paul was asking WHAT you are trying to do, not HOW you are trying to do it.

I think Paul was asking WHAT you are trying to do, not HOW you are trying to do it.

True.

I need to divide a clock frequency between 383. The frequency is variable (between 1 and 5 MHz).

You can clock Timer0 thru an external pin, but I don't think you can prescale it. I'll look into it and let you know. I have no idea if there is some library support for it.

EDIT: It looks like you can also clock Timer1 thru an external pin, same rules no prescaler but it's a 16 bit counter.

Betances.. CTC !!!

where no prescaler! there CTC !!!

XD

Nataly López
Venezuela

You can connect timers to external pins (Timer 0 and Timer 1 on the Uno). That isn't an interrupt and attachInterrupt is not relevant. With an external clock to the timer you can apply prescalers in the usual way.

The datasheet that I have (8271G–AVR–02/2013) says this on page 139 right before Figure 17-2: "An external clock source can not be prescaled." Figure 17-2 shows the T0 and T1 clock signals going thru the synchroniser and straight into the MUX, bypassing the prescaler completely.

Oh, OK, you are right. The prescaler is for the internal clock to give you a wider range of counting intervals.

Oh well, use Timer 1. You can count up to 65535, and that gives you plenty of time to get an interrupt in (timer overflow) to allow for the number of counts exceeding that.

I need to divide a clock frequency between 383. The frequency is variable (between 1 and 5 MHz).

An external clock? Use timer 1, count up to 383, when that is reached an interrupt can fire and output a pulse. Or you can probably make the timer do it in hardware.

Hello friends! I solved the problem:

And, this is mi code:

#include <TimerOne.h>
#include <EEPROM.h>

void setup() {
     
     // initialize Timer1
    cli();          // disable global interrupts
    TCCR1A = 0;     // set entire TCCR1A register to 0
    TCCR1B = 0;     // same for TCCR1B    
    // set compare match register to desired timer count:
    OCR1A = 383;    
    // turn on CTC mode:
    TCCR1B |= (1 << WGM12);    
    // Set CS10, CS11 and CS12 bits for external clock source on T1 
    // (D5 in arduino UNO). Clock on rising edge
    TCCR1B |= (1 << CS10);
    TCCR1B |= (1 << CS11);
    TCCR1B |= (1 << CS12);    
    // enable timer compare interrupt:
    TIMSK1 |= (1 << OCIE1A);    
    // enable global interrupts:
    sei();     
}


void loop() {
  //code loop
}


ISR(TIMER1_COMPA_vect){
     //interrupt code here
}

Indeed, there is no prescaler for external interrupt, but you can use CTC.

Thanks everyone for your help. :grin:

    OCR1A = 383;

Those counters are zero-relative, so that will count to 384.

The counter is'nt reset just at 383?

That is: 381,382, 0,1

OK, sure. No. 383 is counted up to not less than.

0, 1, 2, 3, ... 382, 383, 0, 1, 2, 3, ...

So it counts 384 cycles.

See page 122 of the datasheet:

If TCNT equals OCR1x the comparator signals a match.

So it compares equal to 383. Thus 383 is one of the counts, as is zero.

If the OP needs an odd number of counts, I don't understand why he would need a prescaler. I don't know of any prescaler that aren't even number divisors; 383 is a prime number. At any rate, if it's 384 he needs, then Timer0 with an external prescaler (i.e. flip-flop) might make better use of the hardware by freeing up Timer1.

Hello friends!

In arduino UNO T1 is on pin 5. Where is T1 in arduino MEGA?