求一个基于STM32固件库的串口通讯(有收发的)实例

2019-03-23 18:45发布

如题 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
lzhitwh
1楼-- · 2019-03-24 04:02
/ 过来看看,有没有好的例程
29447945
2楼-- · 2019-03-24 04:42
void Usart_Init(void)
{
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;
       
        USART_DeInit(USART2);
       
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
       
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
       
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        USART_InitStructure.USART_BaudRate = 9600;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_Init(USART2,&USART_InitStructure);
       
        NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
        NVIC_Init(&NVIC_InitStructure);
       
        USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
       
        USART_Cmd(USART2,ENABLE);
}
void USART2_IRQHandler(void)
{
        uint16_t rx,state=0;
        if(USART_GetITStatus(USART2,USART_IT_RXNE))
        {
                USART_ClearITPendingBit(USART2,USART_IT_RXNE);
                rx=USART_ReceiveData(USART2);
                USART_SendData(USART2,rx);
}
}
stm32f072rbt芯片的usart2,引脚是PA2/3引脚。

一周热门 更多>