STM32F407xx内嵌四个通用同步/异步接收器(USART1,USART2,USART3 和USART6)和两个通用异步收发器(UART4和UART5)。这6个接口提供异步通信的IrDASIR ENDEC支持,多机通信模式,单线半双工通信模式LIN主/从功能。 USART1和USART6接口能够速度高达10.5 Mbit / s的通信其他可用的接口通信高达5.25bit/s。USART1,USART2,USART3和USART6还提供硬件管理的CTS,RTS信号,智能卡的模式(ISO7816兼容)和类似的SPI通信能力。所有接口都可以通过DMA控制器
这里只使用了两根线的最简单串口设置。
硬件环境:STM32F4-Discovery
软件环境:IAR6.6 for arm
实现的功能:
1、串口初始化,
2、通过串口发送数据
用的GPIO是PB6,PB7
下面是官方给的库的使用说明
===================================================================
* How to usethis driver
* ===================================================================
* 1. Enable peripheral clock using thefollwoing functions
* RCC_APB2PeriphClockCmd(RCC_APB2Periph_USARTx, ENABLE) for USART1 andUSART6
* RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx,ENABLE) for USART2, USART3, UART4 or UART5.
*
* 2. According to the USART mode, enable the GPIO clocks using
* RCC_AHB1PeriphClockCmd()function. (The I/O can be TX, RX, CTS,
* or/and SCLK).
*
* 3. Peripheral's alternate function:
* - Connect the pin to thedesired peripherals' Alternate
* Function (AF) usingGPIO_PinAFConfig() function
* - Configure the desired pin inalternate function by:
* GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
* - Select the type,pull-up/pull-down and output speed via
* GPIO_PuPd, GPIO_OType andGPIO_Speed members
* - Call GPIO_Init() function
*
* 4. Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
* flow control andMode(Receiver/Transmitter) using the USART_Init()
* function.
*
* 5. For synchronous mode, enable theclock and program the polarity,
* phase and last bit using theUSART_ClockInit() function.
*
* 5. Enable the NVIC and thecorresponding interrupt using the function
* USART_ITConfig() if you need to useinterrupt mode.
*
* 6. When using the DMA mode
* - Configure the DMA usingDMA_Init() function
* - Active the needed channelRequest using USART_DMACmd() function
*
* 7. Enable the USART using theUSART_Cmd() function.
*
* 8. Enable the DMA using the DMA_Cmd()function, when using DMA mode.
*
* Refer to Multi-Processor,LIN, half-duplex, Smartcard, IrDA sub-sections
* for more details
*
* In order to reach highercommunication baudrates, it is possible to
* enable the oversampling by 8 modeusing the function USART_OverSampling8Cmd().
* This function should be called afterenabling the USART clock (RCC_APBxPeriphClockCmd())
* and before calling the functionUSART_Init().
*
设置步骤:
1 使能USART相关时钟。
2.使能对应GPIO的时钟。
3.配置对应引脚的复用功能
4.配置USART信息
5.发送
前4步已经封装到对应的配置函数中:
/*********************************************************************************************************************************************************
* Function name : USART_Config()
* Descriptions : --初始化串口基本配置
* input parameters : 无
* output parameters: 无
* Returned value : 无
* author : 2014/5/2, by KingSong
***********************************************************************************************************************************************************/
void USART_Config()
{
USART_InitTypeDefUSART_InitStructure; // 初始化串口1用结构体
USART_DeInit(USART1); //初始化串口1名称 复位RCC时钟值
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //初始化串口1的时钟
USART_Gpio_Config(); //端口映射映射 PA6 PA7
USART_StructInit(&USART_InitStructure); //初始化串口1的基本数据 8bit 无校验 停止位一位
USART_Init(USART1,&USART_InitStructure); //写入串口1配置信息
USART_Cmd(USART1,ENABLE); //使能串口1模块
USART_ClearFlag(USART1,USART_FLAG_TC); //清除发送标志位
}
/*********************************************************************************************************************************************************
* Function name : USART_Gpio_Config()
* Descriptions : --串口端口映射
* input parameters : 无
* output parameters: 无
* Returned value : 无
* author : 2014/5/2, by KingSong
***********************************************************************************************************************************************************/
void USART_Gpio_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB ,ENABLE);
//PB6->TX PB7->Rx
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 |GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);
}
完整的程序如下
#include<my_config.h>
#include "delay.h"
#include "misc.h"
USART_InitTypeDefUSART_InitStructure;
void main ()
{
// NVIC_Config();
USART_Config();
// NVIC_Init();
LED_Init();
while(1)
{
if(USART_ReceiveData(USART1))
{
LEDOn(LED1);
}
delay_ms(100);
USART_SendData(USART1,0xff);
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and thesource line number
* where the assert_param error hasoccurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his ownimplementation to report the file name and line number,
ex: printf("Wrongparameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/******************* *****END OFFILE****/