| 1 | //-----------------------------------------------------------------------------
|
|---|
| 2 |
|
|---|
| 3 | #include "application.h"
|
|---|
| 4 | #include <avr/wdt.h>
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 | //-----------------------------------------------------------------------------
|
|---|
| 8 |
|
|---|
| 9 | volatile U08 app_reset_source;
|
|---|
| 10 | //-----------------------------------------------------------------------------
|
|---|
| 11 |
|
|---|
| 12 | void app_init(void)
|
|---|
| 13 | {
|
|---|
| 14 | app_reset_source = MCUSR; // Save last reset source
|
|---|
| 15 | MCUSR = 0x00; // Clear reset source for next reset cycle
|
|---|
| 16 |
|
|---|
| 17 | // The watchdog timer is disabled by default ("startup.asm")
|
|---|
| 18 | #ifdef USE_WATCHDOG
|
|---|
| 19 | WDTCSR = WDTOE | (1 << WDE); // Enable watchdog reset (~16ms)
|
|---|
| 20 | #endif
|
|---|
| 21 |
|
|---|
| 22 | #ifndef F_CPU
|
|---|
| 23 | #define F_CPU 8000000UL //cpu frequency
|
|---|
| 24 | #endif
|
|---|
| 25 |
|
|---|
| 26 | //#ifdef (F_CPU == 16000000UL)
|
|---|
| 27 | /*#else
|
|---|
| 28 | #warning *** Compiling for _MCU_CLOCK_FREQUENCY_ Hz
|
|---|
| 29 | #error *** Invalid clock selected! ***
|
|---|
| 30 | #endif*/
|
|---|
| 31 | // Tell the user the operating frequency
|
|---|
| 32 | //#warning *** Compiling for DF_CPU Hz
|
|---|
| 33 | /*
|
|---|
| 34 | // Set selected CPU clock
|
|---|
| 35 | #if (_MCU_CLOCK_FREQUENCY_ == 16000000)
|
|---|
| 36 | CLKPR = CLKPCE; // Enable clock prescaler by writing 0x80 to CLKPR
|
|---|
| 37 | CLKPR = 0; // Set clock prescaler to division by 1 (16MHz clock with 16MHz crystal)
|
|---|
| 38 | #elif (_MCU_CLOCK_FREQUENCY_ == 8000000)
|
|---|
| 39 | CLKPR = CLKPCE; // Enable clock prescaler by writing 0x80 to CLKPR
|
|---|
| 40 | CLKPR = 1; // Set clock prescaler to division by 2 (8MHz clock with 16MHz crystal)
|
|---|
| 41 | #elif (_MCU_CLOCK_FREQUENCY_ == 4000000)
|
|---|
| 42 | CLKPR = CLKPCE; // Enable clock prescaler by writing 0x80 to CLKPR
|
|---|
| 43 | CLKPR = 2; // Set clock prescaler to division by 4 (4MHz clock with 16MHz crystal)
|
|---|
| 44 | #elif (_MCU_CLOCK_FREQUENCY_ == 2000000)
|
|---|
| 45 | CLKPR = CLKPCE; // Enable clock prescaler by writing 0x80 to CLKPR
|
|---|
| 46 | CLKPR = 3; // Set clock prescaler to division by 8 (2MHz clock with 16MHz crystal)
|
|---|
| 47 | #elif (_MCU_CLOCK_FREQUENCY_ == 1000000)
|
|---|
| 48 | CLKPR = CLKPCE; // Enable clock prescaler by writing 0x80 to CLKPR
|
|---|
| 49 | CLKPR = 4; // Set clock prescaler to division by 16 (1MHz clock with 16MHz crystal)
|
|---|
| 50 | #else
|
|---|
| 51 | #warning *** Compiling for _MCU_CLOCK_FREQUENCY_ Hz
|
|---|
| 52 | #error *** Invalid clock selected! ***
|
|---|
| 53 | #endif
|
|---|
| 54 |
|
|---|
| 55 | // Tell the user the operating frequency
|
|---|
| 56 | #warning *** Compiling for _MCU_CLOCK_FREQUENCY_ Hz
|
|---|
| 57 |
|
|---|
| 58 | */
|
|---|
| 59 |
|
|---|
| 60 | /* (ATmega32 has no prescaler)
|
|---|
| 61 | // Set selected CPU clock
|
|---|
| 62 | #if (F_CPU == 16000000)
|
|---|
| 63 | CLKPR = CLKPCE; // Enable clock prescaler by writing 0x80 to CLKPR
|
|---|
| 64 | CLKPR = 0; // Set clock prescaler to division by 1 (16MHz clock with 16MHz crystal)
|
|---|
| 65 | #elif (F_CPU == 8000000)
|
|---|
| 66 | CLKPR = CLKPCE; // Enable clock prescaler by writing 0x80 to CLKPR
|
|---|
| 67 | CLKPR = 1; // Set clock prescaler to division by 2 (8MHz clock with 16MHz crystal)
|
|---|
| 68 | #elif (F_CPU == 4000000)
|
|---|
| 69 | CLKPR = CLKPCE; // Enable clock prescaler by writing 0x80 to CLKPR
|
|---|
| 70 | CLKPR = 2; // Set clock prescaler to division by 4 (4MHz clock with 16MHz crystal)
|
|---|
| 71 | #elif (F_CPU == 2000000)
|
|---|
| 72 | CLKPR = CLKPCE; // Enable clock prescaler by writing 0x80 to CLKPR
|
|---|
| 73 | CLKPR = 3; // Set clock prescaler to division by 8 (2MHz clock with 16MHz crystal)
|
|---|
| 74 | #elif (F_CPU == 1000000)
|
|---|
| 75 | CLKPR = CLKPCE; // Enable clock prescaler by writing 0x80 to CLKPR
|
|---|
| 76 | CLKPR = 4; // Set clock prescaler to division by 16 (1MHz clock with 16MHz crystal)
|
|---|
| 77 | #else
|
|---|
| 78 | #warning *** Compiling for F_CPU Hz
|
|---|
| 79 | #error *** Invalid clock selected! ***
|
|---|
| 80 | #endif
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 | // Tell the user the operating frequency
|
|---|
| 85 | #warning *** Compiling for F_CPU Hz
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 | // Turn off unused modules for this application
|
|---|
| 89 | PRR =
|
|---|
| 90 | (
|
|---|
| 91 | #ifndef USE_TWI
|
|---|
| 92 | TWEN // I2C module
|
|---|
| 93 | #warning *** Module "TWI" not enabled!
|
|---|
| 94 | #endif
|
|---|
| 95 |
|
|---|
| 96 | (ATmega32 Can't disable TIMER modules)
|
|---|
| 97 | #ifndef USE_TIMER2
|
|---|
| 98 | | PRTIM2 // Timer2
|
|---|
| 99 | #warning *** Module "TIMER2" not enabled!
|
|---|
| 100 | #endif
|
|---|
| 101 | #ifndef USE_TIMER1
|
|---|
| 102 | | PRTIM1 // Timer1
|
|---|
| 103 | #warning *** Module "TIMER1" not enabled!
|
|---|
| 104 | #endif
|
|---|
| 105 | #ifndef USE_TIMER0
|
|---|
| 106 | | PRTIM0 // Timer0
|
|---|
| 107 | #warning *** Module "TIMER0" not enabled!
|
|---|
| 108 | #endif
|
|---|
| 109 |
|
|---|
| 110 | #ifndef USE_USART0
|
|---|
| 111 | | PRUSART0 // USART0
|
|---|
| 112 | #warning *** Module "USART0" not enabled!
|
|---|
| 113 | #endif
|
|---|
| 114 |
|
|---|
| 115 | #ifndef USE_SPI
|
|---|
| 116 | | SPE // SPI
|
|---|
| 117 | #warning *** Module "SPI" not enabled!
|
|---|
| 118 | #endif
|
|---|
| 119 |
|
|---|
| 120 | #ifndef USE_ADC
|
|---|
| 121 | | ADEN // ADC
|
|---|
| 122 | #warning *** Module "ADC" not enabled!
|
|---|
| 123 | #endif
|
|---|
| 124 | );
|
|---|
| 125 |
|
|---|
| 126 | #ifndef USE_ACO // Analog comparator
|
|---|
| 127 | ACSR = ACME;
|
|---|
| 128 | #warning *** Module "ACO" not enabled!
|
|---|
| 129 | #endif
|
|---|
| 130 |
|
|---|
| 131 | PORTC |= PC6; // Enable pullup for PC6_RESET
|
|---|
| 132 |
|
|---|
| 133 | // Initialize switches S1, S2 and S3
|
|---|
| 134 | //S1_INIT(); // Set S1 pin to input
|
|---|
| 135 | //S2_INIT(); // Set S2 pin to input
|
|---|
| 136 | //S3_INIT(); // Set S3 pin to input
|
|---|
| 137 | */
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | //-----------------------------------------------------------------------------
|
|---|
| 143 |
|
|---|
| 144 | void app_set_watchdog_prescaler(tWDT_PRESCALE wdt_prescale) // Set watchdog prescale
|
|---|
| 145 | {
|
|---|
| 146 | U08 sreg_backup = SREG; // Copy status register to variable
|
|---|
| 147 | U08 wdtcsr_value = WDE + wdt_prescale; // Set new prescale value to variable
|
|---|
| 148 |
|
|---|
| 149 | cli(); // Disable interrups
|
|---|
| 150 | wdt_reset(); // Reset watchdog
|
|---|
| 151 |
|
|---|
| 152 | WDTCR |= (1 << WDTOE) | (1 << WDE); // Unlock register access, 4 cycles to store new value
|
|---|
| 153 | WDTCR = wdtcsr_value; // Set new watchdog prescaler
|
|---|
| 154 | SREG = sreg_backup; // Restore status register
|
|---|
| 155 | }
|
|---|