您的位置 首页 国产IC

Stm32运用Usart代码比如轮询、中止、DMA

/*转载请注明出处:tedeum.iteye.com/首先是不使用中断的方法使用usart1,管脚pa9,pa10,此方法已在f3discovery上验证通过,来源:https://m…

  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&currentviews=524

C代码
  1. //STM32USART1(TxPA.9,RxPA.10)STM32F3-Discovery-sourcer32@gmail.com
  2. #include”stm32f30x.h”
  3. //
  4. voidRCC_Configuration(void)
  5. {
  6. /*EnableGPIOclock*/
  7. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
  8. /*EnableUSARTclock*/
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
  10. }
  11. //
  12. voidGPIO_Configuration(void)
  13. {
  14. GPIO_InitTypeDefGPIO_InitStructure;
  15. /*ConnectPA9toUSART1_Tx*/
  16. GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_7);
  17. /*ConnectPA10toUSART1_Rx*/
  18. GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_7);
  19. /*ConfigureUSARTTxasalternatefunctionpush-pull*/
  20. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
  21. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
  22. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  23. GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
  24. GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
  25. GPIO_Init(GPIOA,&GPIO_InitStructure);
  26. /*ConfigureUSARTRxasalternatefunctionpush-pull*/
  27. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
  28. GPIO_Init(GPIOA,&GPIO_InitStructure);
  29. }
  30. //
  31. voidUSART1_Configuration(void)
  32. {
  33. USART_InitTypeDefUSART_InitStructure;
  34. /*USARTresourcesconfiguration(Clock,GPIOpinsandUSARTregisters)—-*/
  35. /*USARTconfiguredasfollow:
  36. -BaudRate=115200baud
  37. -WordLength=8Bits
  38. -OneStopBit
  39. -Noparity
  40. -Hardwareflowcontroldisabled(RTSandCTSsignals)
  41. -Receiveandtransmitenabled
  42. */
  43. USART_InitStructure.USART_BaudRate=115200;
  44. USART_InitStructure.USART_WordLength=USART_WordLength_8b;
  45. USART_InitStructure.USART_StopBits=USART_StopBits_1;
  46. USART_InitStructure.USART_Parity=USART_Parity_No;
  47. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  48. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
  49. /*USARTconfiguration*/
  50. USART_Init(USART1,&USART_InitStructure);
  51. /*EnableUSART*/
  52. USART_Cmd(USART1,ENABLE);
  53. }
  54. //
  55. intmain(void)
  56. {
  57. RCC_Configuration();
  58. GPIO_Configuration();
  59. USART1_Configuration();
  60. while(1)
  61. {
  62. while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//WaitforEmpty
  63. USART_SendData(USART1,0x49);//SendI
  64. }
  65. while(1);//Dontwanttoexit
  66. }
  67. //
  68. #ifdefUSE_FULL_ASSERT
  69. /
  70. *@briefReportsthenameofthesourcefileandthesourcelinenumber
  71. *wheretheassert_paramerrorhasoccurred.
  72. *@paramfile:pointertothesourcefilename
  73. *@paramline:assert_paramerrorlinesourcenumber
  74. *@retvalNone
  75. */
  76. voidassert_failed(uint8_t*file,uint32_tline)
  77. {
  78. /*Usercanaddhisownimplementationtoreportthefilenameandlinenumber,
  79. ex:printf(“Wrongparametersvalue:file%sonline%d\r\n”,file,line)*/
  80. /*Infiniteloop*/
  81. while(1)
  82. {
  83. }
  84. }
  85. #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&currentviews=124

