赞
踩
BaseType_t xTaskCreate(TaskFunction_t pxTaskCode,
const char * const pcName,
const uint16_t usStackDepth,
void * const pvParameters,
UBaseType_t uxPriority,
TaskHandle_t * const pxCreatedTask ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
“ lint !e971 Unqualified char types are allowed for strings and single characters only” 告诉编译器在编译的时候不要报警告,这里把char类型和字符串类型混用了,编译器你不要报警告
#define configMAX_TASK_NAME_LEN (16)
#define configMINIMAL_STACK_SIZE ((unsigned short)130)
具体大小需要自己调试
#define configMAX_PRIORITIES (32)
优先级必须比这个宏要小,数字越小,优先级越低,与ucos-II相反的(ucos-II 数字越小,优先级越高)
configMAX_PRIORITIES 宏大小可随意,这里配置的是32,但最好使用实际需要的最小数值以避免内存浪费,FreeRTOS理论上可支持无限个任务。而ucos-II 一般只支持256 个任务(可扩展)
如果优先级设置的比configMAX_PRIORITIES 大,会将优先级自动设置为最大优先级configMAX_PRIORITIES
// create task2 void vTask2( void *pvParameters ) { // each task must be a endless loop for( ;; ) { printf("task2 say: hello world"); } } // create task1 void vTask1( void *pvParameters ) { // each task must be a endless loop, otherwise, the program will crash for (;;) { printf("task1 say: hello world1"); } } int main( void ) { // create task1 xTaskCreate(vTask1, // create task1 "Task 1", // task name 128, // stack size NULL, // the task has no parameter 1, // task priority NULL ); // task handle // creat task2 xTaskCreate(vTask2, // create task2 "Task 2", // task name 128, // stack size NULL,// the task has no parameter 1, // task priority, the task priority can same as other task priority, and can not be same as other task for ucos-II OS NULL ); // The system start to scheduler vTaskStartScheduler(); // the program will not run here if there are no issues with the program, but if executed here, it is likely the memory heap space is insufficient resulting in idle for (;;) { } }
// create task2 void vTask2( void *pvParameters ) { // each task must be a endless loop for( ;; ) { printf("task2 say: hello world"); } } // create task1 void vTask1( void *pvParameters ) { // creat task2 xTaskCreate(vTask2, // create task2 "Task 2", // task name 128, // stack size NULL,// the task has no parameter 1, // task priority, the task priority can not be same as other task priority NULL ); // each task must be a endless loop, otherwise, the program will crash for (;;) { printf("task1 say: hello world1"); } } int main( void ) { // create task1 xTaskCreate(vTask1, // create task1 "Task 1", // task name 128, // stack size NULL, // the task has no parameter 1, // task priority NULL ); // task handle // The system start to scheduler vTaskStartScheduler(); // the program will not run here if there are no issues with the program, but if executed here, it is likely the memory heap space is insufficient resulting in idle for (;;) { } }
// create task2 void vTask2(void *pvParameters) { char *pcTaskName; // (void *) forced conversion to string (char *) pcTaskName = (char *)pvParameters; // each task must be a endless loop for( ;; ) { printf("task2, %s", pcTaskName ); } } // create task1 void vTask1( void *pvParameters ) { char *pcTaskName; pcTaskName = (char *)pvParameters; // each task must be a endless loop, otherwise, the program will crash for (;;) { printf("task1, %s", pcTaskName ); } } int main( void ) { // create task1 xTaskCreate(vTaskFunction, // create task1 "Task 1", // task name 128, // stack size “hello world”, // pass parameters to task1 1, // task priority NULL); // task handle // creat task2 xTaskCreate(vTaskFunction, // create task2 "Task 2", // task name 128, // stack size “hello world”, // pass parameters to task2 2, // task priority, the task priority can not be same as other task priority NULL); // The system start to scheduler vTaskStartScheduler(); // the program will not run here if there are no issues with the program, but if executed here, it is likely the memory heap space is insufficient resulting in idle for (;;) { } }
与上面的第 2 步类似,只是在创建任务的同时传了一个字符串进任务里面了,然后在任务里面接收这个字符串,并打印出来
看起来写了一堆,很复杂,其实很简单,就一个函数调用,会使用就行了,调用函数的时候知道每个参数怎么填,怎么根据返回值判断创建结果就可以了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。