You are on page 1of 1

void setup()

{
PORTB = 0;
DDRB = 0b00000010;
setFrequency(1000000);
}

// set PB1 (= OCR1A) to be an output

void loop()
{
while(1)
{
on();
delay(1);
off();
delay(1);
}
}
// Set the frequency that we will get on pin OCR1A but don't turn it on
void setFrequency(uint16_t freq)
{
uint32_t requiredDivisor = (F_CPU/2)/(uint32_t)freq;
uint16_t prescalerVal = 1;
uint8_t prescalerBits = 1;
while ((requiredDivisor + prescalerVal/2)/prescalerVal > 256)
{
++prescalerBits;
prescalerVal <<= 1;
}
uint8_t
TCCR1 =
GTCCR =
OCR1C =

top = ((requiredDivisor + (prescalerVal/2))/prescalerVal) - 1;


(1 << CTC1) | prescalerBits;
0;
top;

}
// Turn the frequency on
void on()
{
TCNT1 = 0;
TCCR1 |= (1 << COM1A0);
}
// Turn the frequency off and turn off the IR LED.
// We let the counter continue running, we just turn off the OCR1A pin.
void off()
{
TCCR1 &= ~(1 << COM1A0);
}

You might also like