C代码
  1. //STM32USARTIRQTX/RXLoop(USART3TxPD.8,RxPD.9)STM32F4Discovery-sourcer32@gmail.com
  2. #include”stm32f4_discovery.h”
  3. volatilecharStringLoop[]=”Thequickbrownfoxjumpsoverthelazydog\r\n”;
  4. //
  5. voidRCC_Configuration(void)
  6. {
  7. /*—————————SystemClocksConfiguration—————–*/
  8. /*USART3clockenable*/
  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
  10. /*GPIODclockenable*/
  11. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
  12. }
  13. //
  14. voidGPIO_Configuration(void)
  15. {
  16. GPIO_InitTypeDefGPIO_InitStructure;
  17. /*————————–GPIOConfiguration—————————-*/
  18. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9;
  19. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
  20. GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
  21. GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
  22. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  23. GPIO_Init(GPIOD,&GPIO_InitStructure);
  24. /*ConnectUSARTpinstoAF*/
  25. GPIO_PinAFConfig(GPIOD,GPIO_PinSource8,GPIO_AF_USART3);
  26. GPIO_PinAFConfig(GPIOD,GPIO_PinSource9,GPIO_AF_USART3);
  27. }
  28. //
  29. voidUSART3_Configuration(void)
  30. {
  31. USART_InitTypeDefUSART_InitStructure;
  32. /*USARTxconfiguration——————————————————*/
  33. /*USARTxconfiguredasfollow:
  34. -BaudRate=9600baud
  35. -WordLength=8Bits
  36. -OneStopBit
  37. -Noparity
  38. -Hardwareflowcontroldisabled(RTSandCTSsignals)
  39. -Receiveandtransmitenabled
  40. */
  41. USART_InitStructure.USART_BaudRate=9600;
  42. USART_InitStructure.USART_WordLength=USART_WordLength_8b;
  43. USART_InitStructure.USART_StopBits=USART_StopBits_1;
  44. USART_InitStructure.USART_Parity=USART_Parity_No;
  45. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  46. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
  47. USART_Init(USART3,&USART_InitStructure);
  48. USART_Cmd(USART3,ENABLE);
  49. USART_ITConfig(USART3,USART_IT_TXE,ENABLE);
  50. USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
  51. }
  52. //
  53. voidNVIC_Configuration(void)
  54. {
  55. NVIC_InitTypeDefNVIC_InitStructure;
  56. /*ConfiguretheNVICPreemptionPriorityBits*/
  57. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  58. /*EnabletheUSART3Interrupt*/
  59. NVIC_InitStructure.NVIC_IRQChannel=USART3_IRQn;
  60. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
  61. NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
  62. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  63. NVIC_Init(&NVIC_InitStructure);
  64. }
  65. //
  66. voidUSART3_IRQHandler(void)
  67. {
  68. staticinttx_index=0;
  69. staticintrx_index=0;
  70. if(USART_GetITStatus(USART3,USART_IT_TXE)!=RESET)//Transmitthestringinaloop
  71. {
  72. USART_SendData(USART3,StringLoop[tx_index++]);
  73. if(tx_index>=(sizeof(StringLoop)-1))
  74. tx_index=0;
  75. }
  76. if(USART_GetITStatus(USART3,USART_IT_RXNE)!=RESET)//Receivedcharactersmodifystring
  77. {
  78. StringLoop[rx_index++]=USART_ReceiveData(USART3);
  79. if(rx_index>=(sizeof(StringLoop)-1))
  80. rx_index=0;
  81. }
  82. }
  83. //
  84. intmain(void)
  85. {
  86. RCC_Configuration();
  87. GPIO_Configuration();
  88. NVIC_Configuration();
  89. USART3_Configuration();
  90. while(1);//Dontwanttoexit
  91. }
  92. //

最终,是运用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&currentviews=760

