iu

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Jamess
Date:
Wed Apr 22 14:35:49 2015 +0000
Commit message:
fazeraltera??es que professor falou!!!

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Apr 22 14:35:49 2015 +0000
@@ -0,0 +1,184 @@
+// Juntando a programação do sensor de fluxo com o acelerômetro
+// E mais QUATRO ACELERÔMETROS
+
+// Since there`s only two possible adresses to the accelerômeter, we will have to use two I2C buses.
+
+// por Thiago
+
+// Criado dia 02/03/2015
+//
+
+#include "mbed.h"
+
+/*-------ADXL345-------*/
+
+// When SDO grounded
+#define ADXL1_ADDR_WRITE 0xA6
+#define ADXL1_ADDR_READ 0xA7
+#define ADXL1_ADDR 0X53
+
+//When SDO high
+#define ADXL2_ADDR_WRITE 0x3A
+#define ADXL2_ADDR_READ 0x3B
+#define ADXL2_ADDR 0x53
+
+//Registers
+
+#define POWER_CTL 0x2D
+#define DATA_FORMAT 0x31
+#define DATAX0 0x32       //X
+#define DATAX1 0x33
+#define DATAY0 0x34       //Y
+#define DATAY1 0x35
+#define DATAZ0 0x36       //Z
+#define DATAZ1 0x37
+#define OFSX 0x1D         //Accelerometer offset calibration
+#define OFSY 0x1F         
+#define OFSZ 0x20
+#define BW_RATE 0x2C      //Define Baudrate
+
+/*----Transmicao----*/
+
+I2C i2c(PTC2,PTC1);       //
+Serial pc(USBTX,USBRX);   //
+
+/*-----Funcoes------*/
+
+// Acelerômetro:
+int RegisterWrite(char,char,char,char);
+int RegisterRead(char,char,char);
+inline void MultRegisterRead(char,char,char,char*,int);
+void GetOutput(char,char,int*);
+int readings[3] = {0,0,0};
+
+// Sensor de Fluxo:
+AnalogIn ain(PTB3);
+DigitalOut led2(LED2);  // Led red
+void HandlerT1(void);
+void rx_Handler(void);
+
+Ticker t1;              // Timer to send data
+
+volatile bool STATUS = true;
+char test = 0;
+
+int main()
+{
+
+/*-----------------INICIALIZA SENSOR DE FLUXO-----------*/
+    pc.baud(9600);
+    pc.attach(&rx_Handler, pc.RxIrq);
+    led2=0;
+
+
+/*------------------INICIALIZA ACELEROMETRO 1-------------*/
+
+    printf("Entrou no main");   //Debug.
+    
+    i2c.frequency(100000);  //100kHz
+
+// Testa Envio:
+    pc.printf("Sending POWER CONTROL\t");
+    STATUS = RegisterWrite(ADXL1_ADDR,POWER_CTL,0x00);
+
+     if (STATUS != 0){
+      pc.printf("WRITE NO SUCCESS\t");
+     }
+    else{
+      pc.printf("ACKNOLEDGE RECEIVED");
+     }
+
+    wait(0.1);
+      pc.printf("Sending Data Format\t");
+
+    RegisterWrite(ADXL1_ADDR,DATA_FORMAT,0x09); // 
+
+    RegisterWrite(ADXL1_ADDR,BW_RATE,0x0A);     // Default Value... 100kHZ
+
+    RegisterWrite(ADXL1_ADDR,POWER_CTL,0x08);   // MeasurementMode
+    
+
+/*-------------Setting the Offset Value 1 -------------*/
+
+    RegisterWrite(ADXL1_ADDR,OFSX,0xFD);
+    RegisterWrite(ADXL1_ADDR,OFSY,0x03);
+    RegisterWrite(ADXL1_ADDR,OFSZ,0xFE);
+
+/*----------------------------------------------------*/
+
+    wait(0.1);
+    printf("Now, trying to read data\t");
+    printf("%i", RegisterRead(ADXL1_ADDR,0x00));
+    
+    if (STATUS == 0){
+      printf("READ SUCCESSFULL\t");
+    }
+    else {
+      printf("READ NOT SUCCESSFUL\n");
+    }    
+        
+    pc.printf("Press 2 to Start");
+
+    while(1) { }
+
+       
+   
+}//end of main
+
+void HandlerT1(void)
+{
+    ///Colocar pino em alto e 
+    GetOutput(ADXL1_ADDR,readings);
+    //ver o tempo que isso leva.
+    
+    //int adread = ???
+    //putc(adread & 0xFF);
+    //putc((adread >> 8) & 0xFF);
+    pc.printf("0x%04X",ain.read_u16());
+    
+    pc.printf("%i,%i,%i\n",(int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[2]);
+    // Não mandar strings!!!
+    // Depois em baixo para medir o tempo que elas levvam para serem executados
+}
+
+void rx_Handler(void)
+{
+    test = pc.getc();
+    pc.putc(test);  //debug
+    if (test == '2') {
+            t1.attach(&HandlerT1, 0.01);
+        }
+    else if(test == '3'){
+            t1.detach();    // Detaches timer interruption when not needed
+        }
+}
+
+int RegisterWrite(char SLAVE, char RegAddress, char Data){
+    char cmd[2];
+    cmd[0]= RegAddress;
+    cmd[1]= Data;
+    int ack = i2c.write((SLAVE<<1),cmd,2);
+    return ack;
+    }
+    
+int RegisterRead(char SLAVE, char address){
+    char output;
+    char tx = address; 
+    i2c.write((SLAVE<<1),&tx,1);
+    STATUS = i2c.read((SLAVE<<1), &output,1);
+    return output;
+    } 
+    
+inline void MultRegisterRead(char SLAVE, char address,char*output,int size){
+    i2c.write((SLAVE<<1),&address,1);
+    i2c.read((SLAVE<<1),output,size);
+    return;    
+    }
+
+void GetOutput(char SLAVE, int* readings){
+    char buffer[6];
+    MultRegisterRead(SLAVE,DATAX0,buffer, 6);
+    readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
+    readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
+    readings[2] = (int)buffer[5] << 8 | (int)buffer[4];        
+    }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Apr 22 14:35:49 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/433970e64889
\ No newline at end of file