I2C

Dependencies:   TextLCD mbed

Fork of I2C_Debug_for_RealTerm by jim hamblen

Revision:
3:8e2db72d0d7d
Parent:
2:fa8c916b42a9
--- a/main.cpp	Mon Mar 14 00:50:34 2011 +0000
+++ b/main.cpp	Mon Feb 29 18:57:56 2016 +0000
@@ -1,17 +1,10 @@
 #include "mbed.h"
-// The program sends and receives I2C commands using RealTerm's I2C tab feature
-// Run Realterm on PC and this program on mbed
-// In RealTerm connect to the mbed's USB virtual COM port
-// Switch to I2C tab
-// Type in I2C address & data to read and write and see the response
-// It is handy to debug complex I2C hardware setups
-// Prints "No Ack!" when no I2C device responds to the address
-//
-// See Instructions at http://mbed.org/users/4180_1/notebook/i2c-debug-for-realterm/
-//
+#include "TextLCD.h"
+
 DigitalOut myled(LED1);
 Serial pc(USBTX, USBRX);
 I2C i2c(p9,p10);
+TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x2);        // 4bit bus: rs, e, d4-d7
 
 int main() {
     char last_command=0;
@@ -33,7 +26,7 @@
     i2c.start();
     i2c.stop();
     // Send a start message to RealTerm
-    pc.printf("\fmbed I2C debug tool ready\n\r");
+    lcd.printf("\fmbed I2C debug tool ready\n\r");
     // Scan for I2C devices that reply with ack
     for (i=0; i<=254; i=i+2) {
         if (i2c.read(i, &data[0], 1) ==0) printf("I2C device detected at address=%2.2X\n\r", i);
@@ -50,14 +43,14 @@
             if (current_char <='9') current_byte = current_char - '0';
             else current_byte =current_char +10 -'A';
             current_char = pc.getc();
-            if (current_char >'F') pc.printf("error|\n\r");
+            if (current_char >'F') lcd.printf("error|\n\r");
             if (current_char <='9') current_byte = (current_byte <<4) | (current_char - '0');
             else current_byte = (current_byte <<4)|(current_char +10 -'A');
             // first byte after S command is the I2C address
             if (first_data_byte==1) {
                 first_data_byte=0;
                 address=current_byte;
-                pc.printf(" -I2C address=%2.2X ", address);
+                lcd.printf(" -I2C address=%2.2X ", address);
                 //Odd I2C address is a read and even is a write
                 if ((address & 0x01) == 0) write=1;
                 else read=1;
@@ -82,21 +75,21 @@
                 case 'P':
                     // Do the I2C write
                     if (write==1) {
-                        pc.printf(" write ");
-                        if (i2c.write(address,cmd,cmd_count)!=0) pc.printf(" No Ack! ");
+                        lcd.printf(" write ");
+                        if (i2c.write(address,cmd,cmd_count)!=0) lcd.printf(" No Ack! ");
                         for (i=0; i<cmd_count; i++)
-                            pc.printf(" cmd=%2.2X ", cmd[i]);
+                            lcd.printf(" cmd=%2.2X ", cmd[i]);
                     }
                     // Do the I2C read
                     if (read==1) {
-                        pc.printf(" read ");
-                        if (i2c.read(address, data, data_count) != 0) pc.printf(" No Ack! ");
+                        lcd.printf(" read ");
+                        if (i2c.read(address, data, data_count) != 0) lcd.printf(" No Ack! ");
                         for (i=0; i<data_count; i++)
-                            pc.printf(" data=%2.2X ",data[i]);
+                            lcd.printf(" data=%2.2X ",data[i]);
                     }
                     // reset values for next I2C operation
                     i2c.stop();
-                    pc.printf("\n\r");
+                    lcd.printf("\n\r");
                     read=0;
                     data_count=0;
                     cmd_count=0;
@@ -109,11 +102,11 @@
                     break;
                     // Status request
                 case '?':
-                    pc.printf(" mbed ready \n\r");
+                    lcd.printf(" mbed ready \n\r");
                     break;
                     // Unknown or unimplemented command
                 default:
-                    pc.printf(" unknown command \n\r");
+                    lcd.printf(" unknown command \n\r");
                     break;
             }
             previous_command = last_command;