With the Compass 2, you will never again be lost while sitting at your desk. Program away without fear of losing your direction. A magnetometer is used to determine the direction it is facing. In this blog, we will talk about how to use the Compass 2 with the library provided by MikroElektronika, if you'd like a more in depth explanation of how magnetometers work, please check out my MEMS sensor post here.
Getting Our Heading
Source code can be found on either Libstock or Github.
Compass 2 is capable of SPI or I2C communication. Changing communication type requires changing the following four jumpers to SPI or I2C ( default is I2C )
To setup the bus type in your code, simply use the bus_mode_t struct provided, and select either I2C or SPI_bus while initializing the Click. Here is a setup function was written for setting up all GPIOs, and other necessary peripherals:
void system_setup( bus_mode_t mode, uint8_t addr ) { uint8_t my_address = 0; // GPIOs GPIO_Digital_Output( &GPIOB_BASE, _GPIO_PINMASK_13 ); GPIO_Digital_Output( &GPIOC_BASE, _GPIO_PINMASK_2 ); // UART UART1_Init( 9600 ); Delay_ms(200); UART1_Write_Text( "UART Initializedrn" ); // I2C I2C1_Init_Advanced( 100000, &_GPIO_MODULE_I2C1_PB67 ); Delay_ms(500); UART1_Write_Text( "I2C Initializedrn" ); // SPI /* SPI3_Init_Advanced( _SPI_FPCLK_DIV16, _SPI_MASTER | _SPI_8_BIT | //SPI SETUP _SPI_CLK_IDLE_HIGH | _SPI_SECOND_CLK_EDGE_TRANSITION | _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1, &_GPIO_MODULE_SPI3_PC10_11_12 ); UART1_Write_Text( "SPI Initializedrn" ); */ //Reset COMPASS2_RST = 0; Delay_ms(10); COMPASS2_RST = 1; Delay_ms(200); // Compass 2 compass2_hw_init( addr, mode ); UART1_Write_Text( "Compass Initializedrn" ); // Device Address my_address = compass2_get_device_id(); IntToStr( my_address, text ); UART1_Write_Text( text ); UART1_Write_Text( "rn" ); // Continuous Measurment Mode / Output Res compass2_set_output_res( RES_16 ); compass2_set_mode( MODE_CONT_2 ); UART1_Write_Text( "Compass2 Setup Completed..rn" ); }
system_setup() starts the initialization, and sets the mode to MODE_CONT_1 for continuous reading of the magnetometers' x, y, and z values. It also sets the values output to a scale factor of 16 bits by calling compass2_set_output_res() . After setup the module is ready to use.
Finding Direction
Finding an actual direction is easy, simply get the values from the click and send them to the compass2_get_compass_heading() function. After checking, the function returns a value between 0 and 360, sound familiar? It can be done like this...
// Local Declarations uint8_t address = 0x0F; bus_mode_t my_mode = I2C; float heading = 0; char uart_text[5] = { 0 }; system_setup( my_mode, address ); UART1_Write_Text( "rn----Please Hold The Click Level----rnrnrn" ); Delay_ms(2000); while (1) { while( !compass2_get_data_ready() ); //Wait for data to be ready compass2_get_all_values( &mx, &my, &mz ); //Get x,y,z values for heading heading = compass2_get_compass_heading( mx, my, mz ); //Get heading FloatToStr( heading, uart_text ); UART1_Write_Text( "Heading : " ); UART1_Write_Text( uart_text ); UART1_Write_Text( "rn" ); }
The sequence will provide the heading given for finding your direction, and a direction in a terminal.
Summary
Now you're ready to use the Compass 2 Click and find your direction in coding. Use this click for position detection, navigation or orientation for your portable devices! It's accurate, and easy to use, making your project easier to make.