STM32电机培训online,大佬带你玩电机 - X* Q" b: ^7 g( o #include "stm32f10x.h": Z5 p# q, z' v4 k# I #include "stm32f10x_usart.h") b( M) h; {7 o# _" l- X% f8 P #include "stm32f10x_dma.h" " Z+ h3 h8 Q5 p2 h7 c# r; z 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];& P4 i, Z! m; n uint8_t Flag_USART2_DMA_TX_Finished = 1; uint8_t Flag_USART2_Send = 0; # K B5 r4 X8 \: r) j. B- ]' w9 K /**2 d1 [4 K* ]6 ~ * Function Name : USART2_Config * Description : None * Input : None/ r0 o9 U( ~6 f+ d * Output : None& s. g1 o7 k! ~. U/ t9 d. A * Return : None) ~$ T+ f# p6 V8 j */ void USART2_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; e/ s1 H3 J$ R9 `. }/ b /* config USART2 clock */1 ?! Q. @5 b! [' p- z# r RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);% }) B- b& q7 ~9 @' | RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* *********************USART2 GPIO config **********************/ /* Configure USART1 Tx (PA.02) as alternate function push-pull */1 i. F% P* @3 O, k, O/ V GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;& m& n6 R4 J( O+ j# { GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;2 c4 V2 ~5 ?5 D( d8 ~7 x GPIO_Init(GPIOA, &GPIO_InitStructure); ) b3 N( B9 m# t2 ^9 r8 c /* Configure USART2 Rx (PA.03) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);; Z9 k) s( A \ /* USART2 mode config 115200 8-N-1*/ USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b;+ I+ _( [: U8 f USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No ;' {" e( c; u1 ` USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); * r1 b5 o- ^6 M1 B2 ?( C2 s USART_Cmd(USART2, ENABLE); + q4 g; t2 s6 N/ E& f |! Y& Z 1 c9 E% k* L) O, t7 L, d USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE); /* Enable USART2 DMA TX request */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_DeInit(DMA1_Channel7); 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;0 j9 P1 ]# u4 o: i: @% d4 v# P DMA_InitStructure.DMA_BufferSize = 0; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;# r! x5 p/ E+ \& q$ i" _+ S. B) D8 g8 C DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;! x% G+ D# L/ ?8 u% y7 i DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;$ g8 H* S; ? t' B DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel7, &DMA_InitStructure);' r! O4 z6 b+ m' [6 U5 d& i! f( [ DMA_ITConfig(DMA1_Channel7, DMA_IT_TC, ENABLE);1 ?9 D: Z4 A% d ( z u! I2 ^% ^/ v NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);0 m$ l( Y7 `+ X. @. w7 e/ B g /* Enable USART2 DMA TX Finish Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel7_IRQn;9 C( V- O% G3 U9 V; z% S3 ]1 S NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;- `/ T$ f9 N& V2 F# f NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;0 X& f1 b9 k- v9 n* M e, e NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;- r) ?+ X3 ~2 V9 i' I* \% J4 G1 U NVIC_Init(&NVIC_InitStructure); 6 Z' e e: c* [ /* Enable USART2 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_Init(&NVIC_InitStructure); } /**& q% U( |: y) j1 ^& f# |3 \9 {/ ` * Function Name : USART2_Data_Load( j D# W" l: n( [ * Description : None * Input : None7 Z0 c' J- \8 @+ T: A' G2 u * Output : None * Return : None8 N# @) z+ Q1 N" u; t/ ~ */ void USART2_Data_Load(uint16_t temp1, uint16_t temp2, uint8_t temp3){" P5 R1 b! G a f0 J6 o USART2_DMA_TX_Buf[0] = HEX_CODE[temp1 >> 4];9 H2 i) N8 |3 p USART2_DMA_TX_Buf[1] = HEX_CODE[temp1 & 0x0F]; USART2_DMA_TX_Buf[2] = HEX_CODE[temp2 >> 4];& t% q, J/ o; p$ y! B7 Q USART2_DMA_TX_Buf[3] = HEX_CODE[temp2 & 0x0F];* a; z J+ X! @3 b% ?6 ` USART2_DMA_TX_Buf[4] = 0x20; USART2_DMA_TX_Buf[5] = HEX_CODE[temp3]; USART2_DMA_TX_Buf[6] = 0x0D;) N8 g$ D. g4 T& n" A1 ] USART2_DMA_TX_Buf[7] = 0x0A; Flag_USART2_Send = 1;& Z0 S b9 X. s0 W+ m } /** * Function Name : USART2_Data_Send * Description : None# |# L2 M& h9 z, Z * Input : None * Output : None * Return : None */* i) Z3 H) K, T7 r void USART2_Data_Send(uint16_t len) {# J, Z" s+ h, l( _) o if(Flag_USART2_Send){ if(Flag_USART2_DMA_TX_Finished == 1){' X7 a$ l9 t3 ] Flag_USART2_DMA_TX_Finished = 0; DMA1_Channel7->CMAR = (uint32_t)&USART2_DMA_TX_Buf[0];2 v: J( R8 Y4 [: v2 W: ] DMA1_Channel7->CNDTR = 8; // len DMA_Cmd(DMA1_Channel7, ENABLE);3 M1 E/ l# h; L& V" G Flag_USART2_Send = 0;: V2 X* E2 j0 t' { } } }4 q# [% P8 H8 Z1 Y* Z( @/ m! o4 ]( J /**% |7 n; ^( t' W3 U* ] * Function Name : USART2_IRQHandler. G* `9 r0 L3 U; U, } * Description : This function handles USART2 global interrupt request.8 C3 o# @8 D1 x * Input : None4 B6 f7 k' S% Z7 A * Output : None * Return : None */ void USART2_IRQHandler(void) { if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) { (void)USART_ReceiveData(USART2);/ m8 M7 Y5 C; L }8 t) N8 K% R1 y9 R& @/ z" w$ F if (USART_GetITStatus(USART2, USART_IT_TC) != RESET) { /* Disable the USART2 Transmit Complete interrupt */ USART_ITConfig(USART2, USART_IT_TC, DISABLE);! t$ X( ]- ^8 f# s5 C' d Flag_USART2_DMA_TX_Finished = 1; } /* If overrun condition occurs, clear the ORE flag a.nd recover communication */ / G6 E! S) t# u& ^$ R$ r) H if(USART_GetFlagStatus(USART2,USART_FLAG_ORE) != RESET) {2 z4 u8 k3 A3 Z! j2 \. [6 l l (void)USART_ReceiveData(USART2); } } /** * Function Name : DMA1_Channel7_IRQHandler6 m9 `7 O# N$ V* {# B * Description : This function handles DMA1 Channel 7 interrupt request.& D( A# j7 ^' p) u2 b * Input : None A. g ~1 w1 x, \8 w" @ * Output : None * Return : None */8 Q M* O8 \- g5 w void DMA1_Channel7_IRQHandler(void) {% k* `# I7 U4 C% `/ C; N if(DMA_GetITStatus(DMA1_IT_TC7)) { /* USART2 DMA 传输完成 */ DMA_ClearITPendingBit(DMA1_IT_TC7);" a, o! l; f, Q2 l' a# F% F DMA_Cmd(DMA1_Channel7, DISABLE);3 ?8 u& j0 x$ q9 v* ~9 J /* Enable USART2 Transmit complete interrupt */ USART_ITConfig(USART2, USART_IT_TC, ENABLE); } } |
在高容量 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...
所谓STM32F103串口数据的DMA发送,其本质流程如下:: u4 h T( Z0 V6 v2 Z
1. 要发送的数据放在USART2_DMA_TX_Buf缓冲。4 [2 w9 i" ~2 `# }" W
2. STM32串口DMA发送和DSP相比的优势。6 T; M0 ~) ?$ r
启动DMA传输后,USART2_DMA_TX_Buf缓冲中的数据通过DMA1_Channel7通道,' z% [ h/ \: i( O1 U
自动传输到USART2->DR寄存器,这个工作不需要CPU干预,可以极大的节省CPU的资源。; ^6 A" ]4 y3 U( Q
和DSP的FIFO相比,这优势很大,因为DSP的FIFO只有16字节,这意味着DSP的串口通过6 h) L, u" e0 o: M
FIFO发送数据时,如果FIFO缓冲为空,可以迅速填入16个字节,然后去处理其它的事务。
而STM32的DMA发送则没有此种限制。更具体的说,如果DSP发送的数据超过16字节,+ Q o* }& m3 c2 S
则必须等待前16个字节数据发送完毕,然后再继续发送后续的数据,需要第二次甚至
第N次处理。而STM32启动一次DMA就搞定(DMA最大传输65535个数据)。
3. 发送结束的一些事务处理
DMA传输完毕,数据全部通过DMA1_Channel7通道依次传入USART2-DR,这里开启了. h/ d. ?+ p) A& X3 j
DMA传输完毕中断,进这个中断时,USART2-DR寄存器是最后一个要发送的数据,
此时打开USART2发送完毕中断,进入USART2发送完毕中断后,可以设定标识,
也可以操作GPIO, 进行RS485的换向动作。# Z# {& S. h* K$ Y
4 b& v. ?: h8 g- q
最新的STM32F103的HAL库,几乎所有的通信,都可以启用DMA,HAL库函数抽象层次过高,灵活性不够,% e' q9 C- ~$ Z* o7 r
但比较适合使用操作系统的场合。可以把HAL库的宏定义拷贝过来使用,以提高标准外设驱动库的效率,& U: A2 ]7 C; Z. A% N
又保证标准外设驱动库的灵活性。
' H0 i ?3 O9 ]8 E
9 `6 e2 R) y# A& n9 b2 p2 ^
6 m; c1 }2 a( f, `% b8 D. A