第十二篇?优化代码,在ucosiii下,实现同步信号亮.
时间:10-02
整理:3721RD
点击:
考虑到rtos的特点,既然已经使用了ucosiii,那么肯定要借助她的功能,为了实现,系统的多任务实时响应,今天完成了优化之前的多任务下的轮训操作,改为任务信号量同步.本次,主要有三个任务.
task0:dma下4路adc检测,
task1:pwm检测获取输入频率
task2:获取rgb屏幕按键中断
3个任务都在等待task信号量,这里有三个中断分别给他们发送task信号量.
分别是
主任伍不断尝试获取信号量,而3路中断则发给对应的三个任务,
用插图显示如下:
完整的代码,在https://github.com/iysheng/chicken
task0:dma下4路adc检测,
task1:pwm检测获取输入频率
task2:获取rgb屏幕按键中断
3个任务都在等待task信号量,这里有三个中断分别给他们发送task信号量.
分别是
- void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
- {
- static OS_ERR err;
- if(hadc->Instance==ADC1)
- {
- OSTaskSemPost(&AppTaskObj0TCB,OS_OPT_POST_NONE,&err);
- }
- }
- /*输入捕获中断的回调函数*/
- void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
- {
- static OS_ERR err;
- if(htim->Instance==TIM5)
- {
- switch(ic_state)
- {
- case 0x00:ic_state=0x40;ic_value=0x00;__HAL_TIM_DISABLE(htim);__HAL_TIM_SET_COUNTER(htim,0);__HAL_TIM_ENABLE(htim);break;//捕获第一个中断
- case 0x40:ic_state|=0x80;ic_value=HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1);
- OSTaskSemPost(&AppTaskObj1TCB,OS_OPT_POST_NONE,&err);break;//捕获第二个中断,完成一次周期捕获
- default:break;
- }
- }
- }
- void EXTI9_5_IRQHandler(void) {
- static OS_ERR err;
- OSIntEnter();
- OSTaskSemPost(&AppTaskObj2TCB,OS_OPT_POST_NONE,&err);
- __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_7);
- OSIntExit();
- }
- while (DEF_TRUE) {
- BSP_LED_Off(0);
- OSTaskSemPend (
- 100,
- OS_OPT_PEND_BLOCKING,
- NULL,
- &os_err);
- if(os_err==OS_ERR_NONE)
- {
- while(uline<4){
- switch(uline)
- {
- case 0:{sprintf((char *)rstr,"PA4:%4dKg",raw_icekong[1]);break;}
- case 1:{sprintf((char *)rstr,"PA5:%4dN.m",raw_icekong[2]);break;}
- case 2:{sprintf((char *)rstr,"PA6:%4dcm",raw_icekong[3]);break;}
- case 3:{uitemp=raw_icekong[0];ftemp=((float)uitemp)/4095*3300;ftemp=((ftemp-760.0)/2.5)+25;
- sprintf((char *)rstr,"%0.3f",ftemp);break;}
- default:break;
- }
- LCD_ShowString(120,130+uline*80,strlen(rstr)*16,32,32,(uint8_t *)rstr);
- //printf("%s\r\n",rstr);
- uline++;
- }
- uline=0;
- }
- APP_TRACE_INFO(("Object test task 0 running ....\r\n"));
- }
- while (DEF_TRUE) {
- OSTaskSemPend (
- 100,
- OS_OPT_PEND_BLOCKING,
- NULL,
- &os_err);
- if(os_err==OS_ERR_NONE)
- {
- ic_state&=0x3f;
- hole_ic_value=ic_state*(0xffffffff);
- hole_ic_value+=ic_value;
- ic_value=hole_ic_value/1000;
- sprintf((char *)rstr,"PWM:%6dms...%9lldus",(int)ic_value,(long long int)hole_ic_value);
- LCD_ShowString(120,50,strlen(rstr)*16,32,32,(uint8_t *)rstr);
- printf("%s\r\n",rstr);
- ic_state=0x00;
- }
- APP_TRACE_INFO(("Object test task 1 running ....\r\n"));
- }
- while (DEF_TRUE) {
- OSTaskSemPend (100,
- OS_OPT_PEND_BLOCKING,
- NULL,
- &os_err);
- if(os_err==OS_ERR_NONE)
- {
- BSP_LED_On(0);
- FT5206_Scan();
- if(tp_dev.sta!=0)
- {
- tp_dev.sta&=0x1f;
- while(tp_dev.sta&0x01){
- upoint++;
- tp_dev.sta>>=1;
- }
- for(tp_dev.sta=0;tp_dev.sta<upoint;tp_dev.sta++)
- {
- sprintf((char *)rstr,"%dtouchpoint,x=%3d,y=%3d",tp_dev.sta,(uint16_t)tp_dev.x[tp_dev.sta],(uint16_t)tp_dev.y[tp_dev.sta]);
- if((tp_dev.x[tp_dev.sta]<800)&&(tp_dev.y[tp_dev.sta]<480))
- {
- //printf("%s\r\n",rstr);
- LCD_ShowString(300,50+3*80,32*16,32,32,(uint8_t *)rstr);
- }
- }
- upoint=0;
- tp_dev.sta=0;
- }
- }
- else if(os_err==OS_ERR_TIMEOUT)
- {
- printf("task3 time out.\r\n");
- }
- APP_TRACE_INFO(("Object test task 2 running ....\r\n"));
- }
- OS_SEM_CTR OSTaskSemPend (OS_TICK timeout,
- OS_OPT opt,
- CPU_TS *p_ts,
- OS_ERR *p_err)//等待信号量
- OS_SEM_CTR OSTaskSemPost (OS_TCB *p_tcb,
- OS_OPT opt,
- OS_ERR *p_err)//发送信号量
主任伍不断尝试获取信号量,而3路中断则发给对应的三个任务,
用插图显示如下:
完整的代码,在https://github.com/iysheng/chicken