STM32F407的UART串口初始化
原标题:STM32F407的UART串口初始化
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
软件环境:MDK4.7a
实现的功能:1、串口初始化,
2、通过串口发送数据
3、中断方式接收数据,并将接收到的数据回送。
使用库函数操作
首先,配置NVIC使用NVIC_PriorityGroupConfig()设置优先级分组,使用NVIC_Init()对NVIC进行初始化
本文引用地址: http://www.21ic.com/app/mcu/201812/783926.htm
void NVIC_Config()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
第二步:配置引脚功能,因为我的板子上PA9被用来驱动LED了,所以只能将将串口映射到PB6,PB7。这个设置跟F1系列的有点差别。首先初始化端口时钟,第二配置端口引脚功能,第三不设置功能映射将串口连接到引脚。
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_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);
}
第三步:配置串口工作方式。步骤也差不多:打开时钟,用Init函数初始化串口,设置串口的接收中断,最后别忘了使能串口。
void USART_Config(void)
{
USART_Gpio_Config();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_InitStructure.USART_BaudRate = 115200;
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_Cmd(USART1,ENABLE);
}
第四步:添加串口中断函数,函数名是固定的:void USART1_IRQHandler(void)。中断进入时候,先判断接收寄存器是否有数据,有数据时候就接收,然后使用USART_SendData()将数据发回
void USART1_IRQHandler(void)
{
char c;
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET)
{
c = USART_ReceiveData(USART1);
USART_SendData(USART1,c);
}
//while(1);
}
最后就是main了,没啥可说的
int main(void)
{
NVIC_Config();
USART_Config();
while(1)
{
while(RESET == USART_GetFlagStatus(USART1,USART_FLAG_TXE));
USART_SendData(USART1,‘b’);
while(RESET == USART_GetFlagStatus(USART1,USART_FLAG_TXE));
USART_SendData(USART1,‘a’);
delay_ms(1000);
}
}
【STM32F407】
STM32F407/417系列面向需要在小至10 x 10 mm的封装内实现高集成度、高性能、嵌入式存储器和外设的医疗、工业与消费类应用。
STM32F407/417提供了工作频率为168 MHz的Cortex™-M4内核(具有浮点单元)的性能。
性能:在168 MHz频率下,从Flash存储器执行时,STM32F407/417能够提供210 DMIPS/566 CoreMark性能,并且利用意法半导体的ART加速器实现了FLASH零等待状态。DSP指令和浮点单元扩大了产品的应用范围。
功效:该系列产品采用意法半导体90 nm工艺和ART加速器,具有动态功耗调整功能,能够在运行模式下和从Flash存储器执行时实现低至238 µA/MHz的电流消耗(@ 168 MHz)。
丰富的连接功能:出色的创新型外设:与STM32F4x5系列相比,STM32F407/417产品还具有符合IEEE 1588 v2标准要求的以太网MAC10/100和能够连接CMOS照相机传感器的8~14位并行照相机接口。
2个USB OTG(其中一个支持HS)
音频:专用音频PLL和2个全双工I²S
通信接口多达15个(包括6个速度高达11.25 Mb/s的USART、3个速度高达45 Mb/s的SPI、3个I²C、2个CAN和1个SDIO)
模拟:2个12位DAC、3个速度为2.4 MSPS或7.2 MSPS(交错模式)的12位ADC
定时器多达17个:频率高达168 MHz的16和32位定时器
可以利用支持Compact Flash、SRAM、PSRAM、NOR和NAND存储器的灵活静态存储器控制器轻松扩展存储容量
基于模拟电子技术的真随机数发生器
STM32F417还整合了加密/HASH处理器,为AES 128、192、256、3DES和HASH(MD5、SHA-1)实现了硬件加速。
集成:STM32F417x系列具有512KB(仅限于WLCSP90封装)~1MB Flash和192KB SRAM,采用尺寸小至4 x 4.2 mm的64~144引脚封装。
STM32F407/417产品系列具有512KB~1MB Flash和192KB SRAM,采用尺寸小至10 x 10 mm的100~
176引脚封装。
STM32F407VG
High-performance foundation line, ARM Cortex-M4 core with DSP and FPU, 1 Mbyte Flash, 168 MHz CPU, ART Accelerator, Ethernet, FSMC
The STM32F405xx and STM32F407xx family is based on the high-performance ARM®Cortex®-M4 32-bit RISC core operating at a frequency of up to 168 MHz. The Cortex-M4 core features a Floating point unit (FPU) single precision which supports all ARM single-precision data-processing instructions and data types. It also implements a full set of DSP instructions and a memory protection unit (MPU) which enhances application security.
The STM32F405xx and STM32F407xx family incorporates high-speed embedded memories (Flash memory up to 1 Mbyte, up to 192 Kbytes of SRAM), up to 4 Kbytes of backup SRAM, and an extensive range of enhanced I/Os and peripherals connected to two APB buses, three AHB buses and a 32-bit multi-AHB bus matrix.
All devices offer three 12-bit ADCs, two DACs, a low-power RTC, twelve general-purpose 16-bit timers including two PWM timers for motor control, two general-purpose 32-bit timers. a true random number generator (RNG). They also feature standard and advanced communication interfaces.
Key Features
Core: ARM®32-bit Cortex®-M4 CPU with FPU, Adaptive real-time accelerator (ART Accelerator™) allowing 0-wait state execution from Flash memory, frequency up to 168 MHz, memory protection unit, 210 DMIPS/1.25 DMIPS/MHz (Dhrystone 2.1), and DSP instructions
Memories
Up to 1 Mbyte of Flash memory
Up to 192+4 Kbytes of SRAM including 64-Kbyte of CCM (core coupled memory) data RAM
Flexible static memory controller supporting Compact Flash, SRAM, PSRAM, NOR and NAND memories
LCD parallel interface, 8080/6800 modes
Clock, reset and supply management
1.8 V to 3.6 V application supply and I/Os
POR, PDR, PVD and BOR
4-to-26 MHz crystal oscillator
Internal 16 MHz factory-trimmed RC (1% accuracy)
32 kHz oscillator for RTC with calibration
Internal 32 kHz RC with calibration
Sleep, Stop and Standby modes
VBATsupply for RTC, 20×32 bit backup registers + optional 4 KB backup SRAM
3×12-bit, 2.4 MSPS A/D converters: up to 24 channels and 7.2 MSPS in triple interleaved mode
2×12-bit D/A converters
General-purpose DMA: 16-stream DMA controller with FIFOs and burst support
Up to 17 timers: up to twelve 16-bit and two 32-bit timers up to 168 MHz, each with up to 4 IC/OC/PWM or pulse counter and quadrature (incremental) encoder input
Debug mode
Serial wire debug (SWD) & JTAG interfaces
Cortex-M4 Embedded Trace Macrocell™
Up to 140 I/O ports with interrupt capability
Up to 136 fast I/Os up to 84 MHz
Up to 138 5 V-tolerant I/Os
Up to 15 communication interfaces
Up to 3 × I2C interfaces (SMBus/PMBus)
Up to 4 USARTs/2 UARTs (10.5 Mbit/s, ISO 7816 interface, LIN, IrDA, modem control)
Up to 3 SPIs (42 Mbits/s), 2 with muxed full-duplex I2S to achieve audio class accuracy via internal audio PLL or external clock
2 × CAN interfaces (2.0B Active)
SDIO interface
Advanced connectivity
USB 2.0 full-speed device/host/OTG controller with on-chip PHY
USB 2.0 high-speed/full-speed device/host/OTG controller with dedicated DMA, on-chip full-speed PHY and ULPI
10/100 Ethernet MAC with dedicated DMA: supports IEEE 1588v2 hardware, MII/RMII
8- to 14-bit parallel camera interface up to 54 Mbytes/s
True random number generator
CRC calculation unit
96-bit unique ID
RTC: subsecond accuracy, hardware calendar
责任编辑:David
【免责声明】
1、本文内容、数据、图表等来源于网络引用或其他公开资料,版权归属原作者、原发表出处。若版权所有方对本文的引用持有异议,请联系拍明芯城(marketing@iczoom.com),本方将及时处理。
2、本文的引用仅供读者交流学习使用,不涉及商业目的。
3、本文内容仅代表作者观点,拍明芯城不对内容的准确性、可靠性或完整性提供明示或暗示的保证。读者阅读本文后做出的决定或行为,是基于自主意愿和独立判断做出的,请读者明确相关结果。
4、如需转载本方拥有版权的文章,请联系拍明芯城(marketing@iczoom.com)注明“转载原因”。未经允许私自转载拍明芯城将保留追究其法律责任的权利。
拍明芯城拥有对此声明的最终解释权。