[STM32L1]

STM32L151 usart2 串口2 程序

[复制链接]
685|5
手机看帖
扫描二维码
随时随地手机跟帖
renzheshengui|  楼主 | 2019-6-12 15:57 | 显示全部楼层 |阅读模式
原文:https://blog.csdn.net/aixin2012/article/details/78518225

void USART2_config(unsigned int BaudRate)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        USART_InitTypeDef USART_InitStructure;

        /* Enable the USART2 Pins Software Remapping */
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

        /* Configure USART2 Rx (PA.03) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;   
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;        
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* Configure USART2 Tx (PA.02) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* Enable the USART2 Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);   

        USART_InitStructure.USART_BaudRate = BaudRate;
        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_ITConfig(USART2, USART_IT_TXE, ENABLE);
        /* Enable USART2 */
        USART_Cmd(USART2, ENABLE);
               
        USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
               
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
}


使用特权

评论回复
renzheshengui|  楼主 | 2019-6-12 15:57 | 显示全部楼层
void USART2_IRQHandler(void)
{
        unsigned char temp=0;
       
        if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
        {  
                temp=USART_ReceiveData(USART2);
                USART_ClearFlag(USART2,USART_FLAG_RXNE);
               
                usart2_race_buf[usart2_race_count] = temp;
                usart2_race_count++;
                if(usart2_race_count>=2)
                {
                        usart2_race_count = 0;
                        usart2_race_over = 1;
                }
        }  
}


使用特权

评论回复
renzheshengui|  楼主 | 2019-6-12 15:58 | 显示全部楼层
void USART2_send_all(unsigned char *byte,unsigned char send_mun)
{
        unsigned char i;

        for(i=0;i<send_mun;i++)
        {
                USART_SendData(USART2,*byte);
                byte++;
               
                while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){}
        }
}


使用特权

评论回复
木木guainv| | 2019-7-4 16:54 | 显示全部楼层
非常不错的代码

使用特权

评论回复
renzheshengui|  楼主 | 2019-7-5 11:09 | 显示全部楼层

非常感谢支持

使用特权

评论回复
just_2010| | 2020-8-24 15:41 | 显示全部楼层
学习一下,最近用到

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

78

主题

3858

帖子

2

粉丝