STM32电机培训online,大佬带你玩电机 ) ~# [; `/ ~) N( C2 B " l h" }8 Z. B# V #include "stm32f10x.h" #include "stm32f10x_usart.h" #include "stm32f10x_dma.h" uint8_t HEX_CODE[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; uint8_t USART2_DMA_TX_Buf[1024];: W* N* ]8 V' U. R+ P uint8_t Flag_USART2_DMA_TX_Finished = 1; uint8_t Flag_USART2_Send = 0;, i+ z/ {& P3 S: N4 k9 l6 o /** * Function Name : USART2_Config: p2 f9 ]" }" W" N0 P6 e * Description : None" t: X p2 K8 n2 O) A& l' p * Input : None * Output : None; Q& X6 G, j+ ?- G( v( V% R* E * Return : None/ o; F& d8 E$ M) U& L$ c: |9 J* z */ void USART2_Config(void) { GPIO_InitTypeDef GPIO_InitStructure;" k0 Q2 Z( k4 s/ V USART_InitTypeDef USART_InitStructure;/ d8 y$ X3 Q7 e DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* config USART2 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);% r! m9 o" j! I3 e- p: r- d/ P 0 a! [$ W' y4 k5 {! b+ B4 v! n /* *********************USART2 GPIO config **********************/ /* Configure USART1 Tx (PA.02) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; 0 y0 {8 H+ c; S GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;5 l/ ^6 ]" p5 J0 l% R# [( S0 M GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART2 Rx (PA.03) as input floating */- W0 {) X/ {$ L) M; j3 M3 F GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);7 t! X: ?* l: o4 I" c$ M, Q* Y, i: i. c /* USART2 mode config 115200 8-N-1*/ USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b;& ^" n E' f7 s1 ~ USART_InitStructure.USART_StopBits = USART_StopBits_1;8 B% S0 A8 L' t2 w S5 `5 ^ USART_InitStructure.USART_Parity = USART_Parity_No ;- G5 D$ M0 A5 }/ Y USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;# \5 i' N% T9 u8 W# L: h USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); # q$ H4 f6 L5 J, l' f0 E USART_Cmd(USART2, ENABLE); : f5 P% F$ _% y7 n$ c, `1 o 9 L7 T0 Z4 L8 ?, P/ l4 a S5 j; } USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE); /* Enable USART2 DMA TX request */# v; B2 Q0 D; B RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);; T p0 v3 ^8 y8 I' q. M; V1 s0 z / [5 ]3 z0 ?% F! Y# N5 Q) M$ \( L DMA_DeInit(DMA1_Channel7);/ l2 _# d+ c. I2 J8 U* k DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART2_DMA_TX_Buf; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; w" [8 ] g! @8 ]' j9 k4 `/ U+ `0 G DMA_InitStructure.DMA_BufferSize = 0; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;' g8 n# j. W" f1 ^ DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;) F+ A( ^5 [" G- x# z DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;: |- x H1 x2 E- i! g- H5 w1 S DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;+ W+ S6 e5 n7 v7 U DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel7, &DMA_InitStructure);! k6 K9 {8 K# b2 r" Q! c DMA_ITConfig(DMA1_Channel7, DMA_IT_TC, ENABLE); , [% |/ X0 s! a6 a NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);' p, N% L8 }, ^" N8 e 8 h) @; V9 F& G8 X) \8 @ /* Enable USART2 DMA TX Finish Interrupt */# `: o2 s3 u+ K NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel7_IRQn;1 c+ V" V2 S) o4 u0 W9 P# K# v$ p$ U4 @ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); . O# w# o3 ?+ }! Z) O& M /* Enable USART2 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;6 u3 j. ^- ]' \- a6 j NVIC_Init(&NVIC_InitStructure);7 V1 K3 g5 v) o$ H% G1 h }& O/ X8 o+ r- a2 R2 |, ^2 Z: ` /**2 S. S/ d" Z4 P* X( {# r9 B * Function Name : USART2_Data_Load * Description : None * Input : None * Output : None * Return : None */ void USART2_Data_Load(uint16_t temp1, uint16_t temp2, uint8_t temp3){: b4 S; B) v/ n* f1 \8 S7 O USART2_DMA_TX_Buf[0] = HEX_CODE[temp1 >> 4];$ m7 T: a* u) U+ [; l) t USART2_DMA_TX_Buf[1] = HEX_CODE[temp1 & 0x0F]; USART2_DMA_TX_Buf[2] = HEX_CODE[temp2 >> 4]; USART2_DMA_TX_Buf[3] = HEX_CODE[temp2 & 0x0F]; USART2_DMA_TX_Buf[4] = 0x20; USART2_DMA_TX_Buf[5] = HEX_CODE[temp3]; USART2_DMA_TX_Buf[6] = 0x0D;! k) U1 E# T6 l USART2_DMA_TX_Buf[7] = 0x0A; Flag_USART2_Send = 1;% z8 q# @9 i4 k3 D' J }' C e8 v6 }+ A" V /** * Function Name : USART2_Data_Send * Description : None$ u- S! |* n9 q; e7 o * Input : None3 m, r. s( ^# f7 {' q. J, A * Output : None6 w* [& ?/ R1 o z7 J' ^ * Return : None/ v9 u: D3 S8 I: F */5 k k. W$ |9 I+ L; r void USART2_Data_Send(uint16_t len) {, X* u6 K2 B K1 k2 L1 b if(Flag_USART2_Send){ if(Flag_USART2_DMA_TX_Finished == 1){ Flag_USART2_DMA_TX_Finished = 0; DMA1_Channel7->CMAR = (uint32_t)&USART2_DMA_TX_Buf[0];7 d2 J$ t: `& b5 U2 ?7 O+ t+ c DMA1_Channel7->CNDTR = 8; // len DMA_Cmd(DMA1_Channel7, ENABLE);& C2 w! I- N- K+ ^ J Flag_USART2_Send = 0;! Z, z6 p! W! P2 t }( B: O; }& ~5 q1 X } } /**. X" }4 R: W: F9 m6 C, Z+ J * Function Name : USART2_IRQHandler * Description : This function handles USART2 global interrupt request.) E, h. A5 j& Z * Input : None * Output : None * Return : None7 A7 A# Q' D7 Y. |8 C */ void USART2_IRQHandler(void) {8 y' {7 H7 c/ R3 D i } if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) { (void)USART_ReceiveData(USART2);% i4 Y0 d+ R; S# w& `: ] } if (USART_GetITStatus(USART2, USART_IT_TC) != RESET) { /* Disable the USART2 Transmit Complete interrupt */ USART_ITConfig(USART2, USART_IT_TC, DISABLE);$ _! g0 f8 l5 z) C4 \# f Flag_USART2_DMA_TX_Finished = 1; } /* If overrun condition occurs, clear the ORE flag a.nd recover communication */ + J8 x* L6 ]' T if(USART_GetFlagStatus(USART2,USART_FLAG_ORE) != RESET) {; J. c4 y& U8 U5 V (void)USART_ReceiveData(USART2);6 V% }; E) W. t) a: [ } }+ M* `/ }+ d) `1 L) }/ A /**5 f) \; J* h( O- C: D * Function Name : DMA1_Channel7_IRQHandler * Description : This function handles DMA1 Channel 7 interrupt request.' E* V D; f& U * Input : None * Output : None * Return : None */3 i3 U# G& n# Z9 y, ~- {( ?% i: P8 m void DMA1_Channel7_IRQHandler(void) {9 g; y" ]0 A" _5 L4 {+ u if(DMA_GetITStatus(DMA1_IT_TC7)) {3 d4 `( X6 O& l2 P, X /* USART2 DMA 传输完成 */$ i2 n/ v7 b% D2 u DMA_ClearITPendingBit(DMA1_IT_TC7);6 j9 O2 d$ y8 K; {& [+ x$ O. k- T) w DMA_Cmd(DMA1_Channel7, DISABLE);7 B8 Q1 m7 z$ Y0 i3 X8 Y /* Enable USART2 Transmit complete interrupt */6 M- f6 G$ _ u5 x, k USART_ITConfig(USART2, USART_IT_TC, ENABLE); 4 x& U0 p* ]2 A4 D! I0 @; Q% A& A } } 1 @. C& x9 c, s" p |
在高容量 STM32F103xx 微控制器中实现 ADPCM 算法
小马哥STM32F103开源小四轴RoboFly全部资料大放送
游名科技:STM32F103+TMC2160:86步进电机和驱动一体原理图/PCB...
STM32F103标准库开发---SPI实验---读写 W25Q128 外部 Flash
RT-Thread在STM32F103RC上移植
【MCU实战经验】+STM32F103的uCOSII详细移植
STM32固件库分享,超全系列整理
【原创】STM32F103的SPI2的配置函数,DMA发送和接收!
【源代码】STM32F103C8T6最小板搞定CMSIS-DAP和SWO功能
【福利】用STM32库的朋友有福了:STM32F10x_StdPeriph_Lib_V3.5.0chm...
9 s+ C# F! Q! Q ~% U2 Q
所谓STM32F103串口数据的DMA发送,其本质流程如下:
1. 要发送的数据放在USART2_DMA_TX_Buf缓冲。
2. STM32串口DMA发送和DSP相比的优势。
启动DMA传输后,USART2_DMA_TX_Buf缓冲中的数据通过DMA1_Channel7通道,$ T8 _/ z5 k! `8 _' o, t2 t: z
自动传输到USART2->DR寄存器,这个工作不需要CPU干预,可以极大的节省CPU的资源。
和DSP的FIFO相比,这优势很大,因为DSP的FIFO只有16字节,这意味着DSP的串口通过6 K: ` Q/ S o! C S6 D
FIFO发送数据时,如果FIFO缓冲为空,可以迅速填入16个字节,然后去处理其它的事务。
而STM32的DMA发送则没有此种限制。更具体的说,如果DSP发送的数据超过16字节,
则必须等待前16个字节数据发送完毕,然后再继续发送后续的数据,需要第二次甚至
第N次处理。而STM32启动一次DMA就搞定(DMA最大传输65535个数据)。
3. 发送结束的一些事务处理
DMA传输完毕,数据全部通过DMA1_Channel7通道依次传入USART2-DR,这里开启了# L# I0 m& ~& m! @, r
DMA传输完毕中断,进这个中断时,USART2-DR寄存器是最后一个要发送的数据,
此时打开USART2发送完毕中断,进入USART2发送完毕中断后,可以设定标识,
也可以操作GPIO, 进行RS485的换向动作。
. d* u% o8 L" h' a$ B) x
最新的STM32F103的HAL库,几乎所有的通信,都可以启用DMA,HAL库函数抽象层次过高,灵活性不够,/ ~/ {# G& u/ v1 e
但比较适合使用操作系统的场合。可以把HAL库的宏定义拷贝过来使用,以提高标准外设驱动库的效率,
又保证标准外设驱动库的灵活性。: Z9 B4 R7 o9 f+ r
4 c1 a% B) G0 F6 z$ h) y* u% b7 e
# t K- _$ e" `6 _