|
拿到开发板有一段时间,看了下资料,使用mbed平台那简直是太简单了,但mbed封装的太严实,实在是不利于我们了解MCU内部运作机理,所以还是要使用ST的库(直接寄存器操作就免了,我不想回到石器时代),现在ST库除了F1系列已经改变,采用新的HAL Drivers的方式,这种方式更友好,更符合软件工程。使用STM32CubeMX把和MCU相关的代码已经生成的90%了,剩下和10%需要自己来完成,所有的中断基本上采用“伪回调”的方式,使用用户更专注于业务算法本身。废话不多说了,直接上代码: M* p- T2 z* B4 y2 S& I/ Z: ^, m -------------------------------------------------------------------------------传说中的分界线---------------------------------------------------------------------------- main.c #include "stm32l0xx_hal.h" #include "dma.h" #include "i2c.h" #include "tim.h"5 Y$ r- g( i& d$ X7 q+ v #include "usart.h"# `+ D0 S/ d3 A7 m, f0 E* Y9 I #include "gpio.h" #include "mpu6050.h" #include "visualscope.h" #include <string.h> #include <math.h>- @7 T& B$ d( j8 J+ | X6 k, h) R+ @5 D4 I /* Private variables ---------------------------------------------------------*/( K6 {, y8 B7 u #define RXSIZE 8$ {5 c$ [* a2 D0 Z) u& A. q6 Y #define TXSIZE 8% r1 Z- T. F) {7 h6 |5 a" x1 t uint8_t rxBuffer[RXSIZE]; uint8_t txBuffer[TXSIZE];. b( F/ Z* U5 v3 J+ v+ M/ M float Angle_accel = 0;+ Z# O& j9 r# b4 q* e# o float Angle_gyro = 0; float Angle_comp = 0; uint8_t outAngleData[10]; /* USER CODE BEGIN 0 */" E! u9 |/ `+ n& K 0 q) r* T: K0 K! I /*---------------------------------------------------------------------------------------------------------*/ /* 重力加速度算出的角度 */$ i+ k/ a) s$ y0 ?( P8 i/ s( ? /*---------------------------------------------------------------------------------------------------------*/9 ]) r2 ?) N# P* o$ R$ | int32_t Calc_AngleAccel() { int32_t value = 0;+ W4 v7 w O5 e4 u4 s ( Q) j0 n9 {7 g3 g5 n" U value = GetAccelValue('x'); if(value > 16384) Angle_accel = -(asin(1) * 180 / 3.1415296); else# b8 L/ O% @' x: g Angle_accel = -(asin(value * 1.0 / 16384) * 180 / 3.1415296); 8 e& ?! f: e7 }. Z4 p return value;* [. e% { G: F/ A } 1 p9 l1 T8 I1 ^) m7 q. e8 A /*---------------------------------------------------------------------------------------------------------*/; R9 h/ W8 _" a; A' \ /* 角速度算出来的角度(积分) */ /*---------------------------------------------------------------------------------------------------------*/ int32_t Calc_AngleGyro()0 }" y! B9 p% G, _ { int32_t value = 0; value = GetGyroValue('y'); Angle_gyro += (value / 16.384 * 0.01); 4 L. O2 F8 |2 y return value; } /*---------------------------------------------------------------------------------------------------------*/5 W2 {* E7 [4 d5 @0 ^5 ]/ D- ]8 r /* 互补滤波求角度 */ /*---------------------------------------------------------------------------------------------------------*/ float ComplementFilter(int32_t simpleGyro)& A* f) Y" l& Q" b6 ? Y, X" } { Angle_comp = 0.98 * (Angle_comp + -simpleGyro / 16.384 * 0.01) + 0.02 * Angle_accel; return Angle_comp; } /*---------------------------------------------------------------------------------------------------------*/4 F8 b) i: t: ^( L I, N& r /* 输出角度至上位机 */" j9 R% B* @* j9 t /*---------------------------------------------------------------------------------------------------------*/) b% J M: x `3 q. \" Z, m) u void VisualScopeAngleOutput() {4 D* N: n: S6 V$ s int AngleValue_Accel,AngleValue_Gyro,AngleValue_Comp;! D( n. h- j; e! _, Z uint16_t crcValue; - t* F/ l" p% i AngleValue_Accel = ceil(Angle_accel * 10 -0.5); AngleValue_Gyro = ceil(Angle_gyro * 10 - 0.5);! W+ K0 u3 }- G9 F8 G AngleValue_Comp = ceil(Angle_comp * 10 - 0.5); outAngleData[0] = AngleValue_Accel & 0xFF; outAngleData[1] = AngleValue_Accel >> 8; outAngleData[2] = AngleValue_Gyro & 0xFF; outAngleData[3] = AngleValue_Gyro >> 8;. G- s2 I% i5 B7 w. R s6 t outAngleData[4] = AngleValue_Comp & 0xFF; outAngleData[5] = AngleValue_Comp >> 8; //计算CRC" \4 ], L4 b' t. K' B" ?7 r+ |5 _ crcValue = CRC_CHECK(outAngleData,8);. x% ~$ f3 _9 j1 j4 F1 T/ n. [4 { outAngleData[8] = crcValue & 0xFF; outAngleData[9] = crcValue >> 8; //发送至上位机 HAL_UART_Transmit_DMA(&huart2,outAngleData,sizeof(outAngleData)); }' g/ w4 Y# w+ k# @ /* USER CODE END 0 */* G+ R9 h' f9 u$ ~7 S- h ) q3 g" U; m0 v /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void);- M+ z7 I6 b' f2 U g- D8 E ( Y5 S! f( q5 h! k q B 硬件连接图:
波形图:% _* f! ]# k2 O( d; B
最后加一个视频: O3 ?$ J* @, h% [' s" P$ q STM32L053 演示 |
STM32MP1 自举程序编程:USB DFU/USART 协议全解析
STM32大神笔记,超详细单片机学习汇总资料
实战经验 | 如何配置STM32MP257的RMII Switch
简单实用!STM32硬件错误的调试技巧
OpenSTLinux 6.1发布:M33-TD加持+安全升级,STM32MPU开发效率翻倍!
STM32MP135 MP157 MP257 CoreMark移植&&跑分对比&&多核心负载不均衡问题排查与解决
STM32系列芯片之间相互移植注意事项
STM32如何成为现代科技的隐形引擎
STM32--STM32 微控制器详解
大神都是怎么学习STM32的,我也跟着学
微信公众号
手机版
#include "i2c.h"* w, L0 }8 O: j r7 j* P2 \
5 e" T8 @6 _; I* h
#include "gpio.h"( B& ~# `7 z- H5 s) e
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
) S; m9 g% f w# `
I2C_HandleTypeDef hi2cx;
/* I2C1 init function */
void MX_I2C1_Init(void)
{
( N# C2 u z! V' y, m4 }$ |
hi2cx.Instance = I2C1;
hi2cx.Init.Timing = 0x20D22930;4 o" n$ A+ {; P
hi2cx.Init.OwnAddress1 = 0;
hi2cx.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;0 _5 X T: m/ a. }; V* L+ l
hi2cx.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;9 n: S1 F0 T: c
hi2cx.Init.OwnAddress2 = 0;
hi2cx.Init.OwnAddress2Masks = I2C_OA2_NOMASK;3 b+ u. v6 M" |$ f. t5 @' k& ^/ a
hi2cx.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
hi2cx.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;! f4 ?# _+ Y1 U8 g4 s% ]/ L7 \
HAL_I2C_Init(&hi2cx);
/**Configure Analogue filter
*/& {% N5 v- P( W" \* j) c1 _5 k
HAL_I2CEx_AnalogFilter_Config(&hi2cx, I2C_ANALOGFILTER_ENABLED);
}* U' ?; z2 d" G" a% q
( n# ~- G# j+ C- F+ T' N7 b
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c). r9 D7 \$ X! X- _
{: }5 |# h: f' T2 Y
GPIO_InitTypeDef GPIO_InitStruct;
if(hi2c->Instance==I2C1)1 j* S8 W% @* b# R1 U/ C& a
{
/* Peripheral clock enable */& ?9 I0 U3 x S6 ~
__I2C1_CLK_ENABLE();0 Q$ ?7 B) E( I, Y3 F
5 L' [2 r5 O* [- G" V+ O& q: f q, l
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA2 Q% |/ F! x6 K4 L* c7 S$ a
*/
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;" [6 X# K+ x1 B% @6 }, W9 M
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;/ o( S a7 b5 x; A9 N$ F
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;/ G. s+ b2 [5 Z: A
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);0 u# H' \( @% o
/ a8 b7 O7 o5 y) v) f6 v
}: |, ~ Y4 A0 x! e9 d& }
}
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
{
if(hi2c->Instance==I2C1)
{
/* Peripheral clock disable */
__I2C1_CLK_DISABLE();' C7 L5 ]- z h3 J/ n
/ N4 o. I, X: @3 m
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL- z, ~4 w J$ N, y8 U" b
PB7 ------> I2C1_SDA- U& P H9 e4 L0 \( s
*/. Q: e$ v6 h7 i8 Q+ h2 M2 M8 B
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);$ R; W3 j r) U% e* ]
}* _1 ^3 I. W# \9 u8 L
}
------------------------------------------------------------------------------- 传说中的分界线 ----------------------------------------------------------------------------
usart.c- v) K7 G* d- c- {
#include "usart.h"
# L6 L$ F. e5 |% Q1 n" o3 s+ m- L
#include "gpio.h"3 V9 q6 M# Q$ P6 [) y
#include "dma.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
' I$ c, P" k, `$ g
UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart2_rx;
DMA_HandleTypeDef hdma_usart2_tx;5 h3 n; G- J$ H9 }5 k; m
/* USART2 init function */6 p" B8 _8 V2 }! B
void MX_USART2_UART_Init(void)
{! m, n7 T ?9 \: k9 `
. X7 Y+ d6 q3 |
huart2.Instance = USART2;6 |; |/ H% {) X) y, L
huart2.Init.BaudRate = 9600;, }+ X7 m; h1 N0 U% v/ H& h7 V
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;1 @1 Q. C8 i+ c+ \- d- _ t% d
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;; V6 i4 l9 f# K. i3 Y) ]
huart2.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;9 V" N2 s* n5 {/ P
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;! Q! ~4 [/ Q8 \1 a9 n, j( `4 n9 J
HAL_UART_Init(&huart2);
}( k# j$ ?, X2 v# N- e5 \4 @
void HAL_UART_MspInit(UART_HandleTypeDef* huart)' R* J$ U' B3 a
{
7 s4 G/ \' O: S' j- l) E
GPIO_InitTypeDef GPIO_InitStruct; r+ X$ \+ P6 c" o+ O
if(huart->Instance==USART2)% [3 i" P2 [. c4 b. h# F5 g
{1 D/ @* Q2 [, @; H
/* Peripheral clock enable */
__USART2_CLK_ENABLE();( _1 {' ~+ P E
/**USART2 GPIO Configuration
PA2 ------> USART2_TX+ y% ? N3 s) @" m0 j1 a
PA3 ------> USART2_RX( h: n- [3 C3 W' e
*/3 f' ?) I/ h5 U
GPIO_InitStruct.Pin = GPIO_PIN_2;5 f0 g. M) \2 `: j
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;5 X8 K& _3 l& ]! B. b
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;$ U8 c7 H% ?$ H4 x2 S- C4 F, p
GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;( H& `. M% {7 y D' i( y
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;+ @5 L% n V2 y/ c. e6 n
GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);! q; _. H( s' h' ^2 `$ _
5 a' D& c( N) I4 k( P" u. h
/* Peripheral DMA init*/3 @2 n, w7 l8 \: f( `8 w
hdma_usart2_rx.Instance = DMA1_Channel5;
hdma_usart2_rx.Init.Request = DMA_REQUEST_4;# h+ G! X8 [" ]
hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;. N' h% `* M/ B5 {
hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;8 C3 o1 E5 r) X. J. h
hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;. T0 Q7 ]$ b$ _, b; f
hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;$ U9 Q' n" v, l" C7 M6 S; ]5 a( B5 D
hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;3 B$ P7 c- S2 l1 L6 Q
hdma_usart2_rx.Init.Mode = DMA_NORMAL;! |- F. |' v* {! ?9 R
hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;
HAL_DMA_Init(&hdma_usart2_rx);
/ W0 b6 z% L) n. Z _( D }
__HAL_LINKDMA(huart,hdmarx,hdma_usart2_rx);: `4 q( G" O4 L: h4 j* p
# |' b) _, G/ \, {" G
hdma_usart2_tx.Instance = DMA1_Channel4;
hdma_usart2_tx.Init.Request = DMA_REQUEST_4;9 c: M: X* o( L7 k/ U v% z$ b% a
hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE;$ c2 C2 d( H3 J/ s
hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE;3 i0 Y* Z9 ]% |" U
hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;4 @6 c/ w. Q4 |" n
hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;4 U C+ r3 @% t3 C) D
hdma_usart2_tx.Init.Mode = DMA_NORMAL;4 @! p0 _# Y, @. i8 x1 q
hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW;! \) _% t8 C& T: a
HAL_DMA_Init(&hdma_usart2_tx);9 H3 S, Y5 ^, Y" t- p$ d1 ?
9 x* R2 D! ]' P$ K2 y
__HAL_LINKDMA(huart,hdmatx,hdma_usart2_tx);
9 z6 F1 d; I3 B3 o L3 z
}+ F/ r4 V7 _8 l+ |8 N$ j' e
}8 ?8 B$ a5 x1 k2 |
3 u8 r R& h0 o+ m% w ^4 a$ n
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
{
if(huart->Instance==USART2)0 f+ _- b- u5 Z7 \& \
{- a( e5 m% O( s9 \% o
/* Peripheral clock disable */
__USART2_CLK_DISABLE();
% }# e7 y" F, R
/**USART2 GPIO Configuration
PA2 ------> USART2_TX
PA3 ------> USART2_RX3 n8 K R" I3 x' L( E
*/) S5 N2 ?0 G3 D, J& Y7 d
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);$ R6 O2 `& j& R3 l' a9 ]) Y# N
9 w' g p' H' B& I# { U
/* Peripheral DMA DeInit*/8 m( |; Q% j$ }1 v, B0 P; j* y
HAL_DMA_DeInit(huart->hdmarx);
HAL_DMA_DeInit(huart->hdmatx);
}% F9 K% H! b+ a$ C
}: f' E# `) b; }" ^1 @+ X% i
------------------------------------------------------------------------------- 传说中的分界线 ----------------------------------------------------------------------------, u: B: ^' Z( `
mpu6050.c2 z1 _+ L0 C" R' y' a
#include "stm32l0xx_hal.h"' [2 j# T* Y. H9 J5 Z
#include "MPU6050.h"
//各坐标轴上静止偏差(重力,角速度)
int16_t offsetAccelX = -195;1 a% ~' T, M* b) i( h0 m9 z
int16_t offsetAccelY = 560;
int16_t offsetAccelZ = -169;
int16_t offsetGyroX = 12;* V Z" ]% {7 V9 G# {4 H( a$ f
int16_t offsetGyroY = 33;
int16_t offsetGyroZ = 4;
extern I2C_HandleTypeDef hi2cx;$ t7 F0 ~( b9 C3 s4 t$ I
//**************************************
//向I2C设备写入一个字节数据) X& K. h7 W7 d3 v7 O
//**************************************& _6 g( v3 W& _& k) N6 p7 n* ]* a( z
void Single_WriteI2C(uint8_t REG_Address,uint8_t REG_data)
{4 l+ ?/ U& Y2 i0 Y( a" W2 T2 F
uint8_t rxData[2] = {REG_Address,REG_data};
while(HAL_I2C_Master_Transmit(&hi2cx,SlaveAddress,rxData,2,5000) != HAL_OK)" \/ A8 b2 }& ~6 X; d- ?& y# V
{
if(HAL_I2C_GetError(&hi2cx) != HAL_I2C_ERROR_AF)
{}
}, D% P2 g( _, O7 p# E
}
//**************************************; }' k- t% Z/ [2 N- j3 v
//从I2C设备读取一个字节数据
//**************************************# F- O9 l/ d, k8 J' d
uint8_t Single_ReadI2C(uint8_t REG_Address)
{3 @0 q, X5 _; n( R/ C$ e* _
uint8_t REG_data;( K0 Z+ R8 z/ i
while(HAL_I2C_Master_Transmit(&hi2cx,SlaveAddress,®_Address,1,5000) != HAL_OK)
{
if(HAL_I2C_GetError(&hi2cx) != HAL_I2C_ERROR_AF)
{}& P' P! m. ]1 G' Y; h/ m8 C
}
$ G. S4 f6 f) l% G0 f r
if(HAL_I2C_Master_Receive(&hi2cx,SlaveAddress+1,®_data,1,5000) != HAL_OK)3 `4 D3 a$ J w% ^% D
{* U8 X! _/ W# e* ~* _4 J ]
if(HAL_I2C_GetError(&hi2cx) != HAL_I2C_ERROR_AF)
{}
}. F- }) ?8 K0 e
return REG_data;
}
//**************************************8 J0 K! @: p7 {% _. a/ ^( B
//初始化MPU60500 `& `) `9 x% B. I* r; D: p' W5 x
//**************************************
void InitMPU6050()
{" X7 A$ z' s1 B
Single_WriteI2C(PWR_MGMT_1, 0x00); //解除休眠状态6 f7 H2 ^: y$ E
' p# ~- C0 u4 w- @6 h! @
Single_WriteI2C(SMPLRT_DIV, 0x07);
Single_WriteI2C(CONFIG, 0x06);
2 P" h( P/ l4 E# T' c
Single_WriteI2C(GYRO_CONFIG, 0x18);
Single_WriteI2C(ACCEL_CONFIG, 0x01);
}
//**************************************
//合成数据
//**************************************
int16_t GetMPUOutValue(uint8_t REG_Address)9 i$ T1 B8 A; N5 y1 T$ B
{2 U+ z1 u, V2 @6 c# y; p
int16_t result;. D$ x" d4 u' y V c+ O& t
uint8_t H,L;
H=Single_ReadI2C(REG_Address);! L! ^5 ]+ V/ B+ H# g' R
L=Single_ReadI2C(REG_Address+1);
result = (H<<8)+L;) g2 S7 d5 v v5 C& k
return result; //合成数据: n' H4 ]' U2 d0 k( k5 @( a
}
9 M% ~5 p$ d( Z3 }! ` _6 o5 A
//**************************************: G" Z8 |, S# z
//取某一轴上的加速度数据
//**************************************, {8 z6 _! s( c: Q" |
int16_t GetAccelValue(char axis)
{
int16_t result = 0;
switch(axis)' B1 U- J0 e2 _" r6 a
{4 O8 i* o+ z" N7 n, c; T# i
case 'x':
case 'X':
{
result = GetMPUOutValue(ACCEL_XOUT_H) - offsetAccelX;+ w+ Z9 z1 b" ~3 v
}
break;; ^' v: G& D- A6 w/ _' L
case 'y':
case 'Y':
{7 T- e* T" t: l8 j) B0 Q2 f( F3 B
result = GetMPUOutValue(ACCEL_YOUT_H) - offsetAccelY;) y# y) d2 T) G# @2 T9 c K
}
break;) S8 n4 x* m. T
case 'z':; k" y. n: ]/ Z% V/ ]
case 'Z':
{7 w% {! b, I5 `& a
result = GetMPUOutValue(ACCEL_ZOUT_H) - offsetAccelZ;& ]# Q0 w* W8 A6 ]; X5 A9 n
}- X0 @# A, j/ v7 b2 A* D( @
break;
}
return result;) c0 W, l5 c) b5 C# A
}7 Z! r* x2 v/ c) f" e9 j- _2 o
* @3 N" i) h! m
//**************************************1 Q4 e0 _# T' {
//取某一轴上的角速度数据3 g/ A3 o) A6 @' }
//**************************************' N* u' _- ~8 @! a
int16_t GetGyroValue(char axis) `. N( a3 c+ a
{4 C$ @0 Z4 _, o5 k1 T! m/ x; U
int16_t result = 0;' S, h8 w# x2 I8 ~" m: c
switch(axis)1 Q, x$ l- V; E: i( Q
{
case 'x':! Y: l4 f: W* {
case 'X':7 ]( ]% D) t) e" g* L7 P
{
result = GetMPUOutValue(GYRO_XOUT_H) - offsetGyroX;
}1 ~. M4 X3 X- v3 V
break;
case 'y':# m% b3 c) F+ I- s# C d
case 'Y':- E; L" W( u/ I0 u0 D$ B
{8 a7 u' N/ `6 T$ r" b6 C2 U
result = GetMPUOutValue(GYRO_YOUT_H) - offsetGyroY;, c ]8 W1 _" i% v: W
} b. k# {0 G0 w* P* V4 H8 q
break;
case 'z':
case 'Z':
{
result = GetMPUOutValue(GYRO_ZOUT_H) - offsetGyroZ;; {# g i) h& a4 P- F
}
break;
}
return result;$ U: G; U% [: \' [
}
评分
查看全部评分
{
! W9 }' J: c* g
/* USER CODE BEGIN 1 *// [6 {+ T7 Y. _0 V
6 h `% |6 x" X9 H1 u2 v
/* USER CODE END 1 */
! f4 r% o, t* k: s2 g( s @
/* MCU Configuration----------------------------------------------------------*/7 o! [' z& ?2 }6 [/ m: G
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* System interrupt init*/$ T* c+ y& I& h3 u: e
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_I2C1_Init();
MX_TIM2_Init();
MX_USART2_UART_Init();
InitMPU6050();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_DMA(&huart2,rxBuffer,RXSIZE);
HAL_TIM_Base_Start_IT(&htim2);* U- Z& _& F! \6 U
/* USER CODE END 2 */+ y; R8 p) I; V2 C& H
+ O4 }2 J2 K b. w8 v5 d+ {8 u+ ^
/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)2 V% \* W; w4 g/ K1 ]
{
HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
VisualScopeAngleOutput();
HAL_Delay(100);
}
/* USER CODE END 3 */ R, S4 h/ {1 G( E: r
}' r2 I c6 h9 i
/** System Clock Configuration
*// X' D5 p* t1 h; l# X4 X
void SystemClock_Config(void)
{: o) Y9 t9 x: @
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
RCC_OscInitTypeDef RCC_OscInitStruct;
- T* g& b2 A0 n- _! T; h
__PWR_CLK_ENABLE();* ?2 ~6 P- l' i
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);0 j; ?. k6 s2 Y. U9 O1 f3 X7 R' l
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;* ^4 t- i& w1 A$ Y4 [5 A/ V3 I
RCC_OscInitStruct.HSIState = RCC_HSI_ON; O6 t! K: C8 i1 ~ n! b- y) U' A
RCC_OscInitStruct.HSICalibrationValue = 16;3 F/ L6 j9 Q0 }, X8 C+ D; \) |3 f+ {
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;5 e: o7 r! c9 K' W, S
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_6;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3;' q! B ] K, W5 ~- d' |+ C" Q. D
HAL_RCC_OscConfig(&RCC_OscInitStruct);. C3 H4 b. O! H
2 p+ T+ N% k, Q+ C% S; T
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;% e# A2 @+ P5 Q% `
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);$ j0 |7 x3 n& }- k
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;- s4 A: I" b1 y) O( d r4 d
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);( Q% q0 d+ W8 _, j c& u% c e
e6 W* n* `3 t( H; J
__SYSCFG_CLK_ENABLE();; ~, V2 C: k4 l1 b1 i9 C+ ]. w
}
0 M1 q! b' J7 b" T9 H
/* USER CODE BEGIN 4 */# _0 n8 N$ E5 D0 m! I4 w
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart): ~( O" l6 A5 g
{
if(huart->Instance == USART2)
{; U$ K' d$ C- S, ~
memcpy(txBuffer,rxBuffer,RXSIZE);
HAL_UART_Receive_DMA(huart,rxBuffer,RXSIZE);) E- N3 l5 T& r4 u
HAL_UART_Transmit_DMA(huart,txBuffer,TXSIZE);- d' B5 ~9 o$ B& T0 T8 s& x
}7 k/ g1 X. l* T, g4 e: U- G
}
5 I, |- G* \* J7 A$ b2 c( K+ G
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)" G: j$ X+ N9 A9 S- A0 g$ M5 }
{
( A' K+ x6 y/ ]. U
}
. y* U7 ~; Z9 I+ Y5 p- Q( O
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) z5 w3 ^# f- c$ _
{
int32_t GyroValue;5 {3 e1 G' K% z! e' ?! ?& p) w5 [
7 N; Y G- t2 U8 R
Calc_AngleAccel();$ j- p( @ }/ i+ l
GyroValue = Calc_AngleGyro();
ComplementFilter(GyroValue);
}
使用DMA方式,接收发送数据必须是个固定长度。
如果是长度不固定,你使用中断方式,然后加起止,停止标志位来判断。
好的,多谢指点!
请问应该注意什么地方
我使用是没有问题的呀。