Add timer interrupt + PWM work
This commit is contained in:
39
Timer-Interrupt/main.c
Normal file
39
Timer-Interrupt/main.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <stdint.h>
|
||||
|
||||
volatile uint32_t ms = 0;
|
||||
|
||||
void timer1_init(void) {
|
||||
TCCR1A = 0;
|
||||
TCCR1B = 0;
|
||||
TCNT1 = 0;
|
||||
|
||||
TCCR1B |= (1 << WGM12);
|
||||
OCR1A = 249;
|
||||
TIMSK1 |= (1 << OCIE1A);
|
||||
TCCR1B |= (1 << CS11) | (1 << CS10);
|
||||
sei();
|
||||
}
|
||||
|
||||
ISR(TIMER1_COMPA_vect) {
|
||||
ms++;
|
||||
}
|
||||
|
||||
void delay_ms(uint32_t delay){
|
||||
uint32_t start = ms;
|
||||
while ((ms - start) < delay) {
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
DDRC |= (1<<PC0);
|
||||
timer1_init();
|
||||
|
||||
while(1) {
|
||||
PORTC |= (1<<PC0);
|
||||
delay_ms(500);
|
||||
PORTC &= ~(1<<PC0);
|
||||
delay_ms(500);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user