使用HMC5883l的带有stm32f4的I2C

时间:2018-09-11 10:47:37

标签: stm32 magnetometer

我一直在尝试将HMC5883l板连接到stm32f4,但是某些功能无法正常工作。我已经启动了i2c,将配置位发送到REGA,REGG和REGMR(模式寄存器),并通过USB连接与HAL_I2C_MEM_READ一起发送回去。

结果是:

  • xaxis和zaxis具有相同的值
  • 主循环仅工作一次,要获取另一个值,我需要插入STM

enter image description here

  • 我一直在尝试检查示波器的值(x轴)是否有变化,但根本没有变化(旋转电路板)

enter image description here

我在这里想念什么?电路板是否可能损坏?

 int main(void)
{
    /* USER CODE BEGIN 1 */
    /* USER CODE END 1 */

    /* MCU Configuration----------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* USER CODE BEGIN Init */
    /* USER CODE END Init */

    /* Configure the system clock */
    SystemClock_Config();

    /* USER CODE BEGIN SysInit */

    /* USER CODE END SysInit */

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_USB_DEVICE_Init();
    MX_I2C3_Init();
    /* USER CODE BEGIN 2 */
//  HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)

    HAL_I2C_Mem_Write(&hi2c3, HMC5883l_ADDRESS, 0b00000000 , 1, HMC5883l_Enable_A   , 1, 100);
    HAL_I2C_Mem_Write(&hi2c3, HMC5883l_ADDRESS, 0b00000001 , 1, HMC5883l_Enable_B   , 1, 100);
    HAL_I2C_Mem_Write(&hi2c3, HMC5883l_ADDRESS, 0b00000010 , 1, HMC5883l_MR         , 1, 100);

    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        HAL_I2C_Mem_Read(&hi2c3, HMC5883l_ADDRESS, HMC5883l_ADD_DATAX_MSB_LSB, 1, DataX, 2, 100);
        Xaxis = ((DataX[1] << 8) | DataX[0]);
        magn_x_gs = ((float)Zaxis*MAX_RESOLUTION)/(float)INT16_MAX; // conversion

        HAL_I2C_Mem_Read(&hi2c3, HMC5883l_ADDRESS, HMC5883l_ADD_DATAY_MSB_LSB , 1, DataY, 2, 100);
        Yaxis = ((DataY[1] << 8) | DataY[0]);
        magn_y_gs = ((float)Yaxis*MAX_RESOLUTION)/(float)INT16_MAX;

        HAL_I2C_Mem_Read(&hi2c3, HMC5883l_ADDRESS, HMC5883l_ADD_DATAZ_MSB_LSB , 1, DataZ, 2, 100);
        Zaxis = ((DataZ[1] << 8) | DataZ[0]);
        magn_z_gs = ((float)Zaxis*MAX_RESOLUTION)/(float)INT16_MAX;
    }

1 个答案:

答案 0 :(得分:0)

好的,我已经创建了正确的连接版本:)

// variables for receiving DATA
uint8_t DataX[2];
uint16_t Xaxis = 0;
uint8_t DataY[2];
uint16_t Yaxis = 0;
uint8_t DataZ[2];
uint16_t Zaxis = 0;

// adresses     
// HMC5883l - ADDRESS
#define HMC5883l_ADDRESS (0x1E << 1)
// CONTROL REG A
#define HMC5883l_Enable_A (0x78)
// CONTROL REG B
#define HMC5883l_Enable_B (0xA0)
// MODE REGISTER
#define HMC5883l_MR (0x00)
// HMC5883l - MSB / LSB ADDRESSES
#define HMC5883l_ADD_DATAX_MSB (0x03)
#define HMC5883l_ADD_DATAX_LSB (0x04)
#define HMC5883l_ADD_DATAZ_MSB (0x05)
#define HMC5883l_ADD_DATAZ_LSB (0x06)
#define HMC5883l_ADD_DATAY_MSB (0x07)
#define HMC5883l_ADD_DATAY_LSB (0x08)
// SUM (MSB + LSB) DEFINE
#define HMC5883l_ADD_DATAX_MSB_MULTI (HMC5883l_ADD_DATAX_MSB | 0x80)
#define HMC5883l_ADD_DATAY_MSB_MULTI (HMC5883l_ADD_DATAY_MSB | 0x80)
#define HMC5883l_ADD_DATAZ_MSB_MULTI (HMC5883l_ADD_DATAZ_MSB | 0x80) 

在main()内部,我必须创建变量以实际使用RegSetting等。

uint8_t RegSettingA = HMC5883l_Enable_A;
uint8_t RegSettingB = HMC5883l_Enable_B;
uint8_t RegSettingMR = HMC5883l_MR;

清除变量:

Xaxis = 0;
Yaxis = 0;
Zaxis = 0;

初始化连接:

HAL_I2C_Mem_Write(&hi2c3, HMC5883l_ADDRESS, 0x00 , 1, &RegSettingA , 1, 100);
HAL_I2C_Mem_Write(&hi2c3, HMC5883l_ADDRESS, 0x01 , 1, &RegSettingB , 1, 100);
HAL_I2C_Mem_Write(&hi2c3, HMC5883l_ADDRESS, 0x02 , 1, &RegSettingMR , 1, 100);

在while(1)循环内:

// RECEIVE X_axis
HAL_I2C_Mem_Read(&hi2c3,HMC5883l_ADDRESS,HMC5883l_ADD_DATAX_MSB_MULTI,1,DataX,2,100);
Xaxis = ((DataX[1]<<8) | DataX[0])/660.f;
// RECEIVE Y_axis
HAL_I2C_Mem_Read(&hi2c3,HMC5883l_ADDRESS,HMC5883l_ADD_DATAY_MSB_MULTI,1,DataY,2,100);
Yaxis = ((DataY[1]<<8) | DataY[0])/660.f;
// RECEIVE Z_axis
HAL_I2C_Mem_Read(&hi2c3,HMC5883l_ADDRESS,HMC5883l_ADD_DATAZ_MSB_MULTI,1,DataZ,2,100);
Zaxis = ((DataZ[1]<<8) | DataZ[0])/660.f;

由于输出数据是MSB和LSB的连接,所以我们必须将数据[1]向前移动8位。(LSB为0x00)。

这是STM Studio的结果: link

最好的问候:)