FRDM-K64F code to enable takktile communication. Ported from Arduino code provided by Takktile. http://www.takktile.com/tutorial:arduino
Dependencies: mbed
main.cpp@1:a6c636fd2c82, 2015-01-31 (annotated)
- Committer:
- NotCras
- Date:
- Sat Jan 31 22:26:03 2015 +0000
- Revision:
- 1:a6c636fd2c82
- Parent:
- 0:937a1a6771e3
nothing works yet but laying groundwork.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NotCras | 0:937a1a6771e3 | 1 | #include "mbed.h" |
NotCras | 0:937a1a6771e3 | 2 | |
NotCras | 1:a6c636fd2c82 | 3 | #define MAX_STRIPS 8 |
NotCras | 1:a6c636fd2c82 | 4 | #define MAX_SENSORS 5 |
NotCras | 1:a6c636fd2c82 | 5 | |
NotCras | 1:a6c636fd2c82 | 6 | #define NUM_SENSORS MAX_STRIPS*MAX_SENSORS // reserve addresses for 8 strips with 6 sensors on each |
NotCras | 1:a6c636fd2c82 | 7 | #define PRECISION 0 |
NotCras | 1:a6c636fd2c82 | 8 | |
NotCras | 1:a6c636fd2c82 | 9 | #define FREESCALE_ADDRESS 0xC0 |
NotCras | 1:a6c636fd2c82 | 10 | #define SENSOR_ALL_ON 0x0C |
NotCras | 1:a6c636fd2c82 | 11 | #define SENSOR_ALL_OFF 0x0D |
NotCras | 1:a6c636fd2c82 | 12 | |
NotCras | 1:a6c636fd2c82 | 13 | |
NotCras | 1:a6c636fd2c82 | 14 | /**********************************VARIABLE_DEFINITIONS**********************************/ |
NotCras | 1:a6c636fd2c82 | 15 | //SDA, SCL |
NotCras | 1:a6c636fd2c82 | 16 | I2C i2c(PTE25, PTE24); |
NotCras | 1:a6c636fd2c82 | 17 | |
NotCras | 1:a6c636fd2c82 | 18 | const int addr = 0x00; |
NotCras | 1:a6c636fd2c82 | 19 | |
NotCras | 1:a6c636fd2c82 | 20 | Serial pc(USBTX, USBRX); |
NotCras | 1:a6c636fd2c82 | 21 | |
NotCras | 1:a6c636fd2c82 | 22 | /**********************************SUPPORT_FUNCTIONS**********************************/ |
NotCras | 1:a6c636fd2c82 | 23 | |
NotCras | 1:a6c636fd2c82 | 24 | void startDataConversion(){ |
NotCras | 1:a6c636fd2c82 | 25 | char cmd[4]; |
NotCras | 1:a6c636fd2c82 | 26 | |
NotCras | 1:a6c636fd2c82 | 27 | //first set of commands |
NotCras | 1:a6c636fd2c82 | 28 | cmd[0] = 0x0C; //activate all sensors |
NotCras | 1:a6c636fd2c82 | 29 | cmd[1] = 0xC0; //start data conversion |
NotCras | 1:a6c636fd2c82 | 30 | cmd[2] = 0x12; |
NotCras | 1:a6c636fd2c82 | 31 | cmd[3] = 0x0D; //disable the sensors |
NotCras | 1:a6c636fd2c82 | 32 | i2c.write(addr, cmd, 4); |
NotCras | 1:a6c636fd2c82 | 33 | |
NotCras | 1:a6c636fd2c82 | 34 | //wait for data to be processed |
NotCras | 1:a6c636fd2c82 | 35 | wait(2); //2 ms (rounded up 1.6ms) |
NotCras | 1:a6c636fd2c82 | 36 | } |
NotCras | 0:937a1a6771e3 | 37 | |
NotCras | 1:a6c636fd2c82 | 38 | //given a sensor number, get the value |
NotCras | 1:a6c636fd2c82 | 39 | int read_Sensor(int sensor){ |
NotCras | 1:a6c636fd2c82 | 40 | //startDataConversion(); |
NotCras | 1:a6c636fd2c82 | 41 | |
NotCras | 1:a6c636fd2c82 | 42 | char cmd[4]; |
NotCras | 1:a6c636fd2c82 | 43 | |
NotCras | 1:a6c636fd2c82 | 44 | //second set of commands |
NotCras | 1:a6c636fd2c82 | 45 | switch(sensor){ //activate the sensor of your choosing |
NotCras | 1:a6c636fd2c82 | 46 | case 1: |
NotCras | 1:a6c636fd2c82 | 47 | cmd[0] = 0x00; |
NotCras | 1:a6c636fd2c82 | 48 | break; |
NotCras | 1:a6c636fd2c82 | 49 | case 2: |
NotCras | 1:a6c636fd2c82 | 50 | cmd[0] = 0x02; |
NotCras | 1:a6c636fd2c82 | 51 | break; |
NotCras | 1:a6c636fd2c82 | 52 | case 3: |
NotCras | 1:a6c636fd2c82 | 53 | cmd[0] = 0x04; |
NotCras | 1:a6c636fd2c82 | 54 | break; |
NotCras | 1:a6c636fd2c82 | 55 | case 4: |
NotCras | 1:a6c636fd2c82 | 56 | cmd[0] = 0x06; |
NotCras | 1:a6c636fd2c82 | 57 | break; |
NotCras | 1:a6c636fd2c82 | 58 | case 5: |
NotCras | 1:a6c636fd2c82 | 59 | cmd[0] = 0x08; |
NotCras | 1:a6c636fd2c82 | 60 | break; |
NotCras | 1:a6c636fd2c82 | 61 | } |
NotCras | 1:a6c636fd2c82 | 62 | |
NotCras | 1:a6c636fd2c82 | 63 | cmd[1] = 0xC0; //read pressure and temperature |
NotCras | 1:a6c636fd2c82 | 64 | cmd[2] = 0x00; |
NotCras | 1:a6c636fd2c82 | 65 | cmd[3] = 0xC1; |
NotCras | 1:a6c636fd2c82 | 66 | i2c.write(addr, cmd, 4); |
NotCras | 1:a6c636fd2c82 | 67 | |
NotCras | 1:a6c636fd2c82 | 68 | //read sensor |
NotCras | 1:a6c636fd2c82 | 69 | char valIn[4]; |
NotCras | 1:a6c636fd2c82 | 70 | i2c.read(addr, valIn, 4); //read the next 4 bytes |
NotCras | 1:a6c636fd2c82 | 71 | |
NotCras | 1:a6c636fd2c82 | 72 | //deactivate sensor |
NotCras | 1:a6c636fd2c82 | 73 | cmd[0] = cmd[0] + 1; //increment by 1 to get the OFF code |
NotCras | 1:a6c636fd2c82 | 74 | i2c.write(addr, cmd[0], 1); |
NotCras | 1:a6c636fd2c82 | 75 | |
NotCras | 1:a6c636fd2c82 | 76 | |
NotCras | 1:a6c636fd2c82 | 77 | |
NotCras | 1:a6c636fd2c82 | 78 | } |
NotCras | 1:a6c636fd2c82 | 79 | |
NotCras | 1:a6c636fd2c82 | 80 | |
NotCras | 1:a6c636fd2c82 | 81 | /**********************************MAIN_FUNCTION**********************************/ |
NotCras | 1:a6c636fd2c82 | 82 | |
NotCras | 1:a6c636fd2c82 | 83 | int main() { |
NotCras | 1:a6c636fd2c82 | 84 | char cmd[7]; |
NotCras | 1:a6c636fd2c82 | 85 | i2c.frequency(100000); |
NotCras | 1:a6c636fd2c82 | 86 | while (1) { |
NotCras | 1:a6c636fd2c82 | 87 | |
NotCras | 0:937a1a6771e3 | 88 | } |
NotCras | 1:a6c636fd2c82 | 89 | } |