Add timer interrupt + PWM work
This commit is contained in:
24
PWM-generator/Makefile
Normal file
24
PWM-generator/Makefile
Normal 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
|
||||
33
PWM-generator/main.c
Normal file
33
PWM-generator/main.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <avr/io.h>
|
||||
|
||||
int main(void) {
|
||||
// 1. Set PB1 (OC1A) as output
|
||||
DDRB |= (1 << PB1);
|
||||
|
||||
// 2. Fast PWM mode (WGM13:0 = 14)
|
||||
TCCR1A = (1 << COM1A1) | (1 << WGM11);
|
||||
TCCR1B = (1 << WGM12) | (1 << WGM13);
|
||||
|
||||
// 3. Prescaler = 8 (start timer)
|
||||
TCCR1B |= (1 << CS11);
|
||||
|
||||
// 4. Set TOP value (frequency)
|
||||
ICR1 = 19999; // ~50 Hz (good for visible LED dimming demo)
|
||||
|
||||
// 5. Initial duty cycle
|
||||
OCR1A = 0;
|
||||
|
||||
while (1) {
|
||||
// Increase brightness
|
||||
for (uint16_t i = 0; i < 2000; i++) {
|
||||
OCR1A = i * 10; // increase duty
|
||||
for (volatile uint32_t d = 0; d < 5000; d++); // small delay
|
||||
}
|
||||
|
||||
// Decrease brightness
|
||||
for (int16_t i = 2000; i > 0; i--) {
|
||||
OCR1A = i * 10;
|
||||
for (volatile uint32_t d = 0; d < 5000; d++);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user