请问STM32L152用内部HSI的串口如何设置

2019-07-14 13:53发布

弄了块 STM32L152 discovery的板子 现在串口发送一直不成功,请大家帮我看看

int main(void)
{
        USART_InitTypeDef USART_InitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;
        
        RCC_HSICmd(ENABLE);
        /*!< Wait till HSI is ready */
        while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
        /* Set HSI as sys clock*/
        RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);


                /* Enable GPIO clock */
                RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
                /* Enable UART clock */
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
                /* Connect PXx to USARTx_Tx */
                GPIO_PinAFConfig(GPIOA, GPIO_Pin_9,GPIO_AF_USART1);
                /* Connect PXx to USARTx_Rx */
                GPIO_PinAFConfig(GPIOA, GPIO_Pin_10,GPIO_AF_USART1);
  
                /* Configure USART Tx as alternate function push-pull */
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
                GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
                GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
                GPIO_Init(GPIOA, &GPIO_InitStructure);

                /* Configure USART Rx as alternate function push-pull */
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
                GPIO_Init(GPIOA, &GPIO_InitStructure);


                USART_InitStructure.USART_BaudRate = 9600;
                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 configuration */
                USART_Init(USART1, &USART_InitStructure);

                /* Enable USART */
                USART_Cmd(USART1, ENABLE);
        
        while(1)
        {
                USART_SendData(USART1, 'A');

                /* Loop until transmit data register is empty */
                while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
                {}
        }
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
8条回答
asd009
2019-07-15 14:32
        RCC_GetClocksFreq(&RCC_Clocks);
        //GPIO端口配置
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        /* Enable GPIO clock */
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
       
        /* Enable USART1 clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
       
        /* Configure USART1 EN as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;        //输出模式
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;        //推挽
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        /* Configure USART Tx as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* Connect PXx to USARTx_Tx */
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource9,GPIO_AF_USART1);

        /* Connect PXx to USARTx_Rx */
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);

        /* USART configuration */
        USART_InitStructure.USART_BaudRate = 9600;
        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(USART1,&USART_InitStructure);
       
        /* Enable the USARTx Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
       
        USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);

一周热门 更多>