/* Example code for setting up and using timers Two tasks each waiting on a timer, flashing LED's on the evaluation board. See "hardware.h" for LED definitions The kernel Stack is defined by the IAR linker file, or Top or SRAM in GCC (can be altered in the GCC link files, or defined symbol) */ //#define _SFR_ASM_COMPAT 1 /************************************************************************************** *Modify by : yuanguiyou *Date : 2008/06/05 *Reason : change the test project to Tkstudio IDE **************************************************************************************/ #include #include #include "avrx.h" #include "serialio.h" // From AvrX... #include "hardware.h" AVRX_GCC_TASK(Monitor, 40, 0); // External Task: Debug Monitor TimerControlBlock timer1,timer2; // Declare the control blocks needed for timers /* Timer 0 Overflow Interrupt Handler Prototypical Interrupt handler: . Switch to kernel context . handle interrupt . switch back to interrupted context. */ AVRX_SIGINT(SIG_OVERFLOW0) { IntProlog(); // Switch to kernel stack/context // TCNT0 = TCNT0_INIT; TCNT0 = TCNT0_INIT; AvrXTimerHandler(); // Call Time queue manager Epilog(); // Return to tasks } /* Task 1 simply flashes the light off for 1/5 second and then on for 4/5th for a 1 second cycle time. */ AVRX_GCC_TASKDEF(task1, 6, 3) { while (1) { AvrXStartTimer(&timer1, 800); // 800 ms delay AvrXWaitTimer(&timer1); LED = LED ^ 0x01; AvrXStartTimer(&timer1, 200); // 200 ms delay AvrXWaitTimer(&timer1); LED = LED ^ 0x01; } } /* Task 2 cycles the light on/off over 4 seconds. It uses a simplified form of the delay API */ AVRX_GCC_TASKDEF(task2, 6, 2) { while (1) { AvrXDelay(&timer2, 2000); // 2 second delay LED = LED ^ 0x02; } } int main(void) // Main runs under the AvrX Stack { //#define JUST_TOGGLE_PB0 #if defined(JUST_TOGGLE_PB0) int i; LEDDDR = 0xFF; LED = 0xFF; UBRR0L = UBRR_INIT; UBRR0H = 0; // UCSR0A = ; UCSR0B = _BV(TXEN0); UCSR0C = _BV(URSEL0) | _BV(UCSZ01) | _BV(UCSZ00); TCNT0 = TCNT0_INIT; TCCR0 = TMC8_CK256; while (1) { UDR0 = '+'; LED &= ~_BV(0); // LED on for (i = 750; i > 0; i--) { while (0 == (TIFR & _BV(TOV0))) ; TIFR |= _BV(TOV0); TCNT0 = TCNT0_INIT; } UDR0 = '-'; LED |= _BV(0); // LED off for (i = 250; i > 0; i--) { while (0 == (TIFR & _BV(TOV0))) ; TIFR |= _BV(TOV0); TCNT0 = TCNT0_INIT; } } #else AvrXSetKernelStack(0); MCUCR = 1<