打印
[STM32L1]

STM32L151C8T6的串口配置

[复制链接]
783|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
renzheshengui|  楼主 | 2019-6-12 15:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
原文:https://blog.csdn.net/jun2036/article/details/82877844

**STM32L151C8T6的串口配置
    //////////////////////////////////////////////////////////////////
    //加入以下代码,支持printf函数,而不需要选择use MicroLIB         
    #if 1
    #pragma import(__use_no_semihosting)            
    //标准库需要的支持函数                 
    struct __FILE
    {
            int handle;
    };
    FILE __stdout;      
    //定义_sys_exit()以避免使用半主机模式   
    _sys_exit(int x)
    {
            x = x;
    }



使用特权

评论回复
沙发
renzheshengui|  楼主 | 2019-6-12 15:55 | 只看该作者
//重定义fputc函数
    int fputc(int ch, FILE *f)
    {      
        uint16_t CNT=0;
            while((USART1->SR&0X40)==0)//循环发送,直到发送完毕  
        {
            if((CNT++)>60000)//防止异常超时退出
            {
                break;
            }

        }        
        USART1->DR = (u8) ch;

            return ch;
    }
    #endif


使用特权

评论回复
板凳
renzheshengui|  楼主 | 2019-6-12 15:55 | 只看该作者
/**
    * @brief   Uart Init program
    * @param1  USARTx:USART1 USARTUSART2 USART3
    * @param2  bound: USARTx Baud Rate
    * @retval None
    */
    void My_USARTx_Config(USART_TypeDef* USARTx,uint32_t bound)
    {
        //GPIO端口设置
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;

        My_Struct_Clear(USART_InitStructure);
        My_Struct_Clear(NVIC_InitStructure);
            /* Enable the USARTx Pins Software Remapping */       
        if(USARTx == USART1)
        {
            My_Struct_Clear(UART1_Parameter);
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
            RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
        }
        else if(USARTx == USART2)
        {
            My_Struct_Clear(UART2_Parameter);
            RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
            RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
        }
        else if(USARTx == USART3)
        {
            My_Struct_Clear(UART3_Parameter);
            RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
            RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
        }
        if(USARTx == USART1)
        {
            /* Configure USART1 Rx (PA.10) as input floating */       
            My_Struct_Clear(GPIO_InitStructure);
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;          
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;                
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;       
            GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;       
            GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;       
            GPIO_Init(GPIOA, &GPIO_InitStructure);           
            /* Configure USART1 Tx (PA.09) as alternate function push-pull */       
            My_Struct_Clear(GPIO_InitStructure);
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;       
            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_NOPULL;       
            GPIO_Init(GPIOA, &GPIO_InitStructure);   
        }
        else if(USARTx == USART2)
        {
            /* Configure USART2 Rx (PA.03) as input floating */       
            My_Struct_Clear(GPIO_InitStructure);
            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_OD;       
            GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;       
            GPIO_Init(GPIOA, &GPIO_InitStructure);
            /* Configure USART2 Tx (PA.02) as alternate function push-pull */
            My_Struct_Clear(GPIO_InitStructure);        
            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_NOPULL;       
            GPIO_Init(GPIOA, &GPIO_InitStructure);   
        }      
        else if(USARTx == USART3)
        {
            /* Configure USART3 Rx (PB.11) as input floating */       
            My_Struct_Clear(GPIO_InitStructure);
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;          
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;                
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;       
            GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;       
            GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;       
            GPIO_Init(GPIOB, &GPIO_InitStructure);
            /* Configure USART3 Tx (PB.10) as alternate function push-pull */
            My_Struct_Clear(GPIO_InitStructure);        
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;       
            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_NOPULL;       
            GPIO_Init(GPIOB, &GPIO_InitStructure);   
        }   
        /* Enable the USARTx Interrupt */
        if(USARTx == USART1)
        {
            NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        }
        else if(USARTx == USART2)
        {
            NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;       
        }
        else if(USARTx == USART3)
        {
            NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
        }        

        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;       
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;       
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;       
        NVIC_Init(&NVIC_InitStructure);               
        USART_InitStructure.USART_BaudRate = bound;
            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(USARTx, &USART_InitStructure);       
        /* Enable USARTx */       
        USART_Cmd(USARTx, ENABLE);       
        USART_ITConfig(USARTx, USART_IT_RXNE,DISABLE);       
        USART_ITConfig(USARTx, USART_IT_TXE,DISABLE);       
        if(USARTx == USART1)
        {
            GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
            GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
        }
        else if(USARTx == USART2)
        {
            GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
            GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
        }        
        else if(USARTx == USART3)
        {
            GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
            GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);
        }
    }


使用特权

评论回复
地板
renzheshengui|  楼主 | 2019-6-12 15:56 | 只看该作者
/**
    * @brief   USRATx transmit transmit datas or string
    * @param1  USARTx:USART1 USARTUSART2 USART3
    * @param2  Str: Data Addr
    * @retval None
    */
    void USARTx_SendString(USART_TypeDef* USARTx,char * str)
    {
        uint16_t CNT=0;
        while(*str != '\0')// && *str != '\n'
        {
            USARTx->DR = (u8) *str++;
            while((USARTx->SR&0X40)==0)//循环发送,直到发送完毕  
            {
                if((CNT++)>60000)
                {
                    break;
                }
            }        
        }
    }


使用特权

评论回复
5
renzheshengui|  楼主 | 2019-6-12 15:56 | 只看该作者
/**
    * @brief   USRATx transmit transmit datas or string
    * @param1  USARTx:USART1 USARTUSART2 USART3
    * @param2  Str: Data Addr
    * @param3  StrLength: Data Length
    * @retval None
    */
    void USARTx_printf(USART_TypeDef* USARTx,const char *fmt,...)
    {  
        va_list ap;  
        char  string[256];
        My_Struct_Clear(string);
        va_start(ap,fmt);  
        vsprintf(string,fmt,ap);//此处也可以使用sprintf函数,用法差不多,稍加修改即可,此处略去  
        USARTx_SendString(USARTx,string);
        va_end(ap);  
    }**


使用特权

评论回复
6
木木guainv| | 2019-7-4 16:54 | 只看该作者
感谢楼主分享

使用特权

评论回复
7
renzheshengui|  楼主 | 2019-7-5 11:10 | 只看该作者

非常感谢支持

使用特权

评论回复
8
just_2010| | 2020-8-24 15:40 | 只看该作者
学习一下,最近用到

使用特权

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

本版积分规则

78

主题

3858

帖子

2

粉丝