標(biāo)題:
基于STM32F103ZET6驅(qū)動(dòng)MPU9250程序 OLED顯示
[打印本頁(yè)]
作者:
@what1
時(shí)間:
2022-2-10 15:39
標(biāo)題:
基于STM32F103ZET6驅(qū)動(dòng)MPU9250程序 OLED顯示
STM32F103ZET6+MPU9250+0.96寸OLED
MPU9250陀螺儀基本數(shù)據(jù)讀取(定時(shí)器)MPU9250陀螺儀角度數(shù)據(jù)解算
單片機(jī)源程序如下:
/**
******************************************************************************
* MPU9250陀螺儀角度數(shù)據(jù)解算
* STM32F103ZET6系統(tǒng)板+MPU9250模塊+USB轉(zhuǎn)TTL電平工具+0.96寸OLED
*
******************************************************************************
*/
#include "main.h"
int main(void)
{
SYS_Init(); //系統(tǒng)初始化總函數(shù)
while(1) //主循環(huán)
{
MPU_Read(); //MPU6050數(shù)據(jù)讀取
DATA_Report(); //MPU6050數(shù)據(jù)上報(bào)
}
}
/**
* @brief 系統(tǒng)初始化總函數(shù)
* @param 無(wú)
* @retval 無(wú)
*/
void SYS_Init(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中斷優(yōu)先級(jí)分組函數(shù)
delay_init(); //延時(shí)函數(shù)初始化
uart_init(115200); //串口初始化為115200
LED_Init(); //初始化與LED連接的硬件接口
OLED_Init(); //OLED初始化
if(MPU_Init() == 0) //初始化MPU9250
printf("ID讀取正常\r\n"); //ID讀取正常
else
printf("ID讀取不正常\r\n"); //ID讀取不正常
while(1)
{
error = mpu_dmp_init(); //初始化mpu_dmp庫(kù)
printf("ERROR:%d\r\n",error); //串口顯示初始化錯(cuò)誤值
switch (error) //對(duì)不同錯(cuò)誤值類型判斷
{
case 0:printf("DMP庫(kù)初始化正常\r\n");break;
case 1:printf("設(shè)置傳感器失敗\r\n");break;
case 2:printf("設(shè)置FIFO失敗\r\n");break;
case 3:printf("設(shè)置采樣率失敗\r\n");break;
case 4:printf("加載dmp固件失敗\r\n");break;
case 5:printf("設(shè)置陀螺儀方向失敗\r\n");break;
case 6:printf("設(shè)置dmp功能失敗\r\n");break;
case 7:printf("設(shè)置DMP輸出速率失敗\r\n");break;
case 8:printf("自檢失敗\r\n");break;
case 9:printf("使能DMP失敗\r\n");break;
case 10:printf("初始化MPU6050失敗\r\n");break;
default :printf("未知錯(cuò)誤\r\n");break;
}
if(error == 0)break; //如果沒(méi)有錯(cuò)誤直接退出循環(huán)
delay_ms(200); //延時(shí)
LED0 =! LED0; //LED0閃爍報(bào)錯(cuò)
sprintf((char *)tmp_buf," ERROR:%d",error);//字符串格式化命令
OLED_ShowString(0,0,(u8 *)tmp_buf,16); //OLED顯示錯(cuò)誤值
OLED_Refresh(); //刷新顯存
}
delay_ms(999);
OLED_ShowString(0,0,(u8*)" MPU9250",16); //OLED顯示字符串
OLED_Refresh(); //刷新顯存
}
/**
* @brief MPU6050數(shù)據(jù)讀取函數(shù)
* @param 無(wú)
* @retval 無(wú)
*/
void MPU_Read(void)
{
if(mpu_dmp_get_data(&yaw,&pitch,&roll)==0) //dmp處理得到數(shù)據(jù),對(duì)返回值進(jìn)行判斷
{
printf("Pitch:%f Roll:%f Yaw:%f\r\n",pitch,roll,yaw);//串口輸出三軸角度
mpu9250.speed++; //顯示速度自加
if(mpu9250.speed == 1) //顯示速度閾值設(shè)置
{
mpu9250.flag = 1; //采集成功標(biāo)志位設(shè)置為有效
mpu9250.speed = 0; //顯示速度歸零
}
}
else //采集不成功
{
mpu9250.flag = 0; //采集成功標(biāo)志位設(shè)置為無(wú)效
}
}
/**
* @brief MPU6050數(shù)據(jù)上報(bào)
* @param 無(wú)
* @retval 無(wú)
*/
void DATA_Report(void)
{
if(mpu9250.flag == 1) //采集成功時(shí)
{
temp=pitch*100; //賦temp為pitch
if(temp<0) //對(duì)數(shù)據(jù)正負(fù)判斷,判斷為負(fù)時(shí)
{
temp=-temp; //對(duì)負(fù)數(shù)據(jù)取反
sprintf((char *)tmp_buf," Pitch:-%.3d.%.2d",temp/100,temp%100);//字符串格式化命令
}
else //判斷為正時(shí)
{
sprintf((char *)tmp_buf," Pitch: %.3d.%.2d",temp/100,temp%100);//字符串格式化命令
}
OLED_ShowString(0,16,(u8 *)tmp_buf,16); //OLED顯示字符串
temp=roll*100; //賦temp為pitch
if(temp<0) //對(duì)數(shù)據(jù)正負(fù)判斷,判斷為負(fù)時(shí)
{
temp=-temp; //對(duì)負(fù)數(shù)據(jù)取反
sprintf((char *)tmp_buf," Roll :-%.3d.%.2d",temp/100,temp%100);//字符串格式化命令
}
else //判斷為正時(shí)
{
sprintf((char *)tmp_buf," Roll : %.3d.%.2d",temp/100,temp%100);//字符串格式化命令
}
OLED_ShowString(0,48,(u8 *)tmp_buf,16); //OLED顯示字符串
temp=yaw*100; //賦temp為pitch
if(temp<0) //對(duì)數(shù)據(jù)正負(fù)判斷,判斷為負(fù)時(shí)
{
temp=-temp; //對(duì)負(fù)數(shù)據(jù)取反
sprintf((char *)tmp_buf," Yaw :-%.3d.%.2d",temp/100,temp%100);//字符串格式化命令
}
else //判斷為正時(shí)
{
sprintf((char *)tmp_buf," Yaw : %.3d.%.2d",temp/100,temp%100);//字符串格式化命令
}
OLED_ShowString(0,32,(u8 *)tmp_buf,16); //OLED顯示字符串
OLED_Refresh(); //刷新顯存
LED1=!LED1; //LED閃爍
mpu9250.flag = 0; //采集成功標(biāo)志位設(shè)置為無(wú)效
}
else ; //防卡死
}
復(fù)制代碼
Keil代碼下載:
驅(qū)動(dòng)程序.7z
(259.35 KB, 下載次數(shù): 43)
2022-2-13 05:06 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
qinyuning
時(shí)間:
2022-3-3 22:39
其他數(shù)據(jù)怎么讀 加速度 磁力
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1