STM32L151C8T6的串口配置

**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; 
    } 
    //重定义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 
    /**
    * @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);
        } 
    }
    /**
    * @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;
                }
            }        
        }
    }
    /**
    * @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);  
    }**  
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jun2036

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值