This project serves as a template for work with the Freescale FRDM-KL46 board. Support will be added to all on-board peripherals, sensors and I/O.

Dependencies:   FRDM_MMA8451Q MAG3110 TSI mbed

Project Information:

Theory

This project has been created to serve as a template for those who wish to use the Freescale Freedom FRDM-KL46Z board. Existing drivers within mbed have been brought together and board specific configurations for certain inputs and outputs have included.

Libraries

TODOs

Hardware Information:

FRDM-KL46Z Information

Freescale Kinetis L-Series Microcontroller Information

Freescale Sensor Information

MMA8451Q

MAG3110

main.cpp

Committer:
mmaas
Date:
2013-12-19
Revision:
3:1e85b49a3e18
Parent:
2:48ac01f5024d
Child:
4:6cb640167538

File content as of revision 3:1e85b49a3e18:

#include "mbed.h"
#include "MMA8451Q.h"
#include "MAG3110.h"


// This project has been created to bring together the libraries required to support
// the hardware and sensors found on the Freescale FRDM-KL46Z board.  The following
// libraries are included and exercised in this project:
//
//  mbed (source: official mbed library)
//      Serial:
//          Serial console routed through the mbed interface
//              PTA2 / UART0_TX = TX Signal - also on Arduino pin D1
//              PTA1 / UART0_RX = RX Signal - also on Arduino pin D0
//
//      DigitalOut:
//          GPIO to drive onboard LEDs
//              PTD5 / GPIO = LED1 - drive low to turn on LED - also on Arduino pin D13
//              PTE29 / GPIO = LED2 - drive low to turn on LED
//
//      DigitalIn:
//          GPIO to monitor the two onboard push buttons
//              PTC3 / GPIO = SW1 - low input = button pressed
//              PTC12 / GPIO = SW3 - low input = button pressed
//
//  TSI (source: http://mbed.org/users/emilmont/code/TSI/ )
//      Capacitive Touch library to support the onboard Touch-Slider
//
//  FRDM_MMA8451Q (source: http://mbed.org/users/clemente/code/FRDM_MMA8451Q/ )
//      Freescale MMA8451 Accelerometer connected on I2C0
//          PTE24 / I2C0_SCL = I2C bus for communication (shared with MAG3110)
//          PTE25 / I2C0_SDA =  I2C bus for communication (shared with MAG3110)
//          PTC5 / INT1_ACCEL = INT1 output of MMA8451Q
//          PTD1 / INT2_ACCEL = INT2 output of MMA8451Q (shared with MAG3110)
//
//  MAG3110
//      Freescale MAG3110 Magnetomoter connected on I2C0
//          PTE24 / I2C0_SCL = I2C bus for communication (shared with MMA8451)
//          PTE25 / I2C0_SDA =  I2C bus for communication (shared with MMA8451)
//          PTD1 / INT1_MAG / INT2_ACCEL = INT1 output of MAG3110 (shared with MMA8451)
//
//  FRDM_LightSensor
//      Ambient light sensor (Q1)
//          PTE22 / ADC
//
//  FRDM_LCD
//      4 Digit Segment LCD
//  TempSensor - KL46


//////////////////////////////////////////////////////////////////////
// Include support for USB Serial console
Serial pc(USBTX, USBRX);


//////////////////////////////////////////////////////////////////////
// Include support for on-board green and red LEDs
#define LED_ON 0
#define LED_OFF 1
DigitalOut greenLED(LED_GREEN);
DigitalOut redLED(LED_RED);


//////////////////////////////////////////////////////////////////////
// Include support for onboard pushbuttons
DigitalIn  sw1(PTC3);
DigitalIn  sw3(PTC12);


/////////////////////////////////////////////////////////////////////
// Include support for MMA8451Q Acceleromoter
#define MMA8451_I2C_ADDRESS (0x1d<<1)
MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);


/////////////////////////////////////////////////////////////////////
// Include support for MAG3110 Magnetometer
void calXY(); //magnetometer calibration: finding max and min of X, Y axis
MAG3110 mag(PTE25, PTE24);



