久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4651|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

STM32F3使用USART串口DMA發(fā)送數(shù)據(jù),使用藍(lán)牙發(fā)送

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
Author: Wind
OS:  WIN7
IDE:    KEILuvision V5.10.0.2
MCU: STM32 F303 VC T6
一、硬件設(shè)計(jì)
1.MCU使用USART2,USART2_TX——PA2,USART2_RX——PA3
2.DMA:查看ST官方網(wǎng)站的STM32F3參考手冊(官網(wǎng)下載),使用DMA1 Channel7




3.MCU與藍(lán)牙模塊連接:


二、程序代碼

1.USART配置:查看ST官方固件庫手冊(官網(wǎng)下載),按順序進(jìn)行配置,以下復(fù)制原文:

//23.2 USART Firmware driver API description

The following section lists the various functions of the USART library.

23.2.1 How to use this driver

1. Enable peripheral clock using RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE) function for USART1 or using RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE) function for USART2, USART3, UART4 and UART5.

2. According to the USART mode, enable the GPIO clocks using RCC_AHBPeriphClockCmd() function. (The I/O can be TX, RX, CTS, or and SCLK). 3. Peripheral's alternate function:  Connect the pin to the desired peripherals' Alternate Function (AF) using GPIO_PinAFConfig() function.

 Configure the desired pin in alternate function by: GPIO_InitStruct->GPIO_Mode=

GPIO_Mode_AF.

 Select the type, pull-up/pull-down and output speed via GPIO_PuPd,

GPIO_OType and GPIO_Speed members.

 Call GPIO_Init() function.

4. Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware flow control and

Mode(Receiver/Transmitter) using the SPI_Init() function.

5. For synchronous mode, enable the clock and program the polarity, phase and last bit

using the USART_ClockInit() function.

6. Enable the NVIC and the corresponding interrupt using the function

USART_ITConfig() if you need to use interrupt mode.

7. When using the DMA mode:

 Configure the DMA using DMA_Init() function.

 Active the needed channel Request using USART_DMACmd() function.

8. Enable the USART using the USART_Cmd() function.

9. Enable the DMA using the DMA_Cmd() function, when using DMA mode.

2.DMA配置

//9.2 DMA Firmware driver API description

     The following section lists the various functions of the DMA library.

     9.2.1 How to use this driver

1. Enable The DMA controller clock using RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE) function for DMA1 or using    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE) function for DMA2.

2. Enable and configure the peripheral to be connected to the DMA channel (except for internal SRAM / FLASH memories: no initialization is necessary).

3. For a given Channel, program the Source and Destination addresses, the transfer Direction, the Buffer Size, the Peripheral and Memory Incrementation mode and Data Size, the Circular or Normal mode, the channel transfer Priority and the Memory- to- Memory transfer mode (if needed) using the DMA_Init() function.

4. Enable the NVIC and the corresponding interrupt(s) using the function DMA_ITConfig() if you need to use DMA interrupts.

5. Enable the DMA channel using the DMA_Cmd() function.

6. Activate the needed channel Request using PPP_DMACmd() function for any PPP peripheral except internal SRAM and FLASH (ie. SPI, USART ...) The function allowing this operation is provided in each PPP peripheral driver (ie. SPI_DMACmd for SPI peripheral).

7. Optionally, you can configure the number of data to be transferred when the channel is disabled (ie. after each Transfer Complete event or when a Transfer Error occurs) using the function DMA_SetCurrDataCounter(). And you can get the number of remaining data to be transferred using the function DMA_GetCurrDataCounter() at run time (when the DMA channel is enabled and running).

8. To control DMA events you can use one of the following two methods:

a. Check on DMA channel flags using the function DMA_GetFlagStatus().

b. Use DMA interrupts through the function DMA_ITConfig() at initialization phase and DMA_GetITStatus() function into interrupt routines in communication phase. After checking on a flag you should clear it using DMA_ClearFlag() function. And after checking on an interrupt event you should clear it using DMA_ClearITPendingBit() function.

3.開始發(fā)送,使能各功能模塊

