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