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

Fork of FRDM-KL46-Template by Mike Maas

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

Committer:
mmaas
Date:
Tue Dec 31 17:16:57 2013 +0000
Revision:
4:6cb640167538
Parent:
3:1e85b49a3e18
Child:
6:aaf347dfd538
Moved the calibrate operation into the MAG3110 library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mmaas 0:79ae6809c2d0 1 #include "mbed.h"
mmaas 3:1e85b49a3e18 2 #include "MMA8451Q.h"
mmaas 3:1e85b49a3e18 3 #include "MAG3110.h"
mmaas 0:79ae6809c2d0 4
mmaas 3:1e85b49a3e18 5
mmaas 3:1e85b49a3e18 6 // This project has been created to bring together the libraries required to support
mmaas 3:1e85b49a3e18 7 // the hardware and sensors found on the Freescale FRDM-KL46Z board. The following
mmaas 3:1e85b49a3e18 8 // libraries are included and exercised in this project:
mmaas 3:1e85b49a3e18 9 //
mmaas 3:1e85b49a3e18 10 // mbed (source: official mbed library)
mmaas 3:1e85b49a3e18 11 // Serial:
mmaas 3:1e85b49a3e18 12 // Serial console routed through the mbed interface
mmaas 3:1e85b49a3e18 13 // PTA2 / UART0_TX = TX Signal - also on Arduino pin D1
mmaas 3:1e85b49a3e18 14 // PTA1 / UART0_RX = RX Signal - also on Arduino pin D0
mmaas 3:1e85b49a3e18 15 //
mmaas 3:1e85b49a3e18 16 // DigitalOut:
mmaas 3:1e85b49a3e18 17 // GPIO to drive onboard LEDs
mmaas 3:1e85b49a3e18 18 // PTD5 / GPIO = LED1 - drive low to turn on LED - also on Arduino pin D13
mmaas 3:1e85b49a3e18 19 // PTE29 / GPIO = LED2 - drive low to turn on LED
mmaas 3:1e85b49a3e18 20 //
mmaas 3:1e85b49a3e18 21 // DigitalIn:
mmaas 3:1e85b49a3e18 22 // GPIO to monitor the two onboard push buttons
mmaas 3:1e85b49a3e18 23 // PTC3 / GPIO = SW1 - low input = button pressed
mmaas 3:1e85b49a3e18 24 // PTC12 / GPIO = SW3 - low input = button pressed
mmaas 3:1e85b49a3e18 25 //
mmaas 3:1e85b49a3e18 26 // TSI (source: http://mbed.org/users/emilmont/code/TSI/ )
mmaas 3:1e85b49a3e18 27 // Capacitive Touch library to support the onboard Touch-Slider
mmaas 3:1e85b49a3e18 28 //
mmaas 3:1e85b49a3e18 29 // FRDM_MMA8451Q (source: http://mbed.org/users/clemente/code/FRDM_MMA8451Q/ )
mmaas 3:1e85b49a3e18 30 // Freescale MMA8451 Accelerometer connected on I2C0
mmaas 3:1e85b49a3e18 31 // PTE24 / I2C0_SCL = I2C bus for communication (shared with MAG3110)
mmaas 3:1e85b49a3e18 32 // PTE25 / I2C0_SDA = I2C bus for communication (shared with MAG3110)
mmaas 3:1e85b49a3e18 33 // PTC5 / INT1_ACCEL = INT1 output of MMA8451Q
mmaas 3:1e85b49a3e18 34 // PTD1 / INT2_ACCEL = INT2 output of MMA8451Q (shared with MAG3110)
mmaas 3:1e85b49a3e18 35 //
mmaas 3:1e85b49a3e18 36 // MAG3110
mmaas 3:1e85b49a3e18 37 // Freescale MAG3110 Magnetomoter connected on I2C0
mmaas 3:1e85b49a3e18 38 // PTE24 / I2C0_SCL = I2C bus for communication (shared with MMA8451)
mmaas 3:1e85b49a3e18 39 // PTE25 / I2C0_SDA = I2C bus for communication (shared with MMA8451)
mmaas 3:1e85b49a3e18 40 // PTD1 / INT1_MAG / INT2_ACCEL = INT1 output of MAG3110 (shared with MMA8451)
mmaas 3:1e85b49a3e18 41 //
mmaas 3:1e85b49a3e18 42 // FRDM_LightSensor
mmaas 3:1e85b49a3e18 43 // Ambient light sensor (Q1)
mmaas 3:1e85b49a3e18 44 // PTE22 / ADC
mmaas 3:1e85b49a3e18 45 //
mmaas 3:1e85b49a3e18 46 // FRDM_LCD
mmaas 3:1e85b49a3e18 47 // 4 Digit Segment LCD
mmaas 3:1e85b49a3e18 48 // TempSensor - KL46
mmaas 3:1e85b49a3e18 49
mmaas 3:1e85b49a3e18 50
mmaas 3:1e85b49a3e18 51 //////////////////////////////////////////////////////////////////////
mmaas 1:28c81db67f50 52 // Include support for USB Serial console
mmaas 0:79ae6809c2d0 53 Serial pc(USBTX, USBRX);
mmaas 0:79ae6809c2d0 54
mmaas 3:1e85b49a3e18 55 //////////////////////////////////////////////////////////////////////
mmaas 1:28c81db67f50 56 // Include support for on-board green and red LEDs
mmaas 3:1e85b49a3e18 57 #define LED_ON 0
mmaas 3:1e85b49a3e18 58 #define LED_OFF 1
mmaas 2:48ac01f5024d 59 DigitalOut greenLED(LED_GREEN);
mmaas 1:28c81db67f50 60 DigitalOut redLED(LED_RED);
mmaas 1:28c81db67f50 61
mmaas 3:1e85b49a3e18 62
mmaas 3:1e85b49a3e18 63 //////////////////////////////////////////////////////////////////////
mmaas 3:1e85b49a3e18 64 // Include support for onboard pushbuttons
mmaas 3:1e85b49a3e18 65 DigitalIn sw1(PTC3);
mmaas 3:1e85b49a3e18 66 DigitalIn sw3(PTC12);
mmaas 3:1e85b49a3e18 67
mmaas 0:79ae6809c2d0 68
mmaas 3:1e85b49a3e18 69 /////////////////////////////////////////////////////////////////////
mmaas 3:1e85b49a3e18 70 // Include support for MMA8451Q Acceleromoter
mmaas 3:1e85b49a3e18 71 #define MMA8451_I2C_ADDRESS (0x1d<<1)
mmaas 3:1e85b49a3e18 72 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
mmaas 3:1e85b49a3e18 73
mmaas 3:1e85b49a3e18 74
mmaas 3:1e85b49a3e18 75 /////////////////////////////////////////////////////////////////////
mmaas 3:1e85b49a3e18 76 // Include support for MAG3110 Magnetometer
mmaas 3:1e85b49a3e18 77 void calXY(); //magnetometer calibration: finding max and min of X, Y axis
mmaas 3:1e85b49a3e18 78 MAG3110 mag(PTE25, PTE24);
mmaas 3:1e85b49a3e18 79
mmaas 3:1e85b49a3e18 80
mmaas 3:1e85b49a3e18 81
mmaas 3:1e85b49a3e18 82 int main()
mmaas 3:1e85b49a3e18 83 {
mmaas 0:79ae6809c2d0 84
mmaas 1:28c81db67f50 85 // Ensure LEDs are off
mmaas 2:48ac01f5024d 86 greenLED = LED_OFF;
mmaas 1:28c81db67f50 87 redLED = LED_OFF;
mmaas 3:1e85b49a3e18 88
mmaas 0:79ae6809c2d0 89 // Set Serial Port data rate and say Hello
mmaas 0:79ae6809c2d0 90 pc.baud( 230400 );
mmaas 0:79ae6809c2d0 91 pc.printf("Hello World\r\n");
mmaas 0:79ae6809c2d0 92
mmaas 4:6cb640167538 93
mmaas 3:1e85b49a3e18 94 // Turn on pull up resistors on pushbutton inputs
mmaas 3:1e85b49a3e18 95 sw1.mode(PullUp);
mmaas 3:1e85b49a3e18 96 sw3.mode(PullUp);
mmaas 3:1e85b49a3e18 97
mmaas 2:48ac01f5024d 98 // Quick blink LEDs
mmaas 2:48ac01f5024d 99 redLED = LED_ON;
mmaas 2:48ac01f5024d 100 wait(.05);
mmaas 2:48ac01f5024d 101 greenLED = LED_ON;
mmaas 2:48ac01f5024d 102 wait(.05);
mmaas 2:48ac01f5024d 103 greenLED = LED_OFF;
mmaas 2:48ac01f5024d 104 redLED = LED_OFF;
mmaas 3:1e85b49a3e18 105
mmaas 2:48ac01f5024d 106 wait(1);
mmaas 2:48ac01f5024d 107
mmaas 4:6cb640167538 108
mmaas 3:1e85b49a3e18 109 // Get some values
mmaas 3:1e85b49a3e18 110 printf("DR_STATUS %X\r\n", mag.readReg( MAG_DR_STATUS ));
mmaas 3:1e85b49a3e18 111 printf("WHO_AM_I %X\r\n", mag.readReg( MAG_WHO_AM_I ));
mmaas 3:1e85b49a3e18 112 printf("SYSMOD %X\r\n", mag.readReg( MAG_SYSMOD ));
mmaas 3:1e85b49a3e18 113 printf("DIE_TEMP %d\r\n", mag.readReg( MAG_DIE_TEMP ));
mmaas 3:1e85b49a3e18 114
mmaas 3:1e85b49a3e18 115 printf("OFF_X %d\r\n", mag.readVal( MAG_OFF_X_MSB ));
mmaas 3:1e85b49a3e18 116 printf("OFF_Y %d\r\n", mag.readVal( MAG_OFF_Y_MSB ));
mmaas 3:1e85b49a3e18 117 printf("OFF_Z %d\r\n", mag.readVal( MAG_OFF_Z_MSB ));
mmaas 3:1e85b49a3e18 118
mmaas 3:1e85b49a3e18 119 printf("CTRL_REG1 %X\r\n", mag.readReg( MAG_CTRL_REG1 ));
mmaas 3:1e85b49a3e18 120 printf("CTRL_REG2 %X\r\n", mag.readReg( MAG_CTRL_REG2 ));
mmaas 2:48ac01f5024d 121
mmaas 2:48ac01f5024d 122
mmaas 4:6cb640167538 123 printf("Press and release SW1, rotate the board 360 degrees and then press and release SW1 to complete the calibration process.\r\n");
mmaas 4:6cb640167538 124
mmaas 4:6cb640167538 125 mag.calXY(PTC3, 0);
mmaas 4:6cb640167538 126
mmaas 4:6cb640167538 127 printf("Calibration complete.\r\n");
mmaas 4:6cb640167538 128
mmaas 3:1e85b49a3e18 129
mmaas 3:1e85b49a3e18 130 // Loop: Blink LEDs, report pushbutton status, mag data, accel data
mmaas 0:79ae6809c2d0 131 while(1) {
mmaas 4:6cb640167538 132
mmaas 3:1e85b49a3e18 133 // Get Magnetometer data
mmaas 3:1e85b49a3e18 134 int xVal = mag.readVal(MAG_OUT_X_MSB);
mmaas 3:1e85b49a3e18 135 int yVal = mag.readVal(MAG_OUT_Y_MSB);
mmaas 4:6cb640167538 136 float heading = mag.getHeading();
mmaas 4:6cb640167538 137
mmaas 4:6cb640167538 138 // Do something with heading - display direction
mmaas 3:1e85b49a3e18 139 printf("Heading: ");
mmaas 3:1e85b49a3e18 140 if (abs(heading) <= 22.5) printf("N ");
mmaas 3:1e85b49a3e18 141 if (abs(heading) >= 157.5) printf("S ");
mmaas 3:1e85b49a3e18 142 if (heading >= 67.5 && heading <= 112.5) printf("E ");
mmaas 3:1e85b49a3e18 143 if (heading <= -67.5 && heading >= -112.5) printf("W ");
mmaas 3:1e85b49a3e18 144 if (heading > 22.5 && heading < 67.5) printf("NE ");
mmaas 3:1e85b49a3e18 145 if (heading < -22.5 && heading > -67.5) printf("NW ");
mmaas 3:1e85b49a3e18 146 if (heading > 112.5 && heading < 157.5) printf("SE ");
mmaas 3:1e85b49a3e18 147 if (heading < -112.5 && heading > -157.5) printf("SW ");
mmaas 3:1e85b49a3e18 148 if (heading < 0) heading += 360.0;
mmaas 3:1e85b49a3e18 149 printf("%f\r\n", heading);
mmaas 4:6cb640167538 150
mmaas 4:6cb640167538 151
mmaas 2:48ac01f5024d 152 greenLED = LED_ON;
mmaas 1:28c81db67f50 153 redLED = LED_OFF;
mmaas 3:1e85b49a3e18 154
mmaas 3:1e85b49a3e18 155 printf("SW1 = %d, SW3 = %d, X = %f, Y = %f, Z = %f\r\n\n", sw1.read(), sw3.read(), acc.getAccX(), acc.getAccY(), acc.getAccZ());
mmaas 3:1e85b49a3e18 156 wait(0.25);
mmaas 3:1e85b49a3e18 157
mmaas 2:48ac01f5024d 158 greenLED = LED_OFF;
mmaas 1:28c81db67f50 159 redLED = LED_ON;
mmaas 1:28c81db67f50 160
mmaas 3:1e85b49a3e18 161 wait(0.25);
mmaas 1:28c81db67f50 162
mmaas 0:79ae6809c2d0 163 }
mmaas 0:79ae6809c2d0 164 }
mmaas 3:1e85b49a3e18 165
mmaas 3:1e85b49a3e18 166
mmaas 3:1e85b49a3e18 167 void calXY() //magnetometer calibration: finding max and min of X, Y axis
mmaas 3:1e85b49a3e18 168 {
mmaas 4:6cb640167538 169
mmaas 3:1e85b49a3e18 170
mmaas 3:1e85b49a3e18 171 }
mmaas 3:1e85b49a3e18 172
mmaas 3:1e85b49a3e18 173