Revision 0:188e389bc1b7, committed 2010-01-23
- Comitter:
- okano
- Date:
- Sat Jan 23 13:45:32 2010 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 188e389bc1b7 I2cBusDevice.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/I2cBusDevice.h Sat Jan 23 13:45:32 2010 +0000
@@ -0,0 +1,58 @@
+/*
+ * I2C device base class
+ *
+ * A base class for all I2C devices.
+ * This manages the device address and transfers
+ *
+ * Copyright (c) 2010 Tedd OKANO
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * revision 1.0 15-Jan-2010 a. 1st release
+ * revision 1.1 23-Jan-2010 a. The word "MBED_I2cBusDevice" is used instead of _I2cBusDevice_ to avoid symbol conflict
+ * b. copyright notice added
+ */
+
+#ifndef MBED_I2cBusDevice
+#define MBED_I2cBusDevice
+
+#include "mbed.h"
+
+class I2cBusDevice {
+public:
+
+ I2cBusDevice( I2C *i2c, char dev_address ) {
+ bus = i2c;
+ device = dev_address;
+ }
+
+ ~I2cBusDevice() {
+ }
+
+ int write( char *data, int length ) {
+ return ( bus->write( device, data, length) );
+ }
+
+ int read( char *data, int length ) {
+ return ( bus->read( device, data, length) );
+ }
+
+ int read( char reg_ptr, char *data, int length ) {
+ if ( bus->write( device, ®_ptr, 1 ) )
+ return ( 1 );
+
+ if ( bus->read( device, data, length ) )
+ return ( 1 );
+
+ return ( 0 );
+ }
+
+protected:
+ I2C *bus;
+ char device;
+
+private:
+ static char i2c_error;
+}
+;
+
+#endif
diff -r 000000000000 -r 188e389bc1b7 TempSensor_LM75B.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TempSensor_LM75B.h Sat Jan 23 13:45:32 2010 +0000
@@ -0,0 +1,94 @@
+/*
+ * I2C digital temperature sensor "LM75B" library
+ *
+ * LM75B is an I2C based digital temperature sensor
+ * http://www.nxp.com/pip/LM75B_2.html
+ *
+ * This is a library to operate this chip easy.
+ *
+ * Copyright (c) 2010 Tedd OKANO
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * revision 1.0 16-Jan-2010 a. 1st release
+ * revision 1.1 23-Jan-2010 a. class name has been changed from LM75B to TempSensor_LM75B
+ * b. copyright notice added
+ */
+
+#ifndef MBED_TempSensor_LM75B
+#define MBED_TempSensor_LM75B
+
+
+#include "mbed.h"
+#include "I2cBusDevice.h"
+
+
+// LM75B IIC address
+const char LM75B_base_addr = 0x90;
+
+// LM75B registers
+const char Conf = 0x01;
+const char Temp = 0x00;
+const char Tos = 0x03;
+const char Thyst = 0x02;
+
+
+class TempSensor_LM75B : I2cBusDevice {
+public:
+
+ TempSensor_LM75B( I2C *i2c, char dev_address = LM75B_base_addr, char vConf = 0x00, short vTos = 0x5000, short vThyst = 0x4B00 ) : I2cBusDevice( i2c, dev_address ) {
+ char data[ 3 ];
+
+ data[ 0 ] = Conf;
+ data[ 1 ] = vConf;
+
+ if ( write( data, 2 ) )
+ ;
+
+ data[ 0 ] = Tos;
+ data[ 1 ] = (char)(vTos >> 8);
+ data[ 2 ] = (char)vTos;
+
+ if ( write( data, 3 ) )
+ ;
+
+ data[ 0 ] = Thyst;
+ data[ 1 ] = (char)(vThyst >> 8);
+ data[ 2 ] = (char)vThyst;
+
+ if ( write( data, 3 ) )
+ ;
+ }
+
+ ~TempSensor_LM75B() {
+ }
+
+ int temp_short( void ) {
+ char data[ 2 ];
+
+ if ( read( Temp, data, 2 ) )
+ return ( 1e6 );
+
+ return ( (((short)data[ 0 ]) << 8 | data[ 1 ]) >> 5 );
+ }
+
+ float temp( void ) {
+ return ( (float)(temp_short()) / 8.0 );
+ }
+
+ operator float( void ) {
+ return( temp() );
+ }
+
+#if 0
+ operator short( void ) {
+ return( temp_short() );
+ }
+#endif
+
+
+
+private:
+}
+;
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 188e389bc1b7 TextLCD_SB1602E.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD_SB1602E.h Sat Jan 23 13:45:32 2010 +0000
@@ -0,0 +1,239 @@
+/*
+ * LCD module "SB1602E" library
+ *
+ * SB1602E is an I2C based low voltage text LCD panel (based Sitronix ST7032 chip)
+ * The module by StrawberryLinux
+ * http://strawberry-linux.com/catalog/items?code=27002 (Online shop page (Japanese))
+ * http://strawberry-linux.com/pub/ST7032i.pdf (datasheet of the chip)
+ *
+ * This is a library to operate this module easy.
+ *
+ * Copyright (c) 2010 Tedd OKANO
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * revision 1.0 22-Jan-2010 a. 1st release
+ * revision 1.1 23-Jan-2010 a. class name has been changed from lcd_SB1602E to TextLCD_SB1602E
+ * b. printf() added
+ * c. copyright notice added
+ */
+
+#ifndef MBED_TextLCD_SB1602E
+#define MBED_TextLCD_SB1602E
+
+#include <stdarg.h>
+#include "mbed.h"
+#include "I2cBusDevice.h"
+
+// SB1602E IIC address
+
+const char SB1602E_addr = 0x7C;
+
+// SB1602E initialization command sequence
+
+#ifdef INIT_VALUE_DATASHEET_ORIGINAL
+
+const char Comm_FunctionSet_Normal = 0x38;
+const char Comm_FunctionSet_Extended = 0x39;
+const char Comm_InternalOscFrequency = 0x14;
+const char Comm_ContrastSet = 0x78;
+const char Comm_PwrIconContrast = 0x5E;
+const char Comm_FollowerCtrl = 0x6A;
+const char Comm_DisplayOnOff = 0x0C;
+const char Comm_ClearDisplay = 0x01;
+const char Comm_EntryModeSet = 0x06;
+
+#else
+
+const char Comm_FunctionSet_Normal = 0x38;
+const char Comm_FunctionSet_Extended = 0x39;
+const char Comm_InternalOscFrequency = 0x14;
+const char Comm_ContrastSet = 0x70;
+const char Comm_PwrIconContrast = 0x5C;
+const char Comm_FollowerCtrl = 0x60;
+const char Comm_DisplayOnOff = 0x0C;
+const char Comm_ClearDisplay = 0x01;
+const char Comm_EntryModeSet = 0x04;
+const char Comm_ReturnHome = 0x02;
+
+#endif
+
+// SB1602E general commands
+
+const char Comm_SetDDRAMAddress = 0x80;
+const char DDRAMAddress_Ofst[] = { 0x00, 0x40 };
+
+const char Comm_SetCGRAM = 0x40;
+
+// SB1602E setting values
+
+const char default_Contrast = 0x35;
+
+const char COMMAND = 0x00;
+const char DATA = 0x40;
+
+const char MaxCharsInALine = 0x10; // buffer deoth for one line (no scroll function used)
+
+const char init_seq0_length = 7;
+const char init_seq0[ init_seq0_length ]
+= {
+ Comm_FunctionSet_Normal,
+ Comm_ReturnHome, // This may be required to reset the scroll function
+ Comm_FunctionSet_Extended,
+ Comm_InternalOscFrequency,
+ Comm_ContrastSet | ( default_Contrast & 0xF),
+ Comm_PwrIconContrast | ((default_Contrast >> 4) & 0x3),
+ Comm_FollowerCtrl | 0x0A,
+
+};
+// required 30us interval
+
+const char init_seq1_length = 3;
+const char init_seq1[ init_seq1_length ]
+= {
+ Comm_DisplayOnOff,
+ Comm_ClearDisplay,
+ Comm_EntryModeSet,
+};
+// required 30us, 2ms interval
+
+
+class TextLCD_SB1602E : I2cBusDevice {
+public:
+
+ explicit TextLCD_SB1602E( I2C *i2c, char dev_address = SB1602E_addr, char *init_massage = NULL ) : I2cBusDevice( i2c, dev_address ) {
+ wait( 0.04 ); // interval after hardware reset
+
+ for ( int i = 0; i < init_seq0_length; i++ ) {
+ lcd_command( init_seq0[ i ] );
+ wait( 30e-6 );
+ }
+
+ wait( 0.2 );
+
+ for ( int i = 0; i < init_seq1_length; i++ ) {
+ lcd_command( init_seq1[ i ] );
+ wait( 2e-3 );
+ }
+
+ if ( init_massage )
+ puts( 0, init_massage );
+
+ set_CGRAM( 7, '\x1F' );
+
+ curs[ 0 ] = 0;
+ curs[ 1 ] = 0;
+ }
+
+
+ ~TextLCD_SB1602E() {
+ }
+
+ void clear( void ) {
+ lcd_command( Comm_ClearDisplay );
+ wait( 2e-3 );
+ curs[ 0 ] = 0;
+ curs[ 1 ] = 0;
+ }
+
+
+ void put_custom_char( char c_code, const char *cg, char x, char y ) {
+ for ( int i = 0; i < 5; i++ ) {
+ set_CGRAM( c_code, cg );
+ putcxy( c_code, x, y );
+ }
+ }
+
+ void contrast( char contrast ) {
+ lcd_command( Comm_FunctionSet_Extended );
+ lcd_command( Comm_ContrastSet | (contrast & 0x0f) );
+ lcd_command( Comm_PwrIconContrast | ((contrast>>4) & 0x03) );
+ lcd_command( Comm_FunctionSet_Normal );
+ }
+
+ void set_CGRAM( char char_code, const char* cg ) {
+ for ( int i = 0; i < 8; i++ ) {
+ lcd_command( (Comm_SetCGRAM | (char_code << 3) | i) );
+ lcd_data( *cg++ );
+ }
+ }
+
+ void set_CGRAM( char char_code, char v ) {
+ char c[ 8 ];
+
+ for ( int i = 0; i < 8; i++ )
+ c[ i ] = v;
+
+ set_CGRAM( char_code, c );
+ }
+
+ void putcxy( char c, char x, char y ) {
+ if ( (x >= MaxCharsInALine) || (y >= 2) )
+ return;
+
+ lcd_command( (Comm_SetDDRAMAddress | DDRAMAddress_Ofst[ y ]) + x );
+ lcd_data( c );
+ }
+
+ void putc( char line, char c ) {
+ if ( (c == '\n') || (c == '\r') ) {
+ clear_lest_of_line( line );
+ curs[ line ] = 0;
+ return;
+ }
+
+ putcxy( c, curs[ line ]++, line );
+ }
+
+ void puts( char line, char *s ) {
+ while ( char c = *s++ )
+ putc( line, c );
+ }
+
+ void printf( char line, char *format, ... ) {
+ char s[ 32 ];
+ va_list args;
+
+ va_start( args, format );
+ vsnprintf( s, 32, format, args );
+ va_end( args );
+
+ puts( line, s );
+ }
+
+private:
+ char curs[2];
+
+ void clear_lest_of_line( char line ) {
+ for ( int i = curs[ line ]; i < MaxCharsInALine; i++ )
+ putcxy( ' ', i, line );
+ }
+
+ int lcd_write( char first, char second ) {
+ char cmd[2];
+
+ cmd[ 0 ] = first;
+ cmd[ 1 ] = second;
+
+ return ( write( cmd, 2 ) );
+
+ }
+
+ int lcd_command( char command ) {
+ return ( lcd_write( COMMAND, command ) );
+ }
+
+ int lcd_data( char data ) {
+ return ( lcd_write( DATA, data ) );
+ }
+}
+;
+
+#endif
+
+
+
+
+
+
+
+
diff -r 000000000000 -r 188e389bc1b7 mbed.bld
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld Sat Jan 23 13:45:32 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/49a220cc26e0
diff -r 000000000000 -r 188e389bc1b7 temp_meter.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/temp_meter.cpp Sat Jan 23 13:45:32 2010 +0000
@@ -0,0 +1,65 @@
+/*
+ * I2C digital temperature sensor "LM75B" test
+ *
+ * LM75B is an I2C based digital temperature sensor
+ * http://www.nxp.com/pip/LM75B_2.html
+ *
+ * This program is Expecting to use the pins 9 and 10 for I2C bus
+ * these pins should be pulled-up properly.
+ *
+ * The temperature read out will be shown on terminal on the PC screen.
+ *
+ * In this demo code, two LM75B devices can be driven.
+ * These two devices should have different I2C address setting
+ * using its address pins (LM75B's A0 to A2 (pins 5 to 7)).
+ * One LM75B should have all those pins tied to GND.
+ * And another should have the pin A0(pin7) pulled-up.
+ *
+ * From the software, those devices can be accessed by I2C addresses
+ * "0x90" and "0x92".
+ * It will not be as "0x90" and "0x91" because the address has
+ * 7 bit only and stuffed to left. So the "A0" setting become 0xX2.
+ * The LSB does not care because it will be set by I2C libraly when
+ * it transfer the data for read and write.
+ *
+ * This code is new version that can handle the LM75B are the objects.
+ *
+ * Copyright (c) 2010 Tedd OKANO
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * revision 1.0 16-Jan-2010 a. 1st release
+ * revision 1.1 23-Jan-2010 a. class name has been changed from LM75B to TempSensor_LM75B
+ * b. class name has been changed from lcd_SB1602E to TextLCD_SB1602E
+ * c. copyright notice added
+ */
+
+#include "mbed.h"
+#include "TempSensor_LM75B.h"
+#include "TextLCD_SB1602E.h"
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+I2C i2c( p9, p10 ); // sda, scl
+
+TempSensor_LM75B thermo_sensor_0( &i2c ); // sensor_0 using with default I2C address "0x90".
+TempSensor_LM75B thermo_sensor_1( &i2c, 0x92 ); // sensor_1 gaved I2C address since that address pin A0=HIGH.
+TextLCD_SB1602E lcd( &i2c );
+
+int main() {
+ float t0;
+ float t1;
+ int i = 0;
+
+ while (1) {
+ t0 = thermo_sensor_0;
+ t1 = thermo_sensor_1;
+
+ pc.printf( " (%d) sensor_0= %4.1f\xDF, sensor_1= %4.1f(degree-C)\n", i++, t0, t1 );
+
+ lcd.printf( 0, "sensor0 %4.1f%cC\r", t0, 0xDF );
+ lcd.printf( 1, "sensor1 %4.1f%cC\r", t1, 0xDF );
+
+ wait( 1 );
+ }
+}
+