initial avr projects (blink + interrupts)
This commit is contained in:
35
interrupts/delay.h
Normal file
35
interrupts/delay.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef DELAY_H
|
||||
#define DELAY_H
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Call this ONCE in main() before using delay_ms()
|
||||
static inline void timer1_init(void) {
|
||||
// Configure Timer1 in CTC mode
|
||||
TCCR1B |= (1 << WGM12);
|
||||
|
||||
// Set compare match for 1ms delay (16MHz / 64 prescaler)
|
||||
OCR1A = 250;
|
||||
|
||||
// Start Timer1 with prescaler 64
|
||||
TCCR1B |= (1 << CS11) | (1 << CS10);
|
||||
|
||||
// Reset timer count
|
||||
TCNT1 = 0;
|
||||
}
|
||||
|
||||
// Delay function assumes timer already initialized
|
||||
static inline void delay_ms(uint16_t ms) {
|
||||
for (uint16_t i = 0; i < ms; i++) {
|
||||
// Clear compare flag
|
||||
TIFR1 |= (1 << OCF1A);
|
||||
|
||||
// Wait for compare match flag
|
||||
while (!(TIFR1 & (1 << OCF1A))) {
|
||||
// Busy wait
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // DELAY_H
|
||||
Reference in New Issue
Block a user