?? timers.c
字號:
/*
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
#include <avr\io.h>
#include <avr\signal.h>
#include "avrx.h"
#include "serialio.h" // From AvrX...
#include "hardware.h"
AVRX_GCC_TASK(Monitor, 40, 0); // External Task: Debug Monitor
TimerControlBlock timer1, // Declare the control blocks needed for timers
timer2;
/*
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<<SE;
TCNT0 = TCNT0_INIT;
TCCR0 = TMC8_CK256;
TIMSK = 1<<TOIE0;
LEDDDR = 0xFF;
LED = 0xFF;
AvrXRunTask(TCB(task1));
AvrXRunTask(TCB(task2));
AvrXRunTask(TCB(Monitor));
InitSerialIO(UBRR_INIT); // Initialize USART baud rate generator
Epilog(); // Switch from AvrX Stack to first task
#endif // defined(JUST_TOGGLE_PB0)
return(0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -