基于MPU6050的二輪平衡車,注釋比較多,方便理解。
單片機源程序如下:
- /******************** (C) COPYRIGHT (2015)BST BALANCECAR **************************
- * 文件名 :main.c
- **********************************************************************************/
- #include "stm32f10x.h"
- #include "mpu6050.h"
- #include "i2c_mpu6050.h"
- #include "motor.h"
- #include "upstandingcar.h"
- #include "SysTick.h"
- #include "led.h"
- #include "usart.h"
- #include "i2c.h"
- #include "outputdata.h"
- #include "timer.h"
- #include "UltrasonicWave.h"
- #include "stm32f10x_usart.h"
- float gyz;
- int acc;
- int acc1;
- /*協(xié)議相關*/
- //extern u8 newLineReceived = 0;
- /*
- * 函數(shù)名:main
- * 描述 :主函數(shù)
- */
- int main(void)
- {
-
-
- SystemInit(); //=====系統(tǒng)初始化
- Timerx_Init(5000,7199); //定時器TIM1
- UltrasonicWave_Configuration(); //超聲波初始化設置 IO口及中斷設置
- USART1_Config(); //串口1初始化 上位機
- USART3_Config(); //串口3初始化 藍牙與USART3公用相同IO口
-
- TIM2_PWM_Init(); //PWM輸出初始化
- MOTOR_GPIO_Config(); //電機IO口初始化
- LED_GPIO_Config();
-
- TIM3_External_Clock_CountingMode(); //左電機脈沖輸出外部中斷口PA7使用TIM3定時器用作為脈沖數(shù)計算
- TIM4_External_Clock_CountingMode(); //右電機脈沖輸出外部中斷口PB7使用TIM4定時器用作為脈沖數(shù)計算
- TIM3_Encoder_Init(); //編碼器獲取脈沖數(shù) PA6 7
- TIM4_Encoder_Init(); //編碼器獲取脈沖數(shù) PB6 7
- ////////////////////DMP/////////////////////////////////
- i2cInit(); //IIC初始化 用于掛靠在總線上的設備使用
- delay_nms(10); //延時10ms
- MPU6050_Init(); //MPU6050 DMP陀螺儀初始化
-
- SysTick_Init(); //SysTick函數(shù)初始化
- CarUpstandInit(); //小車直立參數(shù)初始化
- SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; //使能總算法時鐘
- while (1)
- {
- GPIO_ResetBits(GPIOC, GPIO_Pin_13);
- MPU6050_Pose(); //獲取MPU6050角度狀態(tài)
- gy0=gyro[0];
- // UltrasonicWave_StartMeasure(); //調(diào)用超聲波發(fā)送程序 給Trig腳 <10us 高電平
- // chaoshengbo(); //計算超聲波測距距離
- // printf("%d",ucBluetoothValue);
- // printf("\t");
- // printf("%f",BST_fSpeedControlOutNew);
- // printf("\t");
- // printf("%f",BST_fCarAngle);
- // printf("\t");
- // printf("%f",BST_fLeftMotorOut);
- // printf("\t");
- // printf("\n");
-
- if (newLineReceived)
- {
- ProtocolCpyData();
- Protocol();
- }
- /*通過狀態(tài)控制小車*/
- CarStateOut();
-
- SendAutoUp();
-
-
- }
-
- }
復制代碼
所有資料51hei提供下載:
STM32平衡車.7z
(500.19 KB, 下載次數(shù): 93)
2019-7-18 21:32 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|