int main()
{

    // Ensure LEDs are off
    greenLED = LED_OFF;
    redLED = LED_OFF;

    // Set Serial Port data rate and say Hello
    pc.baud( 230400 );
    pc.printf("Hello World\r\n");

    // Turn on pull up resistors on pushbutton inputs
    sw1.mode(PullUp);
    sw3.mode(PullUp);

    // Quick blink LEDs
    redLED = LED_ON;
    wait(.05);
    greenLED = LED_ON;
    wait(.05);
    greenLED = LED_OFF;
    redLED = LED_OFF;

    wait(1);

    
    // Get some values
    printf("DR_STATUS %X\r\n", mag.readReg( MAG_DR_STATUS ));
    printf("WHO_AM_I %X\r\n", mag.readReg( MAG_WHO_AM_I ));
    printf("SYSMOD %X\r\n", mag.readReg( MAG_SYSMOD ));
    printf("DIE_TEMP %d\r\n", mag.readReg( MAG_DIE_TEMP ));

    printf("OFF_X %d\r\n", mag.readVal( MAG_OFF_X_MSB ));
    printf("OFF_Y %d\r\n", mag.readVal( MAG_OFF_Y_MSB ));
    printf("OFF_Z %d\r\n", mag.readVal( MAG_OFF_Z_MSB ));

    printf("CTRL_REG1 %X\r\n", mag.readReg( MAG_CTRL_REG1 ));
    printf("CTRL_REG2 %X\r\n", mag.readReg( MAG_CTRL_REG2 ));


    // Calibrate the magnetomoter
    calXY();
    

    // Loop: Blink LEDs, report pushbutton status, mag data, accel data
    while(1) {
        
        // Get Magnetometer data
        int xVal = mag.readVal(MAG_OUT_X_MSB);
        int yVal = mag.readVal(MAG_OUT_Y_MSB);
        float heading = mag.getHeading();        
        
        // Do something with heading - display direction 
        printf("Heading: ");
        if (abs(heading) <= 22.5) printf("N ");
        if (abs(heading) >= 157.5) printf("S ");
        if (heading >= 67.5 && heading <= 112.5) printf("E ");
        if (heading <= -67.5 && heading >= -112.5) printf("W ");
        if (heading > 22.5 && heading < 67.5) printf("NE ");
        if (heading < -22.5 && heading > -67.5) printf("NW ");
        if (heading > 112.5 && heading < 157.5) printf("SE ");
        if (heading < -112.5 && heading > -157.5) printf("SW ");
        if (heading < 0) heading += 360.0;
        printf("%f\r\n", heading);
        
        
        greenLED = LED_ON;
        redLED = LED_OFF;

        printf("SW1 = %d, SW3 = %d, X = %f, Y = %f, Z = %f\r\n\n", sw1.read(), sw3.read(), acc.getAccX(), acc.getAccY(), acc.getAccZ());
        wait(0.25);

        greenLED = LED_OFF;
        redLED = LED_ON;

        wait(0.25);

    }
}


void calXY() //magnetometer calibration: finding max and min of X, Y axis
{
    int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY;


    printf("Press and release SW1 to Initiate Mag Calibration\r\n");
    // Wait for SW1 press and release
    while( sw1 == 1 ) {
    }
    while( sw1 == 0 ) {
    }

    printf("Rotate the board 360 degrees on a level plane then press and release SW1\r\n");

    tempXmax = tempXmin = mag.readVal(MAG_OUT_X_MSB);
    tempYmax = tempYmin = mag.readVal(MAG_OUT_Y_MSB);

    while(sw1 == 1) {

        newX = mag.readVal(MAG_OUT_X_MSB);
        newY = mag.readVal(MAG_OUT_Y_MSB);
        if (newX > tempXmax) tempXmax = newX;
        if (newX < tempXmin) tempXmin = newX;
        if (newY > tempYmax) tempYmax = newY;
        if (newY < tempYmin) tempYmin = newY;
    }

    mag.setCalibration( tempXmin, tempXmax, tempYmin, tempYmax );

    // Wait for release
    while( sw1 == 0 ) {
    }
    
    printf("Calibration complete.\r\n");

}