Delay loop using 16 bit timer/counter

This commit is contained in:
mandlm 2017-03-02 08:21:17 +01:00
parent 99ccb12a0f
commit 224379a37e
2 changed files with 9 additions and 25 deletions

View File

@ -2,5 +2,6 @@
#define F_CPU 1000000 #define F_CPU 1000000
void delay_ms(double delay); #include <stdint.h>
void delay_us(double delay);
void delay_us(const uint16_t delay);

View File

@ -1,34 +1,17 @@
#include "hardware.h" #include "hardware.h"
#include "avr/io.h" #include "avr/io.h"
void delay_ms(double delay) void delay_us(uint16_t delay)
{ {
// prescaler: 1024 TCCR1B = (1 << CS10);
TCCR0B = (1 << CS02) | (1 << CS00); TCNT1 = 0;
TCNT0 = 0;
while (TCNT0 <= delay) while (true)
{ {
if (TCNT0 >= 49) if (TCNT1 >= delay)
{ {
delay -= 50; return;
TCNT0 = 0;
} }
} }
} }
void delay_us(double delay)
{
// prescaler: 1
TCCR0B = (1 << CS00);
TCNT0 = 0;
while (TCNT0 <= delay)
{
if (TCNT0 >= 50)
{
delay -= 50;
TCNT0 = 0;
}
}
}