標題:
stm32 HAL庫 mpu6050源程序
[打印本頁]
作者:
豆腐坊21
時間:
2019-7-31 21:14
標題:
stm32 HAL庫 mpu6050源程序
采用了模擬IIC,讀取mpu6050數(shù)據(jù),以及濾波
人懶,不想附圖。
cubeMX配置的STM32F103ZETx的文件
單片機源程序如下:
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usart.h"
#include "mpu6050.h"
#include "mpu_i2c.h"
#include "stm32f1xx_it.h"
#include "OLED.h"
#include "OLED_I2C.h"
#include "filter.h"
#include "math.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern unsigned int Task_Delay[NumOfTask];
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
#define TASK_ENABLE 0
#define PI 3.14159265
float Angle_Balance,Gyro_Balance,Gyro_Turn; //平衡環(huán)控制相關變量
float Encoder_left,Encoder_right; //速度環(huán)控制相關變量
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
short Accel[3];
short Gyro [3];
short Temp;
int Accel_Y,Accel_X,Accel_Z,Gyro_X,Gyro_Y,Gyro_Z;
float Acceleration_Z; //Z軸加速度計
//uint8_t i;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
//********OLED和OLED對應的iic初始化*******//
OLED_Init();
/*MPU_I2C初始化*/
MPU_I2C_InitGPIO();
/*MPU6050初始化*/
MPU6050_Init();
HAL_Delay(10);
/**********OLED清屏**************/
OLED_CLS();
/* 調(diào)用格式化輸出函數(shù)打印輸出數(shù)據(jù) */
//檢測MPU6050
if (MPU6050ReadID() == 0)
{
OLED_ShowStr(3,0,(unsigned char*)"can't find mpu6050!",1);
while(1);
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(Task_Delay[0]==TASK_ENABLE)
{
Task_Delay[0]=1000;
}
if(Task_Delay[1]==0)
{
MPU6050ReadAcc(Accel);
//dprintf("\r\n加速度: %8d%8d%8d ",Accel[0],Accel[1],Accel[2]);
Accel_X = Accel[0];
Accel_Y = Accel[1];
Accel_Z = Accel[2];
//OLED_ShowNum(40,1,Accel[0],5,20);
MPU6050ReadGyro(Gyro);
//printf("陀螺儀: %8d%8d%8d ",Gyro[0],Gyro[1],Gyro[2]);
Gyro_X = Gyro[0];
Gyro_Y = Gyro[1];
Gyro_Z = Gyro[2];
MPU6050_ReturnTemp(&Temp);
//printf("溫度: %d",Temp);
if(Gyro_Y>32768) Gyro_Y-=65536; //數(shù)據(jù)類型轉(zhuǎn)換 也可通過short強制類型轉(zhuǎn)換
if(Gyro_Z>32768) Gyro_Z-=65536; //數(shù)據(jù)類型轉(zhuǎn)換
if(Accel_X>32768) Accel_X-=65536; //數(shù)據(jù)類型轉(zhuǎn)換
if(Accel_Z>32768) Accel_Z-=65536; //數(shù)據(jù)類型轉(zhuǎn)換
//Gyro_Balance=-Gyro_Y; //更新平衡角速度
Accel_Y=atan2(Accel_X,Accel_Z)*180/PI; //計算傾角
Gyro_Y =Gyro_Y/16.4; //陀螺儀量程轉(zhuǎn)換
Kalman_Filter(Accel_Y,-Gyro_Y); //卡爾曼濾波
//Angle_Balance = angle; //更新平衡傾角
//Gyro_Turn=Gyro_Z; //更新轉(zhuǎn)向角速度
//Acceleration_Z=Accel_Z; //跟新z軸加速度
printf("\r\nY軸加速度和pitch: %8d,%8d",Gyro_Y,Accel_Y);
//printf("%8d\n",Accel_Y);
//Angle_Balance=angle; //更新平衡傾角
//Gyro_Turn=Gyro_Z; //更新轉(zhuǎn)向角速度
//Acceleration_Z=Accel_Z; //===更新Z軸加速度計
//Gyro_Balance=-Gyro_Y;
//printf("卡爾曼濾波值%8f,%8f\n",Angle_Balance,Gyro_Turn);
//OLED_ShowNum(40,2,Temp,5,10);
Task_Delay[1]=100;//此值每1ms會減1,減到0才可以重新進來這里,所以執(zhí)行的周期是100ms
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
復制代碼
所有資料51hei提供下載:
mpu_fliter_oled.7z
(271.84 KB, 下載次數(shù): 143)
2019-7-31 23:44 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
作者:
鵬博士PBs
時間:
2019-8-1 00:23
有些地方不是很清楚,可以注釋解釋一下嗎
作者:
遷就
時間:
2019-8-1 11:08
謝謝分享
作者:
豆腐坊21
時間:
2019-8-1 11:55
鵬博士PBs 發(fā)表于 2019-8-1 00:23
有些地方不是很清楚,可以注釋解釋一下嗎
本人比較菜,現(xiàn)在還在準備著今年電設用代碼,比賽完了之后補解釋
作者:
3697979
時間:
2019-8-1 16:20
看下 學習學習
作者:
704597517
時間:
2019-8-1 16:51
有msp430f5529的6050 pid 代碼嗎
作者:
畫地為牢qy
時間:
2019-8-2 10:02
找了好久,非常感謝,試試看
作者:
SouLT
時間:
2020-6-7 18:37
積分不夠 難受啊
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1