RespiratorySensor Team / Mbed 2 deprecated EXEMPLO_10acc

Dependencies:   ADXL345 mbed

Fork of ADXLHELLOWORD by Thiago .

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Arquivo exemplo de se como utilizar a SPI em com a biblioteca do mbed feita por aaron.
00002 //Programa temporario. logo será republicado
00003 
00004 
00005 //IMPORTANTE:
00006 //OBSERVAÇÃO:
00007 //AILTON:
00008         // Esse programa ainda não foi testado. 
00009         // É uma adaptação para utilizar vários acelerômetros utilizando a classe
00010         // BUSOUT ligado no CS dos acelerômetros;
00011         
00012         
00013 
00014 #include "mbed.h"
00015 
00016 /*-------ADXL345-------*/
00017 
00018 // When SDO grounded
00019 #define ADXL1_ADDR_WRITE 0xA6
00020 #define ADXL1_ADDR_READ 0xA7
00021 #define ADXL1_ADDR 0X53
00022 
00023 //When SDO high
00024 #define ADXL2_ADDR_WRITE 0x3A
00025 #define ADXL2_ADDR_READ 0x3B
00026 #define ADXL2_ADDR 0x53
00027 
00028 //Registers
00029 
00030 #define POWER_CTL 0x2D
00031 #define DATA_FORMAT 0x31
00032 #define DATAX0 0x32       //X
00033 #define DATAX1 0x33
00034 #define DATAY0 0x34       //Y
00035 #define DATAY1 0x35
00036 #define DATAZ0 0x36       //Z
00037 #define DATAZ1 0x37
00038 #define OFSX 0x1D         //accelerometer offset calibration
00039 #define OFSY 0x1F         
00040 #define OFSZ 0x20
00041 #define BW_RATE 0x2C      //define Baudrate
00042 
00043 //SPI Comunication
00044 #define ADXL345_SPI_READ    0x80
00045 #define ADXL345_SPI_WRITE   0x00
00046 #define ADXL345_MULTI_BYTE  0x60
00047 
00048 /*----Transmicao----*/
00049 
00050 #define PORT_MASK_ACC0 002
00051 #define PORT_MASK_ACC1 001
00052 #define PORT_MASK_ACC2 003
00053 #define PORT_MASK_ACC3 005
00054 #define PORT_MASK_ACC4 004
00055 #define PORT_MASK_ACC5 000
00056 #define PORT_MASK_ACC6 000
00057 #define PORT_MASK_ACC7 000
00058 #define PORT_MASK_ACC8 000
00059 #define PORT_MASK_ACC9 000
00060 #define CS_ON 001
00061 
00062 /*----Chip Select----*/
00063 #define CS_ACC0 PTE31
00064 #define CS_ACC1 PTE19
00065 #define CS_ACC2 PTE18
00066 #define CS_ACC3 PTE17
00067 #define CS_ACC4 PTE16
00068 #define CS_ACC5 PTE6
00069 #define CS_ACC6 PTE3
00070 #define CS_ACC7 PTE2
00071 #define CS_ACC8 PTE20
00072 #define CS_ACC9 PTE21
00073 
00074 SPI spi(PTD2,PTD3,PTD1);                    //PTD0);//PinName mosi, PinName miso, PinName sck,     PinName cs)
00075 BusOut CS_Accel(CS_ACC0,CS_ACC1,CS_ACC2,CS_ACC3,CS_ACC4,CS_ACC5,CS_ACC6,CS_ACC7,CS_ACC8,CS_ACC9);  //NA PLACA 46z
00076 Serial pc(USBTX, USBRX);
00077 DigitalOut PIN(PTD5);
00078 
00079 /********************---------FUNÇÕES ACELERÔMETRO**********************************/
00080 
00081 //Escrita:
00082 void RegisterWrite(char,int,char);                                      //char acelerômetro, int endereço, char dado.
00083 void MultRegisterWrite(char acc,char address,char data);               //Faz isso
00084 
00085 //Leitura:
00086 void GetOutputs(void);                                                  //Faz aquilo          
00087 void MultRegisterRead(int,char*, int);                                            //
00088 int RegisterRead(char);
00089 /******************************Variáveis Acelerômetro********************************/
00090 
00091 
00092 
00093 
00094 /************************************************************************************/
00095 
00096 int main() {
00097  
00098     int readings[3] = {0, 0, 0};
00099      
00100     //Go into standby mode to configure the device.
00101   //  pc.printf("Starting setting the accelerômeters");
00102     //accelerometer.setPowerControl(0x00);
00103     RegisterWrite(PORT_MASK_ACC1,POWER_CTL,0x00);
00104     //Full resolution, +/-16g, 4mg/LSB.
00105     //accelerometer.setDataFormatControl(0x0B);
00106     RegisterWrite(PORT_MASK_ACC1,DATA_FORMAT,0x0B);
00107     
00108     //3.2kHz data rate.
00109     //accelerometer.setDataRate(ADXL345_3200HZ);
00110     RegisterWrite(PORT_MASK_ACC1,BW_RATE,0x0F);
00111     
00112     //Measurement mode.
00113     //accelerometer.setPowerControl(0x08);
00114     RegisterWrite(PORT_MASK_ACC1,POWER_CTL,0x08);
00115  
00116     while (1) {
00117         
00118         pc.printf("main");
00119         
00120         PIN = 1;
00121        
00122         PIN = 0;
00123     }
00124  
00125 }
00126 /*---------------------------FUNÇÕES-------------------------*/
00127 
00128 int RegisterRead(char portMaskRead,char address){
00129     
00130     int tx = (ADXL345_SPI_READ | (address & 0x3F));
00131     int rx = 0;
00132 
00133     CS_Accel = portMaskRead;
00134     //Send address to read from.
00135     spi.write(tx);
00136     //Read back contents of address.
00137     rx = spi.write(0x00);
00138     CS_Accel = CS_ON;       //Finishes the comunication putting CS in HIGH
00139 
00140     return rx;
00141 }
00142 
00143 void RegisterWrite(char portMaskWrite,int address, char data) {
00144     
00145          //Char acc para definir qual CS será setado
00146 
00147     int tx = (ADXL345_SPI_WRITE | (address & 0x3F));
00148 
00149     CS_Accel = portMaskWrite; // Aqui na verdade vai ficar uma márcara para decidirvários pinos aomesmo tempo
00150     //Send address to write to.
00151     spi.write(tx);
00152     //Send data to be written.
00153     spi.write(data);
00154     CS_Accel = CS_ON;
00155 
00156 }
00157 void MultRegisterRead(char portMaskMRead,int startAddress, char* buffer, int size) {
00158 
00159     int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
00160 
00161     CS_Accel = portMaskMRead;
00162     //Send address to start reading from.
00163     spi.write(tx);
00164 
00165     for (int i = 0; i < size; i++) {
00166         buffer[i] = spi.write(0x00);
00167     }
00168 
00169     CS_Accel = CS_ON;
00170 
00171 }
00172 
00173 void MultRegisterWrite(char portMaskMWrite, int startAddress, char* buffer, int size) {
00174 
00175     int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
00176 
00177     CS_Accel = portMaskMWrite;
00178     //Send address to start reading from.
00179     spi.write(tx);
00180 
00181     for (int i = 0; i < size; i++) {
00182         buffer[i] = spi.write(0x00);
00183     }
00184 
00185     CS_Accel = portMaskMWrite;
00186 
00187 }
00188 
00189 void GetOutput(void){
00190     //Lê Acelerômetro 1
00191     //Lê Acelerômetro 2
00192     //Lê Acelerômetro 3
00193     //Lê Acelerômetro 4
00194     //Lê Acelerômetro 5
00195     //Lê Acelerômetro 6
00196     //Lê Acelerômetro 7
00197     //Lê Acelerômetro 8
00198     //Lê Acelerômetro 9
00199     //Lê Acelerômetro 10
00200 }
00201 
00202 
00203 /*===============================FUNCOES COPIADAS======================================*/
00204 /*int ADXL345::oneByteRead(int address) {
00205 
00206     int tx = (ADXL345_SPI_READ | (address & 0x3F));
00207     int rx = 0;
00208 
00209     PIN = 0;
00210     //Send address to read from.
00211     spi_.write(tx);
00212     //Read back contents of address.
00213     rx = spi_.write(0x00);
00214     PIN = 1;
00215 
00216     return rx;
00217 
00218 }
00219 
00220 void ADXL345::oneByteWrite(int address, char data) {
00221 
00222     int tx = (ADXL345_SPI_WRITE | (address & 0x3F));
00223 
00224     PIN = 0;
00225     //Send address to write to.
00226     spi_.write(tx);
00227     //Send data to be written.
00228     spi_.write(data);
00229     PIN = 1;
00230 
00231 }
00232 
00233 void ADXL345::multiByteRead(int startAddress, char* buffer, int size) {
00234 
00235     int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
00236 
00237     PIN = 0;
00238     //Send address to start reading from.
00239     spi_.write(tx);
00240 
00241     for (int i = 0; i < size; i++) {
00242         buffer[i] = spi_.write(0x00);
00243     }
00244 
00245     PIN = 1;
00246 
00247 }
00248 
00249 void ADXL345::multiByteWrite(int startAddress, char* buffer, int size) {
00250 
00251     int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
00252 
00253     PIN = 0;
00254     //Send address to start reading from.
00255     spi_.write(tx);
00256 
00257     for (int i = 0; i < size; i++) {
00258         buffer[i] = spi_.write(0x00);
00259     }
00260 
00261     PIN = 1;
00262 
00263 }*/