弄了块
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)
{}
}
}
#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;
// }
}
}
一周热门 更多>