FEZ Panda II with HD44780 controlled LCD

Panda II with HD44780 LCDSecond test: displaying text on a 20×4 LCD controlled by a HD44780 compatible driver chip. As you can see, it’s working ūüôā

After Eric VDB pointed me to Codeshare¬†in a comment on a previous post, I couldn’t resist searching for something that could help me get started.

And I found something useful here. Although the example that came with it was targeted for a¬†Gadgeteer Extender Module (which I don’t have), I had no problems getting it to work. I created an additional constructor that would accept pin numbers, like this:

SerialLCDDisplay lcd = new SerialLCDDisplay(
                     FEZ_Pin.Digital.Di21, // RS
                     FEZ_Pin.Digital.Di23, // ENABLE
                     FEZ_Pin.Digital.Di25, // DB4
                     FEZ_Pin.Digital.Di27, // DB5
                     FEZ_Pin.Digital.Di29, // DB6
                     FEZ_Pin.Digital.Di31); // DB7

It took me some time to figure that out, but within the hour I had the LCD running. I added an endless loop with an increasing counter and a delay of 1 second and let the sketch program run; at the time I took the picture it had been running for more than 4 hours.

One thing I noticed was that the LCD sometimes displayed garbage when the Panda was rebooted; after that everything worked OK, but startup looked a bit ugly. Maybe give the hardware connected to the Panda some more time to settle? I don’t know yet; we’ll see about that later. Another thing I need to have a look at is the fact that this SerialLCDDisplay class was apparently written for an LCD with 2 lines; cause line 0 (first line on the LCD) and 1 (second line on the LCD) were OK, but output destined for line 2 were shown on the first line of the LCD again – probably a minor issue.

And since I’ll have to change the class code for that anyway, I’ll also add 2 additional parameters with which I can set the number of rows and columns of the LCD – cause I have both 20×4 as well as 16×2 LCDs!

Oh, and I saw these automatically generated Debug messages appearing in the Debug output window in Visual Studio on my PC:

GC: 4msec 12132 bytes used, 52248 bytes available
Type 0F (STRING              ):     96 bytes
Type 11 (CLASS               ):   1596 bytes
Type 12 (VALUETYPE           ):     60 bytes
Type 13 (SZARRAY             ):    108 bytes
Type 15 (FREEBLOCK           ):  52248 bytes
Type 17 (ASSEMBLY            ):   7560 bytes
Type 18 (WEAKCLASS           ):     48 bytes
Type 19 (REFLECTION          ):     24 bytes
Type 1B (DELEGATE_HEAD       ):     72 bytes
Type 1D (OBJECT_TO_EVENT     ):    168 bytes
Type 1E (BINARY_BLOB_HEAD    ):    156 bytes
Type 1F (THREAD              ):    384 bytes
Type 20 (SUBTHREAD           ):     48 bytes
Type 21 (STACK_FRAME         ):    828 bytes
Type 27 (FINALIZER_HEAD      ):    168 bytes
Type 31 (IO_PORT             ):    252 bytes
Type 34 (APPDOMAIN_HEAD      ):     72 bytes
Type 36 (APPDOMAIN_ASSEMBLY  ):    492 bytes

This must be some diagnostic information from the GC process kicking in.¬†12 KB used? Woow… that’s 20%. But what’s the baseline of used memory, e.g. what does a program with an ’empty’ main() use in terms of memory? Sounds like a good subject for another post..

First step: FEZ Panda II with LuxPlug

FEZ Panda II with LuxPlugLast evening I created my first program for the FEZ Panda II; I attached a JeeLabs LuxPlug to it and just started trying.

I found a tutorial¬†on how to do I2C with the .Net MF and used that one as a starting point. It didn’t take long before I found out that for me, in this case, it was easier to rely on the TSL2561 datasheet than to try to use someone else’s code to create my first C# class for this sensor.

Probably because this first try was an easy one; I guess in the future, when things become more complicated, I’ll have to take some peeks at how others do certain things. But for now, I’m pleased with how this first exercise worked out!

The code is not finished yet, but it’s in a stage where it works and produces the raw data coming from the data registers. Good enough for a post ūüôā

Here’s a small piece (the constructor part) of the class code:

