刚刚把串口3调试出来
首次使用USART_SendData(USART3;'0');串口助手接收正确,显示0;
现在想单片机接收到数据时 向串口发送一位数据 结果却连续接收到3位数据 如图[url=]图片[/url]
代码如下 求分析[mw_shl_code=applescript,true]void Usart3_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //ê1ÄüGPIOAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//ê1ÄüUSART1ê±Öó
//′®¿ú1¶Ôó|òy½Å¸′óÃó3éä
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_USART3); //GPIOA9¸′óÃÎaUSART1
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_USART3); //GPIOA10¸′óÃÎaUSART1
//USART1¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; //GPIOA9óëGPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOC,&GPIO_InitStructure); //3õê¼»ˉPA9£¬
A10
//USART1 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART3, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_Cmd(USART3, ENABLE); //ê1Äü′®¿ú1
USART_ClearFlag(USART3,USART_FLAG_TC);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//¿aÆôÏà1ØÖD¶Ï
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//′®¿ú1ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
}
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
res=USART_ReceiveData(USART3);
rec_f=1;
}
}[/mw_shl_code][mw_shl_code=applescript,true]//主函数
int i;
delay_ms(1000);
USART_SendData(USART3,'0');
while(1)
{
if(rec_f==1)
{
rec_f=0;
USART_SendData(USART3,i+0x30);
i++;
}
}
[/mw_shl_code]
usartsenddata就是打印函数啊 那个库函数带的
原子哥 还是不行啊[mw_shl_code=applescript,true]void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
res=USART_ReceiveData(USART3);
USART_SendData(USART3,0x32);
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);
//delay_ms(1000);
//while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET);
//uart_i++;
}
}[/mw_shl_code]
我在USART3的接收中断里发送数据 结果串口工具上面还是连续显示3位相同的数字2
一周热门 更多>