3按任务,控制3LED闪烁。
#include "includes.h"
#define OS_TASK_0_STK_SIZE 200
#define OS_TASK_1_STK_SIZE 200
#define OS_TASK_2_STK_SIZE 200
#define OS_TASK_0_PRIO 0
#define OS_TASK_1_PRIO 1
#define OS_TASK_2_PRIO 2
OS_STK Task0Stack[OS_TASK_0_STK_SIZE];
OS_STK Task1Stack[OS_TASK_1_STK_SIZE];
OS_STK Task2Stack[OS_TASK_2_STK_SIZE];
#define LED0_On() GPIOD->ODR |= 1<<3;
#define LED0_Off() GPIOD->ODR &= ~(1<<3);
#define LED1_On() GPIOD->ODR |= 1<<2;
#define LED1_Off() GPIOD->ODR &= ~(1<<2);
#define LED2_On() GPIOD->ODR |= 1<<0;
#define LED2_Off() GPIOD->ODR &= ~(1<<0);
void task0( void * pdata )
{
while(1)
{
LED0_On();
OSTimeDlyHMSM(0,0,0,500 );
LED0_Off();
OSTimeDlyHMSM(0,0,0,500 );
}
}
void task1(void * pdata )
{
while(1)
{
LED1_On();
OSTimeDlyHMSM(0,0,0,300 );
LED1_Off();
OSTimeDlyHMSM(0,0,0,300 );
}
}
void task2(void * pdata)
{
while(1)
{
LED2_On();
OSTimeDlyHMSM(0,0,0,100 );
LED2_Off();
OSTimeDlyHMSM(0,0,0,100);
}
}
void T4_Init(void)
{
TIM4_ARRPreloadConfig(1);
TIM4_PrescalerConfig(TIM4_PRESCALER_32,TIM4_PSCRELOADMODE_IMMEDIATE);
TIM4_SetAutoreload((F_CPU/32/OS_TICKS_PER_SEC));
TIM4_Cmd(1);
TIM4_ITConfig(TIM4_IT_UPDATE,1);
}
int main()
{
__disable_interrupt();
CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);
GPIO_Init(GPIOD, GPIO_PIN_3|GPIO_PIN_2|GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_SLOW);
T4_Init();
OSInit();
OSTaskCreate( task0, (void *)OS_TASK_0_STK_SIZE, &Task0Stack[OS_TASK_0_STK_SIZE-1], OS_TASK_0_PRIO );
OSTaskCreate( task1, (void *)OS_TASK_1_STK_SIZE, &Task1Stack[OS_TASK_1_STK_SIZE-1], OS_TASK_1_PRIO );
OSTaskCreate( task2, (void *)OS_TASK_2_STK_SIZE, &Task2Stack[OS_TASK_2_STK_SIZE-1], OS_TASK_2_PRIO );
OSStart();
return 0;
} |