历史上的今天
返回首页

历史上的今天

今天是:2025年01月29日(星期三)

2019年01月29日 | STM32L151C8T6的串口配置

2019-01-29 来源:eefocus

**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);  

    }** 


推荐阅读

史海拾趣

AirBorn公司的发展小趣事

随着电子行业的竞争日益激烈,AirBorn面临着来自同行的压力和挑战。为了保持领先地位,AirBorn加强了与客户的沟通与合作,深入了解市场需求和变化趋势。同时,公司还加大了对新技术和新材料的研发投入,不断推出具有创新性和竞争力的新产品。这些举措使得AirBorn在激烈的市场竞争中保持了稳健的发展态势。

成都芯进(CrossChip)公司的发展小趣事

2023年6月,成都芯进电子宣布完成超1亿元A轮融资。这一轮融资的成功,不仅为公司的发展提供了充足的资金保障,也吸引了更多知名产业机构和投资基金的关注。公司借此机会扩大了研发团队和生产规模,进一步提升了产品的研发和生产能力。

GETEDZ ( HVGT)公司的发展小趣事
要仔细分析电器设备之间的动作关系,确保联锁逻辑的正确性。
柯爱亚(ceaiya)公司的发展小趣事

作为一家有社会责任感的企业,柯爱亚积极参与各种公益活动。公司不仅捐款捐物支持灾区重建、教育事业等公益事业,还组织员工参与志愿者活动,为社会做出了积极贡献。这些行为展现了柯爱亚作为一家优秀企业的社会担当和良好形象。

请注意,以上故事框架仅供参考,具体内容需要根据柯爱亚公司的实际发展历程进行编写。

启珑(CHIPLON)公司的发展小趣事

在稳固国内市场的同时,柯爱亚积极寻求海外市场的发展机会。公司不仅参加了多个国际电子展览会,还与国际知名企业建立了合作关系。此外,柯爱亚还通过投资、并购等方式,不断拓展业务范围,实现了多元化发展。这些举措使柯爱亚在国际市场上的影响力逐渐增强。

ALTERA(阿尔特拉)公司的发展小趣事

柯爱亚一直秉承“持续改进,满足客户要求,不断创新,超越客户期望”的品质方针。公司严格把控产品质量,从原材料采购到生产流程,再到产品检测,每一个环节都力求精益求精。这种对品质的坚持赢得了客户的信任,柯爱亚的产品逐渐在市场上树立了良好的口碑。

问答坊 | AI 解惑

FPGA高手经验谈

FPGA高手经验谈 …

查看全部问答>

早就想参加飞思卡尔技术论坛,这次终于有机会了

做芯片这行好几年,各种技术会议也参加了不少,只是飞思卡尔的技术论坛还没有去过。一直想参加,毕竟属于行业的老大啊!可是哪有那么容易! 今年的飞思卡尔大学生智能车赛开办了一个博客大赛,参与投票的工程师就有机会参加技术论坛。赶紧去投 ...…

查看全部问答>

关于Stellaris系列UART的FIFO新认识!

【经典提问】   有没有办法去掉UART的FIFO,或者怎样才能实现每收发一个字符就中断一次呢?Stellaris系列ARM的UART好像无法做到这一点,存在缺陷! 【精妙解答】   误解了,根本就不是缺陷,这恰恰是优点!   看来许多人还没有真正理解FI ...…

查看全部问答>

PB编译项疑问??

请问大家这个PB的编译选择项有什么不同么?之前我编译的时候,修改了一点就要重新编译,一等就是一个小时。我在网上找到一个快速编译的方法,可是这好像只是针对修改了的驱动而言。 所以我有几个疑问,还望大家能帮我解答。 如果我只是增加驱动, ...…

查看全部问答>

c置位代码,大家解释解释。

#define bwMCDR2_ADDRESS 4 #define bsMCDR2_ADDRESS 17 #define bmMCDR2_ADDRESS BIT_MASK(MCDR2_ADDRESS) #define BIT_MASK(__bf) (((1U …

查看全部问答>

采用多进程还是多线程?

高压电线的巡检机器人,主要并行执行三个任务: 1.在电线上自主行走; 2.不断的摄像 3.并把摄像的数据传给地面的基站. 用多进程,还是多线程比较合适?有以下三种意见: (1)用三个线程; (2)第一个任务,作为一个进程.第二和第三个任务作为两个线程放 ...…

查看全部问答>

有趣的涂鸦轨道车 随着你的笔尖开动

在八零后的集体记忆里,玻璃珠拍纸片、一款四驱车就征服了一代人。这辆高科技的涂鸦轨道车比四驱车更加妙不可言。它不像其他的远程遥控小车,必须放在固定的玩具轨道里或者用遥控器控制。只要用一个黑色记号笔画出你想要的路途,小车里的光学感应器 ...…

查看全部问答>