faf 发表于 2015-4-13 16:40:28

STM32L152 用内部HSI 的串口该怎么设置

弄了块 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)
                {}
        }
}

zhangdaijin 发表于 2015-4-14 07:25:50

详见数据手册

发表于 2015-4-13 17:32:53

楼主测试一下IO口有波形输出没,乱码也收不到吗?

拼命三郎 发表于 2015-4-13 18:37:40

拼命三郎 发表于 2015-4-13 18:37:54

faf 发表于 2015-4-13 23:44:49

应该是RCC配置的问题,下面这段如果在DEBUG单步一下一下的走完,后面的直接运行就没问题。如果中间没停直接走,就不行。。有谁用过HSI的么

        RCC_HSICmd(ENABLE);
        while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
       
        RCC_HCLKConfig(RCC_SYSCLK_Div1);
        RCC_PCLK1Config(RCC_HCLK_Div1);
        RCC_PCLK2Config(RCC_HCLK_Div2);
        //FLASH_SetLatency(FLASH_Latency_1);       
//        FLASH_PrefetchBufferCmd(ENABLE);          
        RCC_PLLConfig(RCC_PLLSource_HSI, RCC_PLLMul_3, RCC_PLLDiv_3);
        RCC_PLLCmd(ENABLE);
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) != SET);
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK );
        while((RCC_GetSYSCLKSource()) != 0x0c);
        RCC_GetClocksFreq(&RCC_ClockFreq);

LIUBEIHUA 发表于 2015-4-14 12:33:50

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

faf 发表于 2015-4-15 10:59:22

这个问题解决了,搞不清我用HSI 做时钟源PLL跑不到32MHZ ,只有在16MHZ才行 这是调试可用的USART代码

#include "BSP_USART.h"
#include "stm32l1xx.h"
#include <stdio.h>


/*******************************************************************************
* 函数名                : GPIO_Configuration
* 函数描述            : 设置各GPIO端口功能
* 输入参数      : 无
* 输出结果      : 无
* 返回值      : 无
*******************************************************************************/

static void USART_GPIO_Configuration(void)
{
        /* 定义GPIO初始化结构体 GPIO_InitStructure */
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);   /* Enable GPIO clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);   

        /* 设置USART1的Tx脚(PA.9)为第二功能推挽输出功能 */
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
   // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_Init(GPIOA , &GPIO_InitStructure);
   
        /* 设置USART1的Rx脚(PA.10)为浮空输入脚 */
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
        GPIO_Init(GPIOA , &GPIO_InitStructure);

        //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
}




/*******************************************************************************
* 函数名                : USART_Configuration
* 函数描述            : 设置USART1
* 输入参数      : None
* 输出结果      : None
* 返回值      : None
*******************************************************************************/

static void USART_Configuration(void)
{

        /* 定义USART初始化结构体 USART_InitStructure */
        USART_InitTypeDef USART_InitStructure;
        /* 定义USART初始化结构体 USART_ClockInitStructure */
        USART_ClockInitTypeDefUSART_ClockInitStructure;

        /*        波特率为115200bps;
        *        8位数据长度;
        *        1个停止位,无校验;
        *        禁用硬件流控制;
        *        禁止USART时钟;
        *        时钟极性低;
        *        在第2个边沿捕获数据
        *        最后一位数据的时钟脉冲不从 SCLK 输出;
        */

//        USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
//        USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
//        USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
//        USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
//        USART_ClockInit(USART1 , &USART_ClockInitStructure);

        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);
   
       
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
        USART_ITConfig(USART1, USART_IT_TXE, DISABLE);         
    USART_ClearFlag(USART1,USART_FLAG_TC);
        /* 使能USART1 */
        USART_Cmd(USART1 , ENABLE);
}

