我想用串口2实现自发自收,调试了好久都没成功,看了些例程,配置没错啊,各路大神指导下吧
#include "stm32f4xx.h"
void Usart_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //GPIOAòy½Åê±Öóê1Äü
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //′®¿ú2ê±Öóê1Äü
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //PA2éèÖÃÎa′®¿ú2¸′óÃ
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //PA3éèÖÃÎa′®¿ú2¸′óÃ
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3; //PA2£¬
A3òy½Å3õê¼»ˉ
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF ; //éèÖÃÎaòy½Å¸′óÃ
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz; //òy½ÅËùÂêÎa50MHz
GPIO_InitStructure.GPIO_OType= GPIO_OType_PP ; //íÆíì
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIOAòy½Å3õê¼»ˉ
USART_InitStructure.USART_BaudRate=9600; //2¨ìØÂêéèÖÃÎa9600
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //8λ×Ö3¤
USART_InitStructure.USART_StopBits=USART_StopBits_1 ; //ò»Î»í£Ö1λ
USART_InitStructure.USART_Parity=USART_Parity_No ; //ÎTÆæżD£Ñé
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //ÎTó2¼tá÷
USART_Init(USART2,&USART_InitStructure); //′®¿ú3õê¼»ˉ
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn; //ÖD¶Ïí¨μàÎa′®¿ú2
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //ÖD¶Ïí¨μàê1Äü
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; //ÖD¶ÏÇàÕ¼óÅÏ輶Îa1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1; //ÖD¶ÏÏìó|óÅÏ輶Îa1
NVIC_Init(&NVIC_InitStructure); //ÖD¶Ï3õê¼»ˉoˉêy
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //′®¿úÖD¶Ïê1Äüoˉêy
USART_Cmd(USART2,ENABLE); //′®¿ú2ê1Äü
}
void USART2_IRQHandler(void) //ÖD¶Ï·tÎñoˉêy
{
u8 res;
if(USART_GetITStatus(USART2,USART_IT_RXNE))
{
res=USART_ReceiveData(USART2);
USART_SendData(USART2,res);
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Usart_Init();
while(1);
}
谢谢你的指导,我知道哪错了,刚调试成功,谢谢
一周热门 更多>