initial avr projects (blink + interrupts)
This commit is contained in:
24
interrupts/Makefile
Normal file
24
interrupts/Makefile
Normal file
@@ -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 = COM10
|
||||
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
|
||||
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
|
||||
35
interrupts/main.c
Normal file
35
interrupts/main.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "delay.h"
|
||||
#include <avr/interrupt.h>
|
||||
volatile uint8_t flag=0;
|
||||
|
||||
int main(void){
|
||||
timer1_init();
|
||||
DDRC |= (1<<PC0); // PB1 (D9)
|
||||
// prescaler 8
|
||||
DDRD &= ~(1 << PD2);
|
||||
PORTD |= (1 << PD2);
|
||||
EICRA &= ~(1<<ISC00);
|
||||
EICRA |= (1<<ISC01);
|
||||
EIMSK |= (1<<INT0);
|
||||
|
||||
sei();
|
||||
while(1){
|
||||
if(flag){
|
||||
|
||||
flag=0;
|
||||
PORTC |=(1<<PC0);
|
||||
delay_ms(500);
|
||||
PORTC &=~(1<<PC0);
|
||||
delay_ms(500);
|
||||
}
|
||||
else{
|
||||
PORTC |=(1<<PC0);
|
||||
delay_ms(50);
|
||||
PORTC &=~(1<<PC0);
|
||||
delay_ms(50);
|
||||
} // 75%
|
||||
}
|
||||
}
|
||||
ISR(INT0_vect){
|
||||
flag=1;
|
||||
}
|
||||
Reference in New Issue
Block a user