在线时间0 小时
UID281012
ST金币0
蝴蝶豆0
注册时间2011-4-20
新手上路
- 最后登录
- 1970-1-1
|
a0a.1 0b0c
串口始终无法进入中断接收子程序,但是要是在main()里的for(;;)循环里头刚开始加入USART1_Putc(‘c’);就会一直进入接收中断子程序,并且串口调试助手就会无限循环输出“获取到串口1数据:”,当我发数据给它的话毫无反应哈,不知道怎么回事,你们看看:int main(void){ ChipHalInit(); //片内硬件初始化 ChipOutHalInit(); //片外硬件初始化 for(;;) { //串口2使用中断方式置位标志 if(Uart1_Get_Flag) { Uart1_Get_Flag=0; USART1_Puts("\r\n获取到串口1数据:"); USART1_Putc(Uart1_Get_Data); USART1_Puts("\r\n"); } }}------------void USART1_IRQHandler(void){ //接收中断 if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET) { USART_ClearITPendingBit(USART1,USART_IT_RXNE); Uart1_Get_Data=USART_ReceiveData(USART1); Uart1_Get_Flag=1; } //溢出-如果发生溢出需要先清空SR的溢出位,再读DR寄存器 则可清除不断入中断的问题 if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET) { USART_ClearFlag(USART1,USART_FLAG_ORE); //清溢出位 USART_ReceiveData(USART1); //读DR }}-----void USART_Configuration(void){ GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; //使能串口1,PA,AFIO总线 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1 , ENABLE); /* A9 USART1_Tx */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出-TX GPIO_Init(GPIOA, &GPIO_InitStructure); /* A10 USART1_Rx */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入-RX 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_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART1, &USART_ClockInitStructure); USART_Init(USART1, &USART_InitStructure); /* Enable the USARTx */ USART_Cmd(USART1, ENABLE); USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);}void USART1_Putc(unsigned char c){ USART_SendData(USART1, c); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );}void USART1_Puts(char * str){ while(*str) { USART_SendData(USART1, *str++); /* Loop until the end of transmission */ while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); }}-----------------void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/*UART2*/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
} |
|