-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcom_HMC5883L.c
45 lines (38 loc) · 1.3 KB
/
com_HMC5883L.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/************************************************************************/
// File: com_HMC5883L.c
// Author: Erlend Ese, NTNU Spring 2016
// Purpose: Driver for HMC588L magnetic compass.
//
/************************************************************************/
#include "twi_master.h"
#include "com_HMC5883L.h"
#define HMC5883L_WRITE 0x3C
#define HMC5883L_READ 0x3D
void vCOM_init(void){
vTWI_init();
ui8TWI_start(HMC5883L_WRITE);
ui8TWI_write(0x00); // set pointer to CRA
ui8TWI_write(0x70); // write 0x70 to CRA
vTWI_stop();
ui8TWI_start(HMC5883L_WRITE);
ui8TWI_write(0x01); // set pointer to CRB
ui8TWI_write(0xA0); // Set bit 7 and 5 (GN2, GN0)
vTWI_stop();
ui8TWI_start(HMC5883L_WRITE);
ui8TWI_write(0x02); // set pointer to measurement mode
ui8TWI_write(0x00); // continous measurement
vTWI_stop();
}
void vCOM_getData(int16_t *xCom, int16_t *yCom, int16_t *zCom){
ui8TWI_start(HMC5883L_WRITE);
ui8TWI_write(0x03); // set pointer to X axis MSB
vTWI_stop();
ui8TWI_start(HMC5883L_READ);
*xCom = ((uint8_t)ui8TWI_read_ack())<<8;
*xCom |= ui8TWI_read_ack();
*zCom = ((uint8_t)ui8TWI_read_ack())<<8;
*zCom |= ui8TWI_read_ack();
*yCom = ((uint8_t)ui8TWI_read_ack())<<8;
*yCom |= ui8TWI_read_nack();
vTWI_stop();
}