Test

Dependencies:   mbed

Fork of analogTest by Cool_Step

Committer:
cagriakalin
Date:
Tue Nov 10 11:28:34 2015 +0000
Revision:
0:95fad925fbf6
testPush

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cagriakalin 0:95fad925fbf6 1 #include "mbed.h"
cagriakalin 0:95fad925fbf6 2
cagriakalin 0:95fad925fbf6 3 DigitalOut myled(LED1);
cagriakalin 0:95fad925fbf6 4 AnalogIn FSR1(p20);
cagriakalin 0:95fad925fbf6 5 AnalogIn FSR2(p19);
cagriakalin 0:95fad925fbf6 6
cagriakalin 0:95fad925fbf6 7
cagriakalin 0:95fad925fbf6 8 int8_t AD5245_write_result;
cagriakalin 0:95fad925fbf6 9
cagriakalin 0:95fad925fbf6 10 float fsr1_value;
cagriakalin 0:95fad925fbf6 11 float fsr2_value;
cagriakalin 0:95fad925fbf6 12 int truncated_fsr1_value;
cagriakalin 0:95fad925fbf6 13
cagriakalin 0:95fad925fbf6 14 int8_t AD5245_addr = 0x2C;
cagriakalin 0:95fad925fbf6 15 //TODO: LTC4151 address will be change.
cagriakalin 0:95fad925fbf6 16 int8_t LTC4151_addr = 0xCE;
cagriakalin 0:95fad925fbf6 17
cagriakalin 0:95fad925fbf6 18 char data[2];
cagriakalin 0:95fad925fbf6 19
cagriakalin 0:95fad925fbf6 20 Serial pc(USBTX, USBRX);
cagriakalin 0:95fad925fbf6 21
cagriakalin 0:95fad925fbf6 22 I2C i2c(p9,p10);
cagriakalin 0:95fad925fbf6 23
cagriakalin 0:95fad925fbf6 24 int msb = 0;
cagriakalin 0:95fad925fbf6 25 int main()
cagriakalin 0:95fad925fbf6 26 {
cagriakalin 0:95fad925fbf6 27 pc.baud(115200);
cagriakalin 0:95fad925fbf6 28
cagriakalin 0:95fad925fbf6 29 while(1) {
cagriakalin 0:95fad925fbf6 30 data[0] = 0x04;
cagriakalin 0:95fad925fbf6 31 fsr1_value=FSR1.read();
cagriakalin 0:95fad925fbf6 32 truncated_fsr1_value=fsr1_value*255;
cagriakalin 0:95fad925fbf6 33 pc.printf("Truncated Value: %d\n",truncated_fsr1_value);
cagriakalin 0:95fad925fbf6 34 i2c.start();
cagriakalin 0:95fad925fbf6 35 i2c.write(AD5245_addr);
cagriakalin 0:95fad925fbf6 36 i2c.write(truncated_fsr1_value);
cagriakalin 0:95fad925fbf6 37 i2c.stop();
cagriakalin 0:95fad925fbf6 38
cagriakalin 0:95fad925fbf6 39 wait(0.01);
cagriakalin 0:95fad925fbf6 40 #if 1
cagriakalin 0:95fad925fbf6 41 i2c.start(); // I2C start
cagriakalin 0:95fad925fbf6 42 i2c.write(0xCE); // I2C slave address - 0xCE
cagriakalin 0:95fad925fbf6 43 i2c.write(0x04); // I2C slave register address - C_register 02h
cagriakalin 0:95fad925fbf6 44 i2c.start(); // I2C start
cagriakalin 0:95fad925fbf6 45 i2c.write(0xCF); // I2C slave address - 0xCE
cagriakalin 0:95fad925fbf6 46 msb=i2c.read(0); // I2C read with NAK (not-acknowledge)
cagriakalin 0:95fad925fbf6 47 i2c.stop();
cagriakalin 0:95fad925fbf6 48
cagriakalin 0:95fad925fbf6 49 pc.printf("Msb: %d\n",msb);
cagriakalin 0:95fad925fbf6 50 #endif
cagriakalin 0:95fad925fbf6 51 #if 0
cagriakalin 0:95fad925fbf6 52 data[0] = 0x02; //register we want to read
cagriakalin 0:95fad925fbf6 53 int ret = i2c.write(LTC4151_addr, data, 1, true); //Write one byte, dont transmit stop condition, check if we get ACK
cagriakalin 0:95fad925fbf6 54
cagriakalin 0:95fad925fbf6 55 if (ret == 0)
cagriakalin 0:95fad925fbf6 56 printf("Succesfull write\n\r");
cagriakalin 0:95fad925fbf6 57 else
cagriakalin 0:95fad925fbf6 58 printf("Returned: %X\n\r", ret);
cagriakalin 0:95fad925fbf6 59
cagriakalin 0:95fad925fbf6 60 i2c.read(LTC4151_addr, data, 2);
cagriakalin 0:95fad925fbf6 61 pc.printf("Byte 1: 0x%x Byte 2: 0x%x\n",data[0],data[1]);
cagriakalin 0:95fad925fbf6 62 pc.printf("Data = %d\n\r", (data[0]<<8) + data[1]);
cagriakalin 0:95fad925fbf6 63 #endif
cagriakalin 0:95fad925fbf6 64
cagriakalin 0:95fad925fbf6 65 wait(0.5);
cagriakalin 0:95fad925fbf6 66 if(myled) {
cagriakalin 0:95fad925fbf6 67 myled = 0;
cagriakalin 0:95fad925fbf6 68 } else {
cagriakalin 0:95fad925fbf6 69 myled = 1;
cagriakalin 0:95fad925fbf6 70 }
cagriakalin 0:95fad925fbf6 71 }
cagriakalin 0:95fad925fbf6 72 }