sqefqsdf
Dependencies: C12832 EthernetInterface LM75B mbed-rtos mbed
Fork of app-board-LM75B by
main.cpp@0:ce7a8546502b, 2010-01-16 (annotated)
- Committer:
- okano
- Date:
- Sat Jan 16 02:41:51 2010 +0000
- Revision:
- 0:ce7a8546502b
- Child:
- 1:6484448034e3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okano | 0:ce7a8546502b | 1 | /* |
okano | 0:ce7a8546502b | 2 | * NXP LM75B temperature sensor test |
okano | 0:ce7a8546502b | 3 | * www.nxp.com/pip/LM75B_2.html |
okano | 0:ce7a8546502b | 4 | * |
okano | 0:ce7a8546502b | 5 | * Expecting to use the pins 9 and 10 for I2C bus |
okano | 0:ce7a8546502b | 6 | * these pins should be pulled-up properly. |
okano | 0:ce7a8546502b | 7 | * |
okano | 0:ce7a8546502b | 8 | * The temperature read out will be shown on terminal on the PC screen. |
okano | 0:ce7a8546502b | 9 | * |
okano | 0:ce7a8546502b | 10 | * In this demo code, two LM75B devices can be driven. |
okano | 0:ce7a8546502b | 11 | * These two devices should have different I2C address setting |
okano | 0:ce7a8546502b | 12 | * using its address pins (LM75B's A0 to A2 (pins 5 to 7)). |
okano | 0:ce7a8546502b | 13 | * One LM75B should have all those pins tied to GND. |
okano | 0:ce7a8546502b | 14 | * And another should have the pin A0(pin7) pulled-up. |
okano | 0:ce7a8546502b | 15 | * |
okano | 0:ce7a8546502b | 16 | * From the software, those devices can be accessed by I2C addresses |
okano | 0:ce7a8546502b | 17 | * "0x90" and "0x92". |
okano | 0:ce7a8546502b | 18 | * It will not be as "0x90" and "0x91" because the address has |
okano | 0:ce7a8546502b | 19 | * 7 bit only and stuffed to left. So the "A0" setting become 0xX2. |
okano | 0:ce7a8546502b | 20 | * The LSB does not care because it will be set by I2C libraly when |
okano | 0:ce7a8546502b | 21 | * it transfer the data for read and write. |
okano | 0:ce7a8546502b | 22 | */ |
okano | 0:ce7a8546502b | 23 | |
okano | 0:ce7a8546502b | 24 | #include "mbed.h" |
okano | 0:ce7a8546502b | 25 | //#include "SDFileSystem.h" |
okano | 0:ce7a8546502b | 26 | |
okano | 0:ce7a8546502b | 27 | // LM75B IIC address |
okano | 0:ce7a8546502b | 28 | #define LM75B_ADDR 0x90 |
okano | 0:ce7a8546502b | 29 | |
okano | 0:ce7a8546502b | 30 | // LM75B registers |
okano | 0:ce7a8546502b | 31 | #define Conf 0x01 |
okano | 0:ce7a8546502b | 32 | #define Temp 0x00 |
okano | 0:ce7a8546502b | 33 | #define Tos 0x03 |
okano | 0:ce7a8546502b | 34 | #define Thyst 0x02 |
okano | 0:ce7a8546502b | 35 | |
okano | 0:ce7a8546502b | 36 | |
okano | 0:ce7a8546502b | 37 | DigitalOut led[] = { LED4, LED3, LED2, LED1 }; |
okano | 0:ce7a8546502b | 38 | Serial pc(USBTX, USBRX); // tx, rx |
okano | 0:ce7a8546502b | 39 | |
okano | 0:ce7a8546502b | 40 | I2C i2c( p9, p10 ); // sda, scl |
okano | 0:ce7a8546502b | 41 | |
okano | 0:ce7a8546502b | 42 | |
okano | 0:ce7a8546502b | 43 | void iic_write( char addr, char reg, char data ); |
okano | 0:ce7a8546502b | 44 | char iic_read( char addr, char reg ); |
okano | 0:ce7a8546502b | 45 | short iic_read_short( char addr, char reg ); |
okano | 0:ce7a8546502b | 46 | void iic_error( void ); |
okano | 0:ce7a8546502b | 47 | |
okano | 0:ce7a8546502b | 48 | void init_temp_sensor( char dev_num ); |
okano | 0:ce7a8546502b | 49 | float get_temp( char dev_num ); |
okano | 0:ce7a8546502b | 50 | |
okano | 0:ce7a8546502b | 51 | int main() { |
okano | 0:ce7a8546502b | 52 | int i; |
okano | 0:ce7a8546502b | 53 | |
okano | 0:ce7a8546502b | 54 | init_temp_sensor( 0 ); |
okano | 0:ce7a8546502b | 55 | init_temp_sensor( 1 ); |
okano | 0:ce7a8546502b | 56 | |
okano | 0:ce7a8546502b | 57 | while(1) { |
okano | 0:ce7a8546502b | 58 | pc.printf( " (%d) temp[0]=%6.3f, temp[1]=%6.3f(degree-C)\n", i++, get_temp( 0 ), get_temp( 1 ) ); |
okano | 0:ce7a8546502b | 59 | wait( 1 ); |
okano | 0:ce7a8546502b | 60 | } |
okano | 0:ce7a8546502b | 61 | } |
okano | 0:ce7a8546502b | 62 | |
okano | 0:ce7a8546502b | 63 | void init_temp_sensor( char dev_num ) |
okano | 0:ce7a8546502b | 64 | { |
okano | 0:ce7a8546502b | 65 | dev_num <<= 1; |
okano | 0:ce7a8546502b | 66 | |
okano | 0:ce7a8546502b | 67 | iic_write( LM75B_ADDR | dev_num, Conf, 0x00 ); // configuration |
okano | 0:ce7a8546502b | 68 | pc.printf( "sensor[%d] : Conf register read out = 0x%02X\n", dev_num, iic_read( LM75B_ADDR | dev_num, Conf ) ); |
okano | 0:ce7a8546502b | 69 | pc.printf( "sensor[%d] : Tos register read out = 0x%04X\n", dev_num, iic_read_short( LM75B_ADDR | dev_num, Tos ) ); |
okano | 0:ce7a8546502b | 70 | pc.printf( "sensor[%d] : Thyst register read out = 0x%04X\n", dev_num, iic_read_short( LM75B_ADDR | dev_num, Thyst ) ); |
okano | 0:ce7a8546502b | 71 | } |
okano | 0:ce7a8546502b | 72 | |
okano | 0:ce7a8546502b | 73 | |
okano | 0:ce7a8546502b | 74 | float get_temp( char dev_num ) |
okano | 0:ce7a8546502b | 75 | { |
okano | 0:ce7a8546502b | 76 | dev_num <<= 1; |
okano | 0:ce7a8546502b | 77 | |
okano | 0:ce7a8546502b | 78 | return ( (float)(iic_read_short( LM75B_ADDR | dev_num, Temp )) / 256.0 ); |
okano | 0:ce7a8546502b | 79 | } |
okano | 0:ce7a8546502b | 80 | |
okano | 0:ce7a8546502b | 81 | void iic_write( char addr, char reg, char data ) |
okano | 0:ce7a8546502b | 82 | { |
okano | 0:ce7a8546502b | 83 | char cmd[2]; |
okano | 0:ce7a8546502b | 84 | |
okano | 0:ce7a8546502b | 85 | cmd[ 0 ] = reg; |
okano | 0:ce7a8546502b | 86 | cmd[ 1 ] = data; |
okano | 0:ce7a8546502b | 87 | |
okano | 0:ce7a8546502b | 88 | if ( i2c.write( addr, cmd, 2) ) |
okano | 0:ce7a8546502b | 89 | iic_error(); |
okano | 0:ce7a8546502b | 90 | } |
okano | 0:ce7a8546502b | 91 | |
okano | 0:ce7a8546502b | 92 | char iic_read( char addr, char reg ) |
okano | 0:ce7a8546502b | 93 | { |
okano | 0:ce7a8546502b | 94 | char cmd; |
okano | 0:ce7a8546502b | 95 | int nack; |
okano | 0:ce7a8546502b | 96 | |
okano | 0:ce7a8546502b | 97 | cmd = reg; |
okano | 0:ce7a8546502b | 98 | |
okano | 0:ce7a8546502b | 99 | nack = i2c.write( addr, &cmd, 1); // Send command string |
okano | 0:ce7a8546502b | 100 | nack |= i2c.read( addr, &cmd, 1); // Send command string |
okano | 0:ce7a8546502b | 101 | |
okano | 0:ce7a8546502b | 102 | if ( nack ) |
okano | 0:ce7a8546502b | 103 | iic_error(); |
okano | 0:ce7a8546502b | 104 | |
okano | 0:ce7a8546502b | 105 | return ( cmd ); |
okano | 0:ce7a8546502b | 106 | } |
okano | 0:ce7a8546502b | 107 | |
okano | 0:ce7a8546502b | 108 | |
okano | 0:ce7a8546502b | 109 | short iic_read_short( char addr, char reg ) |
okano | 0:ce7a8546502b | 110 | { |
okano | 0:ce7a8546502b | 111 | char cmd[ 2 ]; |
okano | 0:ce7a8546502b | 112 | int nack; |
okano | 0:ce7a8546502b | 113 | |
okano | 0:ce7a8546502b | 114 | cmd[ 0 ] = reg; |
okano | 0:ce7a8546502b | 115 | |
okano | 0:ce7a8546502b | 116 | nack = i2c.write( addr, cmd, 1); // Send command string |
okano | 0:ce7a8546502b | 117 | nack |= i2c.read( addr, cmd, 2); // Send command string |
okano | 0:ce7a8546502b | 118 | |
okano | 0:ce7a8546502b | 119 | if ( nack ) |
okano | 0:ce7a8546502b | 120 | iic_error(); |
okano | 0:ce7a8546502b | 121 | |
okano | 0:ce7a8546502b | 122 | return ( ((short)cmd[ 0 ]) << 8 | cmd[ 1 ] ); |
okano | 0:ce7a8546502b | 123 | } |
okano | 0:ce7a8546502b | 124 | |
okano | 0:ce7a8546502b | 125 | |
okano | 0:ce7a8546502b | 126 | void iic_error( void ) |
okano | 0:ce7a8546502b | 127 | { |
okano | 0:ce7a8546502b | 128 | pc.printf( "I2C error\n" ); |
okano | 0:ce7a8546502b | 129 | |
okano | 0:ce7a8546502b | 130 | for ( int i = 0; i < 4; i++ ) |
okano | 0:ce7a8546502b | 131 | led[ i ] = i & 0x01; |
okano | 0:ce7a8546502b | 132 | |
okano | 0:ce7a8546502b | 133 | wait ( 0.2 ); |
okano | 0:ce7a8546502b | 134 | |
okano | 0:ce7a8546502b | 135 | for ( int i = 0; i < 4; i++ ) |
okano | 0:ce7a8546502b | 136 | led[ i ] = !(i & 0x01); |
okano | 0:ce7a8546502b | 137 | |
okano | 0:ce7a8546502b | 138 | wait ( 0.2 ); |
okano | 0:ce7a8546502b | 139 | } |
okano | 0:ce7a8546502b | 140 |