Add timer interrupt + PWM work

This commit is contained in:
Pratik
2026-04-06 21:25:29 +10:00
parent 4012404504
commit 36d8dbb881
10 changed files with 143 additions and 8 deletions

24
Timer-Interrupt/Makefile Normal file
View File

@@ -0,0 +1,24 @@
MCU = atmega328p
F_CPU = 16000000UL
CC = C:\Users\sharm\Desktop\Embedded Programming\avrprojects\avr8-gnu-toolchain-win32_x86_64\bin\avr-gcc
OBJCOPY = C:\Users\sharm\Desktop\Embedded Programming\avrprojects\avr8-gnu-toolchain-win32_x86_64\bin\avr-objcopy
AVRDUDE = ../avrdude
AVRDUDE_PROGRAMMER = arduino
AVRDUDE_PORT = COM8
AVRDUDE_BAUD = 115200
CFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU) -Os -Wall
all: main.hex
main.elf: main.c
$(CC) $(CFLAGS) main.c -o main.elf
main.hex: main.elf
$(OBJCOPY) -O ihex -R .eeprom main.elf main.hex
upload: main.hex
$(AVRDUDE) -c $(AVRDUDE_PROGRAMMER) -p $(MCU) -P $(AVRDUDE_PORT) -b $(AVRDUDE_BAUD) -D -U flash:w:main.hex:i
clean:
rm -f main.elf main.hex

39
Timer-Interrupt/main.c Normal file
View 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);
}
}