弄了块 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) {} } } |
STM32L151RDT6 SDIO读写SD卡 FIFO下溢错误,不知是什么原因
STM32L152 RTC唤醒STOP mode问题
STM32L152系列自带LCD驱动段码液晶屏,怎么驱动想要亮的段亮
STM32L151为何FLASH擦除后全是是0x00?
求助!!STM32L151中的RTC唤醒(wake up)中断问题
STM32L152普通I/O模拟SPI驱动W25Q16问题
STM32L152开发板原理图及说明文档
STM32L1系列,如何释放PA15为普通GPIO端口
STM32L151读取W25Q16的ID错误,请帮忙看看是什么原因
STM32L151 STOP模式唤醒程序跑哪儿了
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);
//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);
#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_ClockInitTypeDef USART_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_Buffer_Size++] = USART_ReceiveData(USART1);
// if(Usart_Buffer_Size == 7)
// {
// Usart_Buffer_Size = 0;
// Usart_Recieve_flag = 1;
// }
}
}
修改system_stm32l1xx.c文件
* @brief Configures 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).
* @param None
* @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)
{
//...
}
}