public TSL2561(byte I2CAddress, int ClockinKHz)
{
  I2CConfig = new I2CDevice.Configuration(I2CAddress, ClockinKHz);
  I2C = new I2CDevice(I2CConfig);

  // read the ID register.
  var Actions = new I2CDevice.I2CTransaction[2];
  byte[] rx = new byte[1];
  Actions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x0a });
  Actions[1] = I2CDevice.CreateReadTransaction(rx);
  if (I2C.Execute(Actions, 1000) == 0)
  {
    Debug.Print("Read ID Register failed");
    // exit or something
  }
  else
  {
    Debug.Print("ID value: " + rx[0].ToString());
  }
  // 4 msb must be 0001 for a TSL2561
  if ((rx[0] & 0xf0) != 0xa0)
  {
    // exit or something
  }

  setGain(0x10);
  Thread.Sleep(5);   // Mandatory after each Write transaction !!!
}

Not that bad if I may say so…

The sensor is being read every 5 seconds and this produces the following output, where it shows the decimal values of low and high bytes of both ADC channels:

--------------
Result: 156 1
Result: 69 5
--------------
Result: 160 1
Result: 80 5
--------------
Result: 174 1
Result: 114 5
--------------
Result: 177 1
Result: 119 5
--------------

Nice! Calculations with these numbers is something that’s on the¬†to-do¬†list for now, but that should not give any big problems anymore; no, but what I think will be the biggest obstacle to overcome is my knowledge of C#.

For example, I’m used to displaying numbers in hexadecimal format. So I added a x.ToString(“X2”) (where x is a byte) in a Debug.Print(), knowing I’ve used this before myself in VB.Net and have seen this being used in C# too – I got no build errors, but I did get an exception… apparently the .Net Micro Framework doesn’t support this? I have to press F1 for help too much for my taste.. ūüėČ A¬†matter of learning on the job I guess..

Now let’s see if I can find that 20×4 LCD for my next ‘project’; and maybe a spare LCD Plug, to keep the number of wires acceptable.

Arduino on steroids and .Net Micro Framework

FEZ Panda II compared to Duemilanove

Another gadget to tinker with arrived yesterday; a  FEZ Panda II from GHI Electronics. Arduino pin compatible, compatible with most Arduino shields, 72 MHz 32-bit CPU, 512 KB flash, 62 KB RAM for application, Micro SD socket, RTC and more, much more.

It’s a bit bigger though; the length is about ¬†10 mm more than the Duemilanove.

The FEZ Panda II runs on the .NET Micro Framework and you can¬†program (and even debug) the Panda with Microsoft Visual C# Express (free). Deployment to the Panda is done from within the Visual Studio IDE, and setting breakpoints, stepping and adding watches all (seem to) work. I got a ‘blinking LED’ example up and running in no-time – downloading and installing the .NET Micro Framework 4.1 and the GHI NETMF 4.1 cost me more time than getting the LED to blink; so far this looks OK.

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;

namespace MFConsoleApplication1
{
  public class Program
  {
    public static void Main()
    {
      OutputPort LED;
      LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, true);
      while (true)
      {
          LED.Write(true);
          Thread.Sleep(20);
          LED.Write(false);
          Thread.Sleep(2000);
      }
    }
  }
}

Why did I buy this FEZ Panda II? Well, for the fun of it ofcourse! And second, I wanna know more about these kind of devices and check some of the statements made.

For instance, the statement that the Panda is compatible with most Arduino shields intrigues me; to what extent is this true? “The shield fits in the headers” is not enough ofcourse; most shields come with a library to support the hardware that’s on the shield, but those libraries are for the Arduino platform, so how easy is it to plug in a Arduino shield and get it ¬†really working? Can, and if so, how hard is it to port Arduino code to this C# environment?

Another thing is: how does the .NET runtime influence time critical I/O, where output pins have to be switched very fast? How does this Panda perform, knowing there’s an additional layer (the CLR, Common Language Runtime) between the C# code and the bare hardware?

And how about memory usage? 62 KB user memory may sound like a lot compared to the Arduinos 2 KB. OTOH, when I compare my VB.Net touchscreen application to my Delphi Domotica system, the memory usage of the first one is huge compared to the 1.3 MB my Domotica system uses. So what will using a .Net environment do in this particular case? Will adding a few hardware drivers to a C# program quickly make the available memory vanish, or what?

And I’m also interested in how I¬†will perform with C#; I hope that seeing/writing code in all kinds of programming languages from the early 80’s will help me to get started ūüėČ

Questions, questions… which I hope to be able to answer in the next weeks; I was getting bored with watching that blinking LED on the Panda II, so I decided my first “Panda project” will be an I2C¬†exercise¬†with a JeeLabs Lux Plug to see how hard (or easy) it is to talk I2C with this sensor. And if all goes well, next thing will be to write some text on a spare 20×4 LCD I have one laying around. Those sound like some good beginner projects, right?

And if both succeed, the 2.8″ TFT LCD Touchscreen I bought recently, will be next.

That’s it for now; new challenges are waiting!