
STM32电机培训online,大佬带你玩电机 _# @* d/ Y5 @7 t8 _' O6 e2 ` + g" ?+ k% h% N- n) x4 Z #include "stm32f10x.h" #include "stm32f10x_usart.h"+ c5 g9 `. [ f8 U/ F5 @ #include "stm32f10x_dma.h" / p& L1 z/ Q& T0 v8 L+ o 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]; uint8_t Flag_USART2_DMA_TX_Finished = 1;( P* D' W u8 L7 v0 c8 x+ u uint8_t Flag_USART2_Send = 0; /** * Function Name : USART2_Config0 J% X* v& D% C. D' W * Description : None R7 Q, g+ X$ P6 B, h5 \% Z; e' q * Input : None * Output : None( D2 @& O- z3 N* G * Return : None */; `. H! a3 u6 A& `9 k void USART2_Config(void) { GPIO_InitTypeDef GPIO_InitStructure;8 t/ B- o2 `6 D& G3 G- U! J USART_InitTypeDef USART_InitStructure; DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* config USART2 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); s1 E( [' H6 M3 R2 x3 @ 3 X& d5 h+ P: C! V /* *********************USART2 GPIO config **********************/& S( U! |! J' c/ G3 U- q+ U /* Configure USART1 Tx (PA.02) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;* G' U/ O1 d; z$ f: R GPIO_Init(GPIOA, &GPIO_InitStructure); & k* L% K/ a; f* T; V$ | e /* Configure USART2 Rx (PA.03) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;4 _: e; u w z+ n, G3 S: ^ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;! Z% S6 b% m" O0 q GPIO_Init(GPIOA, &GPIO_InitStructure); 6 W: O$ e8 B- X, a /* USART2 mode config 115200 8-N-1*/ USART_InitStructure.USART_BaudRate = 115200;" L9 @. y. K) |" d7 ~& F5 p USART_InitStructure.USART_WordLength = USART_WordLength_8b;" b2 {& n4 [8 o8 f USART_InitStructure.USART_StopBits = USART_StopBits_1;0 J# y8 |0 U9 ^4 P1 n USART_InitStructure.USART_Parity = USART_Parity_No ; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;8 \& b( f6 `) ?+ H USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;0 e5 B) ~- F1 f! h+ c/ y- k$ O USART_Init(USART2, &USART_InitStructure); USART_Cmd(USART2, ENABLE);; Z* n4 W# A8 o$ V W- C" E- ` USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE); /* Enable USART2 DMA TX request */) e* e+ u/ `/ d) X; ? RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);; `; O n# r' }# W1 Y' C" h: s4 n * b7 U6 j( E3 e/ y- I8 B4 m DMA_DeInit(DMA1_Channel7);' `; c, x- g) K4 T% z' L5 c 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;) q$ Y; A& A3 k) z! u7 v, _) @ DMA_InitStructure.DMA_BufferSize = 0;/ q5 R: {0 Z. b/ o# `& S$ @ DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;5 f% f4 g% k1 W" n DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;( n) W7 O/ D+ j( H; k ] DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;8 ~! U/ R' a2 x5 C DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel7, &DMA_InitStructure);. y% o# F" {" w; t; \ DMA_ITConfig(DMA1_Channel7, DMA_IT_TC, ENABLE);+ G7 I1 p; s$ Q " Y% l5 l* u6 z8 T; g# j1 D NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Enable USART2 DMA TX Finish Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel7_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;- J- ^! c. T$ F NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;+ K" S# Q; q1 \# k" m% p NVIC_Init(&NVIC_InitStructure); 3 {! X+ b6 v6 K$ ~& Z" s /* Enable USART2 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;: `6 e2 w3 V: K+ z3 h$ }0 ^4 n NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;7 M# n3 R4 f0 x* R' s* g% G1 G NVIC_Init(&NVIC_InitStructure); B% o" I; M: ?% h2 O }+ x* y3 S* l# p5 |( S8 B /**6 j7 w9 w) L, z) A * Function Name : USART2_Data_Load' P6 F" q3 n3 }- D. B * Description : None& m( t+ H3 {3 ]4 {7 t * Input : None * Output : None * Return : None */ void USART2_Data_Load(uint16_t temp1, uint16_t temp2, uint8_t temp3){, I/ h4 ~* q: T) E3 k5 S USART2_DMA_TX_Buf[0] = HEX_CODE[temp1 >> 4];- _" c) ?- M5 R1 @# s; S& h1 y USART2_DMA_TX_Buf[1] = HEX_CODE[temp1 & 0x0F];( g6 u/ F/ u3 U+ _' |9 M9 G7 L USART2_DMA_TX_Buf[2] = HEX_CODE[temp2 >> 4];) R0 _ s3 T& g; { 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; USART2_DMA_TX_Buf[7] = 0x0A;$ n. `! |+ O6 w Flag_USART2_Send = 1; }# t! a; A# t& K /**# t" y8 B( f* U; D * Function Name : USART2_Data_Send% b* M# J$ | Z, ?0 _+ G * Description : None * Input : None! A* Z# |. y V+ s3 F- `: I * Output : None * Return : None */ void USART2_Data_Send(uint16_t len) {3 u y! C9 r7 a+ v7 W( w if(Flag_USART2_Send){4 h1 N) q+ @ c: C' B& n. d: ^ if(Flag_USART2_DMA_TX_Finished == 1){ Flag_USART2_DMA_TX_Finished = 0; DMA1_Channel7->CMAR = (uint32_t)&USART2_DMA_TX_Buf[0];/ B+ o6 a- J' C: Y2 \. Z8 J/ h DMA1_Channel7->CNDTR = 8; // len DMA_Cmd(DMA1_Channel7, ENABLE);3 |! ^/ c. ]% { Flag_USART2_Send = 0;) t; F- n, Q# P- e } }4 o g5 V% c# a } /**5 n* A; S1 D5 ?. P ^ * Function Name : USART2_IRQHandler * Description : This function handles USART2 global interrupt request.- E- a# }+ R$ z, `1 c; |( x * Input : None * Output : None" G+ H0 \$ A% ^2 m * Return : None* B* ?1 h3 N) L5 g8 V3 G. E+ d4 h8 I */ void USART2_IRQHandler(void) { if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) {$ C0 c, T* C4 ^% ~( ~0 ?3 y: X6 n (void)USART_ReceiveData(USART2);% l o1 v9 h" O$ c0 F C3 b }& C4 C6 I7 V/ ~) g9 s) e9 G- J if (USART_GetITStatus(USART2, USART_IT_TC) != RESET) {: c+ M6 S# n7 S: a! K+ ] r /* Disable the USART2 Transmit Complete interrupt */7 b/ b$ z1 d8 x1 y! r) M4 ~ USART_ITConfig(USART2, USART_IT_TC, DISABLE); Flag_USART2_DMA_TX_Finished = 1; } - U) F9 g$ b8 |% }4 x9 e% O7 k /* If overrun condition occurs, clear the ORE flag a.nd recover communication */ + X8 T9 q) U% A5 m7 l if(USART_GetFlagStatus(USART2,USART_FLAG_ORE) != RESET) { (void)USART_ReceiveData(USART2);/ {) G7 p3 P. y5 e. ^! _ }! G% Y. ^# [: Y0 X$ A m9 M } /** * Function Name : DMA1_Channel7_IRQHandler& k1 U* e3 J1 t* p. h5 `9 M * Description : This function handles DMA1 Channel 7 interrupt request./ ]+ Z) g: g# T6 v. R4 |8 @* a4 ^! q * Input : None * Output : None3 M) g! W9 v8 U6 g- R0 P' s$ P * Return : None */ void DMA1_Channel7_IRQHandler(void)0 E0 V9 ?0 C' \' G; G, R {+ R) ^2 f% `3 R& w if(DMA_GetITStatus(DMA1_IT_TC7)) { /* USART2 DMA 传输完成 */; F# `, ]# ]$ ~7 D9 s. o DMA_ClearITPendingBit(DMA1_IT_TC7);3 _ W+ D7 B) {0 s l. w f! u DMA_Cmd(DMA1_Channel7, DISABLE);1 K* x# s/ f5 M9 B /* Enable USART2 Transmit complete interrupt */0 z5 d; e# G2 p, K1 e, L( S. }7 \ USART_ITConfig(USART2, USART_IT_TC, ENABLE); } } , p/ V/ Z+ J& F7 m |
小马哥STM32F103开源小四轴RoboFly全部资料大放送
STM32固件库分享,超全系列整理
STM32F10xxx 正交编码器接口应用笔记 及源代码
分享一个STM32F103的硬件IIC代码库,完美解决IIC问题!
OpenBLT移植到STM32F103战舰开发板上适用于所有STM32F103系列的Bootloader
MultiButton移植到STM32F103战舰开发板
UDS诊断服务
使用Nano板验证驱动SPI串口屏的颜色显示
使用STM32F103制作的无线下载器
STM32F103移植LittlevGUI代码
所谓STM32F103串口数据的DMA发送,其本质流程如下:9 [9 h, x: i% `! D6 X
1. 要发送的数据放在USART2_DMA_TX_Buf缓冲。& L. {% ~. Q2 J0 }, P
2. STM32串口DMA发送和DSP相比的优势。
启动DMA传输后,USART2_DMA_TX_Buf缓冲中的数据通过DMA1_Channel7通道,/ U. Y1 C H& x* L8 g+ w& @! P
自动传输到USART2->DR寄存器,这个工作不需要CPU干预,可以极大的节省CPU的资源。9 R" [3 |: H4 B' y6 p; `
和DSP的FIFO相比,这优势很大,因为DSP的FIFO只有16字节,这意味着DSP的串口通过/ S# [8 M. g! L/ N
FIFO发送数据时,如果FIFO缓冲为空,可以迅速填入16个字节,然后去处理其它的事务。
而STM32的DMA发送则没有此种限制。更具体的说,如果DSP发送的数据超过16字节,
则必须等待前16个字节数据发送完毕,然后再继续发送后续的数据,需要第二次甚至
第N次处理。而STM32启动一次DMA就搞定(DMA最大传输65535个数据)。- y6 G/ C. v; s+ r! d: r. [2 J' I
3. 发送结束的一些事务处理% p' _, [5 q0 A; |
DMA传输完毕,数据全部通过DMA1_Channel7通道依次传入USART2-DR,这里开启了0 S$ H1 L. y! ~; z+ v$ f; e
DMA传输完毕中断,进这个中断时,USART2-DR寄存器是最后一个要发送的数据, B+ ^. m% o9 W6 l' g1 Q) n
此时打开USART2发送完毕中断,进入USART2发送完毕中断后,可以设定标识,5 G+ ^9 v4 V; t6 E7 T6 O
也可以操作GPIO, 进行RS485的换向动作。
9 g; w" H6 z% F) U3 p& ^/ I
最新的STM32F103的HAL库,几乎所有的通信,都可以启用DMA,HAL库函数抽象层次过高,灵活性不够,
但比较适合使用操作系统的场合。可以把HAL库的宏定义拷贝过来使用,以提高标准外设驱动库的效率,
又保证标准外设驱动库的灵活性。: k2 O7 W+ g2 S2 H& W- I' H% \# P
( g+ N" D! K6 t9 m6 d* i( z