quick reset of a counter

const int pulseOutPin = 0b0111;
const int clkOutPin =  0b1001;
int count = 0b0000;
const uint8_t bin[0b00010001] = {0b0001, 0b0000, 0b0001, 0b0000, 0b0001, 0b0000, 0b0001, 0b0000, 0b0001, 0b0000, 0b0001, 0b0000, 0b0000, 0b0000, 0b0000, 0b0000, 0b0000}; // dauer eines Status 4µs
void initialize(long microseconds = 1000000);



ISR(TIMER1_COMPA_vect)
{
  PORTD = bin[count] << 0b0111;
  count++;

  
 if (count >= 0b00010001)
  {
    count = 0b0000;
  }
}





void setup()
{
  noInterrupts();//stop interrupts
  TCCR1A = _BV(COM1A0) ;            //toggle OC1A on compare match
  OCR1A = 7;   //top value for counter, 7 = 1MHz
  TCCR1B = _BV(WGM12) | _BV(CS10); //CTC mode, prescaler clock/1
  TIMSK1 = bit(OCIE1A); //PCINT23; //bit(OCIE1A);            //interrupt on Compare A Match
  //pinMode (pulseOutPin, OUTPUT);
  DDRD = 0b10000000;
  //pinMode (clkOutPin, OUTPUT);
  DDRB = 0b00000010;
  interrupts();//allow interrupts
}

void loop()
{

}

is there a more elegant or faster way to reset a counter to zero?
Ziel ist es eine Periode von <= 3µs zu erreichen. Aktuell sind es 8µs.

have look here:
ISR(TIMER1_COMPA_vect)
{
PORTD = bin[count] << 0b0111;
count++;

if (count >= 0b00010001)
{
count = 0b0000;
}
}

any help or advice is welcome!

count %= 0b00010001

Although the simple if will be faster. So basically that's the best way.

Couple of other things:

  • Make counter a byte and get the advantage reading it is always atomic
  • Declare counter volatile so the compiler will nog mess it up
  • What makes 0b00010001 so special that it's more clear to write it in binary compared to just 17? Aka, you seem a bit over obsessed with binairy notation.
  • You do know you write to the complete PORTD?
  • And you do know left shifting by 0b111 aka 7 will result in only changing PD7 (aka pin 7 o a Uno) and set the rest to 0?
  • Why not do a simple digitalWrite()?