移植的ucos,建立的三个任务(红灯闪烁任务、绿灯闪烁任务、LCD显示任务),主函数如下:
int main(void)
{
int x=0;
SystemInit();
delay_init(72); //延时函数初始化
led_init();
SysTick_Configuration();
uart_init(9600);
LCD_Init();
OSInit();
OSTaskCreate(startup_task,(void *)0,(OS_STK*)&startup_task_stk[STARTUP_TASK_STK_SIZE-1],START_TASK_PRIO);
OSStart();
return 0;
}
当我把红色背景注释掉时,ucos不能运行,取消注释后,三个任务能正常切换。当同时注释掉LCD_Init()函数和uart_init(9600)时,并删除LCD显示任务时,红灯闪烁任务和绿灯闪烁任务则能够正常切换。也就是说要想使包含LCD显示任务在内的三个任务能正常切换,必须有uart_init(9600)这个函数。
uart_init()我注释的只剩下深绿色背景的那两句,也就是说三个任务能否正常切换和下面深绿色的有关系,但是ucos正常切换为什么和这两天语句有关系呢? 我又没有用到串口,这两条深绿色的函数起到了什么作用呢?
void uart_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//GPIO_Init(GPIOA, &GPIO_InitStructure);
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
//NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART1, ENABLE); //使能串口
} |