Hello,
I need to generate short pulses (around 10us) with a reasonable precision, let say about 20%.
I follow many tutorials but I still do not understand why the following code does not work completely:
byte pin=0;
void loop() {
digitalWrite(pin,HIGH);
delay(100);
digitalWrite(pin,LOW);
timer(200);
delay(3000);
}
void timer(byte b) {
byte portb1mask=1<<PORTB1;
TCCR1=B00000000;
PORTB|=portb1mask;
TCNT1=0;
TCCR1=B00001111;
while(TCNT1<=b);
PORTB&=~portb1mask;
TCCR1=B00000000;
}
void setup() {
pinMode(pin,OUTPUT);
pinMode(PB1,OUTPUT);
}
For each loop() iteration, I expect pin 0 pulse shortly, and normally pin 1 should pulse immediately after using timer 1 comparison.
But it works only for half of the loop iteration:
Iteration n: pin 0 pulse, then pin 1 pulse
Iteration n+1: only pin 0
Iteration n+2: pin 0 then pin 1
Iteration n+3: only pin 0
Does anybody have a suggestion about this ?
Best regards,
Grégoire.