代码如下int main(void){
USART4_Config();
GPIO_ResetBits(GPIOC,GPIO_Pin_12);
NVIC_Config_UART4();
while(1);
}
void USART4_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* config USART1 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* Configure USART1 Tx (PC.10) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure USART1 Rx (PC.11) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure USART1 TX_EN (PC.12) 发送1 接收0 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //12
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &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(UART4, &USART_InitStructure);
USART_ClearFlag(UART4, USART_FLAG_TC);
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
USART_ITConfig(UART4, USART_IT_TXE, ENABLE);
USART_Cmd(UART4, ENABLE);
} void NVIC_Config_UART4(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void UART4_IRQHandler(void)
{
if(USART_GetITStatus(UART4,USART_IT_RXNE)!=RESET)
{
USART_SendData(UART4,0xAA);
USART_ClearITPendingBit(UART4,USART_IT_RXNE);
}
}
开始我是想利用中断收到什么发送什么,但是没能实现,后面就想实现直接发0XAA行不行,结果还是收不到,请大家帮我看看。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
int main(void)
{
USART4_Config();
GPIO_ResetBits(GPIOC,GPIO_Pin_12);
//NVIC_Config_UART4();
while(1)
{
LED_Delay(880000);
GPIO_SetBits(GPIOC,GPIO_Pin_12);
UART4SendByte(Data[0]);
while (USART_GetFlagStatus(UART4,USART_FLAG_TC) !=SET);
LED_Delay(880000);
GPIO_ResetBits(GPIOC,GPIO_Pin_12);
LED_Delay(880000);
}
}
如上主函数所示,如果我注释掉了中断,那么串口助手可以收到单片机发过来的数据,但是我开启中断了之后,能收到PC发给单片机的数据,但是WHILE(1)的循环就不能正常发送了。请指教。
一周热门 更多>