AVR-Template/main.c

109 lines
2.9 KiB
C
Raw Normal View History

2024-03-23 19:36:08 +01:00
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>
2024-04-01 20:38:35 +02:00
#include <avr/wdt.h>
2024-04-02 14:25:11 +02:00
#include <util/delay.h>
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
#define LED_PIN PB5 // Define the pin connected to the LED
2024-03-23 19:36:08 +01:00
2024-04-02 14:25:11 +02:00
// Flipping the led every 0.5s using the overflow interrupt
ISR(TIMER1_OVF_vect) {
PORTB ^= _BV(LED_PIN); // Toggle the LED
TCNT1 = (uint16_t)(65535 - (F_CPU / 1024) * 0.5); // Reset the counter
TIFR1 |= (1 << TOV1); // Clear the interrupt flag
}
// 'Pulse' the LED
2024-04-01 20:38:35 +02:00
ISR(TIMER1_COMPA_vect) {
// Static variable to keep track of the number of interrupts
static uint16_t counter = 0;
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
PORTB ^= _BV(LED_PIN); // Toggle the LED
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
// Modulo magic, see below for explanation
if (++counter % 2 == 0) {
OCR1A = 15624 / 2;
2024-04-02 14:25:11 +02:00
counter = 0;
2024-04-01 20:38:35 +02:00
} else {
OCR1A = 15624 / 16;
}
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
// Clear the interrupt flag
TIFR1 |= (1 << TOV1);
}
2024-03-23 19:36:08 +01:00
2024-04-02 14:25:11 +02:00
// External interrupt
ISR(INT0_vect) {
PORTB ^= _BV(LED_PIN); // Toggle the LED
_delay_ms(200); // Debounce
}
2024-04-01 20:38:35 +02:00
// We will use Timer1 for the interrupt
void configure_interrupt() {
2024-04-02 14:25:11 +02:00
TCCR1A = 0; // Set the timer to normal mode
TCCR1B = 0; // Set the timer to normal mode
2024-04-01 20:38:35 +02:00
TCCR1B |= (1 << WGM12); // Set the timer to CTC mode
TCCR1B |= (1 << CS02) | (1 << CS00); // Set the prescaler to 1024
TIMSK1 |= (1 << OCIE1A); // Enable overflow interrupt
2024-04-02 14:25:11 +02:00
sei(); // Enable global interrupts (cli() to disable)
OCR1A = 0; // 31250 for a 2s delay, 15624 for a 1s delay
TCNT1 = (uint16_t)(65535 - (F_CPU / 1024) * 0.5); // Reset the counter
}
void configure_external_interrupt() {
// Set the external interrupt to trigger on the falling edge
EICRA |= (1 << ISC01);
EICRA &= ~(1 << ISC00);
// Enable the external interrupt
EIMSK |= (1 << INT0); // Pin D2 is INT0
DDRD &= ~_BV(PD2); // Set the pin as an input
PORTD |= _BV(PD2); // Enable the pull-up resistor
// Enable global interrupts
sei();
}
void configure_ovf_interrupt() {
// Set the timer to normal mode
TCCR1A = 0;
TCCR1B = 0;
// Initialize the counter to 0xFFFF
TCNT1 = 0xFFFF;
// Set the prescaler to 1024
TCCR1B |= _BV(CS12) | _BV(CS10);
// Enable overflow interrupt
TIMSK1 |= (1 << TOIE1);
// Enable global interrupts
sei();
2024-04-01 20:38:35 +02:00
}
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
int main(void) {
// Configure the LED pin as an output
DDRB |= _BV(LED_PIN);
2024-04-02 14:25:11 +02:00
// configure_interrupt();
2024-04-02 23:30:57 +02:00
configure_ovf_interrupt();
// configure_external_interrupt();
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
// Set the watchdog timer to 8 seconds
2024-04-02 14:25:11 +02:00
// wdt_enable(WDTO_8S);
// wdt_disable();
// wdt_reset(); // Reset the watchdog timer
2024-03-23 19:36:08 +01:00
2024-04-01 20:38:35 +02:00
// Set the sleep mode to idle
set_sleep_mode(SLEEP_MODE_IDLE);
// Pat the dog and sleep until the next interrupt
2024-03-23 19:36:08 +01:00
while (1) {
2024-04-02 14:25:11 +02:00
sleep_mode(); // Doesent sleep through interrupts
2024-03-23 19:36:08 +01:00
}
2024-04-01 20:38:35 +02:00
return 0;
2024-03-23 19:36:08 +01:00
}