jaume bros
/
i2csrf08
Funcionamiento del SRF08
main.cpp@0:157e9c99cef4, 2010-11-17 (annotated)
- Committer:
- jaume
- Date:
- Wed Nov 17 17:33:57 2010 +0000
- Revision:
- 0:157e9c99cef4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaume | 0:157e9c99cef4 | 1 | // SRF08 - Jaume |
jaume | 0:157e9c99cef4 | 2 | #include "mbed.h" |
jaume | 0:157e9c99cef4 | 3 | BusOut mleds(LED1, LED2, LED3, LED4); |
jaume | 0:157e9c99cef4 | 4 | I2C i2c(p9, p10); // sda, scl |
jaume | 0:157e9c99cef4 | 5 | Serial pc(USBTX, USBRX); // tx, rx a 9600 |
jaume | 0:157e9c99cef4 | 6 | |
jaume | 0:157e9c99cef4 | 7 | const int addr = 0xE0; // define direccion SRF08 |
jaume | 0:157e9c99cef4 | 8 | |
jaume | 0:157e9c99cef4 | 9 | int main() { |
jaume | 0:157e9c99cef4 | 10 | char cmd[2]; // comando |
jaume | 0:157e9c99cef4 | 11 | char dat[3]; // datos a recibir |
jaume | 0:157e9c99cef4 | 12 | cmd[0] = 0x02; // puntero a reg 2 |
jaume | 0:157e9c99cef4 | 13 | cmd[1] = 0x8C; // rango hasta 6m |
jaume | 0:157e9c99cef4 | 14 | i2c.write(addr, cmd, 2); // escribe comandos |
jaume | 0:157e9c99cef4 | 15 | while(1) { |
jaume | 0:157e9c99cef4 | 16 | cmd[0] = 0x00; // puntero a reg 0 |
jaume | 0:157e9c99cef4 | 17 | cmd[1] = 0x54; // comando ANN cm |
jaume | 0:157e9c99cef4 | 18 | i2c.write(addr, cmd, 2); |
jaume | 0:157e9c99cef4 | 19 | wait(0.07); // Espera 65ms segun data SRF08 |
jaume | 0:157e9c99cef4 | 20 | |
jaume | 0:157e9c99cef4 | 21 | cmd[0] = 0x01; // puntero a reg 1 |
jaume | 0:157e9c99cef4 | 22 | i2c.write(addr, cmd, 1); |
jaume | 0:157e9c99cef4 | 23 | i2c.read(addr, dat, 3); // lee 3 bytes 1 luz y 2 echo |
jaume | 0:157e9c99cef4 | 24 | // imprime datos |
jaume | 0:157e9c99cef4 | 25 | pc.printf("reg1 %x reg2H %x reg3L %x\n", dat[0], dat[1], dat[2]); |
jaume | 0:157e9c99cef4 | 26 | pc.printf("---------------------------------\n"); |
jaume | 0:157e9c99cef4 | 27 | pc.printf(" Luz...... = %u\n", dat[0]); |
jaume | 0:157e9c99cef4 | 28 | int echo =(dat[1]<<8)+dat[2]; |
jaume | 0:157e9c99cef4 | 29 | pc.printf(" Distancia = %u cm.\n", echo); |
jaume | 0:157e9c99cef4 | 30 | pc.printf("---------------------------------\n"); |
jaume | 0:157e9c99cef4 | 31 | mleds = echo/8; |
jaume | 0:157e9c99cef4 | 32 | wait (1); // espera 1000ms |
jaume | 0:157e9c99cef4 | 33 | } |
jaume | 0:157e9c99cef4 | 34 | } |