I need a 13 microseconds delay for transmitting IR at a 38 kHz carrier wave. My chip (ATtiny84) is running at 8 MHz. I cannot figure out what's the problem is in my code:
void send(long microsecs)
{
TCCR0B |= (1 << CS00); // Enable Timer 0 - No prescale
while (microsecs > 0)
{
// 38 kHz is about 13 microseconds high and 13 microseconds low
PORTB |= (1 << PB2); // 3 μs
TCNT0 = 0;
while (TCNT0 < 104);
PORTB &= ~(1 << PB2); // 3 μs
TCNT0 = 0;
while (TCNT0 < 104);
// So 26 microseconds altogether
microsecs -= 26;
}
}
The delay (104 clock cycles) sums up to 13 microseconds @8 MHz and the while loop should hang up the code for the required time period, but it's not working.
But
When I replace this:
TCNT0 = 0;
while (TCNT0 < 104);
by this:
_delay_us(13);
everything works fine.
If I'm doing something wrong, please point out.
1 Answer 1
You are not updating PB2, you set it high in the 1st delay and then you set it high again in the second delay, switch from PORTB |= (1 << PB2); to PORTB &= ~(1 << PB2); in one of the delays
-
\$\begingroup\$ Sorry, I edited the code. Just a problem while copying from the editor to here. I was using PORTB &= ~(1 << PB2); \$\endgroup\$user3414278– user34142782015年09月22日 16:50:22 +00:00Commented Sep 22, 2015 at 16:50
while(TCNT0 < 104);
? Also, how do you decide/check if it is working properly or not? And how much frequency error is permitted? \$\endgroup\$