×
单片机 > 单片机程序设计 > 详情

Stm32使用Usart代码例子轮询、中断、DMA

发布时间:2020-06-06 发布时间:
|
  1. /* 
  2. 转载请注明出处:tedeum.iteye.com 
  3. /  

 首先是不使用中断的方法使用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

C代码  
  1. // STM32 USART1 (Tx PA.9, Rx PA.10) STM32F3-Discovery - sourcer32@gmail.com  
  2.    
  3. #include "stm32f30x.h"  
  4.    
  5. //  
  6.     
  7. void RCC_Configuration(void)  
  8. {  
  9.   /* Enable GPIO clock */  
  10.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);  
  11.    
  12.   /* Enable USART clock */  
  13.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);  
  14. }  
  15.    
  16. //  
  17.     
  18. void GPIO_Configuration(void)  
  19. {  
  20.   GPIO_InitTypeDef GPIO_InitStructure;  
  21.    
  22.   /* Connect PA9 to USART1_Tx */  
  23.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);  
  24.    
  25.   /* Connect PA10 to USART1_Rx */  
  26.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);  
  27.    
  28.   /* Configure USART Tx as alternate function push-pull */  
  29.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  
  30.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
  31.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  32.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  33.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  
  34.   GPIO_Init(GPIOA, &GPIO_InitStructure);  
  35.    
  36.   /* Configure USART Rx as alternate function push-pull */  
  37.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;  
  38.   GPIO_Init(GPIOA, &GPIO_InitStructure);  
  39. }  
  40.    
  41. //  
  42.    
  43. void USART1_Configuration(void)  
  44. {  
  45.   USART_InitTypeDef USART_InitStructure;  
  46.    
  47.   /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/  
  48.   /* USART configured as follow: 
  49.         - BaudRate = 115200 baud 
  50.         - Word Length = 8 Bits 
  51.         - One Stop Bit 
  52.         - No parity 
  53.         - Hardware flow control disabled (RTS and CTS signals) 
  54.         - Receive and transmit enabled 
  55.   */  
  56.   USART_InitStructure.USART_BaudRate = 115200;  
  57.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;  
  58.   USART_InitStructure.USART_StopBits = USART_StopBits_1;  
  59.   USART_InitStructure.USART_Parity = USART_Parity_No;  
  60.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  
  61.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  
  62.    
  63.   /* USART configuration */  
  64.   USART_Init(USART1, &USART_InitStructure);  
  65.    
  66.   /* Enable USART */  
  67.   USART_Cmd(USART1, ENABLE);  
  68. }  
  69.    
  70. //  
  71.    
  72. int main(void)  
  73. {  
  74.   RCC_Configuration();  
  75.     
  76.   GPIO_Configuration();  
  77.     
  78.   USART1_Configuration();  
  79.     
  80.   while(1)  
  81.   {  
  82.     while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty  
  83.     
  84.     USART_SendData(USART1, 0x49); // Send 'I'  
  85.   }  
  86.     
  87.   while(1); // Don't want to exit  
  88. }  
  89.    
  90. //  
  91.    
  92. #ifdef  USE_FULL_ASSERT  
  93.    
  94.   * @brief  Reports the name of the source file and the source line number 
  95.   *         where the assert_param error has occurred. 
  96.   * @param  file: pointer to the source file name 
  97.   * @param  line: assert_param error line source number 
  98.   * @retval None 
  99.   */  
  100. void assert_failed(uint8_t* file, uint32_t line)  
  101. {  
  102.   /* User can add his own implementation to report the file name and line number, 
  103.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */  
  104.    
  105.   /* Infinite loop */  
  106.   while (1)  
  107.   {  
  108.   }  
  109. }  
  110. #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

