Thiago . / Mbed 2 deprecated 1

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Juntando a programação do sensor de fluxo com o acelerômetro
00002 // por Thiago
00003 
00004 // Criado dia 02/03/2015
00005 //
00006 
00007 #include "mbed.h"
00008 
00009 /*-------ADXL345-------*/
00010 
00011 // When SDO grounded
00012 #define ADXL1_ADDR_WRITE 0xA6
00013 #define ADXL1_ADDR_READ 0xA7
00014 #define ADXL1_ADDR 0X53
00015 
00016 //When SDO high
00017 #define ADXL2_ADDR_WRITE 0x3A
00018 #define ADXL2_ADDR_READ 0x3B
00019 #define ADXL2_ADDR 0x53
00020 
00021 //Registers
00022 
00023 #define POWER_CTL 0x2D
00024 #define DATA_FORMAT 0x31
00025 #define DATAX0 0x32
00026 #define DATAX1 0x33
00027 #define DATAY0 0x34
00028 #define DATAY1 0x35
00029 #define DATAZ0 0x36
00030 #define DATAZ1 0x37
00031 #define OFSX 0x1D
00032 #define OFSY 0x1F
00033 #define OFSZ 0x20
00034 #define BW_RATE 0x2C
00035 
00036 /*----Transmicao----*/
00037 
00038 I2C i2c(PTC2,PTC1);    
00039 Serial pc(USBTX,USBRX);
00040 
00041 /*-----Funcoes------*/
00042 
00043 // Acelerômetro:
00044 int RegisterWrite(char,char,char);
00045 int RegisterRead(char,char);
00046 inline void MultByteRead(char,char,char*,int);
00047 void GetOutput(char,int*);
00048 
00049 // Sensor de Fluxo:
00050 AnalogIn ain(PTB3);
00051 DigitalOut led2(LED2);  // Led red
00052 void HandlerT1(void);
00053 void rx_Handler(void);
00054 
00055 Ticker t1;              // Timer to send data
00056 
00057 volatile bool STATUS = true;
00058 char test = 0;
00059 
00060 int main()
00061 {
00062 
00063 /*-----------------INICIALIZA SENSOR DE FLUXO-----------*/
00064     pc.baud(9600);
00065     pc.attach(&rx_Handler, pc.RxIrq);
00066     led2=0;
00067 
00068 
00069 /*------------------INICIALIZA ACELEROMETRO-------------*/
00070 int readings[3] = {0,0,0};
00071     printf("Entrou no main");   //Debug.
00072     
00073     i2c.frequency(100000);  //100kHz
00074 
00075 // Testa Envio:
00076     pc.printf("Sending POWER CONTROL\t");
00077     STATUS = RegisterWrite(ADXL1_ADDR,POWER_CTL,0x00);
00078 
00079      if (STATUS != 0){
00080       pc.printf("WRITE NO SUCCESS\t");
00081      }
00082     else{
00083       pc.printf("ACKNOLEDGE RECEIVED");
00084      }
00085 
00086     wait(0.1);
00087       pc.printf("Sending Data Format\t");
00088 
00089     RegisterWrite(ADXL1_ADDR,DATA_FORMAT,0x09); // 
00090 
00091     RegisterWrite(ADXL1_ADDR,BW_RATE,0x0A);     // Default Value... 100kHZ
00092 
00093     RegisterWrite(ADXL1_ADDR,POWER_CTL,0x08);   // MeasurementMode
00094     
00095 
00096 /*-------------Setting the Offset Value---------------*/
00097 
00098     RegisterWrite(ADXL1_ADDR,OFSX,0xFD);
00099     RegisterWrite(ADXL1_ADDR,OFSY,0x03);
00100     RegisterWrite(ADXL1_ADDR,OFSZ,0xFE);
00101 
00102 /*----------------------------------------------------*/
00103 
00104     wait(0.1);
00105     printf("Now, trying to read data\t");
00106     printf("%i", RegisterRead(ADXL1_ADDR,0x00));
00107     
00108     if (STATUS == 0){
00109       printf("READ SUCCESSFULL\t");
00110     }
00111     else {
00112       printf("READ NOT SUCCESSFUL\n");
00113     }    
00114         
00115     pc.printf("Press 2 to Start");
00116 
00117     while(1) {
00118 
00119         wait(0.5);                                  // Debug
00120         led2 = 0;
00121         pc.putc(test);                             // Debug
00122 
00123         if (test == '2') {
00124             t1.attach(&HandlerT1, 0.01);
00125         }
00126 
00127         while(test == '2') {
00128 
00129             if (STATUS == true) {
00130 
00131                 STATUS = false;
00132                 led2 = 1;
00133                 pc.printf("0x%04X",ain.read_u16());
00134             GetOutput(ADXL1_ADDR,readings);
00135             pc.printf("%i,%i,%i\n",(int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[2]);
00136          }
00137 
00138 
00139         } //end of while(test=='2'){}
00140 
00141         t1.detach();    // Detaches timer interruption when not needed
00142     }//end of while(1)
00143 }//end of main
00144 
00145 void HandlerT1(void)
00146 {
00147     STATUS = true;
00148 }
00149 
00150 void rx_Handler(void)
00151 {
00152     test = pc.getc();
00153     pc.putc(test);
00154 }
00155 
00156 int RegisterWrite(char SLAVE, char RegAddress, char Data){
00157     char cmd[2];
00158     cmd[0]= RegAddress;
00159     cmd[1]= Data;
00160     int ack = i2c.write((SLAVE<<1),cmd,2);
00161     return ack;
00162     }
00163     
00164 int RegisterRead(char SLAVE, char address){
00165     char output;
00166     char tx = address; 
00167     i2c.write((SLAVE<<1),&tx,1);
00168     STATUS = i2c.read((SLAVE<<1), &output,1);
00169     return output;
00170     } 
00171     
00172 inline void MultRegisterRead(char SLAVE, char address,char*output,int size){
00173     i2c.write((SLAVE<<1),&address,1);
00174     i2c.read((SLAVE<<1),output,size);
00175     return;    
00176     }
00177 
00178 void GetOutput(char SLAVE, int* readings){
00179     char buffer[6];
00180     MultRegisterRead(SLAVE,DATAX0,buffer, 6);
00181     readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
00182     readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
00183     readings[2] = (int)buffer[5] << 8 | (int)buffer[4];        
00184     }