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 02:33:13 2014 +0000
Revision:
6:aaf347dfd538
Parent:
4:6cb640167538
Child:
7:9e1d22b35dab
Cleaned up code - added loop polling all inputs and routine to print add values formatted nicely to serial port.

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 3:1e85b49a3e18 41 // MAG3110
mmaas 3:1e85b49a3e18 42 // Freescale MAG3110 Magnetomoter connected on I2C0
mmaas 3:1e85b49a3e18 43 // PTE24 / I2C0_SCL = I2C bus for communication (shared with MMA8451)
mmaas 3:1e85b49a3e18 44 // PTE25 / I2C0_SDA = I2C bus for communication (shared with MMA8451)
mmaas 3:1e85b49a3e18 45 // PTD1 / INT1_MAG / INT2_ACCEL = INT1 output of MAG3110 (shared with MMA8451)
mmaas 3:1e85b49a3e18 46 //
mmaas 6:aaf347dfd538 47
mmaas 3:1e85b49a3e18 48
mmaas 3:1e85b49a3e18 49
mmaas 3:1e85b49a3e18 50 //////////////////////////////////////////////////////////////////////
mmaas 1:28c81db67f50 51 // Include support for USB Serial console
mmaas 0:79ae6809c2d0 52 Serial pc(USBTX, USBRX);
mmaas 0:79ae6809c2d0 53
mmaas 6:aaf347dfd538 54
mmaas 3:1e85b49a3e18 55 //////////////////////////////////////////////////////////////////////
mmaas 1:28c81db67f50 56 // Include support for on-board green and red LEDs
mmaas 6:aaf347dfd538 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 6:aaf347dfd538 64 // Include support for onboard pushbuttons (value = 0 when pressed)
mmaas 3:1e85b49a3e18 65 DigitalIn sw1(PTC3);
mmaas 3:1e85b49a3e18 66 DigitalIn sw3(PTC12);
mmaas 3:1e85b49a3e18 67
mmaas 0:79ae6809c2d0 68
mmaas 6:aaf347dfd538 69 //////////////////////////////////////////////////////////////////////
mmaas 6:aaf347dfd538 70 // Include support for onboard Capacitive Touch Slider
mmaas 6:aaf347dfd538 71 TSISensor slider;
mmaas 6:aaf347dfd538 72
mmaas 6:aaf347dfd538 73
mmaas 6:aaf347dfd538 74 //////////////////////////////////////////////////////////////////////
mmaas 6:aaf347dfd538 75 // Include support for analog inputs
mmaas 6:aaf347dfd538 76 AnalogIn lightSense(PTE22);
mmaas 6:aaf347dfd538 77
mmaas 6:aaf347dfd538 78
mmaas 3:1e85b49a3e18 79 /////////////////////////////////////////////////////////////////////
mmaas 3:1e85b49a3e18 80 // Include support for MMA8451Q Acceleromoter
mmaas 3:1e85b49a3e18 81 #define MMA8451_I2C_ADDRESS (0x1d<<1)
mmaas 3:1e85b49a3e18 82 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
mmaas 3:1e85b49a3e18 83
mmaas 3:1e85b49a3e18 84
mmaas 3:1e85b49a3e18 85 /////////////////////////////////////////////////////////////////////
mmaas 3:1e85b49a3e18 86 // Include support for MAG3110 Magnetometer
mmaas 3:1e85b49a3e18 87 MAG3110 mag(PTE25, PTE24);
mmaas 3:1e85b49a3e18 88
mmaas 3:1e85b49a3e18 89
mmaas 6:aaf347dfd538 90 /////////////////////////////////////////////////////////////////////
mmaas 6:aaf347dfd538 91 // Structure to hold FRDM-KL46Z sensor and input data
mmaas 6:aaf347dfd538 92 struct KL46_SENSOR_DATA {
mmaas 6:aaf347dfd538 93 int sw1State;
mmaas 6:aaf347dfd538 94 int sw3State;
mmaas 6:aaf347dfd538 95
mmaas 6:aaf347dfd538 96 float sliderPosition;
mmaas 6:aaf347dfd538 97
mmaas 6:aaf347dfd538 98 float lightSensor;
mmaas 6:aaf347dfd538 99
mmaas 6:aaf347dfd538 100 int magXVal;
mmaas 6:aaf347dfd538 101 int magYVal;
mmaas 6:aaf347dfd538 102 float magHeading;
mmaas 6:aaf347dfd538 103
mmaas 6:aaf347dfd538 104 float accXVal;
mmaas 6:aaf347dfd538 105 float accYVal;
mmaas 6:aaf347dfd538 106 float accZVal;
mmaas 6:aaf347dfd538 107 } sensorData;
mmaas 6:aaf347dfd538 108
mmaas 3:1e85b49a3e18 109
mmaas 6:aaf347dfd538 110 /////////////////////////////////////////////////////////////////////
mmaas 6:aaf347dfd538 111 // Prototype for routine to send all sensor data to serial port
mmaas 6:aaf347dfd538 112 void serialSendSensorData(void);
mmaas 6:aaf347dfd538 113
mmaas 6:aaf347dfd538 114
mmaas 6:aaf347dfd538 115 /////////////////////////////////////////////////////////////////////
mmaas 6:aaf347dfd538 116 // main application
mmaas 3:1e85b49a3e18 117 int main()
mmaas 3:1e85b49a3e18 118 {
mmaas 1:28c81db67f50 119 // Ensure LEDs are off
mmaas 2:48ac01f5024d 120 greenLED = LED_OFF;
mmaas 1:28c81db67f50 121 redLED = LED_OFF;
mmaas 3:1e85b49a3e18 122
mmaas 0:79ae6809c2d0 123 // Set Serial Port data rate and say Hello
mmaas 6:aaf347dfd538 124 pc.baud( 115200 );
mmaas 0:79ae6809c2d0 125 pc.printf("Hello World\r\n");
mmaas 0:79ae6809c2d0 126
mmaas 3:1e85b49a3e18 127 // Turn on pull up resistors on pushbutton inputs
mmaas 3:1e85b49a3e18 128 sw1.mode(PullUp);
mmaas 3:1e85b49a3e18 129 sw3.mode(PullUp);
mmaas 3:1e85b49a3e18 130
mmaas 6:aaf347dfd538 131 // Calibrate Magnetometer
mmaas 6:aaf347dfd538 132 printf("Press and release SW1, rotate the board 360 degrees.\r\n");
mmaas 6:aaf347dfd538 133 printf("Then press and release SW1 to complete the calibration process.\r\n");
mmaas 4:6cb640167538 134
mmaas 4:6cb640167538 135 mag.calXY(PTC3, 0);
mmaas 4:6cb640167538 136
mmaas 4:6cb640167538 137 printf("Calibration complete.\r\n");
mmaas 4:6cb640167538 138
mmaas 4:6cb640167538 139
mmaas 6:aaf347dfd538 140 // Loop forever - read and update sensor data and print to console.
mmaas 6:aaf347dfd538 141 while(1)
mmaas 6:aaf347dfd538 142 {
mmaas 6:aaf347dfd538 143 sensorData.sw1State = sw1;
mmaas 6:aaf347dfd538 144 sensorData.sw3State = sw3;
mmaas 6:aaf347dfd538 145
mmaas 6:aaf347dfd538 146 sensorData.sliderPosition = slider.readPercentage() * 100;
mmaas 6:aaf347dfd538 147
mmaas 6:aaf347dfd538 148 sensorData.lightSensor = lightSense;
mmaas 6:aaf347dfd538 149
mmaas 6:aaf347dfd538 150 sensorData.accXVal = acc.getAccX();
mmaas 6:aaf347dfd538 151 sensorData.accYVal = acc.getAccY();
mmaas 6:aaf347dfd538 152 sensorData.accZVal = acc.getAccZ();
mmaas 4:6cb640167538 153
mmaas 6:aaf347dfd538 154 sensorData.magXVal = mag.readVal(MAG_OUT_X_MSB);
mmaas 6:aaf347dfd538 155 sensorData.magYVal = mag.readVal(MAG_OUT_Y_MSB);
mmaas 6:aaf347dfd538 156 sensorData.magHeading = mag.getHeading();
mmaas 6:aaf347dfd538 157
mmaas 6:aaf347dfd538 158 serialSendSensorData();
mmaas 6:aaf347dfd538 159
mmaas 6:aaf347dfd538 160 // Blink LEDs (show life)
mmaas 6:aaf347dfd538 161 redLED = LED_ON;
mmaas 6:aaf347dfd538 162 wait(.03);
mmaas 6:aaf347dfd538 163 greenLED = LED_ON;
mmaas 6:aaf347dfd538 164 wait(.03);
mmaas 6:aaf347dfd538 165 redLED = LED_OFF;
mmaas 6:aaf347dfd538 166 wait(.03);
mmaas 6:aaf347dfd538 167 greenLED = LED_OFF;
mmaas 4:6cb640167538 168
mmaas 6:aaf347dfd538 169 wait(1);
mmaas 6:aaf347dfd538 170 }
mmaas 0:79ae6809c2d0 171 }
mmaas 3:1e85b49a3e18 172
mmaas 3:1e85b49a3e18 173
mmaas 6:aaf347dfd538 174 void serialSendSensorData(void)
mmaas 3:1e85b49a3e18 175 {
mmaas 6:aaf347dfd538 176 printf("Switches:\r\n SW1 = %d\r\n SW3 = %d\r\n\r\n", sensorData.sw1State, sensorData.sw3State);
mmaas 6:aaf347dfd538 177 printf("Slider:\r\n %2.0f %% \r\n\r\n", sensorData.sliderPosition);
mmaas 6:aaf347dfd538 178 printf("Light Sensor:\r\n %1.3f \r\n\r\n", sensorData.lightSensor);
mmaas 6:aaf347dfd538 179 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 6:aaf347dfd538 180 printf("Magnetometer:\r\n X = %d\r\n Y = %d\r\n Heading = %f \r\n\r\n", sensorData.magXVal, sensorData.magYVal, sensorData.magHeading);
mmaas 6:aaf347dfd538 181
mmaas 6:aaf347dfd538 182 printf("\r\n");
mmaas 6:aaf347dfd538 183
mmaas 6:aaf347dfd538 184 }
mmaas 3:1e85b49a3e18 185
mmaas 3:1e85b49a3e18 186
mmaas 6:aaf347dfd538 187