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

Revision:
6:aaf347dfd538
Parent:
4:6cb640167538
Child:
7:9e1d22b35dab
--- a/main.cpp	Tue Dec 31 17:19:02 2013 +0000
+++ b/main.cpp	Sat Jan 04 02:33:13 2014 +0000
@@ -1,4 +1,5 @@
 #include "mbed.h"
+#include "TSISensor.h"
 #include "MMA8451Q.h"
 #include "MAG3110.h"
 
@@ -22,6 +23,10 @@
 //          GPIO to monitor the two onboard push buttons
 //              PTC3 / GPIO = SW1 - low input = button pressed
 //              PTC12 / GPIO = SW3 - low input = button pressed
+// 
+//      AnalogIn:
+//          ADC channel to monitor ambient light sensor
+//              PTE22 / ADC = Light Sensor - higher value = darker
 //
 //  TSI (source: http://mbed.org/users/emilmont/code/TSI/ )
 //      Capacitive Touch library to support the onboard Touch-Slider
@@ -39,33 +44,38 @@
 //          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_ON  0
 #define LED_OFF 1
 DigitalOut greenLED(LED_GREEN);
 DigitalOut redLED(LED_RED);
 
 
 //////////////////////////////////////////////////////////////////////
-// Include support for onboard pushbuttons
+// Include support for onboard pushbuttons (value = 0 when pressed)
 DigitalIn  sw1(PTC3);
 DigitalIn  sw3(PTC12);
 
 
+//////////////////////////////////////////////////////////////////////
+// Include support for onboard Capacitive Touch Slider
+TSISensor slider;
+
+
+//////////////////////////////////////////////////////////////////////
+// Include support for analog inputs
+AnalogIn  lightSense(PTE22);
+
+
 /////////////////////////////////////////////////////////////////////
 // Include support for MMA8451Q Acceleromoter
 #define MMA8451_I2C_ADDRESS (0x1d<<1)
@@ -74,100 +84,104 @@
 
 /////////////////////////////////////////////////////////////////////
 // Include support for MAG3110 Magnetometer
-void calXY(); //magnetometer calibration: finding max and min of X, Y axis
 MAG3110 mag(PTE25, PTE24);
 
 
+/////////////////////////////////////////////////////////////////////
+// Structure to hold FRDM-KL46Z sensor and input data
+struct KL46_SENSOR_DATA {
+    int     sw1State;
+    int     sw3State;
+    
+    float   sliderPosition;
+    
+    float   lightSensor;
+    
+    int     magXVal;
+    int     magYVal;
+    float   magHeading;
+    
+    float   accXVal;
+    float   accYVal;
+    float   accZVal;
+} sensorData;
+    
 
+/////////////////////////////////////////////////////////////////////
+// Prototype for routine to send all sensor data to serial port
+void serialSendSensorData(void);    
+
+
+/////////////////////////////////////////////////////////////////////
+// main application
 int main()
 {
-
     // Ensure LEDs are off
     greenLED = LED_OFF;
     redLED = LED_OFF;
 
     // Set Serial Port data rate and say Hello
-    pc.baud( 230400 );
+    pc.baud( 115200 );
     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 ));
-
-
-    printf("Press and release SW1, rotate the board 360 degrees and then press and release SW1 to complete the calibration process.\r\n");
+    // Calibrate Magnetometer
+    printf("Press and release SW1, rotate the board 360 degrees.\r\n");
+    printf("Then press and release SW1 to complete the calibration process.\r\n");
 
     mag.calXY(PTC3, 0);
 
     printf("Calibration complete.\r\n");
 
-    
-    // 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();
+    // Loop forever - read and update sensor data and print to console.    
+    while(1)
+    {
+        sensorData.sw1State = sw1;
+        sensorData.sw3State = sw3;
+        
+        sensorData.sliderPosition = slider.readPercentage() * 100;
+        
+        sensorData.lightSensor = lightSense;
+        
+        sensorData.accXVal = acc.getAccX();
+        sensorData.accYVal = acc.getAccY();
+        sensorData.accZVal = acc.getAccZ();
 
-        // 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);
+        sensorData.magXVal = mag.readVal(MAG_OUT_X_MSB);
+        sensorData.magYVal = mag.readVal(MAG_OUT_Y_MSB);
+        sensorData.magHeading = mag.getHeading();
+                
+        serialSendSensorData();
+        
+        // Blink LEDs (show life)
+        redLED = LED_ON;
+        wait(.03);
+        greenLED = LED_ON;
+        wait(.03);
+        redLED = LED_OFF;
+        wait(.03);
+        greenLED = LED_OFF;
 
-
-        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);
-
-    }
+        wait(1);
+    }  
 }
 
 
-void calXY() //magnetometer calibration: finding max and min of X, Y axis
+void serialSendSensorData(void)
 {
- 
-
-}
+    printf("Switches:\r\n  SW1 = %d\r\n  SW3 = %d\r\n\r\n", sensorData.sw1State, sensorData.sw3State);
+    printf("Slider:\r\n %2.0f %% \r\n\r\n", sensorData.sliderPosition);
+    printf("Light Sensor:\r\n  %1.3f \r\n\r\n", sensorData.lightSensor);
+    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);
+    printf("Magnetometer:\r\n  X = %d\r\n  Y = %d\r\n  Heading = %f  \r\n\r\n", sensorData.magXVal, sensorData.magYVal, sensorData.magHeading);
+    
+    printf("\r\n");
+    
+}    
 
 
+