/***********************************************这使用信号量,然后直接控制为wifi小车*******************************************/
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "key.h"
#include "pwm.h"
#include "track.h"
#include "includes.h"
//UCOSIII中以下优先级用户程序不能使用,ALIENTEK
//将这些优先级分配给了UCOSIII的5个系统内部任务
//优先级0:中断服务服务管理任务 OS_IntQTask()
//优先级1:时钟节拍任务 OS_TickTask()
//优先级2:定时任务 OS_TmrTask()
//优先级OS_CFG_PRIO_MAX-2:统计任务 OS_StatTask()
//优先级OS_CFG_PRIO_MAX-1:空闲任务 OS_IdleTask()
#define START_TASK_PRIO 3 //任务优先级
#define START_STK_SIZE 1024 //任务堆栈大小
OS_TCB StartTaskTCB; //任务控制块
CPU_STK START_TASK_STK[START_STK_SIZE];//任务堆栈
void start_task(void *p_arg); //任务函数
#define forward_TASK_PRIO 4 //任务优先级
#define forward_STK_SIZE 128 //任务堆栈大小
OS_TCB forward_TaskTCB; //任务控制块
CPU_STK forward_TASK_STK[forward_STK_SIZE];//任务堆栈
void forward_task(void *p_arg); //任务函数
#define back_TASK_PRIO 5 //任务优先级
#define back_STK_SIZE 128 //任务堆栈大小
OS_TCB back_TaskTCB; //任务控制块
CPU_STK back_TASK_STK[back_STK_SIZE];//任务堆栈
void back_task(void *p_arg); //任务函数
#define stop_TASK_PRIO 6 //任务优先级
#define stop_STK_SIZE 128 //任务堆栈大小
OS_TCB stop_TaskTCB; //任务控制块
CPU_STK stop_TASK_STK[stop_STK_SIZE];//任务堆栈
void stop_task(void *p_arg); //任务函数
#define turnright_TASK_PRIO 7 //任务优先级
#define turnright_STK_SIZE 128 //任务堆栈大小
OS_TCB turnright_TaskTCB; //任务控制块
CPU_STK turnright_TASK_STK[turnright_STK_SIZE];//任务堆栈
void turnright_task(void *p_arg); //任务函数
#define turnleft_TASK_PRIO 8 //任务优先级
#define turnleft_STK_SIZE 128 //任务堆栈大小
OS_TCB turnleft_TaskTCB; //任务控制块
CPU_STK turnleft_TASK_STK[turnleft_STK_SIZE]; //任务堆栈
void turnleft_task(void *p_arg); //任务函数
#define quick_TASK_PRIO 9 //任务优先级
#define quick_STK_SIZE 128 //任务堆栈大小
OS_TCB quick_TaskTCB; //任务控制块
CPU_STK quick_TASK_STK[quick_STK_SIZE];//任务堆栈
void quick_task(void *p_arg); //任务函数
#define slow_TASK_PRIO 10 //任务优先级
#define slow_STK_SIZE 128 //任务堆栈大小
OS_TCB slow_TaskTCB; //任务控制块
CPU_STK slow_TASK_STK[slow_STK_SIZE]; //任务堆栈
void slow_task(void *p_arg); //任务函数
#define track_TASK_PRIO 11 //任务优先级
#define track_STK_SIZE 128 //任务堆栈大小
OS_TCB track_TaskTCB; //任务控制块
CPU_STK track_TASK_STK[track_STK_SIZE];//任务堆栈
void track_task(void *p_arg); //任务函数
#define usartcheck_TASK_PRIO 12 //任务优先级
#define usartcheck_STK_SIZE 128 //任务堆栈大小
OS_TCB usartcheck_TaskTCB; //任务控制块
CPU_STK usartcheck_TASK_STK[usartcheck_STK_SIZE];//任务堆栈
void usartcheck_task(void *p_arg); //任务函数
OS_SEM forward_SEM; //定义信号量
OS_SEM back_SEM; //定义信号量
OS_SEM stop_SEM; //定义信号量
OS_SEM turnright_SEM;//定义信号量
OS_SEM turnleft_SEM; //定义信号量
OS_SEM quick_SEM; //定义信号量
OS_SEM slow_SEM; //定义信号量
OS_SEM track_SEM; //定义信号量
//s16 ini_ccr=60;//赋值给比较捕获寄存器CCR的初值
//s16 com_ccr=60;//赋值给比较捕获寄存器CCR的公共值
//主函数
int main(void)
{
OS_ERR err;
CPU_SR_ALLOC();
delay_init(); //时钟初始化
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断分组配置
//uart_init(115200); //串口初始化
//LED_Init(); //LED初始化
//LCD_Init(); //LCD初始化
//KEY_Init(); //按键初始化
TIM1_PWM_Init(100,9);
uart_init(115200); //串口初始化
track_init();
OSInit(&err); //初始化UCOSIII
OS_CRITICAL_ENTER(); //进入临界区
//创建开始任务
OSTaskCreate((OS_TCB * )&StartTaskTCB, //任务控制块
(CPU_CHAR * )"start task", //任务名字
(OS_TASK_PTR )start_task, //任务函数
(void * )0, //传递给任务函数的参数
(OS_PRIO )START_TASK_PRIO, //任务优先级
(CPU_STK * )&START_TASK_STK[0], //任务堆栈基地址
(CPU_STK_SIZE)START_STK_SIZE/10, //任务堆栈深度限位
(CPU_STK_SIZE)START_STK_SIZE, //任务堆栈大小
(OS_MSG_QTY )0, //任务内部消息队列能够接收的最大消息数目,为0时禁止接收消息
(OS_TICK )0, //当使能时间片轮转时的时间片长度,为0时为默认长度,
(void * )0, //用户补充的存储区
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, //任务选项
(OS_ERR * )&err); //存放该函数错误时的返回值
OS_CRITICAL_EXIT(); //退出临界区
OSStart(&err); //开启UCOSIII
}
//开始任务函数
void start_task(void *p_arg)//void *p_arg是空类型(无类型)指针,由函数内部强制转换为某一具体类型的指针
{
OS_ERR err;
CPU_SR_ALLOC();
p_arg = p_arg;//表示暂时不用p_arg,防止编译器告警
//(void)p_arg;//表示暂时不用p_arg,防止编译器告警
CPU_Init();
#if OS_CFG_STAT_TASK_EN > 0u
OSStatTaskCPUUsageInit(&err); //统计任务
#endif
#ifdef CPU_CFG_INT_DIS_MEAS_EN //如果使能了测量中断关闭时间
CPU_IntDisMeasMaxCurReset();
#endif
#if OS_CFG_SCHED_ROUND_ROBIN_EN //当使用时间片轮转的时候
//使能时间片轮转调度功能,时间片长度为1个系统时钟节拍,既1*5=5ms
OSSchedRoundRobinCfg(DEF_ENABLED,1,&err);
#endif
OS_CRITICAL_ENTER(); //进入临界区
OSSemCreate ((OS_SEM* )&forward_SEM, //创建信号量forward_SEM
(CPU_CHAR* )"forward_SEM",
(OS_SEM_CTR)0,
(OS_ERR* )&err);
OSSemCreate ((OS_SEM* )&back_SEM, //创建信号量back_SEM
(CPU_CHAR* )"back_SEM",
(OS_SEM_CTR)0,
(OS_ERR* )&err);
OSSemCreate ((OS_SEM* )&stop_SEM, //创建信号量stop_SEM
(CPU_CHAR* )"stop_SEM",
(OS_SEM_CTR)0,
(OS_ERR* )&err);
OSSemCreate ((OS_SEM* )&turnright_SEM, //创建信号量turnright_SEM
(CPU_CHAR* )"turnright_SEM",
(OS_SEM_CTR)0,
(OS_ERR* )&err);
OSSemCreate ((OS_SEM* )&turnleft_SEM, //创建信号量turnleft_SEM
(CPU_CHAR* )"turnleft_SEM",
(OS_SEM_CTR)0,
(OS_ERR* )&err);
OSSemCreate ((OS_SEM* )&quick_SEM, //创建信号量quick_SEM
(CPU_CHAR* )"quick_SEM",
(OS_SEM_CTR)0,
(OS_ERR* )&err);
OSSemCreate ((OS_SEM* )&slow_SEM, //创建信号量slow_SEM
(CPU_CHAR* )"slow_SEM",
(OS_SEM_CTR)0,
(OS_ERR* )&err);
OSSemCreate ((OS_SEM* )&track_SEM, //创建信号量track_SEM
(CPU_CHAR* )"track_SEM",
(OS_SEM_CTR)0,
(OS_ERR* )&err);
//创建任务forward_task
OSTaskCreate((OS_TCB * )&forward_TaskTCB,
(CPU_CHAR * )"forward_task",
(OS_TASK_PTR )forward_task,
(void * )0,
(OS_PRIO )forward_TASK_PRIO,
(CPU_STK * )&forward_TASK_STK[0],
(CPU_STK_SIZE)forward_STK_SIZE/10,
(CPU_STK_SIZE)forward_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err);
//创建任务back_task
OSTaskCreate((OS_TCB * )&back_TaskTCB,
(CPU_CHAR * )"back_task",
(OS_TASK_PTR )back_task,
(void * )0,
(OS_PRIO )back_TASK_PRIO,
(CPU_STK * )&back_TASK_STK[0],
(CPU_STK_SIZE)back_STK_SIZE/10,
(CPU_STK_SIZE)back_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err);
//创建任务stop_task
OSTaskCreate((OS_TCB * )&stop_TaskTCB,
(CPU_CHAR * )"stop_task",
(OS_TASK_PTR )stop_task,
(void * )0,
(OS_PRIO )stop_TASK_PRIO,
(CPU_STK * )&stop_TASK_STK[0],
(CPU_STK_SIZE)stop_STK_SIZE/10,
(CPU_STK_SIZE)stop_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err);
//创建任务turnright_task
OSTaskCreate((OS_TCB * )&turnright_TaskTCB,
(CPU_CHAR * )"turnright_task",
(OS_TASK_PTR )turnright_task,
(void * )0,
(OS_PRIO )turnright_TASK_PRIO,
(CPU_STK * )&turnright_TASK_STK[0],
(CPU_STK_SIZE)turnright_STK_SIZE/10,
(CPU_STK_SIZE)turnright_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err);
//创建任务turnleft_task
OSTaskCreate((OS_TCB * )&turnleft_TaskTCB,
(CPU_CHAR * )"turnleft_task",
(OS_TASK_PTR )turnleft_task,
(void * )0,
(OS_PRIO )turnleft_TASK_PRIO,
(CPU_STK * )&turnleft_TASK_STK[0],
(CPU_STK_SIZE)turnleft_STK_SIZE/10,
(CPU_STK_SIZE)turnleft_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err);
//创建任务quick_task
OSTaskCreate((OS_TCB * )&quick_TaskTCB,
(CPU_CHAR * )"quick_task",
(OS_TASK_PTR )quick_task,
(void * )0,
(OS_PRIO )quick_TASK_PRIO,
(CPU_STK * )&quick_TASK_STK[0],
(CPU_STK_SIZE)quick_STK_SIZE/10,
(CPU_STK_SIZE)quick_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err);
//创建任务slow_task
OSTaskCreate((OS_TCB * )&slow_TaskTCB,
(CPU_CHAR * )"slow_task",
(OS_TASK_PTR )slow_task,
(void * )0,
(OS_PRIO )slow_TASK_PRIO,
(CPU_STK * )&slow_TASK_STK[0],
(CPU_STK_SIZE)slow_STK_SIZE/10,
(CPU_STK_SIZE)slow_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err);
//创建任务track_task
OSTaskCreate((OS_TCB * )&track_TaskTCB,
(CPU_CHAR * )"track_task",
(OS_TASK_PTR )track_task,
(void * )0,
(OS_PRIO )track_TASK_PRIO,
(CPU_STK * )&track_TASK_STK[0],
(CPU_STK_SIZE)track_STK_SIZE/10,
(CPU_STK_SIZE)track_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err);
//创建任务usartcheck_task
OSTaskCreate((OS_TCB * )&usartcheck_TaskTCB,
(CPU_CHAR * )"usartcheck_task",
(OS_TASK_PTR )usartcheck_task,
(void * )0,
(OS_PRIO )usartcheck_TASK_PRIO,
(CPU_STK * )&usartcheck_TASK_STK[0],
(CPU_STK_SIZE)usartcheck_STK_SIZE/10,
(CPU_STK_SIZE)usartcheck_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err);
OS_CRITICAL_EXIT(); //退出临界区
OSTaskDel((OS_TCB*)0,&err); //删除start_task任务自身
}
//任务usartcheck_task的任务函数
void usartcheck_task(void *p_arg)
{
OS_ERR err;
u8 Res;
while(1)
{
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)!=RESET)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //读取接收到的数据
switch(Res)//
{
case 'A'://前进
{
OSSemPost(&forward_SEM,OS_OPT_POST_1,&err); //发送信号量forward_SEM
break;
}
case 'b'://后退
{
OSSemPost(&back_SEM,OS_OPT_POST_1,&err); //发送信号量back_SEM
break;
}
case 'C'://停止
{
OSSemPost(&stop_SEM,OS_OPT_POST_1,&err); //发送信号量stop_SEM
break;
}
case 'r'://右转
{
OSSemPost(&turnright_SEM,OS_OPT_POST_1,&err);//发送信号量turnright_SEM
break;
}
case 'l'://左转
{
OSSemPost(&turnleft_SEM,OS_OPT_POST_1,&err); //发送信号量turnleft_SEM
break;
}
case 'k'://加速
{
OSSemPost(&quick_SEM,OS_OPT_POST_1,&err); //发送信号量quick_SEM
break;
}
case 'm'://减速
{
OSSemPost(&slow_SEM,OS_OPT_POST_1,&err); //发送信号量slow_SEM
break;
}
case 't'://循迹
{
OSSemPost(&track_SEM,OS_OPT_POST_1,&err); //发送信号量track_SEM
break;
}
default: break;
}
}
}
}
//任务forward_task的任务函数
void forward_task(void *p_arg)
{
OS_ERR err;
OSSemPend(&forward_SEM,0,OS_OPT_PEND_BLOCKING,0,&err); //等待信号量
forward(ini_ccr);
}
//任务back_task的任务函数
void back_task(void *p_arg)
{
OS_ERR err;
OSSemPend(&back_SEM,0,OS_OPT_PEND_BLOCKING,0,&err); //等待信号量
back(ini_ccr);
}
//任务stop_task的任务函数
void stop_task(void *p_arg)
{
OS_ERR err;
OSSemPend(&stop_SEM,0,OS_OPT_PEND_BLOCKING,0,&err); //等待信号量
stop();
}
//任务turnright_task的任务函数
void turnright_task(void *p_arg)
{
OS_ERR err;
OSSemPend(&turnright_SEM,0,OS_OPT_PEND_BLOCKING,0,&err);//等待信号量
turn_right();
}
//任务turnleft_task的任务函数
void turnleft_task(void *p_arg)
{
OS_ERR err;
OSSemPend(&turnleft_SEM,0,OS_OPT_PEND_BLOCKING,0,&err); //等待信号量
turn_left();
}
//任务quick_task的任务函数
void quick_task(void *p_arg)
{
OS_ERR err;
OSSemPend(&quick_SEM,0,OS_OPT_PEND_BLOCKING,0,&err); //等待信号量
quick();
}
//任务slow_task的任务函数
void slow_task(void *p_arg)
{
OS_ERR err;
OSSemPend(&slow_SEM,0,OS_OPT_PEND_BLOCKING,0,&err); //等待信号量
slow();
}
//任务track_task的任务函数
void track_task(void *p_arg)
{
OS_ERR err;
OSSemPend(&track_SEM,0,OS_OPT_PEND_BLOCKING,0,&err); //等待信号量
while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET)
{
track();
}
}