Apenas UM acelerômetro em I2C, UM sensor de fluxo. Utiliza a biblioteca serial para enviar para o computador.
Dependencies: mbed
Fork of 1 by
main.cpp@0:aa70f7963d9f, 2015-07-13 (annotated)
- Committer:
- Jamess
- Date:
- Mon Jul 13 17:40:42 2015 +0000
- Revision:
- 0:aa70f7963d9f
Leitura do sensor de fluxo analogico e dos aceler?metros utilizando I2C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jamess | 0:aa70f7963d9f | 1 | // Juntando a programação do sensor de fluxo com o acelerômetro |
Jamess | 0:aa70f7963d9f | 2 | // por Thiago |
Jamess | 0:aa70f7963d9f | 3 | |
Jamess | 0:aa70f7963d9f | 4 | // Criado dia 02/03/2015 |
Jamess | 0:aa70f7963d9f | 5 | // |
Jamess | 0:aa70f7963d9f | 6 | |
Jamess | 0:aa70f7963d9f | 7 | #include "mbed.h" |
Jamess | 0:aa70f7963d9f | 8 | |
Jamess | 0:aa70f7963d9f | 9 | /*-------ADXL345-------*/ |
Jamess | 0:aa70f7963d9f | 10 | |
Jamess | 0:aa70f7963d9f | 11 | // When SDO grounded |
Jamess | 0:aa70f7963d9f | 12 | #define ADXL1_ADDR_WRITE 0xA6 |
Jamess | 0:aa70f7963d9f | 13 | #define ADXL1_ADDR_READ 0xA7 |
Jamess | 0:aa70f7963d9f | 14 | #define ADXL1_ADDR 0X53 |
Jamess | 0:aa70f7963d9f | 15 | |
Jamess | 0:aa70f7963d9f | 16 | //When SDO high |
Jamess | 0:aa70f7963d9f | 17 | #define ADXL2_ADDR_WRITE 0x3A |
Jamess | 0:aa70f7963d9f | 18 | #define ADXL2_ADDR_READ 0x3B |
Jamess | 0:aa70f7963d9f | 19 | #define ADXL2_ADDR 0x53 |
Jamess | 0:aa70f7963d9f | 20 | |
Jamess | 0:aa70f7963d9f | 21 | //Registers |
Jamess | 0:aa70f7963d9f | 22 | |
Jamess | 0:aa70f7963d9f | 23 | #define POWER_CTL 0x2D |
Jamess | 0:aa70f7963d9f | 24 | #define DATA_FORMAT 0x31 |
Jamess | 0:aa70f7963d9f | 25 | #define DATAX0 0x32 |
Jamess | 0:aa70f7963d9f | 26 | #define DATAX1 0x33 |
Jamess | 0:aa70f7963d9f | 27 | #define DATAY0 0x34 |
Jamess | 0:aa70f7963d9f | 28 | #define DATAY1 0x35 |
Jamess | 0:aa70f7963d9f | 29 | #define DATAZ0 0x36 |
Jamess | 0:aa70f7963d9f | 30 | #define DATAZ1 0x37 |
Jamess | 0:aa70f7963d9f | 31 | #define OFSX 0x1D |
Jamess | 0:aa70f7963d9f | 32 | #define OFSY 0x1F |
Jamess | 0:aa70f7963d9f | 33 | #define OFSZ 0x20 |
Jamess | 0:aa70f7963d9f | 34 | #define BW_RATE 0x2C |
Jamess | 0:aa70f7963d9f | 35 | |
Jamess | 0:aa70f7963d9f | 36 | /*----Transmicao----*/ |
Jamess | 0:aa70f7963d9f | 37 | |
Jamess | 0:aa70f7963d9f | 38 | I2C i2c(PTC2,PTC1); |
Jamess | 0:aa70f7963d9f | 39 | Serial pc(USBTX,USBRX); |
Jamess | 0:aa70f7963d9f | 40 | |
Jamess | 0:aa70f7963d9f | 41 | /*-----Funcoes------*/ |
Jamess | 0:aa70f7963d9f | 42 | |
Jamess | 0:aa70f7963d9f | 43 | // Acelerômetro: |
Jamess | 0:aa70f7963d9f | 44 | int RegisterWrite(char,char,char); |
Jamess | 0:aa70f7963d9f | 45 | int RegisterRead(char,char); |
Jamess | 0:aa70f7963d9f | 46 | inline void MultByteRead(char,char,char*,int); |
Jamess | 0:aa70f7963d9f | 47 | void GetOutput(char,int*); |
Jamess | 0:aa70f7963d9f | 48 | |
Jamess | 0:aa70f7963d9f | 49 | // Sensor de Fluxo: |
Jamess | 0:aa70f7963d9f | 50 | AnalogIn ain(PTB3); |
Jamess | 0:aa70f7963d9f | 51 | DigitalOut led2(LED2); // Led red |
Jamess | 0:aa70f7963d9f | 52 | void HandlerT1(void); |
Jamess | 0:aa70f7963d9f | 53 | void rx_Handler(void); |
Jamess | 0:aa70f7963d9f | 54 | |
Jamess | 0:aa70f7963d9f | 55 | Ticker t1; // Timer to send data |
Jamess | 0:aa70f7963d9f | 56 | |
Jamess | 0:aa70f7963d9f | 57 | volatile bool STATUS = true; |
Jamess | 0:aa70f7963d9f | 58 | char test = 0; |
Jamess | 0:aa70f7963d9f | 59 | |
Jamess | 0:aa70f7963d9f | 60 | int main() |
Jamess | 0:aa70f7963d9f | 61 | { |
Jamess | 0:aa70f7963d9f | 62 | |
Jamess | 0:aa70f7963d9f | 63 | /*-----------------INICIALIZA SENSOR DE FLUXO-----------*/ |
Jamess | 0:aa70f7963d9f | 64 | pc.baud(9600); |
Jamess | 0:aa70f7963d9f | 65 | pc.attach(&rx_Handler, pc.RxIrq); |
Jamess | 0:aa70f7963d9f | 66 | led2=0; |
Jamess | 0:aa70f7963d9f | 67 | |
Jamess | 0:aa70f7963d9f | 68 | |
Jamess | 0:aa70f7963d9f | 69 | /*------------------INICIALIZA ACELEROMETRO-------------*/ |
Jamess | 0:aa70f7963d9f | 70 | int readings[3] = {0,0,0}; |
Jamess | 0:aa70f7963d9f | 71 | printf("Entrou no main"); //Debug. |
Jamess | 0:aa70f7963d9f | 72 | |
Jamess | 0:aa70f7963d9f | 73 | i2c.frequency(100000); //100kHz |
Jamess | 0:aa70f7963d9f | 74 | |
Jamess | 0:aa70f7963d9f | 75 | // Testa Envio: |
Jamess | 0:aa70f7963d9f | 76 | pc.printf("Sending POWER CONTROL\t"); |
Jamess | 0:aa70f7963d9f | 77 | STATUS = RegisterWrite(ADXL1_ADDR,POWER_CTL,0x00); |
Jamess | 0:aa70f7963d9f | 78 | |
Jamess | 0:aa70f7963d9f | 79 | if (STATUS != 0){ |
Jamess | 0:aa70f7963d9f | 80 | pc.printf("WRITE NO SUCCESS\t"); |
Jamess | 0:aa70f7963d9f | 81 | } |
Jamess | 0:aa70f7963d9f | 82 | else{ |
Jamess | 0:aa70f7963d9f | 83 | pc.printf("ACKNOLEDGE RECEIVED"); |
Jamess | 0:aa70f7963d9f | 84 | } |
Jamess | 0:aa70f7963d9f | 85 | |
Jamess | 0:aa70f7963d9f | 86 | wait(0.1); |
Jamess | 0:aa70f7963d9f | 87 | pc.printf("Sending Data Format\t"); |
Jamess | 0:aa70f7963d9f | 88 | |
Jamess | 0:aa70f7963d9f | 89 | RegisterWrite(ADXL1_ADDR,DATA_FORMAT,0x09); // |
Jamess | 0:aa70f7963d9f | 90 | |
Jamess | 0:aa70f7963d9f | 91 | RegisterWrite(ADXL1_ADDR,BW_RATE,0x0A); // Default Value... 100kHZ |
Jamess | 0:aa70f7963d9f | 92 | |
Jamess | 0:aa70f7963d9f | 93 | RegisterWrite(ADXL1_ADDR,POWER_CTL,0x08); // MeasurementMode |
Jamess | 0:aa70f7963d9f | 94 | |
Jamess | 0:aa70f7963d9f | 95 | |
Jamess | 0:aa70f7963d9f | 96 | /*-------------Setting the Offset Value---------------*/ |
Jamess | 0:aa70f7963d9f | 97 | |
Jamess | 0:aa70f7963d9f | 98 | RegisterWrite(ADXL1_ADDR,OFSX,0xFD); |
Jamess | 0:aa70f7963d9f | 99 | RegisterWrite(ADXL1_ADDR,OFSY,0x03); |
Jamess | 0:aa70f7963d9f | 100 | RegisterWrite(ADXL1_ADDR,OFSZ,0xFE); |
Jamess | 0:aa70f7963d9f | 101 | |
Jamess | 0:aa70f7963d9f | 102 | /*----------------------------------------------------*/ |
Jamess | 0:aa70f7963d9f | 103 | |
Jamess | 0:aa70f7963d9f | 104 | wait(0.1); |
Jamess | 0:aa70f7963d9f | 105 | printf("Now, trying to read data\t"); |
Jamess | 0:aa70f7963d9f | 106 | printf("%i", RegisterRead(ADXL1_ADDR,0x00)); |
Jamess | 0:aa70f7963d9f | 107 | |
Jamess | 0:aa70f7963d9f | 108 | if (STATUS == 0){ |
Jamess | 0:aa70f7963d9f | 109 | printf("READ SUCCESSFULL\t"); |
Jamess | 0:aa70f7963d9f | 110 | } |
Jamess | 0:aa70f7963d9f | 111 | else { |
Jamess | 0:aa70f7963d9f | 112 | printf("READ NOT SUCCESSFUL\n"); |
Jamess | 0:aa70f7963d9f | 113 | } |
Jamess | 0:aa70f7963d9f | 114 | |
Jamess | 0:aa70f7963d9f | 115 | pc.printf("Press 2 to Start"); |
Jamess | 0:aa70f7963d9f | 116 | |
Jamess | 0:aa70f7963d9f | 117 | while(1) { |
Jamess | 0:aa70f7963d9f | 118 | |
Jamess | 0:aa70f7963d9f | 119 | wait(0.5); // Debug |
Jamess | 0:aa70f7963d9f | 120 | led2 = 0; |
Jamess | 0:aa70f7963d9f | 121 | pc.putc(test); // Debug |
Jamess | 0:aa70f7963d9f | 122 | |
Jamess | 0:aa70f7963d9f | 123 | if (test == '2') { |
Jamess | 0:aa70f7963d9f | 124 | t1.attach(&HandlerT1, 0.01); |
Jamess | 0:aa70f7963d9f | 125 | } |
Jamess | 0:aa70f7963d9f | 126 | |
Jamess | 0:aa70f7963d9f | 127 | while(test == '2') { |
Jamess | 0:aa70f7963d9f | 128 | |
Jamess | 0:aa70f7963d9f | 129 | if (STATUS == true) { |
Jamess | 0:aa70f7963d9f | 130 | |
Jamess | 0:aa70f7963d9f | 131 | STATUS = false; |
Jamess | 0:aa70f7963d9f | 132 | led2 = 1; |
Jamess | 0:aa70f7963d9f | 133 | pc.printf("0x%04X",ain.read_u16()); |
Jamess | 0:aa70f7963d9f | 134 | GetOutput(ADXL1_ADDR,readings); |
Jamess | 0:aa70f7963d9f | 135 | pc.printf("%i,%i,%i\n",(int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[2]); |
Jamess | 0:aa70f7963d9f | 136 | } |
Jamess | 0:aa70f7963d9f | 137 | |
Jamess | 0:aa70f7963d9f | 138 | |
Jamess | 0:aa70f7963d9f | 139 | } //end of while(test=='2'){} |
Jamess | 0:aa70f7963d9f | 140 | |
Jamess | 0:aa70f7963d9f | 141 | t1.detach(); // Detaches timer interruption when not needed |
Jamess | 0:aa70f7963d9f | 142 | }//end of while(1) |
Jamess | 0:aa70f7963d9f | 143 | }//end of main |
Jamess | 0:aa70f7963d9f | 144 | |
Jamess | 0:aa70f7963d9f | 145 | void HandlerT1(void) |
Jamess | 0:aa70f7963d9f | 146 | { |
Jamess | 0:aa70f7963d9f | 147 | STATUS = true; |
Jamess | 0:aa70f7963d9f | 148 | } |
Jamess | 0:aa70f7963d9f | 149 | |
Jamess | 0:aa70f7963d9f | 150 | void rx_Handler(void) |
Jamess | 0:aa70f7963d9f | 151 | { |
Jamess | 0:aa70f7963d9f | 152 | test = pc.getc(); |
Jamess | 0:aa70f7963d9f | 153 | pc.putc(test); |
Jamess | 0:aa70f7963d9f | 154 | } |
Jamess | 0:aa70f7963d9f | 155 | |
Jamess | 0:aa70f7963d9f | 156 | int RegisterWrite(char SLAVE, char RegAddress, char Data){ |
Jamess | 0:aa70f7963d9f | 157 | char cmd[2]; |
Jamess | 0:aa70f7963d9f | 158 | cmd[0]= RegAddress; |
Jamess | 0:aa70f7963d9f | 159 | cmd[1]= Data; |
Jamess | 0:aa70f7963d9f | 160 | int ack = i2c.write((SLAVE<<1),cmd,2); |
Jamess | 0:aa70f7963d9f | 161 | return ack; |
Jamess | 0:aa70f7963d9f | 162 | } |
Jamess | 0:aa70f7963d9f | 163 | |
Jamess | 0:aa70f7963d9f | 164 | int RegisterRead(char SLAVE, char address){ |
Jamess | 0:aa70f7963d9f | 165 | char output; |
Jamess | 0:aa70f7963d9f | 166 | char tx = address; |
Jamess | 0:aa70f7963d9f | 167 | i2c.write((SLAVE<<1),&tx,1); |
Jamess | 0:aa70f7963d9f | 168 | STATUS = i2c.read((SLAVE<<1), &output,1); |
Jamess | 0:aa70f7963d9f | 169 | return output; |
Jamess | 0:aa70f7963d9f | 170 | } |
Jamess | 0:aa70f7963d9f | 171 | |
Jamess | 0:aa70f7963d9f | 172 | inline void MultRegisterRead(char SLAVE, char address,char*output,int size){ |
Jamess | 0:aa70f7963d9f | 173 | i2c.write((SLAVE<<1),&address,1); |
Jamess | 0:aa70f7963d9f | 174 | i2c.read((SLAVE<<1),output,size); |
Jamess | 0:aa70f7963d9f | 175 | return; |
Jamess | 0:aa70f7963d9f | 176 | } |
Jamess | 0:aa70f7963d9f | 177 | |
Jamess | 0:aa70f7963d9f | 178 | void GetOutput(char SLAVE, int* readings){ |
Jamess | 0:aa70f7963d9f | 179 | char buffer[6]; |
Jamess | 0:aa70f7963d9f | 180 | MultRegisterRead(SLAVE,DATAX0,buffer, 6); |
Jamess | 0:aa70f7963d9f | 181 | readings[0] = (int)buffer[1] << 8 | (int)buffer[0]; |
Jamess | 0:aa70f7963d9f | 182 | readings[1] = (int)buffer[3] << 8 | (int)buffer[2]; |
Jamess | 0:aa70f7963d9f | 183 | readings[2] = (int)buffer[5] << 8 | (int)buffer[4]; |
Jamess | 0:aa70f7963d9f | 184 | } |