commit 2c9714c8514a86146c5dc08e9910bf9d99f00912 Author: Pratik Date: Fri Apr 3 09:42:37 2026 +1100 initial avr projects (blink + interrupts) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea1664e --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +# ignore toolchain +avr8-gnu-toolchain-win32_x86_64/ +avrdude.conf +# ignore build / binary files +*.exe +*.hex +*.elf +*.o +*.map +*.log +*.bin + +# optional (AVR builds) +*.lss +*.eep \ No newline at end of file diff --git a/LED Blink Input Output/Makefile b/LED Blink Input Output/Makefile new file mode 100644 index 0000000..cf4d044 --- /dev/null +++ b/LED Blink Input Output/Makefile @@ -0,0 +1,24 @@ +MCU = atmega328p +F_CPU = 16000000UL +CC = C:\Users\sharm\Desktop\our main folder\avrprojects\avr8-gnu-toolchain-win32_x86_64\bin\avr-gcc +OBJCOPY = C:\Users\sharm\Desktop\our main folder\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 delay.h + $(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 \ No newline at end of file diff --git a/LED Blink Input Output/delay.h b/LED Blink Input Output/delay.h new file mode 100644 index 0000000..db4e991 --- /dev/null +++ b/LED Blink Input Output/delay.h @@ -0,0 +1,35 @@ +#ifndef DELAY_H +#define DELAY_H + +#include +#include + +// 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 = 249; + + // 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 \ No newline at end of file diff --git a/LED Blink Input Output/main.c b/LED Blink Input Output/main.c new file mode 100644 index 0000000..56c22e2 --- /dev/null +++ b/LED Blink Input Output/main.c @@ -0,0 +1,28 @@ +#include "delay.h" + +int main(void) { + // PC2 as output (LED), PC3 as input (button) + timer1_init(); + + DDRB |= (1< pin reads LOW because of pull-up + if (PINB & (1< +#include + +// 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 \ No newline at end of file diff --git a/interrupts/main.c b/interrupts/main.c new file mode 100644 index 0000000..ba31c01 --- /dev/null +++ b/interrupts/main.c @@ -0,0 +1,35 @@ +#include "delay.h" +#include +volatile uint8_t flag=0; + +int main(void){ +timer1_init(); + DDRC |= (1<