3 * @author Sam Moore - Adapted from ATMEL AVR Butterfly module code
\r
4 * @purpose The main loop, etc
\r
8 #include <avr/interrupt.h>
\r
9 #include <avr/pgmspace.h>
\r
10 #include <avr/sleep.h>
\r
11 #include <avr/wdt.h>
\r
12 #include <avr/version.h>
\r
15 #if __AVR_LIBC_VERSION__ < 10405UL
\r
16 #warning "avr-libc >= version 1.4.5 recommended"
\r
17 #warning "This code has not been tested with older versions."
\r
22 #include "LCD_functions.h"
\r
23 #include "LCD_Driver.h"
\r
33 #define pLCDREG_test (*(char *)(0xEC))
\r
37 /*****************************************************************************
\r
39 * Function name : main
\r
45 * Purpose : Contains the main loop of the program
\r
47 *****************************************************************************/
\r
48 // mt __C_task void main(void)
\r
49 __attribute__ ((OS_main)) int main(void)
\r
54 // Program initalization
\r
56 sei(); // mt __enable_interrupt();
\r
61 USART_printf("\r\n# hello\r\n");
\r
75 /*****************************************************************************
\r
77 * Function name : Initialization
\r
83 * Purpose : Initializate the different modules
\r
85 *****************************************************************************/
\r
86 void Initialization(void)
\r
88 OSCCAL_calibration(); // calibrate the OSCCAL byte
\r
90 CLKPR = (1<<CLKPCE); // set Clock Prescaler Change Enable
\r
92 // set prescaler = 8, Inter RC 8Mhz / 8 = 1Mhz
\r
93 CLKPR = (1<<CLKPS1) | (1<<CLKPS0);
\r
95 Timer0_Init(); // Used when playing music etc.
\r
97 USART_Init(UART_4800); // Baud rate = 4800bps
\r
99 LCD_Init(); // initialize the LCD
\r
102 //Disable JTAG to use ADC4-7
\r
103 MCUCR |= ( 1 <<JTD );
\r
104 MCUCR |= ( 1 <<JTD );
\r
110 setup.adc_averages = 1;
\r
111 setup.poll_delay = 1;
\r
112 setup.poll_timeout = (uint32)(-1);
\r
113 setup.elapsed_time = 0;
\r
114 setup.dac_value = 0;
\r
117 /*****************************************************************************
\r
119 * Function name : Delay
\r
123 * Parameters : unsigned int millisec
\r
125 * Purpose : Delay-loop
\r
127 *****************************************************************************/
\r
128 void Delay(unsigned int millisec)
\r
130 // mt, int i did not work in the simulator: int i;
\r
133 while (millisec--) {
\r
134 for (i=0; i<125; i++) {
\r
135 asm volatile ("nop"::);
\r
144 /*****************************************************************************
\r
146 * Function name : OSCCAL_calibration
\r
150 * Parameters : None
\r
152 * Purpose : Calibrate the internal OSCCAL byte, using the external
\r
153 * 32,768 kHz crystal as reference
\r
155 *****************************************************************************/
\r
156 void OSCCAL_calibration(void)
\r
158 unsigned char calibrate = FALSE;
\r
160 unsigned char tempL;
\r
162 CLKPR = (1<<CLKPCE); // set Clock Prescaler Change Enable
\r
163 // set prescaler = 8, Inter RC 8Mhz / 8 = 1Mhz
\r
164 CLKPR = (1<<CLKPS1) | (1<<CLKPS0);
\r
166 TIMSK2 = 0; //disable OCIE2A and TOIE2
\r
168 ASSR = (1<<AS2); //select asynchronous operation of timer2 (32,768kHz)
\r
170 OCR2A = 200; // set timer2 compare value
\r
172 TIMSK0 = 0; // delete any interrupt sources
\r
174 TCCR1B = (1<<CS10); // start timer1 with no prescaling
\r
175 TCCR2A = (1<<CS20); // start timer2 with no prescaling
\r
177 while((ASSR & 0x01) | (ASSR & 0x04)); //wait for TCN2UB and TCR2UB to be cleared
\r
179 Delay(1000); // wait for external crystal to stabilise
\r
183 cli(); // mt __disable_interrupt(); // disable global interrupt
\r
185 TIFR1 = 0xFF; // delete TIFR1 flags
\r
186 TIFR2 = 0xFF; // delete TIFR2 flags
\r
188 TCNT1H = 0; // clear timer1 counter
\r
190 TCNT2 = 0; // clear timer2 counter
\r
192 // shc/mt while ( !(TIFR2 && (1<<OCF2A)) ); // wait for timer2 compareflag
\r
193 while ( !(TIFR2 & (1<<OCF2A)) ); // wait for timer2 compareflag
\r
195 TCCR1B = 0; // stop timer1
\r
197 sei(); // __enable_interrupt(); // enable global interrupt
\r
199 // shc/mt if ( (TIFR1 && (1<<TOV1)) )
\r
200 if ( (TIFR1 & (1<<TOV1)) )
\r
202 temp = 0xFFFF; // if timer1 overflows, set the temp to 0xFFFF
\r
205 { // read out the timer1 counter value
\r
208 temp = (temp << 8);
\r
214 OSCCAL--; // the internRC oscillator runs to fast, decrease the OSCCAL
\r
216 else if (temp < 6120)
\r
218 OSCCAL++; // the internRC oscillator runs to slow, increase the OSCCAL
\r
221 calibrate = TRUE; // the interRC is correct
\r
223 TCCR1B = (1<<CS10); // start timer1
\r