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:
Thu Dec 19 20:05:17 2013 +0000
Revision:
3:1e85b49a3e18
Parent:
2:48ac01f5024d
Child:
4:6cb640167538
Added support for MMA8451Q and MAG3110 with calibration function.

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 3:1e85b49a3e18 56 //////////////////////////////////////////////////////////////////////
mmaas 1:28c81db67f50 57 // Include support for on-board green and red LEDs
mmaas 3:1e85b49a3e18 58 #define LED_ON 0
mmaas 3:1e85b49a3e18 59 #define LED_OFF 1
mmaas 2:48ac01f5024d 60 DigitalOut greenLED(LED_GREEN);
mmaas 1:28c81db67f50 61 DigitalOut redLED(LED_RED);
mmaas 1:28c81db67f50 62
mmaas 3:1e85b49a3e18 63
mmaas 3:1e85b49a3e18 64 //////////////////////////////////////////////////////////////////////
mmaas 3:1e85b49a3e18 65 // Include support for onboard pushbuttons
mmaas 3:1e85b49a3e18 66 DigitalIn sw1(PTC3);
mmaas 3:1e85b49a3e18 67 DigitalIn sw3(PTC12);
mmaas 3:1e85b49a3e18 68
mmaas 0:79ae6809c2d0 69
mmaas 3:1e85b49a3e18 70 /////////////////////////////////////////////////////////////////////
mmaas 3:1e85b49a3e18 71 // Include support for MMA8451Q Acceleromoter
mmaas 3:1e85b49a3e18 72 #define MMA8451_I2C_ADDRESS (0x1d<<1)
mmaas 3:1e85b49a3e18 73 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
mmaas 3:1e85b49a3e18 74
mmaas 3:1e85b49a3e18 75
mmaas 3:1e85b49a3e18 76 /////////////////////////////////////////////////////////////////////
mmaas 3:1e85b49a3e18 77 // Include support for MAG3110 Magnetometer
mmaas 3:1e85b49a3e18 78 void calXY(); //magnetometer calibration: finding max and min of X, Y axis
mmaas 3:1e85b49a3e18 79 MAG3110 mag(PTE25, PTE24);
mmaas 3:1e85b49a3e18 80
mmaas 3:1e85b49a3e18 81
mmaas 3:1e85b49a3e18 82
mmaas 3:1e85b49a3e18 83 int main()
mmaas 3:1e85b49a3e18 84 {
mmaas 0:79ae6809c2d0 85
mmaas 1:28c81db67f50 86 // Ensure LEDs are off
mmaas 2:48ac01f5024d 87 greenLED = LED_OFF;
mmaas 1:28c81db67f50 88 redLED = LED_OFF;
mmaas 3:1e85b49a3e18 89
mmaas 0:79ae6809c2d0 90 // Set Serial Port data rate and say Hello
mmaas 0:79ae6809c2d0 91 pc.baud( 230400 );
mmaas 0:79ae6809c2d0 92 pc.printf("Hello World\r\n");
mmaas 0:79ae6809c2d0 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 3:1e85b49a3e18 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 3:1e85b49a3e18 123 // Calibrate the magnetomoter
mmaas 3:1e85b49a3e18 124 calXY();
mmaas 3:1e85b49a3e18 125
mmaas 3:1e85b49a3e18 126
mmaas 3:1e85b49a3e18 127 // Loop: Blink LEDs, report pushbutton status, mag data, accel data
mmaas 0:79ae6809c2d0 128 while(1) {
mmaas 3:1e85b49a3e18 129
mmaas 3:1e85b49a3e18 130 // Get Magnetometer data
mmaas 3:1e85b49a3e18 131 int xVal = mag.readVal(MAG_OUT_X_MSB);
mmaas 3:1e85b49a3e18 132 int yVal = mag.readVal(MAG_OUT_Y_MSB);
mmaas 3:1e85b49a3e18 133 float heading = mag.getHeading();
mmaas 3:1e85b49a3e18 134
mmaas 3:1e85b49a3e18 135 // Do something with heading - display direction
mmaas 3:1e85b49a3e18 136 printf("Heading: ");
mmaas 3:1e85b49a3e18 137 if (abs(heading) <= 22.5) printf("N ");
mmaas 3:1e85b49a3e18 138 if (abs(heading) >= 157.5) printf("S ");
mmaas 3:1e85b49a3e18 139 if (heading >= 67.5 && heading <= 112.5) printf("E ");
mmaas 3:1e85b49a3e18 140 if (heading <= -67.5 && heading >= -112.5) printf("W ");
mmaas 3:1e85b49a3e18 141 if (heading > 22.5 && heading < 67.5) printf("NE ");
mmaas 3:1e85b49a3e18 142 if (heading < -22.5 && heading > -67.5) printf("NW ");
mmaas 3:1e85b49a3e18 143 if (heading > 112.5 && heading < 157.5) printf("SE ");
mmaas 3:1e85b49a3e18 144 if (heading < -112.5 && heading > -157.5) printf("SW ");
mmaas 3:1e85b49a3e18 145 if (heading < 0) heading += 360.0;
mmaas 3:1e85b49a3e18 146 printf("%f\r\n", heading);
mmaas 3:1e85b49a3e18 147
mmaas 3:1e85b49a3e18 148
mmaas 2:48ac01f5024d 149 greenLED = LED_ON;
mmaas 1:28c81db67f50 150 redLED = LED_OFF;
mmaas 3:1e85b49a3e18 151
mmaas 3:1e85b49a3e18 152 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 153 wait(0.25);
mmaas 3:1e85b49a3e18 154
mmaas 2:48ac01f5024d 155 greenLED = LED_OFF;
mmaas 1:28c81db67f50 156 redLED = LED_ON;
mmaas 1:28c81db67f50 157
mmaas 3:1e85b49a3e18 158 wait(0.25);
mmaas 1:28c81db67f50 159
mmaas 0:79ae6809c2d0 160 }
mmaas 0:79ae6809c2d0 161 }
mmaas 3:1e85b49a3e18 162
mmaas 3:1e85b49a3e18 163
mmaas 3:1e85b49a3e18 164 void calXY() //magnetometer calibration: finding max and min of X, Y axis
mmaas 3:1e85b49a3e18 165 {
mmaas 3:1e85b49a3e18 166 int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY;
mmaas 3:1e85b49a3e18 167
mmaas 3:1e85b49a3e18 168
mmaas 3:1e85b49a3e18 169 printf("Press and release SW1 to Initiate Mag Calibration\r\n");
mmaas 3:1e85b49a3e18 170 // Wait for SW1 press and release
mmaas 3:1e85b49a3e18 171 while( sw1 == 1 ) {
mmaas 3:1e85b49a3e18 172 }
mmaas 3:1e85b49a3e18 173 while( sw1 == 0 ) {
mmaas 3:1e85b49a3e18 174 }
mmaas 3:1e85b49a3e18 175
mmaas 3:1e85b49a3e18 176 printf("Rotate the board 360 degrees on a level plane then press and release SW1\r\n");
mmaas 3:1e85b49a3e18 177
mmaas 3:1e85b49a3e18 178 tempXmax = tempXmin = mag.readVal(MAG_OUT_X_MSB);
mmaas 3:1e85b49a3e18 179 tempYmax = tempYmin = mag.readVal(MAG_OUT_Y_MSB);
mmaas 3:1e85b49a3e18 180
mmaas 3:1e85b49a3e18 181 while(sw1 == 1) {
mmaas 3:1e85b49a3e18 182
mmaas 3:1e85b49a3e18 183 newX = mag.readVal(MAG_OUT_X_MSB);
mmaas 3:1e85b49a3e18 184 newY = mag.readVal(MAG_OUT_Y_MSB);
mmaas 3:1e85b49a3e18 185 if (newX > tempXmax) tempXmax = newX;
mmaas 3:1e85b49a3e18 186 if (newX < tempXmin) tempXmin = newX;
mmaas 3:1e85b49a3e18 187 if (newY > tempYmax) tempYmax = newY;
mmaas 3:1e85b49a3e18 188 if (newY < tempYmin) tempYmin = newY;
mmaas 3:1e85b49a3e18 189 }
mmaas 3:1e85b49a3e18 190
mmaas 3:1e85b49a3e18 191 mag.setCalibration( tempXmin, tempXmax, tempYmin, tempYmax );
mmaas 3:1e85b49a3e18 192
mmaas 3:1e85b49a3e18 193 // Wait for release
mmaas 3:1e85b49a3e18 194 while( sw1 == 0 ) {
mmaas 3:1e85b49a3e18 195 }
mmaas 3:1e85b49a3e18 196
mmaas 3:1e85b49a3e18 197 printf("Calibration complete.\r\n");
mmaas 3:1e85b49a3e18 198
mmaas 3:1e85b49a3e18 199 }
mmaas 3:1e85b49a3e18 200
mmaas 3:1e85b49a3e18 201