You are reading a single comment by @RFPlus and its replies. Click here to read the full conversation.
  • I think I've understood the problem ... it's a speed/timing issue. Maybe my LCDs are running a little slow, certainly the processor at 168MHz is fast. I started getting a response from the LCD when I built a small init routine in the IDE.

    After some playing I moved the HD44780.js module into the IDE right-hand pane and it worked(!) essentially unmodified. However, after a save() and restart, the display corrupted. I think it's because the timing on the write() routine is too quick for the LCD interface when it's minified. I changed the digitalWrite() to digitalPulse() on the 'en' line and it now works fairly reliably, even standalone after a save().

    It's a hack ... that it works at all is probably coincidental. There's work to be done on ensuring the timing constraints on the HD44780 are always met, regardless of processor speed.

    var exports={};
    function HD44780J(write) {
      // initialise
      write(0x33,1);
      write(0x32,1);
      write(0x28,1);
      write(0x0C,1);
      write(0x06,1);
      write(0x01,1);
      // add functions
      return {
        write : write,
        // clear screen
        clear : function() { write(0x01,1); },
        // print text
        print : function(str) {
          for (var i=0;i<str.length;i++)
            write(str.charCodeAt(i));
        },
        // set cursor pos, top left = 0,0
        setCursor : function(x,y) { var l=[0x00,0x40,0x14,0x54];write(0x80|(l[y]+x),1); }
      };
    }
    
    
    exports.connect = function(_rs,_en,_d4,_d5,_d6,_d7)  {
      var data = [_d7,_d6,_d5,_d4];
      var rs = _rs;
      var en = _en;
      digitalWrite(rs, 1);
      digitalWrite([rs,en], 0);
      var write = function(x, c) 
        {
        digitalWrite(rs, !c);
        digitalWrite(data, x>>4);
    //    digitalWrite(en, 1);     Original pulse
    //    digitalWrite(en, 0);
        digitalPulse(en,1,1);    // 1ms timed pulse
        digitalPulse(en,1,0);    // wait for pulse end
        digitalWrite(data, x);
    //    digitalWrite(en, 1);       Original  pulse
    //    digitalWrite(en, 0);
        digitalPulse(en,1,1);     // 1ms timed pulse
        digitalPulse(en,1,0);    // wait for pulse end
        };
      return new HD44780J(write);
      };
    
    var lcd;
    var sensor;
    
    setInterval(function()  {
      //lcd.clear();
      lcd.setCursor(7,1);
      lcd.print(" "+sensor.getTemp());
      }, 1000);
    
    function onInit()  {
      // Set up display and sensor
      lcd = exports.connect(A3,A1,C0,C1,C2,C3);
      lcd.clear();
      lcd.setCursor(1,1);
      lcd.print("Temp =");
      lcd.setCursor(14,1);
      lcd.print("C");
      lcd.setCursor(0,0);
      lcd.print("* RFPlus  2014 *");
        
      var ow = new OneWire(A2);
      sensor = require("DS18B20").connect(ow);
      sensor.setRes(10);  // 10 bit mode
      }
    
    
    onInit();
    
About

Avatar for RFPlus @RFPlus started