我跑裸机的时候在中断里面接收到数据就马上发送出来没有问题,但是在Ucos系统下只有第一次进了UART中断服务程序,ucos其它任务照常运行。这是怎么回事?(备注:接收的是GPS模块传过来的数据,然后在任务中串口输出是没问题的,就是中断接收有问题)
中断服务程序如下:
void USART1_IRQHandler(void)
{
INT8U i = 0;
OS_CPU_SR cpu_sr;
OS_ENTER_CRI
tiCAL();
OSIntNesting++;
OS_EXIT_CRITICAL();
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{
}
i = USART_ReceiveData(USART1);
printf("%c",i);
OSIntExit();
}
初始化那些应该没问题,跑裸机是可以的
下面是初始化程序:
void NvicInit(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x03;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void UartInit(void)
{
INT8U i;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE); // 使能GPIOA端口
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); // 使能串口1时钟
GPIO_PinAFConfig(GPIOA ,GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA ,GPIO_Pin_10, GPIO_AF_1);
/////////////////////////////////////////////////////////////////////////////////////
/* PA9==TX PA10-RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA , &GPIO_InitStructure);
/////////////////////////////////////////////////////////////////////////////////////
USART_InitStructure.USART_BaudRate = 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);//使能串口1
USART_ClearFlag(USART1, USART_FLAG_TC);
}
void NvicInit(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x03;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void UartInit(void)
{
INT8U i;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE); // 使能GPIOA端口
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); // 使能串口1时钟
GPIO_PinAFConfig(GPIOA ,GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA ,GPIO_Pin_10, GPIO_AF_1);
/////////////////////////////////////////////////////////////////////////////////////
/* PA9==TX PA10-RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA , &GPIO_InitStructure);
/////////////////////////////////////////////////////////////////////////////////////
USART_InitStructure.USART_BaudRate = 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);//使能串口1
USART_ClearFlag(USART1, USART_FLAG_TC);
}
以上是初始化,我单步调试发现执行完 USART_Cmd(USART1, ENABLE);后RXNE标志位就被置位了
这是ST的库函数,执行完USARTx->CR1 |= USART_CR1_UE;后标志位就被置位了
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected USART by setting the UE bit in the CR1 register */
USARTx->CR1 |= USART_CR1_UE;
}
else
{
/* Disable the selected USART by clearing the UE bit in the CR1 register */
USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
}
}
看汇编代码如下:
0x080021B6 4770 BX lr
403: if (NewState != DISABLE)
404: {
405: /* Enable the selected USART by setting the UE bit in the CR1 register */
0x080021B8 2900 CMP r1,#0x00
0x080021BA D004 BEQ 0x080021C6
406: USARTx->CR1 |= USART_CR1_UE;
407: }
408: else
409: {
410: /* Disable the selected USART by clearing the UE bit in the CR1 register */
0x080021BC 6802 LDR r2,[r0,#0x00]
0x080021BE 2301 MOVS r3,#0x01
0x080021C0 431A ORRS r2,r2,r3
0x080021C2 6002 STR r2,[r0,#0x00]
0x080021C4 E003 B 0x080021CE
411: USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
412: }
0x080021C6 6802 LDR r2,[r0,#0x00]
0x080021C8 0852 LSRS r2,r2,#1
0x080021CA 0052 LSLS r2,r2,#1
0x080021CC 6002 STR r2,[r0,#0x00]
413: }
截图如下所示
人多力量大。
一周热门 更多>