/*******************************************************************************
* 函数名                : USART_Init
* 函数描述            : USART初始化
* 输入参数      : None
* 输出结果      : None
* 返回值      : None
*******************************************************************************/
void BSP_USART_Init(void)
{
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);   /* Enable GPIO clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);   
        USART_GPIO_Configuration();
        USART_Configuration();
}

void USART1_IRQHandler(void)
{
        if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)      //??????????
{
                USART_ClearITPendingBit(USART1, USART_IT_TXE);
                USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
        if(USART_GetITStatus(USART1, USART_IT_RXNE)==SET)
        {
                USART_ClearITPendingBit(USART1,USART_IT_RXNE);
                USART_SendData(USART1,USART_ReceiveData(USART1 ));
               
//                USART2_Buffer_Rx = USART_ReceiveData(USART1);
//                if(Usart_Buffer_Size == 7)
//                {
//                        Usart_Buffer_Size = 0;
//                        Usart_Recieve_flag = 1;
//                }         
        }
}

LIUBEIHUA 发表于 2015-4-18 16:14:51

上次遗漏了HSI时钟设置,可以跑到32MHz。
修改system_stm32l1xx.c文件
* @briefConfigures the System clock frequency, AHB/APBx prescalers and Flash
*         settings.
* @note   This function should be called only once the RCC clock configuration
*         is reset to the default reset state (done in SystemInit() function).            
* @paramNone
* @retval None
*/
static void SetSysClock(void)
{
__IO uint32_t StartUpCounter = 0, HSIStatus = 0;

/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
/* Enable HSI 内部时钟使能*/
RCC->CR |= ((uint32_t)RCC_CR_HSION);                //RCC_CR_HSEON替换成RCC_CR_HSION

/* Wait till HSI is ready and if Time out is reached exit */
/* 等待外部时钟就绪,超时推出 */
do
{
    HSIStatus = RCC->CR & RCC_CR_HSIRDY;        //RCC_CR_HSEON替换成RCC_CR_HSION,HSIStatus替换成HSEStatus
    StartUpCounter++;
} while((HSIStatus == 0) && (StartUpCounter != HSI_STARTUP_TIMEOUT));//HSIStatus替换成HSEStatus

if ((RCC->CR & RCC_CR_HSIRDY) != RESET)        //RCC_CR_HSEON替换成RCC_CR_HSION
{
    HSIStatus = (uint32_t)0x01;        //超时置1        //HSEStatus替换成HSIStatus
}
else
{
    HSIStatus = (uint32_t)0x00;                        //HSEStatus替换成HSIStatus
}

if (HSIStatus == (uint32_t)0x01)                //HSEStatus替换成HSIStatus
{
    /* Enable 64-bit access */
    FLASH->ACR |= FLASH_ACR_ACC64;
   
    /* Enable Prefetch Buffer */
    FLASH->ACR |= FLASH_ACR_PRFTEN;

    /* Flash 1 wait state */
    FLASH->ACR |= FLASH_ACR_LATENCY;
   
    /* Power enable */
    RCC->APB1ENR |= RCC_APB1ENR_PWREN;

    /* Select the Voltage Range 1 (1.8 V) */
    PWR->CR = PWR_CR_VOS_0;

    /* Wait Until the Voltage Regulator is ready */
    while((PWR->CSR & PWR_CSR_VOSF) != RESET)
    {
    }
      
    /* HCLK = SYSCLK /1*/
    RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;

    /* PCLK2 = HCLK /1*/
    RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
   
    /* PCLK1 = HCLK /1*/
    RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
   
    /*PLL configuration */
    RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL |RCC_CFGR_PLLDIV));
    RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI | RCC_CFGR_PLLMUL4 | RCC_CFGR_PLLDIV2);                //RCC_CFGR_PLLSRC_HSE替换成RCC_CFGR_PLLSRC_HSI

    /* Enable PLL */
    RCC->CR |= RCC_CR_PLLON;

    /* Wait till PLL is ready */
    while((RCC->CR & RCC_CR_PLLRDY) == 0)
    {
    }
      
    /* Select PLL as system clock source */
    RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
    RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;

    /* Wait till PLL is used as system clock source */
    while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
    {
    }
}
else
{
    /* If HSI fails to start-up, the application will have wrong clock
       configuration. User can add here some code to deal with this error */
}


然后

int main(void)
{
      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);
        while(1)
        {
                //...
        }
}

于平 发表于 2015-5-7 16:37:16

单步没问题而全速不行的,我调试stm32f091的串口时碰到过一次,后来发现是因为全速时有个标志没有判断,导致运行到下一句时上一个字节还没有发送完成,而单步调试时因为每一步时间都足够所以没有问题,供参考。
页: [1]
查看完整版本: STM32L152 用内部HSI 的串口该怎么设置