源代碼:
  1. // Header:

  2. // File Name: main.h

  3. // Author: Wind

  4. // Date: 2015.01.25



  5. #ifndef MAIN_H

  6. #define MAIN_H

  7. #include "stm32f30x.h"

  8. #include "stm32f30x_rcc.h"

  9. #include "stm32f30x_dma.h"

  10. #include "Config.h"

  11. #endif





  12. // Header:

  13. // File Name: main.c

  14. // Author: Wind

  15. // Date: 2015.01.25



  16. #include "main.h"

  17. #define BT_TX_BUFFER_LENGTH 26 //發(fā)送26個(gè)字母

  18. static uint8_t BT_TX_Buffer[BT_TX_BUFFER_LENGTH];



  19. int main()

  20. {

  21. uint8_t i=0,data='A';

  22. BTUSARTConfig(115200);//波特率115200

  23. BTDMAConfig((uint32_t) BT_TX_Buffer,BT_TX_BUFFER_LENGTH);

  24. //準(zhǔn)備好待發(fā)送數(shù)據(jù)

  25. for (i=0;i

  26. {

  27. BT_TX_Buffer[i] = data;

  28. }

  29. //開始發(fā)送

  30. StartDataTransfer();

  31. while(1);  //連續(xù)發(fā)送,同時(shí)CPU可以做其他事情



  32. }





  33. // Header:

  34. // File Name: Config.h

  35. // Author: Wind

  36. // Date: 2015.01.25



  37. #ifndef CONFIG_H

  38. #define CONFIG_H

  39. #include "stm32f30x.h"

  40. #include "stm32f30x_rcc.h"

  41. #include "stm32f30x_dma.h"

  42. // Bluetooth port settings

  43. #define BT_USART_IO_PORT GPIOA

  44. #define BT_TX_PIN GPIO_Pin_2

  45. #define BT_RX_PIN GPIO_Pin_3

  46. #define BT_TX_SOURCE GPIO_PinSource2

  47. #define BT_RX_SOURCE GPIO_PinSource3

  48. #define BT_USART_GPIO_CLK RCC_AHBPeriph_GPIOA

  49. #define BT_USART_CLK RCC_APB1Periph_USART2

  50. #define BT_TX_AF GPIO_AF_USART2

  51. #define BT_RX_AF GPIO_AF_USART2

  52. #define GPIO_AF_USART2 GPIO_AF_7

  53. #define BT_USART_PORT USART2

  54. #define BT_USART_TDR_ADDRESS ((uint32_t)USART2 + 0x28)

  55. #define BT_USART_RDR_ADDRESS ((uint32_t)USART2 + 0x24)

  56. #define BT_USART_DMA_PORT DMA1

  57. #define BT_USART_DMA_CLK RCC_AHBPeriph_DMA1

  58. #define BT_USART_TX_DMA_CHANNEL DMA1_Channel7

  59. #define BT_USART_RX_DMA_CHANNEL DMA1_Channel6

  60. #define BT_USART_TX_DMA_FLAG_TCIF DMA1_FLAG_TC7

  61. #define BT_USART_RX_DMA_FLAG_TCIF DMA1_FLAG_TC6

  62. //Function declaration

  63. void BTUSARTConfig(uint32_t baudrate);

  64. void BTDMAConfig(uint32_t DataAddr,uint8_t dataLength);

  65. void StartDataTransfer(void);

  66. #endif





  67. // Header:

  68. // File Name: Config.c

  69. // Author: Wind

  70. // Date: 2015.01.25



  71. #include "Config.h"



  72. //USART配置

  73. void BTUSARTConfig(uint32_t baudrate)

  74. {

  75. USART_InitTypeDef USART_InitStructure;

  76. GPIO_InitTypeDef GPIO_InitStructure;

  77. RCC_AHBPeriphClockCmd(BT_USART_GPIO_CLK, ENABLE);

  78. RCC_APB1PeriphClockCmd(BT_USART_CLK, ENABLE);

  79. GPIO_InitStructure.GPIO_Pin = (BT_TX_PIN | BT_RX_PIN);

  80. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;              //¸′óÃÄ£ê½

  81. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  82. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  83. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  84. GPIO_Init(BT_USART_IO_PORT, &GPIO_InitStructure);

  85. GPIO_PinAFConfig(BT_USART_IO_PORT, BT_TX_SOURCE, BT_TX_AF);//ÅäÖÃòy½Åμĸ′óÃ1|Äü

  86. GPIO_PinAFConfig(BT_USART_IO_PORT, BT_RX_SOURCE, BT_RX_AF);

  87. USART_InitStructure.USART_BaudRate = baudrate;

  88. USART_InitStructure.USART_WordLength = USART_WordLength_8b;

  89. USART_InitStructure.USART_StopBits = USART_StopBits_1;

  90. USART_InitStructure.USART_Parity = USART_Parity_No;

  91. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  92. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  93. USART_DeInit(BT_USART_PORT);

  94. USART_Init(BT_USART_PORT, &USART_InitStructure);

  95. USART_Cmd(BT_USART_PORT, ENABLE);

  96. }

  97. //DMA配置

  98. //para:dataAddr:Transffer data address;dataLength:Transffer data length

  99. void BTDMAConfig(uint32_t dataAddr,uint8_t dataLength)

  100. {

  101. DMA_InitTypeDef DMA_InitStructure;

  102. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);

  103. DMA_StructInit(&DMA_InitStructure);

  104. DMA_DeInit(BT_USART_TX_DMA_CHANNEL);

  105. DMA_InitStructure.DMA_PeripheralBaseAddr = BT_USART_TDR_ADDRESS;

  106. DMA_InitStructure.DMA_MemoryBaseAddr = dataAddr;

  107. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;         //外設(shè)作為發(fā)送目的地

  108. DMA_InitStructure.DMA_BufferSize = dataLength;

  109. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

  110. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

  111. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;

  112. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

  113. DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循環(huán)發(fā)送模式

  114. DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;

  115. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

  116. DMA_Init(BT_USART_TX_DMA_CHANNEL,&DMA_InitStructure);

  117. }

  118. //開始發(fā)送

  119. void StartDataTransfer(void)

  120. {

  121. //清除標(biāo)志位,否則可能丟失數(shù)據(jù)

  122. USART_ClearFlag(BT_USART_PORT,USART_FLAG_TC);

  123. DMA_ClearFlag(BT_USART_TX_DMA_FLAG_TCIF);

  124. //發(fā)送

  125. USART_DMACmd(BT_USART_PORT,USART_DMAReq_Tx,ENABLE);

  126. DMA_Cmd(BT_USART_TX_DMA_CHANNEL,ENABLE);

  127. //等待發(fā)送完畢

  128. while (!DMA_GetFlagStatus(BT_USART_TX_DMA_FLAG_TCIF));

  129. }
