AnsweredAssumed Answered

Configuring I2C on FRDM-KL46Z

Question asked by Aman Jain on Feb 3, 2016
Latest reply on Feb 10, 2016 by Aman Jain

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;

}

Attachments

Outcomes