stm32使用库函数编写USART还是很方便的,现在转几个例子:
/*************************************** 转载请注明出处:tedeum.iteye.com ****************************************/
首先是不使用中断的方法使用usart1,管脚pa9,pa10,此方法已在f3discovery上验证通过,来源:https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2Fusart%20code&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=524
// STM32 USART1 (Tx PA.9, Rx PA.10) STM32F3-Discovery - sourcer32@gmail.com #include "stm32f30x.h" /**************************************************************************************/ void RCC_Configuration(void) { /* Enable GPIO clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Enable USART clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); } /**************************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Connect PA9 to USART1_Tx */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7); /* Connect PA10 to USART1_Rx */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7); /* 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_50MHz; 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); } /**************************************************************************************/ void USART1_Configuration(void) { USART_InitTypeDef USART_InitStructure; /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/ /* USART configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ 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 configuration */ USART_Init(USART1, &USART_InitStructure); /* Enable USART */ USART_Cmd(USART1, ENABLE); } /**************************************************************************************/ int main(void) { RCC_Configuration(); GPIO_Configuration(); USART1_Configuration(); while(1) { while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty USART_SendData(USART1, 0x49); // Send 'I' } while(1); // Don't want to exit } /**************************************************************************************/ #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @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 own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */ while (1) { } } #endif
接下来是使用中断的方法,使用USART3,管脚pd8,pd9,来源:https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32F4%20USART%20receive%20problem&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=124
// STM32 USART IRQ TX/RX Loop (USART3 Tx PD.8, Rx PD.9) STM32F4 Discovery - sourcer32@gmail.com #include "stm32f4_discovery.h" volatile char StringLoop[] = "The quick brown fox jumps over the lazy dog\r\n"; /**************************************************************************************/ void RCC_Configuration(void) { /* --------------------------- System Clocks Configuration -----------------*/ /* USART3 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); /* GPIOD clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); } /**************************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /*-------------------------- GPIO Configuration ----------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Connect USART pins to AF */ GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); } /**************************************************************************************/ void USART3_Configuration(void) { USART_InitTypeDef USART_InitStructure; /* USARTx configuration ------------------------------------------------------*/ /* USARTx configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ 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(USART3, &USART_InitStructure); USART_Cmd(USART3, ENABLE); USART_ITConfig(USART3, USART_IT_TXE, ENABLE); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); } /**************************************************************************************/ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Enable the USART3 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } /**************************************************************************************/ void USART3_IRQHandler(void) { static int tx_index = 0; static int rx_index = 0; if (USART_GetITStatus(USART3, USART_IT_TXE) != RESET) // Transmit the string in a loop { USART_SendData(USART3, StringLoop[tx_index++]); if (tx_index >= (sizeof(StringLoop) - 1)) tx_index = 0; } if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) // Received characters modify string { StringLoop[rx_index++] = USART_ReceiveData(USART3); if (rx_index >= (sizeof(StringLoop) - 1)) rx_index = 0; } } /**************************************************************************************/ int main(void) { RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); USART3_Configuration(); while(1); // Don't want to exit } /**************************************************************************************/
最后,是使用DMA的方法,使用usart5,管脚:pc12,pd2,来源:https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/DMA%20Memory%20To%20UART5&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=760
// STM32 UART5 DMA TX (Tx PC.12, Rx PD.2) STM32F4 Discovery - sourcer32@gmail.com #include "stm32f4_discovery.h" /**************************************************************************************/ void RCC_Configuration(void) { /* --------------------------- System Clocks Configuration -----------------*/ /* UART5 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); /* GPIOC and GPIOD clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE); /* DMA1 clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); } /**************************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /*-------------------------- GPIO Configuration ----------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; // PC.12 UART5_TX, potential clash SDIN CS43L22 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // PD.2 UART5_RX GPIO_Init(GPIOD, &GPIO_InitStructure); /* Connect USART pins to AF */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5); GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5); } /**************************************************************************************/ void UART5_Configuration(void) { USART_InitTypeDef USART_InitStructure; /* USARTx configuration ------------------------------------------------------*/ /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ 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(UART5, &USART_InitStructure); USART_Cmd(UART5, ENABLE); } /**************************************************************************************/ char Buffer[] = "The quick brown fox jumps over the lazy dog\r\n"; void DMA_Configuration(void) { DMA_InitTypeDef DMA_InitStructure; DMA_DeInit(DMA1_Stream7); DMA_InitStructure.DMA_Channel = DMA_Channel_4; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; // Transmit DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Buffer; DMA_InitStructure.DMA_BufferSize = (uint16_t)sizeof(Buffer) - 1; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&UART5->DR; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream7, &DMA_InitStructure); /* Enable the USART Tx DMA request */ USART_DMACmd(UART5, USART_DMAReq_Tx, ENABLE); /* Enable DMA Stream Transfer Complete interrupt */ DMA_ITConfig(DMA1_Stream7, DMA_IT_TC, ENABLE); /* Enable the DMA RX Stream */ DMA_Cmd(DMA1_Stream7, ENABLE); } /**************************************************************************************/ void DMA1_Stream7_IRQHandler(void) { /* Test on DMA Stream Transfer Complete interrupt */ if (DMA_GetITStatus(DMA1_Stream7, DMA_IT_TCIF7)) { /* Clear DMA Stream Transfer Complete interrupt pending bit */ DMA_ClearITPendingBit(DMA1_Stream7, DMA_IT_TCIF7); } } /**************************************************************************************/ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Configure the Priority Group to 2 bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* Enable the UART5 RX DMA Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream7_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } /**************************************************************************************/ int main(void) { RCC_Configuration(); NVIC_Configuration(); GPIO_Configuration(); UART5_Configuration(); DMA_Configuration(); while(1); // Don't want to exit } /**************************************************************************************/ #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @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 own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */ while (1) { } } #endif /** * @} */ /**************************************************************************************/
相关推荐
stm32 的usart模块使用方便,在很多地方都有应用
stm32串口的多个例子,适用于开发人员的学习,很详细的,很好用
STM32 开发例程 初学者使用 开发环境为IARv4.42
STM32F103 usart串口通信实例,想学习的可以下载。。
单片机串口寄存器使用例子。简单、实用。适用于初学者。希望对想学习STM32的同学有帮助。
STM32 串口转CAN 代码例程 含 1.STM32_LED ,2.STM32_USART,3.STM32_USART_LED_CAN500K,等几个例子程序,方便新手入门。 并含有C#上位机程序,,通过UART串口转CAN 发送接收数据。
基于stm32单片机的RS232串口通讯的代码例子。
软件一行,经常需要用到很多重复的代码,我们有必要花...每个驱动的使用都配有单独的keil工程的例子: 每个工程都支持四个目标: 有相关开发板的话可以直接用来测试。 核心代码放在 00-STM32F429_LIBRARIES 文件夹下:
stm32H743VIT6实现的串口1收发通信的例程代码,已验证通过可以正常使用,欢迎广大朋友借鉴,提高开发效率。
通过分时接收,接收成功后,设定标记,主任务中查询。并解析 STM32F103 串口接收代码例子。
主要介绍了STM32F2xx单片机的串口使用。官方例程中串口的接收都是采用查询方式,这往往使得程序的调试不是很方便。所以我就根据芯片用户手册等资料将在官方例程的基础上改写了一个以中断方式接收的例程,经测试通信...
STM32F0的 USART自动识别问题 自动识别问题 自动识别问题 自动识别...某客户用STM32F051的芯片做了一个自动波特率的实验(代码是仿照ST库里面的例子写的),用串口发送数字键的ASCII码,在接收端只有奇数ASCII被确认
开发板:正点原子 STM32F103 精英版 语言:C语言 开发环境:Keil5 开发板使用了 LED SPI USART RFID-RC522模块 钥匙扣卡 M1卡 Win10软件 SSCOM串口调试 FlyMcu烧录(ps:电脑安装驱动CH340) 安卓软件 NFC Writer...
8.3.1 把OSC32_IN/OSC32_OUT作为GPIO 端口PC14/PC15 116 8.3.2 把OSC_IN/OSC_OUT引脚作为GPIO端口PD0/PD1 117 8.3.3 CAN1复用功能重映射 117 8.3.4 CAN2复用功能重映射 117 8.3.5 JTAG/SWD复用功能重映射 117 ...