復(fù)制代碼




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 一区二区三区不卡视频 | 国产成人精品一区二区三区四区 | 干一干操一操 | 日韩欧美三级 | 国产成人一区二区三区精 | 久久久精品网 | 91精品国产91久久久久久丝袜 | 日韩在线播放av | 日本三级网站在线 | 麻豆视频在线看 | 中文字幕av一区 | 理论片午午伦夜理片影院 | 日韩精品免费 | 亚洲精品久久久久久首妖 | 亚洲精品视频一区 | 欧美日韩免费 | 欧美人妇做爰xxxⅹ性高电影 | 日韩中文字幕第一页 | 日韩av在线中文字幕 | 亚洲女人天堂成人av在线 | 欧美一区二区三区在线视频 | 国产精品99久久久久久www | 久久久久久久91 | 中文字幕日韩一区 | 国产精品高潮呻吟久久 | 国产91在线播放 | 久久久久久久网 | 亚洲高清视频一区二区 | 99久久久久久久久 | 午夜精品久久久久久久99黑人 | 第一区在线观看免费国语入口 | 成人精品一区亚洲午夜久久久 | 天天操天天射综合网 | 影音先锋欧美资源 | 一区天堂 | 国产精品99久久久久久动医院 | 日本成人毛片 | 精品欧美一区二区精品久久 | 欧美成人精品欧美一级 | 国产精品久久九九 | 国产日韩欧美二区 |