本帖最后由 snyanglq 于 2015-2-4 11:56 编辑
不知道各位有没有出现过这样的情况,我使用STM32F103RCT6芯片编写串口程序,重定向printf函数到串口1,数据可以正常地发送和接收,但是有个问题
我仿真的时候发现,我每次用printf发出去的数据,接收数组同样会接收到,举个例子,我用串口发送123到上位机,上位机发送456回到MCU,我在中断的
接收函数里的数组 W_Rec[]会接收到123456,本来应该是456才对,我想问下是不是重定向后都会这样,如果不是要如何解决?请大家指点下小弟
附一下代码:
串口配置
- void USART1_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
- /* config USART1 clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
-
- /* USART1 GPIO config */
- /* Configure USART1 Tx (PA.09) as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* Configure USART1 Rx (PA.10) as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /* USART1 mode config */
- USART_InitStructure.USART_BaudRate = 115200;
- 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);
- }
复制代码- void USART1_NVIC(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- //分配到第0组
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
-
- /* Enable the USARTy Interrupt */
- 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);
- }
复制代码 重定向函数
- int fputc(int ch, FILE *f)
- {
- /* 将Printf内容发往串口 */
- USART_SendData(USART1, (unsigned char) ch);
- while (!(USART1->SR & USART_FLAG_TXE));
-
- return (ch);
- }
复制代码 中断函数
- void USART1_IRQHandler(void)
- {
- uint8_t c;
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- {
- USART_ClearITPendingBit(USART1, USART_IT_RXNE);
- // W_Rec[wn++] = USART1->DR;
- c = USART_ReceiveData(USART1);
- W_Rec[wn++] = c;
- }
-
- }
复制代码 请大家指点下如何才能不接收MCU发出去的数据
|