C代码
  1. //STM32UART5DMATX(TxPC.12,RxPD.2)STM32F4Discovery-sourcer32@gmail.com
  2. #include”stm32f4_discovery.h”
  3. //
  4. voidRCC_Configuration(void)
  5. {
  6. /*—————————SystemClocksConfiguration—————–*/
  7. /*UART5clockenable*/
  8. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);
  9. /*GPIOCandGPIODclockenable*/
  10. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD,ENABLE);
  11. /*DMA1clockenable*/
  12. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);
  13. }
  14. //
  15. voidGPIO_Configuration(void)
  16. {
  17. GPIO_InitTypeDefGPIO_InitStructure;
  18. /*————————–GPIOConfiguration—————————-*/
  19. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;//PC.12UART5_TX,potentialclashSDINCS43L22
  20. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
  21. GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
  22. GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
  23. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  24. GPIO_Init(GPIOC,&GPIO_InitStructure);
  25. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//PD.2UART5_RX
  26. GPIO_Init(GPIOD,&GPIO_InitStructure);
  27. /*ConnectUSARTpinstoAF*/
  28. GPIO_PinAFConfig(GPIOC,GPIO_PinSource12,GPIO_AF_UART5);
  29. GPIO_PinAFConfig(GPIOD,GPIO_PinSource2,GPIO_AF_UART5);
  30. }
  31. //
  32. voidUART5_Configuration(void)
  33. {
  34. USART_InitTypeDefUSART_InitStructure;
  35. /*USARTxconfiguration——————————————————*/
  36. /*USARTxconfiguredasfollow:
  37. -BaudRate=115200baud
  38. -WordLength=8Bits
  39. -OneStopBit
  40. -Noparity
  41. -Hardwareflowcontroldisabled(RTSandCTSsignals)
  42. -Receiveandtransmitenabled
  43. */
  44. USART_InitStructure.USART_BaudRate=115200;
  45. USART_InitStructure.USART_WordLength=USART_WordLength_8b;
  46. USART_InitStructure.USART_StopBits=USART_StopBits_1;
  47. USART_InitStructure.USART_Parity=USART_Parity_No;
  48. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  49. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
  50. USART_Init(UART5,&USART_InitStructure);
  51. USART_Cmd(UART5,ENABLE);
  52. }
  53. //
  54. charBuffer[]=”Thequickbrownfoxjumpsoverthelazydog\r\n”;
  55. voidDMA_Configuration(void)
  56. {
  57. DMA_InitTypeDefDMA_InitStructure;
  58. DMA_DeInit(DMA1_Stream7);
  59. DMA_InitStructure.DMA_Channel=DMA_Channel_4;
  60. DMA_InitStructure.DMA_DIR=DMA_DIR_MemoryToPeripheral;//Transmit
  61. DMA_InitStructure.DMA_Memory0BaseAddr=(uint32_t)Buffer;
  62. DMA_InitStructure.DMA_BufferSize=(uint16_t)sizeof(Buffer)-1;
  63. DMA_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)&UART5->DR;
  64. DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
  65. DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
  66. DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;
  67. DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;
  68. DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;
  69. DMA_InitStructure.DMA_Priority=DMA_Priority_High;
  70. DMA_InitStructure.DMA_FIFOMode=DMA_FIFOMode_Enable;
  71. DMA_InitStructure.DMA_FIFOThreshold=DMA_FIFOThreshold_Full;
  72. DMA_InitStructure.DMA_MemoryBurst=DMA_MemoryBurst_Single;
  73. DMA_InitStructure.DMA_PeripheralBurst=DMA_PeripheralBurst_Single;
  74. DMA_Init(DMA1_Stream7,&DMA_InitStructure);
  75. /*EnabletheUSARTTxDMArequest*/
  76. USART_DMACmd(UART5,USART_DMAReq_Tx,ENABLE);
  77. /*EnableDMAStreamTransferCompleteinterrupt*/
  78. DMA_ITConfig(DMA1_Stream7,DMA_IT_TC,ENABLE);
  79. /*EnabletheDMARXStream*/
  80. DMA_Cmd(DMA1_Stream7,ENABLE);
  81. }
  82. //
  83. voidDMA1_Stream7_IRQHandler(void)
  84. {
  85. /*TestonDMAStreamTransferCompleteinterrupt*/
  86. if(DMA_GetITStatus(DMA1_Stream7,DMA_IT_TCIF7))
  87. {
  88. /*ClearDMAStreamTransferCompleteinterruptpendingbit*/
  89. DMA_ClearITPendingBit(DMA1_Stream7,DMA_IT_TCIF7);
  90. }
  91. }
  92. //
  93. voidNVIC_Configuration(void)
  94. {
  95. NVIC_InitTypeDefNVIC_InitStructure;
  96. /*ConfigurethePriorityGroupto2bits*/
  97. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  98. /*EnabletheUART5RXDMAInterrupt*/
  99. NVIC_InitStructure.NVIC_IRQChannel=DMA1_Stream7_IRQn;
  100. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
  101. NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
  102. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  103. NVIC_Init(&NVIC_InitStructure);
  104. }
  105. //
  106. intmain(void)
  107. {
  108. RCC_Configuration();
  109. NV%&&&&&%_Configuration();
  110. GPIO_Configuration();
  111. UART5_Configuration();
  112. DMA_Configuration();
  113. while(1);//Dontwanttoexit
  114. }
  115. //
  116. #ifdefUSE_FULL_ASSERT
  117. /
  118. *@briefReportsthenameofthesourcefileandthesourcelinenumber
  119. *wheretheassert_paramerrorhasoccurred.
  120. *@paramfile:pointertothesourcefilename
  121. *@paramline:assert_paramerrorlinesourcenumber
  122. *@retvalNone
  123. */
  124. voidassert_failed(uint8_t*file,uint32_tline)
  125. {
  126. /*Usercanaddhisownimplementationtoreportthefilenameandlinenumber,
  127. ex:printf(“Wrongparametersvalue:file%sonline%d\r\n”,file,line)*/
  128. /*Infiniteloop*/
  129. while(1)
  130. {
  131. }
  132. }
  133. #endif
  134. /
  135. *@}
  136. */
  137. //

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/bandaoti/ic/264857.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部