Hello,
I am trying to implement MAG3110 to my project and I have already communicated with the device and read the register contents. I am also reading ZYXDR bit successfully of which I can see that it changes. The datasheet says that this bit is logically connected to the interrupt pin but the interrupt pin doesn't change.
I am using FRDM-KL25Z to interface the sensor. I have connected the interrupt pin of MAG3110 to NMI pin. Also, for test purposes I have left this pin floating but nothing changes. It is always HIGH.
Here is my code:
/* ###################################################################
** Filename : main.c
** Project : MAG3110-Breakout
** Processor : MKL25Z128VLK4
** Version : Driver 01.01
** Compiler : GNU C Compiler
** Date/Time : 2015-01-10, 19:02, # CodeGen: 0
** Abstract :
** Main module.
** This module contains user's application code.
** Settings :
** Contents :
** No public methods
**
** ###################################################################*/
/*!
** @file main.c
** @version 01.01
** @brief
** Main module.
** This module contains user's application code.
*/
/*!
** @addtogroup main_module main module documentation
** @{
*/
/* MODULE main */
/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "Events.h"
#include "I2C0.h"
#include "PTA.h"
#include "SysTick.h"
/* Including shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
/* User includes (#include below this line is not maintained by Processor Expert) */
#include "I2C.h"
unsigned int _1_ms_ctr = 0;
unsigned int dr_status;
int mag3310_x = 0;
int mag3310_y = 0;
int mag3310_z = 0;
void main_loop(void)
{
static volatile unsigned char sysmod = 0;
I2C_WriteRegister(MAG3110_addr, MAG3110_CTRL_REG2, 0x80);
I2C_WriteRegister(MAG3110_addr, MAG3110_CTRL_REG1, 0x79);
while (1)
{
sysmod = I2C_ReadRegister(MAG3110_addr, MAG3110_SYSMOD);
if (sysmod == 2)
{
dr_status = I2C_ReadRegister(MAG3110_addr, MAG3110_DR_STATUS);
}
if (dr_status > 7)
{
mag3310_x = I2C_ReadRegister(MAG3110_addr, MAG3110_OUT_X_MSB);
mag3310_x <<= 8;
mag3310_x += I2C_ReadRegister(MAG3110_addr, MAG3110_OUT_X_LSB);
mag3310_y = I2C_ReadRegister(MAG3110_addr, MAG3110_OUT_Y_MSB);
mag3310_y <<= 8;
mag3310_y += I2C_ReadRegister(MAG3110_addr, MAG3110_OUT_Y_LSB);
mag3310_z = I2C_ReadRegister(MAG3110_addr, MAG3110_OUT_Z_MSB);
mag3310_z <<= 8;
mag3310_z += I2C_ReadRegister(MAG3110_addr, MAG3110_OUT_Z_LSB);
}
}
}
/*lint -save -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
/* Write your local variable definition here */
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
PE_low_level_init();
/*** End of Processor Expert internal initialization. ***/
/* Write your code here */
/* For example: for(;;) { } */
main_loop();
/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of RTOS startup code. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;){}
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
** This file was created by Processor Expert 10.3 [05.09]
** for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/
/*
* I2C.h
*
* Created on: Jan 10, 2015
* Author: Abdullah
*/
#ifndef I2C_H_
#define I2C_H_
#define I2C_DisableAck() I2C0_C1 |= I2C_C1_TXAK_MASK
#define I2C_EnableAck() I2C0_C1 &= ~I2C_C1_TXAK_MASK
#define I2C_RepeatedStart() I2C0_C1 |= I2C_C1_RSTA_MASK
#define I2C_EnterRxMode() I2C0_C1 &= ~I2C_C1_TX_MASK
#define I2C_write_byte(data) I2C0_D = data
#define I2C_Start() I2C0_C1 |= I2C_C1_TX_MASK;\
I2C0_C1 |= I2C_C1_MST_MASK
#define I2C_Stop() I2C0_C1 &= ~I2C_C1_MST_MASK;\
I2C0_C1 &= ~I2C_C1_TX_MASK
#define I2C_Wait() while((I2C0_S & I2C_S_IICIF_MASK)==0) {} \
I2C0_S |= I2C_S_IICIF_MASK;
#define MAG3110_addr 0x0E
#define MAG3110_DR_STATUS 0x00
#define MAG3110_OUT_X_MSB 0x01
#define MAG3110_OUT_X_LSB 0x02
#define MAG3110_OUT_Y_MSB 0x03
#define MAG3110_OUT_Y_LSB 0x04
#define MAG3110_OUT_Z_MSB 0x05
#define MAG3110_OUT_Z_LSB 0x06
#define MAG3110_WHO_AM_I 0x07
#define MAG3110_SYSMOD 0x08
#define MAG3110_OFF_X_MSB 0x09
#define MAG3110_OFF_X_LSB 0x0A
#define MAG3110_OFF_Y_MSB 0x0B
#define MAG3110_OFF_Y_LSB 0x0C
#define MAG3110_OFF_Z_MSB 0x0D
#define MAG3110_OFF_Z_LSB 0x0E
#define MAG3110_DIE_TEMP 0x0F
#define MAG3110_CTRL_REG1 0x10
#define MAG3110_CTRL_REG2 0x11
void I2C_WriteRegister(unsigned char u8SlaveAddress,
unsigned char u8RegisterAddress, /*unsigned*/char u8Data);
unsigned char I2C_ReadRegister(unsigned char u8SlaveAddress,
unsigned char u8RegisterAddress);
void I2C_ReadMultiRegisters(unsigned char u8SlaveAddress,
unsigned char u8RegisterAddress, unsigned char n, unsigned char *r);
void Pause(int number);
#endif /* I2C_H_ */
/*
* I2C.c
*
* Created on: Jan 10, 2015
* Author: Abdullah
*/
#include "Cpu.h"
#include "I2C.h"
unsigned int time_buffer;
extern unsigned int _1_ms_ctr;
void I2C_WriteRegister(unsigned char u8SlaveAddress, unsigned char u8RegisterAddress, char u8Data)
{
I2C_Start();
I2C0_D = u8SlaveAddress << 1; /* Send I2C device address with W/R bit = 0 */
I2C_Wait();
I2C0_D = u8RegisterAddress; /* Send register address */
I2C_Wait();
I2C0_D = u8Data; /* Send the data */
I2C_Wait();
I2C_Stop();
Pause(500);
}
unsigned char I2C_ReadRegister(unsigned char u8SlaveAddress, unsigned char u8RegisterAddress)
{
unsigned char result;
I2C_Start();
I2C0_D = u8SlaveAddress << 1; /* Send I2C device address with W/R bit = 0 */
I2C_Wait();
I2C0_D = u8RegisterAddress; /* Send register address */
I2C_Wait();
I2C_RepeatedStart();
I2C0_D = (u8SlaveAddress << 1) | 0x01; /* Send I2C device address this time with W/R bit = 1 */
I2C_Wait();
I2C_EnterRxMode();
I2C_DisableAck();
result = I2C0_D;
I2C_Wait();
I2C_Stop();
result = I2C0_D;
Pause(500);
return result;
}
void I2C_ReadMultiRegisters(unsigned char u8SlaveAddress, unsigned char u8RegisterAddress, unsigned char n, unsigned char *r)
{
char i;
I2C_Start();
I2C0_D = u8SlaveAddress << 1; /* Send I2C device address with W/R bit = 0 */
I2C_Wait();
I2C0_D = u8RegisterAddress; /* Send register address */
I2C_Wait();
I2C_RepeatedStart();
I2C0_D = (u8SlaveAddress << 1) | 0x01; /* Send I2C device address this time with W/R bit = 1 */
I2C_Wait();
I2C_EnterRxMode();
I2C_EnableAck();
i = I2C0_D;
I2C_Wait();
for(i=0; i<n-2; i++)
{
*r = I2C0_D;
r++;
I2C_Wait();
}
I2C_DisableAck();
*r = I2C0_D;
r++;
I2C_Wait();
I2C_Stop();
*r = I2C0_D;
Pause(500);
}
void Pause(int number)
{
int cnt;
for(cnt=0; cnt<number; cnt++)
{
asm("nop");
};
}
/* ###################################################################
** Filename : Events.c
** Project : MAG3110-Breakout
** Processor : MKL25Z128VLK4
** Component : Events
** Version : Driver 01.00
** Compiler : GNU C Compiler
** Date/Time : 2015-01-10, 19:02, # CodeGen: 0
** Abstract :
** This is user's event module.
** Put your event handler code here.
** Settings :
** Contents :
** Cpu_OnNMIINT - void Cpu_OnNMIINT(void);
**
** ###################################################################*/
/*!
** @file Events.c
** @version 01.00
** @brief
** This is user's event module.
** Put your event handler code here.
*/
/*!
** @addtogroup Events_module Events module documentation
** @{
*/
/* MODULE Events */
#include "Cpu.h"
#include "Events.h"
#include "I2C.h"
#ifdef __cplusplus
extern "C" {
#endif
extern unsigned int _1_ms_ctr;
/* User includes (#include below this line is not maintained by Processor Expert) */
/*
** ===================================================================
** Event : Cpu_OnNMIINT (module Events)
**
** Component : Cpu [MKL25Z128LK4]
*/
/*!
** @brief
** This event is called when the Non maskable interrupt had
** occurred. This event is automatically enabled when the [NMI
** interrupt] property is set to 'Enabled'.
*/
/* ===================================================================*/
void Cpu_OnNMIINT(void)
{
static volatile unsigned char result = 0;
result = I2C_ReadRegister(MAG3110_addr, MAG3110_OUT_X_MSB);
}
void SysTick_Interrupt(void)
{
_1_ms_ctr++;
}
void I2C0_Interrupt(void)
{
}
/* END Events */
#ifdef __cplusplus
} /* extern "C" */
#endif
/*!
** @}
*/
/*
** ###################################################################
**
** This file was created by Processor Expert 10.3 [05.09]
** for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/