freereos.zip
立即下载
资源介绍:
freereos.zip
#include "freertos_demo.h"
#include "stm32f10x.h"
/*FreeRTOS*********************************************************************************************/
//#include "FreeRTOS.h"
//#include "task.h"
//#include "timers.h"
#include "OLED.h"
#include "Encoder.h"
#include "event_groups.h"
#include "task.h"
unsigned char okay,flg;
unsigned char flag=1;
int16_t Num; //定义待被旋转编码器调节的变量
unsigned char hour,min,second;
unsigned char tim_arv=0;
EventBits_t bits1;
EventGroupHandle_t eventGroup;
BaseType_t xHigherPriorityTaskWoken=pdFALSE;
#define START_TASK_PRIO 1
#define START_TASK_STACK_SIZE 128
TaskHandle_t start_task_handler;
void start_task( void * pvParameters );
#define TASK1_PRIO 1
#define TASK1_STACK_SIZE 800
TaskHandle_t task1_handler;
void task1( void * pvParameters );
#define TASK2_PRIO 2
#define TASK2_STACK_SIZE 128
TaskHandle_t task2_handler;
void task2( void * pvParameters );
#define TASK3_PRIO 1
#define TASK3_STACK_SIZE 800
TaskHandle_t task3_handler;
void task3( void * pvParameters );
#define TASK4_PRIO 2
#define TASK4_STACK_SIZE 800
TaskHandle_t task4_handler;
//void task4( void * pvParameters );
#define TaskTimer_PRIO 2
#define TaskTimer_STACK_SIZE 128
TaskHandle_t TaskTimer_handler;
//#define a_handler TaskTimer_handler
void TaskTimer( void * pvParameters );
TimerHandle_t xTimer;
void vTimerCallback(TimerHandle_t xTimer);
void freertos_demo(void)
{
xTaskCreate((TaskFunction_t ) start_task,
(char * ) "start_task",
(configSTACK_DEPTH_TYPE ) START_TASK_STACK_SIZE,
(void * ) NULL,
(UBaseType_t ) START_TASK_PRIO,
(TaskHandle_t * ) &start_task_handler );
vTaskStartScheduler();
}
void start_task( void * pvParameters )
{
taskENTER_CRITICAL();
// TimerHandle_t xTimer;
// if(xTimer!=NULL){
//
// if(xTimerStart(xTimer,0)!=pdPASS){print("Failed to start timer\n");}
// else{print("Failed to create timer\n");}
//
// }
eventGroup=xEventGroupCreate();
xTaskCreate((TaskFunction_t ) TaskTimer,
(char * ) "TaskTimer",
(configSTACK_DEPTH_TYPE ) TaskTimer_STACK_SIZE,
(void * ) NULL,
(UBaseType_t ) TaskTimer_PRIO,
(TaskHandle_t * ) &TaskTimer_handler );
xTaskCreate((TaskFunction_t ) task1,
(char * ) "task1",
(configSTACK_DEPTH_TYPE ) TASK1_STACK_SIZE,
(void * ) NULL,
(UBaseType_t ) TASK1_PRIO,
(TaskHandle_t * ) &task1_handler );
xTaskCreate((TaskFunction_t ) task2,
(char * ) "task2",
(configSTACK_DEPTH_TYPE ) TASK2_STACK_SIZE,
(void * ) NULL,
(UBaseType_t ) TASK2_PRIO,
(TaskHandle_t * ) &task2_handler );
xTaskCreate((TaskFunction_t ) task3,
(char * ) "task3",
(configSTACK_DEPTH_TYPE ) TASK3_STACK_SIZE,
(void * ) NULL,
(UBaseType_t ) TASK3_PRIO,
(TaskHandle_t * ) &task3_handler );
xTaskCreate((TaskFunction_t ) task4,
(char * ) "task4",
(configSTACK_DEPTH_TYPE ) TASK4_STACK_SIZE,
(void * ) NULL,
(UBaseType_t ) TASK4_PRIO,
(TaskHandle_t * ) &task4_handler );
vTaskDelete(NULL);
taskEXIT_CRITICAL();
}
void TaskTimer( void * pvParameters )
{
while(1){
// EventBits_t bits1;
bits1=xEventGroupWaitBits(eventGroup,BIT1,pdTRUE,pdTRUE,portMAX_DELAY);
if(bits1)OLED_ShowString(4,7, "YE");
OLED_ShowString(4,11, "ok");
break;
}
}
void vTimerCallback(TimerHandle_t xTimer)
{
tim_arv=1;
// vTaskDelay(pdMS_TO_TICKS(1000));
// vTaskDelete(NULL);
// Num += Encoder_Get(); //获取自上此调用此函数后,旋转编码器的增量值,并将增量值加到Num上
// okay=ok1();
TaskHandle_t xTask3Handle=(TaskHandle_t)pvTimerGetTimerID(xTimer);
xTaskNotifyGive(xTask3Handle);
}
void task1( void * pvParameters )
{
uint32_t task1_num = 0;
// unsigned char ok_task2=0;
// OLED_ShowString(2, 1, "V:");
// OLED_ShowString(3, 1, "R:");
// OLED_ShowString(4, 1, "H:");
//
// Num += Encoder_Get(); //获取自上此调用此函数后,旋转编码器的增量值,并将增量值加到Num上
// okay=ok1();
OLED_ShowSignedNum(1, 5, Num, 2); //显示Num
OLED_ShowString(2, 1, "V:");
OLED_ShowString(3, 1, "R:");
OLED_ShowString(4, 1, "H:");
// if(Num==1&&okay==1)task1(task1);
// if(Num==2&&okay==1)task2(task2);
// if(Num==3&&okay==1)task3(task3);
task1_num=1;
vTaskDelay(5000);
OLED_ShowString( 2, 11, "put");
vTaskDelay(2000);
// while(1)
// {
// ok_task2=ok1();
// if(ok_task2==1)break;
// }
OLED_ShowString(2, 10, "c");
task1_num=0;
vTaskDelay(3000);
OLED_ShowString(2, 10, "ok ");
vTaskDelete(NULL);
}
void task2( void * pvParameters )
{
uint32_t task2_num = 0;
task2_num=1;
vTaskDelay(10000);
OLED_ShowString(3, 10, "ok");
vTaskDelete(NULL);
}
void task3( void * pvParameters )
{
// vTaskDelay(pdMS_TO_TICKS(2000));
xTimer=xTimerCreate("Timer",pdMS_TO_TICKS(1000),pdTRUE,
(void*)xTaskGetCurrentTaskHandle(),
vTimerCallback);
if(xTimer!=NULL){
xTimerStart(xTimer,0);}
flg=1;
while(1){
ulTaskNotifyTake(pdTRUE,portMAX_DELAY);
if(tim_arv==1)
{
tim_arv=0;
second++;
if(second>60){min++;second=0;}
if(min>60){hour++;min=0;}
if(hour>24){hour=0;}
OLED_ShowString(1, 9, "TIM: ");
OLED_ShowSignedNum(1, 14,second, 2); //显示Num
// vTaskDelay(pdMS_TO_TICKS(10));
}
}
}
void task4( void * pvParameters )
{
EventBits_t bits;
// BaseType_t xHigherPriorityTaskWoken=pdFALSE;
while(1)
{
bits=xEventGroupWaitBits(eventGroup,BIT0|BIT2,pdTRUE,pdFALSE,portMAX_DELAY);
if(bits&&flag)
{
Num += Encoder_Get(); //获取自上此调用此函数后,旋转编码器的增量值,并将增量值加到Num上
okay=ok1();
OLED_ShowSignedNum(1, 5, Num, 2); //显示Num
flag=0;
}
// portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
// portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
// xEventGroupSetBitsFromISR(eventGroup,BIT1,NULL);
}