Exemple 1 CRTI

Dependencies:   C12832 LM75B mbed

Fork of app-board-LM75B by Chris Styles

Committer:
chris
Date:
Thu Oct 18 13:09:21 2012 +0000
Revision:
1:6484448034e3
Parent:
0:ce7a8546502b
Child:
2:9e757151de9b
First commit

Who changed what in which revision?

UserRevisionLine numberNew 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
okano 0:ce7a8546502b 26 // LM75B IIC address
okano 0:ce7a8546502b 27 #define LM75B_ADDR 0x90
okano 0:ce7a8546502b 28
okano 0:ce7a8546502b 29 // LM75B registers
okano 0:ce7a8546502b 30 #define Conf 0x01
okano 0:ce7a8546502b 31 #define Temp 0x00
okano 0:ce7a8546502b 32 #define Tos 0x03
okano 0:ce7a8546502b 33 #define Thyst 0x02
okano 0:ce7a8546502b 34
okano 0:ce7a8546502b 35
okano 0:ce7a8546502b 36 DigitalOut led[] = { LED4, LED3, LED2, LED1 };
okano 0:ce7a8546502b 37 Serial pc(USBTX, USBRX); // tx, rx
okano 0:ce7a8546502b 38
chris 1:6484448034e3 39 I2C i2c( p28, p27 ); // sda, scl
okano 0:ce7a8546502b 40
okano 0:ce7a8546502b 41
okano 0:ce7a8546502b 42 void iic_write( char addr, char reg, char data );
okano 0:ce7a8546502b 43 char iic_read( char addr, char reg );
okano 0:ce7a8546502b 44 short iic_read_short( char addr, char reg );
okano 0:ce7a8546502b 45 void iic_error( void );
okano 0:ce7a8546502b 46
okano 0:ce7a8546502b 47 void init_temp_sensor( char dev_num );
okano 0:ce7a8546502b 48 float get_temp( char dev_num );
okano 0:ce7a8546502b 49
okano 0:ce7a8546502b 50 int main() {
okano 0:ce7a8546502b 51 int i;
okano 0:ce7a8546502b 52
okano 0:ce7a8546502b 53 init_temp_sensor( 0 );
okano 0:ce7a8546502b 54
okano 0:ce7a8546502b 55 while(1) {
chris 1:6484448034e3 56 pc.printf( " (%d) temp[0]=%6.3f, \n", i++, get_temp( 0 ) );
okano 0:ce7a8546502b 57 wait( 1 );
okano 0:ce7a8546502b 58 }
okano 0:ce7a8546502b 59 }
okano 0:ce7a8546502b 60
okano 0:ce7a8546502b 61 void init_temp_sensor( char dev_num )
okano 0:ce7a8546502b 62 {
chris 1:6484448034e3 63 dev_num <<= 1;
okano 0:ce7a8546502b 64 iic_write( LM75B_ADDR | dev_num, Conf, 0x00 ); // configuration
okano 0:ce7a8546502b 65 pc.printf( "sensor[%d] : Conf register read out = 0x%02X\n", dev_num, iic_read( LM75B_ADDR | dev_num, Conf ) );
okano 0:ce7a8546502b 66 pc.printf( "sensor[%d] : Tos register read out = 0x%04X\n", dev_num, iic_read_short( LM75B_ADDR | dev_num, Tos ) );
okano 0:ce7a8546502b 67 pc.printf( "sensor[%d] : Thyst register read out = 0x%04X\n", dev_num, iic_read_short( LM75B_ADDR | dev_num, Thyst ) );
okano 0:ce7a8546502b 68 }
okano 0:ce7a8546502b 69
okano 0:ce7a8546502b 70
okano 0:ce7a8546502b 71 float get_temp( char dev_num )
okano 0:ce7a8546502b 72 {
okano 0:ce7a8546502b 73 dev_num <<= 1;
okano 0:ce7a8546502b 74
okano 0:ce7a8546502b 75 return ( (float)(iic_read_short( LM75B_ADDR | dev_num, Temp )) / 256.0 );
okano 0:ce7a8546502b 76 }
okano 0:ce7a8546502b 77
okano 0:ce7a8546502b 78 void iic_write( char addr, char reg, char data )
okano 0:ce7a8546502b 79 {
okano 0:ce7a8546502b 80 char cmd[2];
okano 0:ce7a8546502b 81
okano 0:ce7a8546502b 82 cmd[ 0 ] = reg;
okano 0:ce7a8546502b 83 cmd[ 1 ] = data;
okano 0:ce7a8546502b 84
okano 0:ce7a8546502b 85 if ( i2c.write( addr, cmd, 2) )
okano 0:ce7a8546502b 86 iic_error();
okano 0:ce7a8546502b 87 }
okano 0:ce7a8546502b 88
okano 0:ce7a8546502b 89 char iic_read( char addr, char reg )
okano 0:ce7a8546502b 90 {
okano 0:ce7a8546502b 91 char cmd;
okano 0:ce7a8546502b 92 int nack;
okano 0:ce7a8546502b 93
okano 0:ce7a8546502b 94 cmd = reg;
okano 0:ce7a8546502b 95
okano 0:ce7a8546502b 96 nack = i2c.write( addr, &cmd, 1); // Send command string
okano 0:ce7a8546502b 97 nack |= i2c.read( addr, &cmd, 1); // Send command string
okano 0:ce7a8546502b 98
okano 0:ce7a8546502b 99 if ( nack )
okano 0:ce7a8546502b 100 iic_error();
okano 0:ce7a8546502b 101
okano 0:ce7a8546502b 102 return ( cmd );
okano 0:ce7a8546502b 103 }
okano 0:ce7a8546502b 104
okano 0:ce7a8546502b 105
okano 0:ce7a8546502b 106 short iic_read_short( char addr, char reg )
okano 0:ce7a8546502b 107 {
okano 0:ce7a8546502b 108 char cmd[ 2 ];
okano 0:ce7a8546502b 109 int nack;
okano 0:ce7a8546502b 110
okano 0:ce7a8546502b 111 cmd[ 0 ] = reg;
okano 0:ce7a8546502b 112
okano 0:ce7a8546502b 113 nack = i2c.write( addr, cmd, 1); // Send command string
okano 0:ce7a8546502b 114 nack |= i2c.read( addr, cmd, 2); // Send command string
okano 0:ce7a8546502b 115
okano 0:ce7a8546502b 116 if ( nack )
okano 0:ce7a8546502b 117 iic_error();
okano 0:ce7a8546502b 118
okano 0:ce7a8546502b 119 return ( ((short)cmd[ 0 ]) << 8 | cmd[ 1 ] );
okano 0:ce7a8546502b 120 }
okano 0:ce7a8546502b 121
okano 0:ce7a8546502b 122
okano 0:ce7a8546502b 123 void iic_error( void )
okano 0:ce7a8546502b 124 {
okano 0:ce7a8546502b 125 pc.printf( "I2C error\n" );
okano 0:ce7a8546502b 126
okano 0:ce7a8546502b 127 for ( int i = 0; i < 4; i++ )
okano 0:ce7a8546502b 128 led[ i ] = i & 0x01;
okano 0:ce7a8546502b 129
okano 0:ce7a8546502b 130 wait ( 0.2 );
okano 0:ce7a8546502b 131
okano 0:ce7a8546502b 132 for ( int i = 0; i < 4; i++ )
okano 0:ce7a8546502b 133 led[ i ] = !(i & 0x01);
okano 0:ce7a8546502b 134
okano 0:ce7a8546502b 135 wait ( 0.2 );
okano 0:ce7a8546502b 136 }
okano 0:ce7a8546502b 137