📄 app.c
字号:
/*
*************************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
*
* ATmega256 Sample code
*
* File : APP.C
* By : Jean J. Labrosse
*************************************************************************************************************
*/
#include <includes.h>
/*
**************************************************************************************************************
* VARIABLES
**************************************************************************************************************
*/
INT32U AppTickISRCtr;
OS_STK AppTaskStartStk[OS_TASK_START_STK_SIZE];
/*
**************************************************************************************************************
* FUNCTION PROTOTYPES
**************************************************************************************************************
*/
void main(void);
static void AppTaskStart(void *p_arg);
static void AppTaskCreate(void);
static void AppTickInit(void);
#if OS_VIEW_MODULE > 0
static void AppTerminalRx(INT8U rx_data);
#endif
/*
**************************************************************************************************************
* MAIN
**************************************************************************************************************
*/
void main (void)
{
INT8U err;
/* ATmega256 oscillator settings */
CLKPR = 0x80; /* Set internal oscillator to DIV by 1, 8 MHz OSC */
CLKPR = 0x00;
/*---- Any initialization code prior to calling OSInit() goes HERE --------------------------------*/
OSTaskStkSize = OS_TASK_IDLE_STK_SIZE; /* Setup the Idle and Statistics Task sizes */
OSTaskStkSizeHard = OS_TASK_IDLE_STK_SIZE_HARD;
OSInit(); /* Initialize "uC/OS-II, The Real-Time Kernel" */
/*---- Any initialization code before starting multitasking ---------------------------------------*/
OSTaskStkSize = OS_TASK_START_STK_SIZE;
OSTaskStkSizeHard = OS_TASK_START_STK_SIZE_HARD;
OSTaskCreateExt(AppTaskStart,
(void *)0,
(OS_STK *)&AppTaskStartStk[OSTaskStkSize - 1],
OS_TASK_START_PRIO,
OS_TASK_START_PRIO,
(OS_STK *)&AppTaskStartStk[OSTaskStkSizeHard],
OSTaskStkSize - OSTaskStkSizeHard,
(void *)0,
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
#if (OS_TASK_NAME_SIZE > 14) && (OS_TASK_STAT_EN > 0)
OSTaskNameSet(OS_TASK_START_PRIO, "Start Task", &err);
#endif
/*---- Create any other task you want before we start multitasking --------------------------------*/
OSStart(); /* Start multitasking (i.e. give control to uC/OS-II) */
}
/*
*********************************************************************************************************
* STARTUP TASK
*
* Description : This is an example of a startup task. As mentioned in the book's text, you MUST
* initialize the ticker only once multitasking has started.
*
* Arguments : p_arg is the argument passed to 'AppTaskStart()' by 'OSTaskCreate()'.
*
* Notes : 1) The first line of code is used to prevent a compiler warning because 'p_arg' is not
* used. The compiler should not generate any code for this statement.
*********************************************************************************************************
*/
static void AppTaskStart (void *p_arg)
{
INT8U i;
(void)p_arg; /* Prevent compiler warnings */
BSP_Init(); /* Initialize the Board Support Package */
AppTickInit(); /* Initialize the tick interrupts needed by uC/OS-II */
#if OS_TASK_STAT_EN > 0
OSStatInit(); /* Start stats task */
#endif
#if OS_VIEW_MODULE > 0
OSView_Init(38400); /* Initialize uC/OS-View if module is present */
OSView_TerminalRxSetCallback(AppTerminalRx);
#endif
AppTaskCreate(); /* Create other application tasks */
LED_Off(0);
while (TRUE) {
for (i = 1; i <= 8; i++) {
LED_On(i);
OSTimeDly(OS_TICKS_PER_SEC / 20);
LED_Off(i);
}
for (i = 7; i > 1; i--) {
LED_On(i);
OSTimeDly(OS_TICKS_PER_SEC / 20);
LED_Off(i);
}
}
}
/*
*********************************************************************************************************
* TERMINAL WINDOW CALLBACK
*********************************************************************************************************
*/
#if OS_VIEW_MODULE > 0
static void AppTerminalRx (INT8U rx_data)
{
}
#endif
/*
*********************************************************************************************************
* CREATE APPLICATION TASKS
*********************************************************************************************************
*/
static void AppTaskCreate (void)
{
}
/*
*********************************************************************************************************
* TICKER INITIALIZATION
*
* Description : This function is called to initialize uC/OS-II's tick source (typically a timer generating
* interrupts every 1 to 100 mS).
*
* Arguments : none
*********************************************************************************************************
*/
static void AppTickInit (void)
{
AppTickISRCtr = 0;
TCCR0A = 0x02; /* Set TIMER0 prescaler to CTC Mode, CLK/1024 */
TCCR0B = 0x05; /* Set CLK/1024 Prescale */
TCNT0 = 0; /* Start TCNT at 0 for a new cycle */
OCR0A = CPU_CLK_FREQ / OS_TICKS_PER_SEC / 1024 - 1;
TIFR0 |= 0x02; /* Clear TIMER0 compare Interrupt Flag */
TIMSK0 |= 0x02; /* Enable TIMER0 compare Interrupt */
}
/*
*********************************************************************************************************
* TIMER #0 IRQ HANDLER
*
* Description : This function handles the timer interrupt that is used to generate TICKs for uC/OS-II.
*
* Arguments : none
*
* Note(s) : 1) There is no need to clear the interrupt since this is done automatically in Output
* Compare mode when the ISR is executed.
*********************************************************************************************************
*/
void AppTickISR_Handler (void)
{
AppTickISRCtr++;
OSTimeTick(); /* If the interrupt is from the tick source, call OSTimeTick() */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -