Skip to main content
Arduino

Return to Answer

replaced http://arduino.stackexchange.com/ with https://arduino.stackexchange.com/
Source Link

Building upon Nick Gammon's excellent answer at How many interrupt pins can an Uno handle? How many interrupt pins can an Uno handle? here's some untested code:

Building upon Nick Gammon's excellent answer at How many interrupt pins can an Uno handle? here's some untested code:

Building upon Nick Gammon's excellent answer at How many interrupt pins can an Uno handle? here's some untested code:

fix code to compile
Source Link
Dave X
  • 2.4k
  • 15
  • 29
...
volatile unsigned long int t1Cycles,times[events];
...
ISR(TIMER1_OVF_vect){t1Cycles++;} 
void setup(){
 ...
 TMSK1TIMSK1 = bit(TOIE1);
 t1cyclest1Cycles = 0;
 ...
} 
ISR (PCINT1_vect)
{
 ...
 times[i]times[index] = t1Cycles << 16 | TCNT1; // store 32 bit count of clocks
 ...
}
...
volatile unsigned long int t1Cycles,times[events];
...
ISR(TIMER1_OVF_vect){t1Cycles++;} 
void setup(){
 ...
 TMSK1 = bit(TOIE1);
 t1cycles = 0;
 ...
} 
ISR (PCINT1_vect)
{
 ...
 times[i] = t1Cycles << 16 | TCNT1; // store 32 bit count of clocks
 ...
}
...
volatile unsigned long int t1Cycles,times[events];
...
ISR(TIMER1_OVF_vect){t1Cycles++;} 
void setup(){
 ...
 TIMSK1 = bit(TOIE1);
 t1Cycles = 0;
 ...
} 
ISR (PCINT1_vect)
{
 ...
 times[index] = t1Cycles << 16 | TCNT1; // store 32 bit count of clocks
 ...
}
fix code to compile
Source Link
Dave X
  • 2.4k
  • 15
  • 29
 const int events = 256; // events to store
 unsigned volatile int index; 
 byte pinStates[events];
 unsigned int times[events];
 ISR (PCINT1_vect) // Watch for pin changes on the C port (Uno pins A0-A5)
 {
 if (index == 0) { // start clock
 TCCR1B = bit(CS12) | bit (CS10) ; // clock/1024 for <4.19s x 64us
 //TCCR1B = bit(CS12); // clock/256 for <1.05s x 16us 
 //TCCR1B = bit(CS11) | bit (CS10) ; // clock/64 for <0.25s x 4us
 //TCCR1B = bit(CS11) ; // clock/8 for <0.03s x 0.5us
 //TCCR1B = bit(CS10) ; // clock/1 for <0.004s x 62ns
 }
 times[i]times[index] = TCNT1;
 pinstates[i]pinStates[index] = PORTC;
 iindex = (i+1index+1) & 0xff; // increment and protect from overflow
 } // end of PCINT1_vect
void setup ()
 { 
 // activate pin change interrupts on port C / pins A0-A5
 
 PCICR = bit (PCIE1); // enable PCINT[14:8] pin triggers 
 PCMSK1 |= bit (PCINT8) | 
 bit (PCINT9) | 
 bit (PCINT10) | 
 bit (PCINT11) | 
 bit (PCINT12) | 
 bit (PCINT13); // enable individual pins
 // stop and clear 16 bit timer CS1[0-2]=0; TCNT1 = 0;
 TCCR1A = 0; // no comparisons, normal WGM mode, stop the counting 
 TCCR1B = 0; 
 TCNT1 = 0; // Clear the counter
 pinMode (A0, INPUT_PULLUP);
 pinMode (A1, INPUT_PULLUP);
 pinMode (A2, INPUT_PULLUP);
 pinMode (A3, INPUT_PULLUP);
 pinMode (A4, INPUT_PULLUP);
 pinMode (A5, INPUT_PULLUP);
 index = 0 ; // nothing happened yet
 pinMode (5, OUTPUT);
 } // end of setup
void loop ()
 {
 // check index, parse and report times, check for arming, etc...
 }
 const int events = 256; // events to store
 unsigned volatile int index; 
 byte pinStates[events];
 unsigned int times[events];
 ISR (PCINT1_vect) // Watch for pin changes on the C port (Uno pins A0-A5)
 {
 if (index == 0) { // start clock
 TCCR1B = bit(CS12) | bit (CS10) ; // clock/1024 for <4.19s x 64us
 //TCCR1B = bit(CS12); // clock/256 for <1.05s x 16us 
 //TCCR1B = bit(CS11) | bit (CS10) ; // clock/64 for <0.25s x 4us
 //TCCR1B = bit(CS11) ; // clock/8 for <0.03s x 0.5us
 //TCCR1B = bit(CS10) ; // clock/1 for <0.004s x 62ns
 }
 times[i] = TCNT1;
 pinstates[i] = PORTC;
 i = (i+1) & 0xff; // increment and protect from overflow
 } // end of PCINT1_vect
void setup ()
 { 
 // activate pin change interrupts on port C / pins A0-A5
 
 PCICR = bit (PCIE1); // enable PCINT[14:8] pin triggers 
 PCMSK1 |= bit (PCINT8) | 
 bit (PCINT9) | 
 bit (PCINT10) | 
 bit (PCINT11) | 
 bit (PCINT12) | 
 bit (PCINT13); // enable individual pins
 // stop and clear 16 bit timer CS1[0-2]=0; TCNT1 = 0;
 TCCR1A = 0; // no comparisons, normal WGM mode, stop the counting 
 TCCR1B = 0; 
 TCNT1 = 0; // Clear the counter
 pinMode (A0, INPUT_PULLUP);
 pinMode (A1, INPUT_PULLUP);
 pinMode (A2, INPUT_PULLUP);
 pinMode (A3, INPUT_PULLUP);
 pinMode (A4, INPUT_PULLUP);
 pinMode (A5, INPUT_PULLUP);
 index = 0 ; nothing happened yet
 pinMode (5, OUTPUT);
 } // end of setup
void loop ()
 {
 // check index, parse and report times, check for arming, etc...
 }
 const int events = 256; // events to store
 unsigned volatile int index; 
 byte pinStates[events];
 unsigned int times[events];
 ISR (PCINT1_vect) // Watch for pin changes on the C port (Uno pins A0-A5)
 {
 if (index == 0) { // start clock
 TCCR1B = bit(CS12) | bit (CS10) ; // clock/1024 for <4.19s x 64us
 //TCCR1B = bit(CS12); // clock/256 for <1.05s x 16us 
 //TCCR1B = bit(CS11) | bit (CS10) ; // clock/64 for <0.25s x 4us
 //TCCR1B = bit(CS11) ; // clock/8 for <0.03s x 0.5us
 //TCCR1B = bit(CS10) ; // clock/1 for <0.004s x 62ns
 }
 times[index] = TCNT1;
 pinStates[index] = PORTC;
 index = (index+1) & 0xff; // increment and protect from overflow
 } // end of PCINT1_vect
void setup ()
 { 
 // activate pin change interrupts on port C / pins A0-A5
 
 PCICR = bit (PCIE1); // enable PCINT[14:8] pin triggers 
 PCMSK1 |= bit (PCINT8) | 
 bit (PCINT9) | 
 bit (PCINT10) | 
 bit (PCINT11) | 
 bit (PCINT12) | 
 bit (PCINT13); // enable individual pins
 // stop and clear 16 bit timer CS1[0-2]=0; TCNT1 = 0;
 TCCR1A = 0; // no comparisons, normal WGM mode, stop the counting 
 TCCR1B = 0; 
 TCNT1 = 0; // Clear the counter
 pinMode (A0, INPUT_PULLUP);
 pinMode (A1, INPUT_PULLUP);
 pinMode (A2, INPUT_PULLUP);
 pinMode (A3, INPUT_PULLUP);
 pinMode (A4, INPUT_PULLUP);
 pinMode (A5, INPUT_PULLUP);
 index = 0 ; // nothing happened yet
 pinMode (5, OUTPUT);
 } // end of setup
void loop ()
 {
 // check index, parse and report times, check for arming, etc...
 }
+volatile and 16 bit shift
Source Link
Dave X
  • 2.4k
  • 15
  • 29
Loading
Add suggestion on longer cycles by cascading into a varaible.
Source Link
Dave X
  • 2.4k
  • 15
  • 29
Loading
Add comments for faster clocking.
Source Link
Dave X
  • 2.4k
  • 15
  • 29
Loading
Source Link
Dave X
  • 2.4k
  • 15
  • 29
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /