@@ -32,29 +32,32 @@ void task_hmc5883l(void *ignore) {
32
32
i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
33
33
i2c_master_start (cmd );
34
34
i2c_master_write_byte (cmd , (I2C_ADDRESS << 1 ) | I2C_MASTER_WRITE , 1 );
35
- i2c_master_write_byte (cmd , 0x02 , 1 ); // Mode register
36
- i2c_master_write_byte (cmd , 0x00 , 1 ); // value 0
35
+ i2c_master_write_byte (cmd , 0x02 , 1 ); // 0x02 = " Mode register"
36
+ i2c_master_write_byte (cmd , 0x00 , 1 ); // 0x00 = "Continuous-Measurement Mode"
37
37
i2c_master_stop (cmd );
38
38
i2c_master_cmd_begin (I2C_NUM_0 , cmd , 1000 /portTICK_PERIOD_MS );
39
39
i2c_cmd_link_delete (cmd );
40
40
41
+ //Set value in "Configuration Register B"
41
42
cmd = i2c_cmd_link_create ();
42
43
i2c_master_start (cmd );
43
44
i2c_master_write_byte (cmd , (I2C_ADDRESS << 1 ) | I2C_MASTER_WRITE , 1 );
44
- i2c_master_write_byte (cmd , 0x01 , 1 ); // Mode register
45
- i2c_master_write_byte (cmd , 0x20 , 1 ); // value 0
45
+ i2c_master_write_byte (cmd , 0x01 , 1 ); // 0x01 = "Configuration Register B"
46
+ i2c_master_write_byte (cmd , 0x20 , 1 ); // 0x20 = default Gain setting
46
47
i2c_master_stop (cmd );
47
48
i2c_master_cmd_begin (I2C_NUM_0 , cmd , 1000 /portTICK_PERIOD_MS );
48
49
i2c_cmd_link_delete (cmd );
49
50
51
+ //Set active register to "Identification Register A"
50
52
cmd = i2c_cmd_link_create ();
51
53
ESP_ERROR_CHECK (i2c_master_start (cmd ));
52
54
ESP_ERROR_CHECK (i2c_master_write_byte (cmd , (I2C_ADDRESS << 1 ) | I2C_MASTER_WRITE , 1 ));
53
- ESP_ERROR_CHECK (i2c_master_write_byte (cmd , 10 , 1 )); // Data registers
55
+ ESP_ERROR_CHECK (i2c_master_write_byte (cmd , 10 , 1 )); //10 = 0x0A = "Identification Register A"
54
56
ESP_ERROR_CHECK (i2c_master_stop (cmd ));
55
57
ESP_ERROR_CHECK (i2c_master_cmd_begin (I2C_NUM_0 , cmd , 100 /portTICK_PERIOD_MS ));
56
58
i2c_cmd_link_delete (cmd );
57
59
60
+ //Get data from Identification Register A, B and C
58
61
cmd = i2c_cmd_link_create ();
59
62
ESP_ERROR_CHECK (i2c_master_start (cmd ));
60
63
ESP_ERROR_CHECK (i2c_master_write_byte (cmd , (I2C_ADDRESS << 1 ) | I2C_MASTER_READ , 1 ));
@@ -66,23 +69,25 @@ void task_hmc5883l(void *ignore) {
66
69
i2c_cmd_link_delete (cmd );
67
70
68
71
while (1 ) {
72
+ //Set active registert to "Data Output X MSB Register"
69
73
cmd = i2c_cmd_link_create ();
70
74
i2c_master_start (cmd );
71
75
i2c_master_write_byte (cmd , (I2C_ADDRESS << 1 ) | I2C_MASTER_WRITE , 1 );
72
- i2c_master_write_byte (cmd , 0x03 , 1 ); // Data registers
76
+ i2c_master_write_byte (cmd , 0x03 , 1 ); //0x03 = " Data Output X MSB Register"
73
77
i2c_master_stop (cmd );
74
78
i2c_master_cmd_begin (I2C_NUM_0 , cmd , 1000 /portTICK_PERIOD_MS );
75
79
i2c_cmd_link_delete (cmd );
76
80
81
+ //Read values for X, Y and Z
77
82
cmd = i2c_cmd_link_create ();
78
83
i2c_master_start (cmd );
79
84
i2c_master_write_byte (cmd , (I2C_ADDRESS << 1 ) | I2C_MASTER_READ , 1 );
80
- i2c_master_read_byte (cmd , data , 0 );
81
- i2c_master_read_byte (cmd , data + 1 , 0 );
82
- i2c_master_read_byte (cmd , data + 2 , 0 );
83
- i2c_master_read_byte (cmd , data + 3 , 0 );
84
- i2c_master_read_byte (cmd , data + 4 , 0 );
85
- i2c_master_read_byte (cmd , data + 5 , 1 );
85
+ i2c_master_read_byte (cmd , data , 0 ); //"Data Output X MSB Register"
86
+ i2c_master_read_byte (cmd , data + 1 , 0 ); //"Data Output X LSB Register"
87
+ i2c_master_read_byte (cmd , data + 2 , 0 ); //"Data Output Z MSB Register"
88
+ i2c_master_read_byte (cmd , data + 3 , 0 ); //"Data Output Z LSB Register"
89
+ i2c_master_read_byte (cmd , data + 4 , 0 ); //"Data Output Y MSB Register"
90
+ i2c_master_read_byte (cmd , data + 5 , 1 ); //"Data Output Y LSB Register "
86
91
i2c_master_stop (cmd );
87
92
i2c_master_cmd_begin (I2C_NUM_0 , cmd , 1000 /portTICK_PERIOD_MS );
88
93
i2c_cmd_link_delete (cmd );
0 commit comments