本帖最后由 xsophiax 于 2015-9-1 15:30 编辑
STM32F429I - USART2怎么Rx/Tx对接也不通呢,在TX端也抓不对输出信号。
GPIOA-(PA2-PA3)void NVIC2_Config()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART2_Gpio_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE);
//PA2->TX PA3->Rx
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
}
void USART2_Config(void)
{
USART2_Gpio_Config();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
USART_InitTypeDef USART_InitStructure;
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(USART2,&USART_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_Cmd(USART2,ENABLE);
}
void USART2_IRQHandler(void)
{
char c;
if(USART_GetFlagStatus(USART2,USART_FLAG_RXNE)!= RESET)
{
c = USART_ReceiveData(USART2);
USART_SendData(USART2,c);
}
}
void USART2_Send(void)
{
while(1)
{
USART_SendData(USART2,0x55);
while(RESET == USART_GetFlagStatus(USART2,USART_FLAG_TC));
USART_SendData(USART2,0xAA);
while(RESET == USART_GetFlagStatus(USART2,USART_FLAG_TC));
Delay_us(1000);
}
}
|