iu

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 // E mais QUATRO ACELERÔMETROS
00003 
00004 // Since there`s only two possible adresses to the accelerômeter, we will have to use two I2C buses.
00005 
00006 // por Thiago
00007 
00008 // Criado dia 02/03/2015
00009 //
00010 
00011 #include "mbed.h"
00012 
00013 /*-------ADXL345-------*/
00014 
00015 // When SDO grounded
00016 #define ADXL1_ADDR_WRITE 0xA6
00017 #define ADXL1_ADDR_READ 0xA7
00018 #define ADXL1_ADDR 0X53
00019 
00020 //When SDO high
00021 #define ADXL2_ADDR_WRITE 0x3A
00022 #define ADXL2_ADDR_READ 0x3B
00023 #define ADXL2_ADDR 0x53
00024 
00025 //Registers
00026 
00027 #define POWER_CTL 0x2D
00028 #define DATA_FORMAT 0x31
00029 #define DATAX0 0x32       //X
00030 #define DATAX1 0x33
00031 #define DATAY0 0x34       //Y
00032 #define DATAY1 0x35
00033 #define DATAZ0 0x36       //Z
00034 #define DATAZ1 0x37
00035 #define OFSX 0x1D         //Accelerometer offset calibration
00036 #define OFSY 0x1F         
00037 #define OFSZ 0x20
00038 #define BW_RATE 0x2C      //Define Baudrate
00039 
00040 /*----Transmicao----*/
00041 
00042 I2C i2c(PTC2,PTC1);       //
00043 Serial pc(USBTX,USBRX);   //
00044 
00045 /*-----Funcoes------*/
00046 
00047 // Acelerômetro:
00048 int RegisterWrite(char,char,char,char);
00049 int RegisterRead(char,char,char);
00050 inline void MultRegisterRead(char,char,char,char*,int);
00051 void GetOutput(char,char,int*);
00052 int readings[3] = {0,0,0};
00053 
00054 // Sensor de Fluxo:
00055 AnalogIn ain(PTB3);
00056 DigitalOut led2(LED2);  // Led red
00057 void HandlerT1(void);
00058 void rx_Handler(void);
00059 
00060 Ticker t1;              // Timer to send data
00061 
00062 volatile bool STATUS = true;
00063 char test = 0;
00064 
00065 int main()
00066 {
00067 
00068 /*-----------------INICIALIZA SENSOR DE FLUXO-----------*/
00069     pc.baud(9600);
00070     pc.attach(&rx_Handler, pc.RxIrq);
00071     led2=0;
00072 
00073 
00074 /*------------------INICIALIZA ACELEROMETRO 1-------------*/
00075 
00076     printf("Entrou no main");   //Debug.
00077     
00078     i2c.frequency(100000);  //100kHz
00079 
00080 // Testa Envio:
00081     pc.printf("Sending POWER CONTROL\t");
00082     STATUS = RegisterWrite(ADXL1_ADDR,POWER_CTL,0x00);
00083 
00084      if (STATUS != 0){
00085       pc.printf("WRITE NO SUCCESS\t");
00086      }
00087     else{
00088       pc.printf("ACKNOLEDGE RECEIVED");
00089      }
00090 
00091     wait(0.1);
00092       pc.printf("Sending Data Format\t");
00093 
00094     RegisterWrite(ADXL1_ADDR,DATA_FORMAT,0x09); // 
00095 
00096     RegisterWrite(ADXL1_ADDR,BW_RATE,0x0A);     // Default Value... 100kHZ
00097 
00098     RegisterWrite(ADXL1_ADDR,POWER_CTL,0x08);   // MeasurementMode
00099     
00100 
00101 /*-------------Setting the Offset Value 1 -------------*/
00102 
00103     RegisterWrite(ADXL1_ADDR,OFSX,0xFD);
00104     RegisterWrite(ADXL1_ADDR,OFSY,0x03);
00105     RegisterWrite(ADXL1_ADDR,OFSZ,0xFE);
00106 
00107 /*----------------------------------------------------*/
00108 
00109     wait(0.1);
00110     printf("Now, trying to read data\t");
00111     printf("%i", RegisterRead(ADXL1_ADDR,0x00));
00112     
00113     if (STATUS == 0){
00114       printf("READ SUCCESSFULL\t");
00115     }
00116     else {
00117       printf("READ NOT SUCCESSFUL\n");
00118     }    
00119         
00120     pc.printf("Press 2 to Start");
00121 
00122     while(1) { }
00123 
00124        
00125    
00126 }//end of main
00127 
00128 void HandlerT1(void)
00129 {
00130     ///Colocar pino em alto e 
00131     GetOutput(ADXL1_ADDR,readings);
00132     //ver o tempo que isso leva.
00133     
00134     //int adread = ???
00135     //putc(adread & 0xFF);
00136     //putc((adread >> 8) & 0xFF);
00137     pc.printf("0x%04X",ain.read_u16());
00138     
00139     pc.printf("%i,%i,%i\n",(int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[2]);
00140     // Não mandar strings!!!
00141     // Depois em baixo para medir o tempo que elas levvam para serem executados
00142 }
00143 
00144 void rx_Handler(void)
00145 {
00146     test = pc.getc();
00147     pc.putc(test);  //debug
00148     if (test == '2') {
00149             t1.attach(&HandlerT1, 0.01);
00150         }
00151     else if(test == '3'){
00152             t1.detach();    // Detaches timer interruption when not needed
00153         }
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     }