|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Christopher Kormanyos |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | + |
| 12 | +#include <chrono> |
| 13 | + |
| 14 | +#include <avr/interrupt.h> |
| 15 | +#include <avr/io.h> |
| 16 | +#include <avr/wdt.h> |
| 17 | + |
| 18 | +static void app_hw_init(); |
| 19 | + |
| 20 | +int main() |
| 21 | +{ |
| 22 | + // Initialize the application hardware. This includes WDT, PORTB.5 and TIMER0. |
| 23 | + app_hw_init(); |
| 24 | + |
| 25 | + for(;;) |
| 26 | + { |
| 27 | + // Toggle the LED on portb.5. |
| 28 | + PINB = (1U << PORTB5); |
| 29 | + |
| 30 | + |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +static void app_hw_init() |
| 35 | +{ |
| 36 | + // Initialize the application including WDT, PORTB.5 and TIMER0 |
| 37 | + |
| 38 | + // We will now disable the watchdog. |
| 39 | + // Service the watchdog just to be sure to avoid pending timeout. |
| 40 | + wdt_reset(); |
| 41 | + |
| 42 | + // Clear WDRF in MCUSR. |
| 43 | + MCUSR &= ~(1U << WDRF); |
| 44 | + |
| 45 | + // Write logical one to WDCE and WDE. |
| 46 | + // Keep the old prescaler setting to prevent unintentional time-out. |
| 47 | + WDTCSR |= (1U << WDCE) | (1U << WDE); |
| 48 | + |
| 49 | + // Turn off the WDT. |
| 50 | + WDTCSR = 0x00; |
| 51 | + |
| 52 | + // We will now initialize PORTB.5 to be used as an LED driver port. |
| 53 | + // Set PORTB.5 value to low. |
| 54 | + PORTB &= ~(1U << PORTB5); |
| 55 | + |
| 56 | + // Set PORTB.5 direction to output. |
| 57 | + DDRB |= (1U << DDB5); |
| 58 | + |
| 59 | + // We will now initialize the TIMER0 clock and interrupt. |
| 60 | + // Clear the TIMER0 overflow flag. |
| 61 | + TIFR0 = static_cast<std::uint8_t>(1U << TOV0); |
| 62 | + |
| 63 | + // Enable the TIMER0 overflow interrupt. |
| 64 | + TIMSK0 = static_cast<std::uint8_t>(1U << TOIE0); |
| 65 | + |
| 66 | + // Set the TIMER0 clock source to f_osc/8 = 2MHz and begin counting. |
| 67 | + TCCR0B = static_cast<std::uint8_t>(1U << CS01); |
| 68 | + |
| 69 | + // Enable all interrupts. |
| 70 | + sei(); |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +ISR(TIMER0_OVF_vect) |
| 76 | +{ |
| 77 | + |
| 78 | +} |
0 commit comments