打印

GD32芯片的USART1初始化代码(转载)

[复制链接]
1379|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
木木guainv|  楼主 | 2018-10-26 15:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
void usart_init(void)
{
/* Enable USART2 APB clock */
        RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_USART1, ENABLE);
        /* USART1 Pins configuration **************************************************/
        {
                /* Configure the GPIO ports */
                GPIO_InitPara  GPIO_InitStructure;

                /* Connect pin to Periph */
                GPIO_PinAFConfig(GPIOA , GPIO_PINSOURCE9, GPIO_AF_1);
                GPIO_PinAFConfig(GPIOA , GPIO_PINSOURCE10, GPIO_AF_1);

                /* Configure pins as AF pushpull */
                GPIO_InitStructure.GPIO_Pin     = GPIO_PIN_9 | GPIO_PIN_10;
                GPIO_InitStructure.GPIO_Mode    = GPIO_MODE_AF;
                GPIO_InitStructure.GPIO_Speed   = GPIO_SPEED_50MHZ;
                GPIO_InitStructure.GPIO_OType   = GPIO_OTYPE_PP;
                GPIO_InitStructure.GPIO_PuPd    = GPIO_PUPD_NOPULL;
                GPIO_Init(GPIOA , &GPIO_InitStructure);
        } //GPIO setting

               
        {
                USART_InitPara  USART_InitStructure;

                USART_DeInit(USART1);
                USART_InitStructure.USART_BRR =9600;
                USART_InitStructure.USART_WL                  = USART_WL_8B;
                USART_InitStructure.USART_STBits              = USART_STBITS_1;
                USART_InitStructure.USART_Parity              = USART_PARITY_RESET;
                USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
                USART_InitStructure.USART_RxorTx              = USART_RXORTX_RX | USART_RXORTX_TX;
                USART_Init(USART1, &USART_InitStructure);
        }
        {
                NVIC_InitPara  NVIC_InitStructure;

                /* Configure the NVIC Preemption Priority Bits */
                /* Configure the preemption priority and subpriority:
                   - 1 bits for pre-emption priority: possible value are 0 or 1
                   - 3 bits for subpriority: possible value are 0..7
                   - Lower values gives higher priority
                 */

                /* Re-configure and enable USART interrupt to have the higher priority */
                NVIC_InitStructure.NVIC_IRQ                                                = USART1_IRQn;
                NVIC_InitStructure.NVIC_IRQPreemptPriority                = 5;
                NVIC_InitStructure.NVIC_IRQSubPriority                        = 1;
                NVIC_InitStructure.NVIC_IRQEnable                                = ENABLE;
                NVIC_Init(&NVIC_InitStructure);
        } //NVIC  usart2 setting
        /* Enable the USART1 Receive interrupt */
        USART_INT_Set(USART1, USART_INT_RBNE, ENABLE);
        /* USART enable */
        USART_Enable(USART1, ENABLE);
       
}


沙发
木木guainv|  楼主 | 2018-10-26 15:39 | 只看该作者
//发送单字节
void usart_transmit( unsigned char data )
{
USART_ClearBitState (USART1,USART_FLAG_TC);
USART_DataSend(USART1,data);
while(USART_GetBitState(USART1,USART_FLAG_TC)==RESET);
USART_ClearBitState (USART1,USART_FLAG_TC);
}


使用特权

评论回复
板凳
木木guainv|  楼主 | 2018-10-26 15:39 | 只看该作者
//发送字符串
void usart_send_string(char *str)
{
while(*str)
{
usart_transmit(*str) ;
str++;
}
}


使用特权

评论回复
地板
liuqiangdong| | 2018-10-30 12:05 | 只看该作者
void usart_init(void) { /* Enable USART2 APB clock */         RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_USART1, ENABLE);         /* USART1 Pins configuration

使用特权

评论回复
5
木木guainv|  楼主 | 2018-11-5 17:19 | 只看该作者
请问这个有什么问题吗

使用特权

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

本版积分规则

134

主题

3870

帖子

5

粉丝