I'm trying to establish communication between Micro-controller MKL46Z256VLL4 and Magnetometer chip present on-board MAG3110. I'm not able to get the desired values on the variables for xaxis, yaxis and zaxis from the Magnetometer chip. My code for reference-
#include "MKL46Z4.h"
void InitLEDR(void)
{
SIM_SCGC5 = SIM_SCGC5 | SIM_SCGC5_PORTE_MASK; //clock to Port E for PTE29
PORTE_PCR29 = 256; //Alternate 001 for bits 10, 9, 8 respectively
GPIOE_PDDR = GPIOE_PDDR | (1u<<29);
}
void InitI2C(void)
{
SIM_CLKDIV1 = SIM_CLKDIV1 | (1u<<17) | (1u<<16); //bus clock is 24/3 = 8MHz
SIM_SCGC5 = SIM_SCGC5 | SIM_SCGC5_PORTE_MASK; //clock to PTE24 and PTE25 for I2C0
SIM_SCGC4 = SIM_SCGC4 | SIM_SCGC4_I2C0_MASK; //clock to I2C0
PORTE_PCR24 = PORTE_PCR24 | (1u<<10) & ~(1u<<9) | (1u<<8); //alternative 5 - 101 for bits 10, 9 8 respectively
PORTE_PCR25 = PORTE_PCR25 | (1u<<10) & ~(1u<<9) | (1u<<8); //alternative 5 - 101 for bits 10, 9 8 respectively
//I2C1_A1 = 0x01; //address when module acts as slave
I2C0_F = 0x80; //mult=2h ICR=00h
I2C0_C1 = 0xB0; //10110000 - module enable, interrupt disable, master, transmit,
//acknowledge bit sent,repeated start off, wake up off, DMA off
I2C0_C2 = 0x00;
}
//UART0,PTA1=RX and PTA1=TX
void InitUART(void)
{
SIM_SOPT2=(1u<<26);//SIM_SOPT2_UART0SRC(1);Clock to UART0 MCGFLLCLK 20.97152MHz as MCG_C4=0x00 low range 20Mhz
//SIM_SOPT5=0x00;kar na kar
//open-drain mode disabled(Clear bit 16) plus
//connection to UART_TX and UART_RX(Module to module interconnect) Clear bit 2,1 and 0
SIM_SCGC4=SIM_SCGC4|(1u<<10);//set bit 10 for giving clock to UART0
SIM_SCGC5=SIM_SCGC5_PORTA_MASK;//set bit 9 for giving clock to PORTA
PORTA_PCR1=PORTA_PCR1&~(1u<<10)|(1u<<9)&~(1u<<8);//PTA1 as UART1_RX (010 9 for alternative 2 which is UART0_RX)
PORTA_PCR2=PORTA_PCR1&~(1u<<10)|(1u<<9)&~(1u<<8);//PTA2 as UART1_RX (010 9 for alternative 2 which is UART0_TX)
UART0_C2=0x00;//TX and RX both disabled
UART0_BDH=0x00;//0x02;//Baud rate =9600,20.97152Mhz clock
UART0_BDL=0x88;//BR=136//0x22;//BR=132.5=132=100111000
UART0_C1=0x00;
//UART0_S2=UART0_S1 & 0xC1;
UART0_C3=0x00;
UART0_C4=0x0F;//OSR=15//OSR=3
UART0_C5=0x00;//not both edges 0x02;//as OSR=4, sampling on both edges of baud rate clock of received data
UART0_C2=UART0_C2 | (1u<<3);//only TX enable
}
void WriteToI2C(unsigned char data)
{
while(!(I2C0_S & 0x80));
I2C0_D = data;
}
void SetMag(void)
{
WriteToI2C(0x0E); //device ID
WriteToI2C(0x11); //register address - C2
WriteToI2C(0xB0); //10110000 Magnetic Reset, raw data and auto reset enabled
WriteToI2C(0x0E); //device ID
WriteToI2C(0x10); //register address - C1
WriteToI2C(0x01); //AC=1 all other 0
}
void UART_OutChar(unsigned char data)
{
while(!(UART0_S1 & (1u<<7)));
UART0_D=data;
}
void UART_OutUDec(unsigned int n)
{
unsigned cnt=0;
unsigned char buffer[50];
do
{
buffer[cnt] = n%10;// digit
n = n/10;
cnt++;
} while(n);// repeat until n==0
do
{
UART_OutChar(buffer[cnt-1]+'0');
cnt--;
}while(cnt != 0);
}
unsigned int ReadMag(unsigned char reg)
{
I2C0_C1 = 0xB0; //10110000 - module enable, interrupt disable, master, transmit
WriteToI2C(0x0E); //Device ID
WriteToI2C(reg); //Register
I2C0_C1 = 0xB4; //10110000 - module enable, interrupt disable, master, transmit,
//acknowledge bit sent,repeated start on, wake up off, DMA off
WriteToI2C(0x8E); //Device ID + 1(=Read)
I2C0_C1 = 0xA0; //10100000 - module enable, interrupt disable, master, receive
while(!(I2C0_S & 0x80)); //wait till transfer is complete
return (I2C0_D);
}
int main(void)
{
InitI2C();
InitUART();
InitLEDR();
SetMag();
int i;
unsigned int xread, yread, zread;
while(1)
{
i=10000;
xread=ReadMag(0x01);
yread=ReadMag(0x03);
zread=ReadMag(0x05);
UART_OutUDec(xread);
UART_OutUDec(yread);
UART_OutUDec(zread);
GPIOE_PTOR = GPIOE_PTOR | (1u<<29);
while(i>0)
{
i--;
}
}
return 0;
}
解決済! 解決策の投稿を見る。
Hi Aman Jain,
The sample code which I recommend your just have IAR, Codewarrior project, and don't have the KDS project.
You can download a Codewarrior 10.6 to open it, or you can just refer to the source code and add it to your KDS project.
The picture which you give me is the IAR project, the CW project folder is :\kinetis_kl46_sc\kinetis_kl46_sc_rev2\klxx-sc-baremetal\build\cw\FRDMKL46_Demo\kl46_FRDMKL46_Demo
If you want to refer to the source code, you can find it from folder: kinetis_kl46_sc\kinetis_kl46_sc_rev2\klxx-sc-baremetal\src\projects\FRDMKL46_Demo
Wish it helps you!
If you still have question, please contact with me!
Have a great day,
Jingjing
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi Aman Jain,
Please refer to the sample code of FRDM-KL46, there has an sample code for MAG3100.
Please download the sample code from this link:
Then after you install it, you will find the code in folder:kinetis_kl46_sc\kinetis_kl46_sc_rev2\klxx-sc-baremetal\build\iar\FRDMKL46_Demo
Wish it helps you!
If you still have question, please contact with me!
Have a great day,
Jingjing
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi,
I am using KDS V3.0. I'm not sure how to open the file and which file will be opened to view the code.
Hi Aman Jain,
The sample code which I recommend your just have IAR, Codewarrior project, and don't have the KDS project.
You can download a Codewarrior 10.6 to open it, or you can just refer to the source code and add it to your KDS project.
The picture which you give me is the IAR project, the CW project folder is :\kinetis_kl46_sc\kinetis_kl46_sc_rev2\klxx-sc-baremetal\build\cw\FRDMKL46_Demo\kl46_FRDMKL46_Demo
If you want to refer to the source code, you can find it from folder: kinetis_kl46_sc\kinetis_kl46_sc_rev2\klxx-sc-baremetal\src\projects\FRDMKL46_Demo
Wish it helps you!
If you still have question, please contact with me!
Have a great day,
Jingjing
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi,
I'm able to get some values from the magnetometer, but it is a constant value. How do I calibrate it so that I can use it to judge my orientation. The board moves on the back of a door, so the axis on which 'g' is acting remains unchanged.
Hi
For KL46 there is also the option to try
http://www.utasker.com/kinetis/FRDM-KL46Z.html
which supports various accelerometer sensors in non-blocking maximum-speed I2C polling or interrupt modes, as well as I2c and accelerometer simulation.
No IDE restrictions since it works with CW, KDS, IAR, uVision, Green Hills, Atollic, Crossworks CooCox, GCC, Visual Studio.
Rather than example code it is a ready to use industrially proven solution.
Regards
Mark
Kinetis: http://www.utasker.com/kinetis.html
I2C: http://www.utasker.com/docs/uTasker/uTaskerIIC.PDF
KL46: http://www.utasker.com/kinetis/FRDM-KL46Z.html / http://www.utasker.com/kinetis/TWR-KL46Z48M.html
For the complete "out-of-the-box" Kinetis experience and faster time to market