C代码  
  1. // STM32 USART IRQ TX/RX Loop (USART3 Tx PD.8, Rx PD.9) STM32F4 Discovery - sourcer32@gmail.com  
  2.    
  3. #include "stm32f4_discovery.h"  
  4.    
  5. volatile char StringLoop[] = "The quick brown fox jumps over the lazy dog\r\n";  
  6.    
  7. //  
  8.    
  9. void RCC_Configuration(void)  
  10. {  
  11.   /* --------------------------- System Clocks Configuration -----------------*/  
  12.   /* USART3 clock enable */  
  13.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);  
  14.    
  15.   /* GPIOD clock enable */  
  16.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);  
  17. }  
  18.    
  19. //  
  20.    
  21. void GPIO_Configuration(void)  
  22. {  
  23.   GPIO_InitTypeDef GPIO_InitStructure;  
  24.    
  25.   /*-------------------------- GPIO Configuration ----------------------------*/  
  26.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;  
  27.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
  28.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  29.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  
  30.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  31.   GPIO_Init(GPIOD, &GPIO_InitStructure);  
  32.    
  33.   /* Connect USART pins to AF */  
  34.   GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);  
  35.   GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);  
  36. }  
  37.    
  38. //  
  39.    
  40. void USART3_Configuration(void)  
  41. {  
  42.     USART_InitTypeDef USART_InitStructure;  
  43.    
  44.   /* USARTx configuration ------------------------------------------------------*/  
  45.   /* USARTx configured as follow: 
  46.         - BaudRate = 9600 baud 
  47.         - Word Length = 8 Bits 
  48.         - One Stop Bit 
  49.         - No parity 
  50.         - Hardware flow control disabled (RTS and CTS signals) 
  51.         - Receive and transmit enabled 
  52.   */  
  53.   USART_InitStructure.USART_BaudRate = 9600;  
  54.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;  
  55.   USART_InitStructure.USART_StopBits = USART_StopBits_1;  
  56.   USART_InitStructure.USART_Parity = USART_Parity_No;  
  57.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  
  58.    
  59.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  
  60.    
  61.   USART_Init(USART3, &USART_InitStructure);  
  62.    
  63.   USART_Cmd(USART3, ENABLE);  
  64.   
  65.  USART_ITConfig(USART3, USART_IT_TXE, ENABLE);  
  66.   
  67.  USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);  
  68. }  
  69.    
  70. //  
  71.    
  72. void NVIC_Configuration(void)  
  73. {  
  74.   NVIC_InitTypeDef NVIC_InitStructure;  
  75.    
  76.   /* Configure the NVIC Preemption Priority Bits */  
  77.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);  
  78.    
  79.   /* Enable the USART3 Interrupt */  
  80.   NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;  
  81.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
  82.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  83.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  84.   NVIC_Init(&NVIC_InitStructure);  
  85. }  
  86.    
  87. //  
  88.    
  89. void USART3_IRQHandler(void)  
  90. {  
  91.   static int tx_index = 0;  
  92.   static int rx_index = 0;  
  93.    
  94.   if (USART_GetITStatus(USART3, USART_IT_TXE) != RESET) // Transmit the string in a loop  
  95.   {  
  96.     USART_SendData(USART3, StringLoop[tx_index++]);  
  97.    
  98.     if (tx_index >= (sizeof(StringLoop) - 1))  
  99.       tx_index = 0;  
  100.   }  
  101.    
  102.   if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) // Received characters modify string  
  103.   {  
  104.     StringLoop[rx_index++] = USART_ReceiveData(USART3);  
  105.    
  106.     if (rx_index >= (sizeof(StringLoop) - 1))  
  107.       rx_index = 0;  
  108.   }  
  109. }  
  110.    
  111. //  
  112.    
  113. int main(void)  
  114. {  
  115.     RCC_Configuration();  
  116.    
  117.     GPIO_Configuration();  
  118.    
  119.     NVIC_Configuration();  
  120.    
  121.   USART3_Configuration();  
  122.    
  123.   while(1); // Don't want to exit  
  124. }  
  125.    
  126. //  

 最后,是使用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

