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

Committer:
mmaas
Date:
Sat Jan 04 04:40:00 2014 +0000
Revision:
7:9e1d22b35dab
Parent:
6:aaf347dfd538
Child:
8:b61a953b6128
Changed back to pre-compiled mbed library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mmaas 0:79ae6809c2d0 1 #include "mbed.h"
mmaas 6:aaf347dfd538 2 #include "TSISensor.h"
mmaas 3:1e85b49a3e18 3 #include "MMA8451Q.h"
mmaas 3:1e85b49a3e18 4 #include "MAG3110.h"
mmaas 0:79ae6809c2d0 5
mmaas 3:1e85b49a3e18 6
mmaas 3:1e85b49a3e18 7 // This project has been created to bring together the libraries required to support
mmaas 3:1e85b49a3e18 8 // the hardware and sensors found on the Freescale FRDM-KL46Z board. The following
mmaas 3:1e85b49a3e18 9 // libraries are included and exercised in this project:
mmaas 3:1e85b49a3e18 10 //
mmaas 3:1e85b49a3e18 11 // mbed (source: official mbed library)
mmaas 3:1e85b49a3e18 12 // Serial:
mmaas 3:1e85b49a3e18 13 // Serial console routed through the mbed interface
mmaas 3:1e85b49a3e18 14 // PTA2 / UART0_TX = TX Signal - also on Arduino pin D1
mmaas 3:1e85b49a3e18 15 // PTA1 / UART0_RX = RX Signal - also on Arduino pin D0
mmaas 3:1e85b49a3e18 16 //
mmaas 3:1e85b49a3e18 17 // DigitalOut:
mmaas 3:1e85b49a3e18 18 // GPIO to drive onboard LEDs
mmaas 3:1e85b49a3e18 19 // PTD5 / GPIO = LED1 - drive low to turn on LED - also on Arduino pin D13
mmaas 3:1e85b49a3e18 20 // PTE29 / GPIO = LED2 - drive low to turn on LED
mmaas 3:1e85b49a3e18 21 //
mmaas 3:1e85b49a3e18 22 // DigitalIn:
mmaas 3:1e85b49a3e18 23 // GPIO to monitor the two onboard push buttons
mmaas 3:1e85b49a3e18 24 // PTC3 / GPIO = SW1 - low input = button pressed
mmaas 3:1e85b49a3e18 25 // PTC12 / GPIO = SW3 - low input = button pressed
mmaas 6:aaf347dfd538 26 //
mmaas 6:aaf347dfd538 27 // AnalogIn:
mmaas 6:aaf347dfd538 28 // ADC channel to monitor ambient light sensor
mmaas 6:aaf347dfd538 29 // PTE22 / ADC = Light Sensor - higher value = darker
mmaas 3:1e85b49a3e18 30 //
mmaas 3:1e85b49a3e18 31 // TSI (source: http://mbed.org/users/emilmont/code/TSI/ )
mmaas 3:1e85b49a3e18 32 // Capacitive Touch library to support the onboard Touch-Slider
mmaas 3:1e85b49a3e18 33 //
mmaas 3:1e85b49a3e18 34 // FRDM_MMA8451Q (source: http://mbed.org/users/clemente/code/FRDM_MMA8451Q/ )
mmaas 3:1e85b49a3e18 35 // Freescale MMA8451 Accelerometer connected on I2C0
mmaas 3:1e85b49a3e18 36 // PTE24 / I2C0_SCL = I2C bus for communication (shared with MAG3110)
mmaas 3:1e85b49a3e18 37 // PTE25 / I2C0_SDA = I2C bus for communication (shared with MAG3110)
mmaas 3:1e85b49a3e18 38 // PTC5 / INT1_ACCEL = INT1 output of MMA8451Q
mmaas 3:1e85b49a3e18 39 // PTD1 / INT2_ACCEL = INT2 output of MMA8451Q (shared with MAG3110)
mmaas 3:1e85b49a3e18 40 //
mmaas 7:9e1d22b35dab 41 // MAG3110 (source: http://mbed.org/users/mmaas/code/MAG3110/)
mmaas 7:9e1d22b35dab 42 // (based on: http://mbed.org/users/SomeRandomBloke/code/MAG3110/)
mmaas 3:1e85b49a3e18 43 // Freescale MAG3110 Magnetomoter connected on I2C0
mmaas 3:1e85b49a3e18 44 // PTE24 / I2C0_SCL = I2C bus for communication (shared with MMA8451)
mmaas 3:1e85b49a3e18 45 // PTE25 / I2C0_SDA = I2C bus for communication (shared with MMA8451)
mmaas 3:1e85b49a3e18 46 // PTD1 / INT1_MAG / INT2_ACCEL = INT1 output of MAG3110 (shared with MMA8451)
mmaas 3:1e85b49a3e18 47 //
mmaas 6:aaf347dfd538 48
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 6:aaf347dfd538 55
mmaas 3:1e85b49a3e18 56 //////////////////////////////////////////////////////////////////////
mmaas 1:28c81db67f50 57 // Include support for on-board green and red LEDs
mmaas 6:aaf347dfd538 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 6:aaf347dfd538 65 // Include support for onboard pushbuttons (value = 0 when pressed)
mmaas 3:1e85b49a3e18 66 DigitalIn sw1(PTC3);
mmaas 3:1e85b49a3e18 67 DigitalIn sw3(PTC12);
mmaas 3:1e85b49a3e18 68
mmaas 0:79ae6809c2d0 69
mmaas 6:aaf347dfd538 70 //////////////////////////////////////////////////////////////////////
mmaas 6:aaf347dfd538 71 // Include support for onboard Capacitive Touch Slider
mmaas 6:aaf347dfd538 72 TSISensor slider;
mmaas 6:aaf347dfd538 73
mmaas 6:aaf347dfd538 74
mmaas 6:aaf347dfd538 75 //////////////////////////////////////////////////////////////////////
mmaas 6:aaf347dfd538 76 // Include support for analog inputs
mmaas 6:aaf347dfd538 77 AnalogIn lightSense(PTE22);
mmaas 6:aaf347dfd538 78
mmaas 6:aaf347dfd538 79
mmaas 3:1e85b49a3e18 80 /////////////////////////////////////////////////////////////////////
mmaas 3:1e85b49a3e18 81 // Include support for MMA8451Q Acceleromoter
mmaas 3:1e85b49a3e18 82 #define MMA8451_I2C_ADDRESS (0x1d<<1)
mmaas 3:1e85b49a3e18 83 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
mmaas 3:1e85b49a3e18 84
mmaas 3:1e85b49a3e18 85
mmaas 3:1e85b49a3e18 86 /////////////////////////////////////////////////////////////////////
mmaas 3:1e85b49a3e18 87 // Include support for MAG3110 Magnetometer
mmaas 3:1e85b49a3e18 88 MAG3110 mag(PTE25, PTE24);
mmaas 3:1e85b49a3e18 89
mmaas 3:1e85b49a3e18 90
mmaas 6:aaf347dfd538 91 /////////////////////////////////////////////////////////////////////
mmaas 6:aaf347dfd538 92 // Structure to hold FRDM-KL46Z sensor and input data
mmaas 6:aaf347dfd538 93 struct KL46_SENSOR_DATA {
mmaas 6:aaf347dfd538 94 int sw1State;
mmaas 6:aaf347dfd538 95 int sw3State;
mmaas 6:aaf347dfd538 96
mmaas 6:aaf347dfd538 97 float sliderPosition;
mmaas 6:aaf347dfd538 98
mmaas 6:aaf347dfd538 99 float lightSensor;
mmaas 6:aaf347dfd538 100
mmaas 6:aaf347dfd538 101 int magXVal;
mmaas 6:aaf347dfd538 102 int magYVal;
mmaas 6:aaf347dfd538 103 float magHeading;
mmaas 6:aaf347dfd538 104
mmaas 6:aaf347dfd538 105 float accXVal;
mmaas 6:aaf347dfd538 106 float accYVal;
mmaas 6:aaf347dfd538 107 float accZVal;
mmaas 6:aaf347dfd538 108 } sensorData;
mmaas 6:aaf347dfd538 109
mmaas 3:1e85b49a3e18 110
mmaas 6:aaf347dfd538 111 /////////////////////////////////////////////////////////////////////
mmaas 6:aaf347dfd538 112 // Prototype for routine to send all sensor data to serial port
mmaas 6:aaf347dfd538 113 void serialSendSensorData(void);
mmaas 6:aaf347dfd538 114
mmaas 6:aaf347dfd538 115
mmaas 6:aaf347dfd538 116 /////////////////////////////////////////////////////////////////////
mmaas 6:aaf347dfd538 117 // main application
mmaas 3:1e85b49a3e18 118 int main()
mmaas 3:1e85b49a3e18 119 {
mmaas 1:28c81db67f50 120 // Ensure LEDs are off
mmaas 2:48ac01f5024d 121 greenLED = LED_OFF;
mmaas 1:28c81db67f50 122 redLED = LED_OFF;
mmaas 3:1e85b49a3e18 123
mmaas 0:79ae6809c2d0 124 // Set Serial Port data rate and say Hello
mmaas 6:aaf347dfd538 125 pc.baud( 115200 );
mmaas 0:79ae6809c2d0 126 pc.printf("Hello World\r\n");
mmaas 0:79ae6809c2d0 127
mmaas 3:1e85b49a3e18 128 // Turn on pull up resistors on pushbutton inputs
mmaas 3:1e85b49a3e18 129 sw1.mode(PullUp);
mmaas 3:1e85b49a3e18 130 sw3.mode(PullUp);
mmaas 3:1e85b49a3e18 131
mmaas 6:aaf347dfd538 132 // Calibrate Magnetometer
mmaas 6:aaf347dfd538 133 printf("Press and release SW1, rotate the board 360 degrees.\r\n");
mmaas 6:aaf347dfd538 134 printf("Then press and release SW1 to complete the calibration process.\r\n");
mmaas 4:6cb640167538 135
mmaas 4:6cb640167538 136 mag.calXY(PTC3, 0);
mmaas 4:6cb640167538 137
mmaas 4:6cb640167538 138 printf("Calibration complete.\r\n");
mmaas 4:6cb640167538 139
mmaas 4:6cb640167538 140
mmaas 6:aaf347dfd538 141 // Loop forever - read and update sensor data and print to console.
mmaas 6:aaf347dfd538 142 while(1)
mmaas 6:aaf347dfd538 143 {
mmaas 6:aaf347dfd538 144 sensorData.sw1State = sw1;
mmaas 6:aaf347dfd538 145 sensorData.sw3State = sw3;
mmaas 6:aaf347dfd538 146
mmaas 6:aaf347dfd538 147 sensorData.sliderPosition = slider.readPercentage() * 100;
mmaas 6:aaf347dfd538 148
mmaas 6:aaf347dfd538 149 sensorData.lightSensor = lightSense;
mmaas 6:aaf347dfd538 150
mmaas 6:aaf347dfd538 151 sensorData.accXVal = acc.getAccX();
mmaas 6:aaf347dfd538 152 sensorData.accYVal = acc.getAccY();
mmaas 6:aaf347dfd538 153 sensorData.accZVal = acc.getAccZ();
mmaas 4:6cb640167538 154
mmaas 6:aaf347dfd538 155 sensorData.magXVal = mag.readVal(MAG_OUT_X_MSB);
mmaas 6:aaf347dfd538 156 sensorData.magYVal = mag.readVal(MAG_OUT_Y_MSB);
mmaas 6:aaf347dfd538 157 sensorData.magHeading = mag.getHeading();
mmaas 6:aaf347dfd538 158
mmaas 6:aaf347dfd538 159 serialSendSensorData();
mmaas 6:aaf347dfd538 160
mmaas 6:aaf347dfd538 161 // Blink LEDs (show life)
mmaas 6:aaf347dfd538 162 redLED = LED_ON;
mmaas 6:aaf347dfd538 163 wait(.03);
mmaas 6:aaf347dfd538 164 greenLED = LED_ON;
mmaas 6:aaf347dfd538 165 wait(.03);
mmaas 6:aaf347dfd538 166 redLED = LED_OFF;
mmaas 6:aaf347dfd538 167 wait(.03);
mmaas 6:aaf347dfd538 168 greenLED = LED_OFF;
mmaas 4:6cb640167538 169
mmaas 6:aaf347dfd538 170 wait(1);
mmaas 6:aaf347dfd538 171 }
mmaas 0:79ae6809c2d0 172 }
mmaas 3:1e85b49a3e18 173
mmaas 3:1e85b49a3e18 174
mmaas 6:aaf347dfd538 175 void serialSendSensorData(void)
mmaas 3:1e85b49a3e18 176 {
mmaas 6:aaf347dfd538 177 printf("Switches:\r\n SW1 = %d\r\n SW3 = %d\r\n\r\n", sensorData.sw1State, sensorData.sw3State);
mmaas 6:aaf347dfd538 178 printf("Slider:\r\n %2.0f %% \r\n\r\n", sensorData.sliderPosition);
mmaas 6:aaf347dfd538 179 printf("Light Sensor:\r\n %1.3f \r\n\r\n", sensorData.lightSensor);
mmaas 6:aaf347dfd538 180 printf("Accelerometer:\r\n X = %1.3f\r\n Y = %1.3f\r\n Z = %1.3f\r\n\r\n", sensorData.accXVal, sensorData.accYVal, sensorData.accZVal);
mmaas 7:9e1d22b35dab 181 printf("Magnetometer:\r\n X = %d\r\n Y = %d\r\n Heading = %.2f \r\n\r\n", sensorData.magXVal, sensorData.magYVal, sensorData.magHeading);
mmaas 6:aaf347dfd538 182
mmaas 6:aaf347dfd538 183 printf("\r\n");
mmaas 6:aaf347dfd538 184
mmaas 6:aaf347dfd538 185 }
mmaas 3:1e85b49a3e18 186
mmaas 3:1e85b49a3e18 187
mmaas 6:aaf347dfd538 188