Test
Dependencies: mbed AccelSensor
main.cpp@2:3e6b509d7eca, 2013-01-15 (annotated)
- Committer:
- Alegrowin
- Date:
- Tue Jan 15 04:19:52 2013 +0000
- Revision:
- 2:3e6b509d7eca
- Parent:
- 1:60bb79c9a01e
- Child:
- 3:4e4e4d7f6ee1
Fully functionnal I2C for temperature sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Alegrowin | 0:b325845b05af | 1 | #include "mbed.h" |
Alegrowin | 0:b325845b05af | 2 | |
Alegrowin | 2:3e6b509d7eca | 3 | I2C i2c(p9, p10); // sda, scl |
Alegrowin | 0:b325845b05af | 4 | Serial pc(USBTX, USBRX); // tx, rx |
Alegrowin | 0:b325845b05af | 5 | |
Alegrowin | 0:b325845b05af | 6 | const int addr = 0x9A; // define the I2C Address for TC74-A0 |
Alegrowin | 0:b325845b05af | 7 | |
Alegrowin | 0:b325845b05af | 8 | int main() |
Alegrowin | 0:b325845b05af | 9 | { |
Alegrowin | 0:b325845b05af | 10 | int temp = 0; |
Alegrowin | 0:b325845b05af | 11 | int a,b,c; |
Alegrowin | 0:b325845b05af | 12 | char cmd[2]; |
Alegrowin | 0:b325845b05af | 13 | |
Alegrowin | 1:60bb79c9a01e | 14 | wait(1); //Make sure system is fully initialized |
Alegrowin | 1:60bb79c9a01e | 15 | |
Alegrowin | 2:3e6b509d7eca | 16 | |
Alegrowin | 0:b325845b05af | 17 | while(1) { |
Alegrowin | 1:60bb79c9a01e | 18 | pc.printf("\r\n\nStart reading the temperature of TC74 on I2C\r\n"); |
Alegrowin | 0:b325845b05af | 19 | |
Alegrowin | 0:b325845b05af | 20 | //Méthode 1 |
Alegrowin | 1:60bb79c9a01e | 21 | //Utilisation des fonctions bas niveau |
Alegrowin | 0:b325845b05af | 22 | |
Alegrowin | 1:60bb79c9a01e | 23 | i2c.start(); // Start condition |
Alegrowin | 1:60bb79c9a01e | 24 | a = i2c.write(addr); // Write Device Address |
Alegrowin | 1:60bb79c9a01e | 25 | b = i2c.write(0x00); // Write READ command of TC74 (voir page 8 de la datasheet du TC74) |
Alegrowin | 1:60bb79c9a01e | 26 | |
Alegrowin | 1:60bb79c9a01e | 27 | i2c.start(); //Reissue start condition |
Alegrowin | 1:60bb79c9a01e | 28 | //Au lieu de faire Stop condition et Start de nouveau |
Alegrowin | 0:b325845b05af | 29 | |
Alegrowin | 1:60bb79c9a01e | 30 | c= i2c.write(addr|1); //Adresse du Device en mode Lecture |
Alegrowin | 1:60bb79c9a01e | 31 | temp = i2c.read(0); //Lecture de la valeur du registre de température |
Alegrowin | 1:60bb79c9a01e | 32 | i2c.stop(); //Fermeture de la trame |
Alegrowin | 0:b325845b05af | 33 | |
Alegrowin | 0:b325845b05af | 34 | //Méthode 2 |
Alegrowin | 1:60bb79c9a01e | 35 | //Utilisation des fonctions haut niveau |
Alegrowin | 1:60bb79c9a01e | 36 | |
Alegrowin | 1:60bb79c9a01e | 37 | cmd[0] = 0x0; //Command :: READ |
Alegrowin | 1:60bb79c9a01e | 38 | cmd[1] = 0x0; //Param :: Unused in this case |
Alegrowin | 1:60bb79c9a01e | 39 | i2c.write(addr, cmd, 1); //Issue required command to perform a write of the command |
Alegrowin | 1:60bb79c9a01e | 40 | i2c.read(addr, cmd, 1); //Read the Data from the device |
Alegrowin | 0:b325845b05af | 41 | |
Alegrowin | 1:60bb79c9a01e | 42 | //-----------------Print out section ---------------------- |
Alegrowin | 1:60bb79c9a01e | 43 | //Display device Address and informations |
Alegrowin | 1:60bb79c9a01e | 44 | pc.printf("Device with address 0x%x with\r\n", addr); |
Alegrowin | 0:b325845b05af | 45 | |
Alegrowin | 1:60bb79c9a01e | 46 | //Prints out the result of Method 1 |
Alegrowin | 1:60bb79c9a01e | 47 | //pc.printf("ACK1 :: %d\n\rACK2 :: %d\n\rACK3 :: %d\n\r", a,b,c); //ACK bits |
Alegrowin | 1:60bb79c9a01e | 48 | pc.printf("Method 1 :: %d\n\r", temp); |
Alegrowin | 1:60bb79c9a01e | 49 | |
Alegrowin | 1:60bb79c9a01e | 50 | //Prints out the Data from Method 2 |
Alegrowin | 2:3e6b509d7eca | 51 | pc.printf("Method 2 :: %d\r\n\n", cmd[0]); |
Alegrowin | 0:b325845b05af | 52 | |
Alegrowin | 1:60bb79c9a01e | 53 | |
Alegrowin | 2:3e6b509d7eca | 54 | |
Alegrowin | 0:b325845b05af | 55 | wait(1); |
Alegrowin | 0:b325845b05af | 56 | } |
Alegrowin | 0:b325845b05af | 57 | } |