C代码  
  1. // STM32 UART5 DMA TX (Tx PC.12, Rx PD.2) STM32F4 Discovery - sourcer32@gmail.com  
  2.    
  3. #include "stm32f4_discovery.h"  
  4.    
  5. //  
  6.    
  7. void RCC_Configuration(void)  
  8. {  
  9.   /* --------------------------- System Clocks Configuration -----------------*/  
  10.   /* UART5 clock enable */  
  11.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);  
  12.    
  13.   /* GPIOC and GPIOD clock enable */  
  14.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE);  
  15.    
  16.   /* DMA1 clock enable */  
  17.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);  
  18. }  
  19.    
  20. //  
  21.    
  22. void GPIO_Configuration(void)  
  23. {  
  24.   GPIO_InitTypeDef GPIO_InitStructure;  
  25.    
  26.   /*-------------------------- GPIO Configuration ----------------------------*/  
  27.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; // PC.12 UART5_TX, potential clash SDIN CS43L22  
  28.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
  29.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  30.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  
  31.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  32.   GPIO_Init(GPIOC, &GPIO_InitStructure);  
  33.    
  34.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // PD.2 UART5_RX  
  35.   GPIO_Init(GPIOD, &GPIO_InitStructure);  
  36.    
  37.   /* Connect USART pins to AF */  
  38.   GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);  
  39.   GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);  
  40. }  
  41.    
  42. //  
  43.    
  44. void UART5_Configuration(void)  
  45. {  
  46.     USART_InitTypeDef USART_InitStructure;  
  47.    
  48.   /* USARTx configuration ------------------------------------------------------*/  
  49.   /* USARTx configured as follow: 
  50.         - BaudRate = 115200 baud 
  51.         - Word Length = 8 Bits 
  52.         - One Stop Bit 
  53.         - No parity 
  54.         - Hardware flow control disabled (RTS and CTS signals) 
  55.         - Receive and transmit enabled 
  56.   */  
  57.   USART_InitStructure.USART_BaudRate = 115200;  
  58.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;  
  59.   USART_InitStructure.USART_StopBits = USART_StopBits_1;  
  60.   USART_InitStructure.USART_Parity = USART_Parity_No;  
  61.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  
  62.    
  63.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  
  64.    
  65.   USART_Init(UART5, &USART_InitStructure);  
  66.    
  67.   USART_Cmd(UART5, ENABLE);  
  68. }  
  69.    
  70. //  
  71.    
  72. char Buffer[] = "The quick brown fox jumps over the lazy dog\r\n";  
  73.    
  74. void DMA_Configuration(void)  
  75. {  
  76.   DMA_InitTypeDef  DMA_InitStructure;  
  77.    
  78.   DMA_DeInit(DMA1_Stream7);  
  79.    
  80.   DMA_InitStructure.DMA_Channel = DMA_Channel_4;  
  81.   DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; // Transmit  
  82.   DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Buffer;  
  83.   DMA_InitStructure.DMA_BufferSize = (uint16_t)sizeof(Buffer) - 1;  
  84.   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&UART5->DR;  
  85.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  
  86.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  
  87.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  
  88.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;  
  89.   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;  
  90.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;  
  91.   DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;  
  92.   DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;  
  93.   DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;  
  94.   DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;  
  95.    
  96.   DMA_Init(DMA1_Stream7, &DMA_InitStructure);  
  97.    
  98.   /* Enable the USART Tx DMA request */  
  99.   USART_DMACmd(UART5, USART_DMAReq_Tx, ENABLE);  
  100.    
  101.   /* Enable DMA Stream Transfer Complete interrupt */  
  102.   DMA_ITConfig(DMA1_Stream7, DMA_IT_TC, ENABLE);  
  103.    
  104.   /* Enable the DMA RX Stream */  
  105.   DMA_Cmd(DMA1_Stream7, ENABLE);  
  106. }  
  107.    
  108. //  
  109.    
  110. void DMA1_Stream7_IRQHandler(void)  
  111. {  
  112.   /* Test on DMA Stream Transfer Complete interrupt */  
  113.   if (DMA_GetITStatus(DMA1_Stream7, DMA_IT_TCIF7))  
  114.   {  
  115.     /* Clear DMA Stream Transfer Complete interrupt pending bit */  
  116.     DMA_ClearITPendingBit(DMA1_Stream7, DMA_IT_TCIF7);  
  117.   }  
  118. }  
  119.    
  120. //  
  121.    
  122. void NVIC_Configuration(void)  
  123. {  
  124.   NVIC_InitTypeDef NVIC_InitStructure;  
  125.    
  126.   /* Configure the Priority Group to 2 bits */  
  127.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  
  128.    
  129.   /* Enable the UART5 RX DMA Interrupt */  
  130.   NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream7_IRQn;  
  131.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
  132.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  133.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  134.   NVIC_Init(&NVIC_InitStructure);  
  135. }  
  136.    
  137. //  
  138.    
  139. int main(void)  
  140. {  
  141.     RCC_Configuration();  
  142.    
  143.   NVIC_Configuration();  
  144.    
  145.     GPIO_Configuration();  
  146.    
  147.   UART5_Configuration();  
  148.    
  149.   DMA_Configuration();  
  150.    
  151.   while(1); // Don't want to exit  
  152. }  
  153.    
  154. //  
  155.    
  156. #ifdef  USE_FULL_ASSERT  
  157.    
  158.   * @brief  Reports the name of the source file and the source line number 
  159.   *   where the assert_param error has occurred. 
  160.   * @param  file: pointer to the source file name 
  161.   * @param  line: assert_param error line source number 
  162.   * @retval None 
  163.   */  
  164. void assert_failed(uint8_t* file, uint32_t line)  
  165. {  
  166.   /* User can add his own implementation to report the file name and line number, 
  167.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */  
  168.    
  169.   /* Infinite loop */  
  170.   while (1)  
  171.   {  
  172.   }  
  173. }  
  174. #endif  
  175.   * @} 
  176.   */  
  177.    
  178. //  




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
STM32单片机的复用端口初始化的步骤及方法