#include "include.h"
#include <math.h>
uint8_t BUF[8];
double angle;
int16_t X_HMC,Y_HMC,Z_HMC,x,y,z;
int16_t x_offest=10,y_offest=175;
double y_gain=0.967;
//******************************************************
//连续读出HMC5883内部角度数据,地址范围0x3~0x5
//******************************************************
void Multiple_Read_HMC5883L(void)
{
u8 i;
I2C_Start(); //起始信号
I2C_SendByte(HMC5883L_ADDRESS); //发送设备地址+写信号
I2C_WaitAck();
I2C_SendByte(0x03); //发送存储单元地址,从0x3开始
I2C_WaitAck();
I2C_Start(); //起始信号
I2C_SendByte(HMC5883L_ADDRESS+1); //发送设备地址+读信号
I2C_WaitAck();
for (i=0; i<6; i++) //连续读取6个地址数据,存储中BUF
{
BUF[ i] = I2C_RadeByte(); //BUF[0]存储数据
if (i == 5)
I2C_NoAck(); //最后一个数据需要回NOACK
else
I2C_Ack(); //回应ACK
}
I2C_Stop(); //停止信号
delay5ms();
x=BUF[0] << 8 | BUF[1]; //Combine MSB and LSB of X Data output register
z=BUF[2] << 8 | BUF[3]; //Combine MSB and LSB of Z Data output register
y=BUF[4] << 8 | BUF[5]; //Combine MSB and LSB of Y Data output register
X_HMC = 1 *(x + x_offest);
Y_HMC = (double)(y_gain * (y +y_offest));
Z_HMC = (double)(1.073 * (z +30));
angle= atan2((double)Y_HMC,(double)X_HMC) * (180 / 3.14159265) + 180; // angle in degrees
angle*=-10;
}
void Init_HMC5883L()
{
Single_Write(HMC5883L_ADDRESS,0x02,0x00);
}
- #include "include.h"
- int main(void)
- {
- SystemInit();
- delay_init(72);
- LED_Init();
- //OLED_Init();
- //Draw_LibLogo();
- KEY_Init();
- //ADC1_Init();
- delay(0XFFF);
- I2C_INIT();
- delay(0XFFF);
- InitMPU6050(); //MPU6050初始化
- //Init_HMC5883L(); //地磁初始化
- // MS5611_Init();
- //TIM4_Cap_Init(); //PWM输入捕获初始化
- TIM4_Cap_Init(100,72);
- //Moto_Init(); //电机初始化
- Nvic_Init();
- usart1_config(); //串口1初始化
- //usart3_config(); //串口3初始化
- delay(0XFFF);
- //ALGH_set(); //设置油门形成
- //OLED_Fill(0x00);
- TIM6_Init(1000); //定时2.5MS
- TIM3_Init(1);
- TIM2_Init(1);
- //PID_INIT(); //加载PID参数
- while(1)
- {
- //TODO
- // if(getLIR())
- // {
- // LED1(ON);LED2(OFF);LED3(ON);LED4(OFF);
- // }
- // else
- // {
- // LED1(OFF);LED2(OFF);LED3(OFF);LED4(OFF);
- // }
-
- }
- }
-
复制代码
初学电子,如有错误,请大家多多指导,代码仅供参考:
stm32-smart-car-20200509.7z
(210.06 KB, 下载次数: 47)
|