I was reading a tutorial on timers. My aim is to generate 16MHz pulse output from the pin 8.
Here is the code I modified from the tutorial (I use Arduino's ATmega328P):
#include <avr/io.h>
#include <stdint.h>
int main (void)
{
DDRB |= (1 << 0); // Set LED as output
TCCR1B &= ~(1 << CS11); // Set up timer
TCCR1B &= ~(1 << CS12); // Set up timer
TCCR1B |= (1 << CS10); // Set up timer
for (;;)
{
// Check timer value in if statement
if (TCNT1 >= 0)
{
PORTB ^= (1 << 0); // Toggle the LED
TCNT1 = 0; // Reset timer value
}
}
}
I use the following formula:
Target Timer Count = (1 / Target Frequency) / (1 / Timer Clock Frequency) - 1
So Target Timer Count = 0 in my case.
But I get 615kHz on scope.
My timer setting should output 16MHz since I use no prescaler:
What am I doing wrong?
while(true){PORTB ^= (1 << 0);}
. – Gerben Feb 24 '17 at 16:30