Test
Dependencies: mbed AccelSensor
main.cpp@1:60bb79c9a01e, 2013-01-14 (annotated)
- Committer:
- Alegrowin
- Date:
- Mon Jan 14 18:05:34 2013 +0000
- Revision:
- 1:60bb79c9a01e
- Parent:
- 0:b325845b05af
- Child:
- 2:3e6b509d7eca
Commented and functionnal; +Added output format on Serial to PC
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 | 0:b325845b05af | 3 | I2C i2c(p28, p27); // 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 | 0:b325845b05af | 16 | while(1) { |
Alegrowin | 1:60bb79c9a01e | 17 | pc.printf("\r\n\nStart reading the temperature of TC74 on I2C\r\n"); |
Alegrowin | 0:b325845b05af | 18 | |
Alegrowin | 0:b325845b05af | 19 | //Méthode 1 |
Alegrowin | 1:60bb79c9a01e | 20 | //Utilisation des fonctions bas niveau |
Alegrowin | 0:b325845b05af | 21 | |
Alegrowin | 1:60bb79c9a01e | 22 | i2c.start(); // Start condition |
Alegrowin | 1:60bb79c9a01e | 23 | a = i2c.write(addr); // Write Device Address |
Alegrowin | 1:60bb79c9a01e | 24 | b = i2c.write(0x00); // Write READ command of TC74 (voir page 8 de la datasheet du TC74) |
Alegrowin | 1:60bb79c9a01e | 25 | |
Alegrowin | 1:60bb79c9a01e | 26 | i2c.start(); //Reissue start condition |
Alegrowin | 1:60bb79c9a01e | 27 | //Au lieu de faire Stop condition et Start de nouveau |
Alegrowin | 0:b325845b05af | 28 | |
Alegrowin | 1:60bb79c9a01e | 29 | c= i2c.write(addr|1); //Adresse du Device en mode Lecture |
Alegrowin | 1:60bb79c9a01e | 30 | temp = i2c.read(0); //Lecture de la valeur du registre de température |
Alegrowin | 1:60bb79c9a01e | 31 | i2c.stop(); //Fermeture de la trame |
Alegrowin | 0:b325845b05af | 32 | |
Alegrowin | 0:b325845b05af | 33 | //Méthode 2 |
Alegrowin | 1:60bb79c9a01e | 34 | //Utilisation des fonctions haut niveau |
Alegrowin | 1:60bb79c9a01e | 35 | |
Alegrowin | 1:60bb79c9a01e | 36 | cmd[0] = 0x0; //Command :: READ |
Alegrowin | 1:60bb79c9a01e | 37 | cmd[1] = 0x0; //Param :: Unused in this case |
Alegrowin | 1:60bb79c9a01e | 38 | i2c.write(addr, cmd, 1); //Issue required command to perform a write of the command |
Alegrowin | 1:60bb79c9a01e | 39 | i2c.read(addr, cmd, 1); //Read the Data from the device |
Alegrowin | 0:b325845b05af | 40 | |
Alegrowin | 1:60bb79c9a01e | 41 | //-----------------Print out section ---------------------- |
Alegrowin | 1:60bb79c9a01e | 42 | //Display device Address and informations |
Alegrowin | 1:60bb79c9a01e | 43 | pc.printf("Device with address 0x%x with\r\n", addr); |
Alegrowin | 0:b325845b05af | 44 | |
Alegrowin | 1:60bb79c9a01e | 45 | //Prints out the result of Method 1 |
Alegrowin | 1:60bb79c9a01e | 46 | //pc.printf("ACK1 :: %d\n\rACK2 :: %d\n\rACK3 :: %d\n\r", a,b,c); //ACK bits |
Alegrowin | 1:60bb79c9a01e | 47 | pc.printf("Method 1 :: %d\n\r", temp); |
Alegrowin | 1:60bb79c9a01e | 48 | |
Alegrowin | 1:60bb79c9a01e | 49 | //Prints out the Data from Method 2 |
Alegrowin | 1:60bb79c9a01e | 50 | pc.printf("Method 1 :: %d\r\n\n", cmd[0]); |
Alegrowin | 0:b325845b05af | 51 | |
Alegrowin | 1:60bb79c9a01e | 52 | |
Alegrowin | 0:b325845b05af | 53 | wait(1); |
Alegrowin | 0:b325845b05af | 54 | } |
Alegrowin | 0:b325845b05af | 55 | } |