mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-02-20 10:54:14 +01:00
Finalised and verified PIOS_HMC5843 library.
git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@624 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
parent
e05f4cf068
commit
73b5e29e02
@ -51,32 +51,45 @@ int main()
|
||||
PIOS_ADC_Init();
|
||||
|
||||
/* Magnetic sensor system */
|
||||
#if 1
|
||||
PIOS_I2C_Init();
|
||||
PIOS_HMC5843_Init();
|
||||
#endif
|
||||
|
||||
/* Toggle LED's forever */
|
||||
PIOS_LED_On(LED1);
|
||||
/* Configure the HMC5843 Sensor */
|
||||
PIOS_HMC5843_ConfigTypeDef HMC5843_InitStructure;
|
||||
HMC5843_InitStructure.M_ODR = PIOS_HMC5843_ODR_10;
|
||||
HMC5843_InitStructure.Meas_Conf = PIOS_HMC5843_MEASCONF_NORMAL;
|
||||
HMC5843_InitStructure.Gain = PIOS_HMC5843_GAIN_2;
|
||||
HMC5843_InitStructure.Mode = PIOS_HMC5843_MODE_CONTINUOS;
|
||||
PIOS_HMC5843_Config(&HMC5843_InitStructure);
|
||||
|
||||
uint8_t buffer[3] = {0};
|
||||
int32_t result;
|
||||
uint8_t id[3] = {0};
|
||||
int16_t data[3] = {0};
|
||||
int32_t heading = 0;
|
||||
|
||||
// Main loop
|
||||
for(;;) {
|
||||
// Alive signal
|
||||
PIOS_LED_Toggle(LED1);
|
||||
|
||||
#if 1
|
||||
result = PIOS_HMC5843_Read(0x0A, buffer, 3);
|
||||
//PIOS_COM_SendFormattedString(COM_USART1, "Result: %d\r", result);
|
||||
//PIOS_COM_SendFormattedString(COM_USART1, "Ident: \r", buffer);
|
||||
#endif
|
||||
// Get 3 ID bytes
|
||||
PIOS_HMC5843_ReadID(id);
|
||||
|
||||
// Get magnetic readings
|
||||
PIOS_HMC5843_ReadMag(data);
|
||||
|
||||
// Calculate the heading
|
||||
heading = atan2((double)(data[0]), (double)(-1 * data[1])) * (180 / M_PI);
|
||||
if(heading < 0) heading += 360;
|
||||
|
||||
// Output Heading data to
|
||||
PIOS_COM_SendFormattedString(COM_USART1, "Chip ID: %s\rHeading: %d\rRaw Mag Values: X=%d Y=%d Y=%d\r", id, heading, data[0], data[1], data[2]);
|
||||
|
||||
// Test ADC
|
||||
PIOS_COM_SendFormattedString(COM_USART1, "%s,%d,%d,%d,%d,%d,%d\r\n", buffer, PIOS_ADC_PinGet(0), PIOS_ADC_PinGet(1), PIOS_ADC_PinGet(2), PIOS_ADC_PinGet(3), PIOS_ADC_PinGet(4), PIOS_ADC_PinGet(5));
|
||||
PIOS_COM_SendFormattedString(COM_USART1, "ADC Values: %d,%d,%d,%d,%d,%d\r\n", PIOS_ADC_PinGet(0), PIOS_ADC_PinGet(1), PIOS_ADC_PinGet(2), PIOS_ADC_PinGet(3), PIOS_ADC_PinGet(4), PIOS_ADC_PinGet(5));
|
||||
|
||||
PIOS_DELAY_WaitmS(100);
|
||||
PIOS_DELAY_WaitmS(250);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -35,16 +35,162 @@
|
||||
|
||||
/* Local Variables */
|
||||
|
||||
|
||||
/**
|
||||
* Initialise the HMC5843 sensor
|
||||
*/
|
||||
void PIOS_HMC5843_Init(void)
|
||||
{
|
||||
// Set in continuous mode
|
||||
PIOS_HMC5843_Write(0x02, 0x00);
|
||||
// Nothing to do here
|
||||
// If we were using the DRDY (data ready) interrupt input, we would set it up here
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise the HMC5843 sensor
|
||||
*
|
||||
* CTRL_REGA: Control Register A
|
||||
* Read Write
|
||||
* Default value: 0x10
|
||||
* 7:5 0 These bits must be cleared for correct operation.
|
||||
* 4:2 DO2-DO0: Data Output Rate Bits
|
||||
* DO2 | DO1 | DO0 | Minimum Data Output Rate (Hz)
|
||||
* ------------------------------------------------------
|
||||
* 0 | 0 | 0 | 0.5
|
||||
* 0 | 0 | 1 | 1
|
||||
* 0 | 1 | 0 | 2
|
||||
* 0 | 1 | 1 | 5
|
||||
* 1 | 0 | 0 | 10 (default)
|
||||
* 1 | 0 | 1 | 20
|
||||
* 1 | 1 | 0 | 50
|
||||
* 1 | 1 | 1 | Not Used
|
||||
* 1:0 MS1-MS0: Measurement Configuration Bits
|
||||
* MS1 | MS0 | MODE
|
||||
* ------------------------------
|
||||
* 0 | 0 | Normal
|
||||
* 0 | 1 | Positive Bias
|
||||
* 1 | 0 | Negative Bias
|
||||
* 1 | 1 | Not Used
|
||||
*
|
||||
* CTRL_REGB: Control RegisterB
|
||||
* Read Write
|
||||
* Default value: 0x20
|
||||
* 7:5 GN2-GN0: Gain Configuration Bits.
|
||||
* GN2 | GN1 | GN0 | Mag Input | Gain | Output Range
|
||||
* | | | Range[Ga] | [LSB/mGa] |
|
||||
* ------------------------------------------------------
|
||||
* 0 | 0 | 0 | ±0.7Ga | 1620 | 0xF800–0x07FF (-2048:2047)
|
||||
* 0 | 0 | 1 | ±1.0Ga (def) | 1300 | 0xF800–0x07FF (-2048:2047)
|
||||
* 0 | 1 | 0 | ±1.5Ga | 970 | 0xF800–0x07FF (-2048:2047)
|
||||
* 0 | 1 | 1 | ±2.0Ga | 780 | 0xF800–0x07FF (-2048:2047)
|
||||
* 1 | 0 | 0 | ±3.2Ga | 530 | 0xF800–0x07FF (-2048:2047)
|
||||
* 1 | 0 | 1 | ±3.8Ga | 460 | 0xF800–0x07FF (-2048:2047)
|
||||
* 1 | 1 | 0 | ±4.5Ga | 390 | 0xF800–0x07FF (-2048:2047)
|
||||
* 1 | 1 | 1 | ±6.5Ga | 280 | 0xF800–0x07FF (-2048:2047)
|
||||
* |Not recommended|
|
||||
*
|
||||
* 4:0 CRB4-CRB: 0 This bit must be cleared for correct operation.
|
||||
*
|
||||
* _MODE_REG: Mode Register
|
||||
* Read Write
|
||||
* Default value: 0x02
|
||||
* 7:2 0 These bits must be cleared for correct operation.
|
||||
* 1:0 MD1-MD0: Mode Select Bits
|
||||
* MS1 | MS0 | MODE
|
||||
* ------------------------------
|
||||
* 0 | 0 | Continuous-Conversion Mode.
|
||||
* 0 | 1 | Single-Conversion Mode
|
||||
* 1 | 0 | Negative Bias
|
||||
* 1 | 1 | Sleep Mode
|
||||
*/
|
||||
void PIOS_HMC5843_Config(PIOS_HMC5843_ConfigTypeDef *HMC5843_Config_Struct)
|
||||
{
|
||||
uint8_t CRTLA = 0x00;
|
||||
uint8_t CRTLB = 0x00;
|
||||
uint8_t MODE = 0x00;
|
||||
|
||||
CRTLA |= (uint8_t) (HMC5843_Config_Struct->M_ODR | HMC5843_Config_Struct->Meas_Conf);
|
||||
CRTLB |= (uint8_t) (HMC5843_Config_Struct->Gain);
|
||||
MODE |= (uint8_t) (HMC5843_Config_Struct->Mode);
|
||||
|
||||
// CRTL_REGA
|
||||
PIOS_HMC5843_Write(PIOS_HMC5843_CONFIG_REG_A, CRTLA);
|
||||
|
||||
// CRTL_REGB
|
||||
PIOS_HMC5843_Write(PIOS_HMC5843_CONFIG_REG_B, CRTLB);
|
||||
|
||||
// Mode register
|
||||
PIOS_HMC5843_Write(PIOS_HMC5843_MODE_REG, MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the magnetic readings from the sensor
|
||||
*/
|
||||
void PIOS_HMC5843_ReadMag(int16_t *out)
|
||||
{
|
||||
uint8_t buffer[6];
|
||||
uint8_t crtlB;
|
||||
|
||||
PIOS_HMC5843_Read(PIOS_HMC5843_CONFIG_REG_B, &crtlB, 1);
|
||||
PIOS_HMC5843_Read(PIOS_HMC5843_DATAOUT_XMSB_REG, buffer, 6);
|
||||
|
||||
switch(crtlB & 0xE0) {
|
||||
case 0x00:
|
||||
for(int i = 0; i < 3; i++)
|
||||
out[i] = ((int16_t) ((uint16_t) buffer[2 * i] << 8)
|
||||
+ buffer[2 * i + 1]) * 1000
|
||||
/ PIOS_HMC5843_Sensitivity_0_7Ga;
|
||||
break;
|
||||
case 0x20:
|
||||
for(int i = 0; i < 3; i++)
|
||||
out[i] = ((int16_t) ((uint16_t) buffer[2 * i] << 8)
|
||||
+ buffer[2 * i + 1]) * 1000
|
||||
/ PIOS_HMC5843_Sensitivity_1Ga;
|
||||
break;
|
||||
case 0x40:
|
||||
for(int i = 0; i < 3; i++)
|
||||
out[i] = (int16_t) (((uint16_t) buffer[2 * i] << 8)
|
||||
+ buffer[2 * i + 1]) * 1000
|
||||
/ PIOS_HMC5843_Sensitivity_1_5Ga;
|
||||
break;
|
||||
case 0x60:
|
||||
for(int i = 0; i < 3; i++)
|
||||
out[i] = (int16_t) (((uint16_t) buffer[2 * i] << 8)
|
||||
+ buffer[2 * i + 1]) * 1000
|
||||
/ PIOS_HMC5843_Sensitivity_2Ga;
|
||||
break;
|
||||
case 0x80:
|
||||
for(int i = 0; i < 3; i++)
|
||||
out[i] = (int16_t) (((uint16_t) buffer[2 * i] << 8)
|
||||
+ buffer[2 * i + 1]) * 1000
|
||||
/ PIOS_HMC5843_Sensitivity_3_2Ga;
|
||||
break;
|
||||
case 0xA0:
|
||||
for(int i = 0; i < 3; i++)
|
||||
out[i] = (int16_t) (((uint16_t) buffer[2 * i] << 8)
|
||||
+ buffer[2 * i + 1]) * 1000
|
||||
/ PIOS_HMC5843_Sensitivity_3_8Ga;
|
||||
break;
|
||||
case 0xC0:
|
||||
for(int i = 0; i < 3; i++)
|
||||
out[i] = (int16_t) (((uint16_t) buffer[2 * i] << 8)
|
||||
+ buffer[2 * i + 1]) * 1000
|
||||
/ PIOS_HMC5843_Sensitivity_4_5Ga;
|
||||
break;
|
||||
case 0xE0:
|
||||
for(int i = 0; i < 3; i++)
|
||||
out[i] = (int16_t) (((uint16_t) buffer[2 * i] << 8)
|
||||
+ buffer[2 * i + 1]) * 1000
|
||||
/ PIOS_HMC5843_Sensitivity_6_5Ga;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the identification bytes from the sensor
|
||||
*/
|
||||
void PIOS_HMC5843_ReadID(uint8_t *out)
|
||||
{
|
||||
PIOS_HMC5843_Read(PIOS_HMC5843_DATAOUT_IDA_REG, out, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads one or more bytes into a buffer
|
||||
@ -60,11 +206,11 @@ int32_t PIOS_HMC5843_Read(uint8_t address, uint8_t *buffer, uint8_t len)
|
||||
/* Send I2C address and register address */
|
||||
/* To avoid issues copy address into temporary buffer */
|
||||
uint8_t addr_buffer[1] = {(uint8_t)address};
|
||||
int32_t error = PIOS_I2C_Transfer(I2C_Write_WithoutStop, HMC5843_I2C_ADDR, addr_buffer, 1);
|
||||
int32_t error = PIOS_I2C_Transfer(I2C_Write_WithoutStop, PIOS_HMC5843_I2C_ADDR, addr_buffer, 1);
|
||||
|
||||
/* Now receive byte(s) */
|
||||
if(!error) {
|
||||
error = PIOS_I2C_Transfer(I2C_Read, HMC5843_I2C_ADDR, buffer, len);
|
||||
error = PIOS_I2C_Transfer(I2C_Read, PIOS_HMC5843_I2C_ADDR, buffer, len);
|
||||
}
|
||||
|
||||
/* Return error status */
|
||||
@ -86,7 +232,7 @@ int32_t PIOS_HMC5843_Write(uint8_t address, uint8_t buffer)
|
||||
WriteBuffer[0] = address;
|
||||
WriteBuffer[1] = buffer;
|
||||
|
||||
int32_t error = PIOS_I2C_Transfer(I2C_Write, HMC5843_I2C_ADDR, WriteBuffer, 2);
|
||||
int32_t error = PIOS_I2C_Transfer(I2C_Write, PIOS_HMC5843_I2C_ADDR, WriteBuffer, 2);
|
||||
|
||||
/* Return error status */
|
||||
return error < 0 ? -1 : 0;
|
||||
|
@ -27,17 +27,77 @@
|
||||
#define PIOS_HMC5843_H
|
||||
|
||||
/* BMP085 Addresses */
|
||||
#define HMC5843_I2C_ADDR 0x3C
|
||||
#define PIOS_HMC5843_I2C_ADDR 0x3C
|
||||
#define PIOS_HMC5843_CONFIG_REG_A (uint8_t)0x00
|
||||
#define PIOS_HMC5843_CONFIG_REG_B (uint8_t)0x01
|
||||
#define PIOS_HMC5843_MODE_REG (uint8_t)0x02
|
||||
#define PIOS_HMC5843_DATAOUT_XMSB_REG 0x03
|
||||
#define PIOS_HMC5843_DATAOUT_XLSB_REG 0x04
|
||||
#define PIOS_HMC5843_DATAOUT_YMSB_REG 0x05
|
||||
#define PIOS_HMC5843_DATAOUT_YLSB_REG 0x06
|
||||
#define PIOS_HMC5843_DATAOUT_ZMSB_REG 0x07
|
||||
#define PIOS_HMC5843_DATAOUT_ZLSB_REG 0x08
|
||||
#define PIOS_HMC5843_DATAOUT_STATUS_REG 0x09
|
||||
#define PIOS_HMC5843_DATAOUT_IDA_REG 0x0A
|
||||
#define PIOS_HMC5843_DATAOUT_IDB_REG 0x0B
|
||||
#define PIOS_HMC5843_DATAOUT_IDC_REG 0x0C
|
||||
|
||||
/* Output Data Rate */
|
||||
#define PIOS_HMC5843_ODR_05 0x00
|
||||
#define PIOS_HMC5843_ODR_1 0x04
|
||||
#define PIOS_HMC5843_ODR_2 0x08
|
||||
#define PIOS_HMC5843_ODR_5 0x0C
|
||||
#define PIOS_HMC5843_ODR_10 0x10
|
||||
#define PIOS_HMC5843_ODR_20 0x14
|
||||
#define PIOS_HMC5843_ODR_50 0x18
|
||||
|
||||
/* Local Types */
|
||||
/* Measure configuration */
|
||||
#define PIOS_HMC5843_MEASCONF_NORMAL 0x00
|
||||
#define PIOS_HMC5843_MEASCONF_BIAS_POS 0x01
|
||||
#define PIOS_HMC5843_MEASCONF_BIAS_NEG 0x02
|
||||
|
||||
/* Gain settings */
|
||||
#define PIOS_HMC5843_GAIN_0_7 0x00
|
||||
#define PIOS_HMC5843_GAIN_1 0x20
|
||||
#define PIOS_HMC5843_GAIN_1_5 0x40
|
||||
#define PIOS_HMC5843_GAIN_2 0x60
|
||||
#define PIOS_HMC5843_GAIN_3_2 0x80
|
||||
#define PIOS_HMC5843_GAIN_3_8 0xA0
|
||||
#define PIOS_HMC5843_GAIN_4_5 0xC0
|
||||
#define PIOS_HMC5843_GAIN_6_5 0xE0
|
||||
|
||||
/* Modes */
|
||||
#define PIOS_HMC5843_MODE_CONTINUOS 0x00
|
||||
#define PIOS_HMC5843_MODE_SINGLE 0x01
|
||||
#define PIOS_HMC5843_MODE_IDLE 0x02
|
||||
#define PIOS_HMC5843_MODE_SLEEP 0x02
|
||||
|
||||
/* Sensitivity Conversion Values */
|
||||
#define PIOS_HMC5843_Sensitivity_0_7Ga 1602 // LSB/Ga
|
||||
#define PIOS_HMC5843_Sensitivity_1Ga 1300 // LSB/Ga
|
||||
#define PIOS_HMC5843_Sensitivity_1_5Ga 970 // LSB/Ga
|
||||
#define PIOS_HMC5843_Sensitivity_2Ga 780 // LSB/Ga
|
||||
#define PIOS_HMC5843_Sensitivity_3_2Ga 530 // LSB/Ga
|
||||
#define PIOS_HMC5843_Sensitivity_3_8Ga 460 // LSB/Ga
|
||||
#define PIOS_HMC5843_Sensitivity_4_5Ga 390 // LSB/Ga
|
||||
#define PIOS_HMC5843_Sensitivity_6_5Ga 280 // LSB/Ga --> NOT RECOMMENDED
|
||||
|
||||
/* Global Types */
|
||||
typedef struct {
|
||||
uint8_t M_ODR; /* OUTPUT DATA RATE --> here below the relative define (See datasheet page 11 for more details) */
|
||||
uint8_t Meas_Conf; /* Measurement Configuration,: Normal, positive bias, or negative bias --> here below the relative define */
|
||||
uint8_t Gain; /* Gain Configuration, select the full scale --> here below the relative define (See datasheet page 11 for more details) */
|
||||
uint8_t Mode;
|
||||
} PIOS_HMC5843_ConfigTypeDef;
|
||||
|
||||
/* Global Variables */
|
||||
|
||||
|
||||
/* Public Functions */
|
||||
extern void PIOS_HMC5843_Init(void);
|
||||
extern void PIOS_HMC5843_Config(PIOS_HMC5843_ConfigTypeDef *HMC5843_Config_Struct);
|
||||
extern void PIOS_HMC5843_ReadMag(int16_t *out);
|
||||
extern void PIOS_HMC5843_ReadID(uint8_t *out);
|
||||
extern int32_t PIOS_HMC5843_Read(uint8_t address, uint8_t *buffer, uint8_t len);
|
||||
extern int32_t PIOS_HMC5843_Write(uint8_t address, uint8_t buffer);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user