GY-271+QMC5883L电子指南针罗盘模块+三轴磁场传感器
单片机源程序如下:- #include "GY273.h"
- #include "i2c.h"
- #include "delay.h"
- #include "math.h"
- int16_t data[3];
- double gy273_xy_angle,gy273_yz_angle,gy273_xz_angle;
- double x,y,z;
- void hmc_write_reg(u8 reg,u8 data)
- {
- IIC_Start();
- IIC_Send_Byte(WRITE_ADDRESS);
- IIC_Wait_Ack();
- IIC_Send_Byte(reg);
- IIC_Wait_Ack();
- IIC_Send_Byte(data);
- IIC_Wait_Ack();
- IIC_Stop();
- //delay_ms(5);
- }
- u8 hmc_read_reg(u8 reg)
- {
- u8 data;
- IIC_Start();
- IIC_Send_Byte(WRITE_ADDRESS);
- IIC_Wait_Ack();
- IIC_Send_Byte(reg);
- IIC_Wait_Ack();
- IIC_Stop();
- IIC_Start();
- IIC_Send_Byte(READ_ADDRESS);
- IIC_Wait_Ack();
- data=IIC_Read_Byte();
- IIC_NAck();
- IIC_Stop();
- return data;
- }
- void hmc_read_XYZ(int16_t *data)
- {
- // int16_t temp;
- // temp=hmc_read_reg(DATAX_M);
- // *data++=(temp<<8)+hmc_read_reg(DATAX_L);
- *data++=(hmc_read_reg(DATAX_M)<<8)|hmc_read_reg(DATAX_L);
- // temp=hmc_read_reg(DATAY_M);
- // *data++=(temp<<8)+hmc_read_reg(DATAY_L);
- *data++=(hmc_read_reg(DATAY_M)<<8)|hmc_read_reg(DATAY_L);
- // temp=hmc_read_reg(DATAZ_M);
- // *data++=(temp<<8)+hmc_read_reg(DATAZ_L);
- *data++=(hmc_read_reg(DATAZ_M)<<8)|hmc_read_reg(DATAZ_L);
- // printf("%f %f %f %f\r\n",x,y,z,angle);
- }
- void hmc_init(void)
- {
- hmc_write_reg(0X0B,0x01);
- hmc_write_reg(0X20,0x40);
- hmc_write_reg(0X21,0x01);
- hmc_write_reg(CONFIGA,0x1D);
- delay_ms(10);
- }
- void gy273_Work(void){
- hmc_read_XYZ(data);
- gy273_xy_angle=atan2((double)data[0],(double)data[1])*(180 / 3.14159265)+180;
- gy273_yz_angle=atan2((double)data[1],(double)data[2])*(180 / 3.14159265)+180;
- gy273_xz_angle=atan2((double)data[0],(double)data[2])*(180 / 3.14159265)+180;
- x=(double)data[0];
- y=(double)data[1];
- z=(double)data[2];
- delay_ms(100);
- }
复制代码
程序.7z
(3.12 MB, 下载次数: 15)
|