(5)mbed实战——HMC5883地磁传感器(指南针)
时间:10-02
整理:3721RD
点击:
mbed实战——HMC5883地磁传感器(指南针)还是先上效果图:
亮着绿色LED的板子专门应对IIC总线接多个设备用的,可以接8个设备。
HMC5883L模块也就是电子指南针罗盘模块,也叫三轴磁场传感器
通信方式:IIC通信协议
测量范围:±1.3-8 高斯
对于数据量不太大的场合,IIC非常方便。
下面是程序:
- #include "mbed.h"
- #include "1602_iic_sw.h"
- #include "HMC5883L.h"
- #define GREEN_LED PB_0
- #define BLUE_LED PB_7
- #define RED_LED PB_14
- //DigitalOut myled1(GREEN_LED);
- //DigitalOut myled2(BLUE_LED);
- //DigitalOut myled3(RED_LED);
- PwmOut PWM1(GREEN_LED);
- PwmOut PWM2(BLUE_LED);
- PwmOut PWM3(RED_LED);
- // I2C_SCL = PB_8,
- // I2C_SDA = PB_9,
- IIC_1602 my1602(I2C_SDA, I2C_SCL);
- HMC5883L myHMC5883L(I2C_SDA, I2C_SCL);
- float gValue = 0;
- char data = 0;
- int xdata[3] = {0};
- Serial pc(PA_2,PA_3);
- int main() {
- PWM1.period(0.01);
- PWM2.period(0.01);
- PWM3.period(0.01);
-
- myHMC5883L.setOpMode(HMC5883L_CONTINUOUS, HMC5883L_1HZ_NORMAL, HMC5883L_0_7GA);
- while(1) {
- PWM1 = gValue;
- PWM2 = 1 - gValue;
- PWM3 = gValue;
- gValue = gValue + 0.001;
- wait(0.001); // 1 sec
- if(gValue >= 1)
- {
- gValue = 0;
- myHMC5883L.readData(xdata);
- my1602.setCursor(0, 0);
- my1602.lcdPrint("x:");
- my1602.lcdPrint_int(xdata[0]);
-
- my1602.setCursor(8, 0);
- my1602.lcdPrint("y:");
- my1602.lcdPrint_int(xdata[1]);
-
- my1602.setCursor(0, 1);
- my1602.lcdPrint("z:");
- my1602.lcdPrint_int(xdata[2]);
-
- printf("xdata[0] = %d ", xdata[0]);
- printf("xdata[1] = %d ", xdata[1]);
- printf("xdata[2] = %d\n", xdata[2]);
- }
- }
- }
1602显示的同时,串口上也发送着同样的数据
大神,有原理图么?学习学习下!感谢!
IIC总线的,看程序接上去就好了