Paul Thomsen – Technology & Science

my thoughts on science and (mostly) computer-related technologies and technologies other than computer management

The importance of locking your U421 while using timed events November 15, 2010

Filed under: programming,U421 — Paul Thomsen @ 9:56 pm

My U421’s have been working well for me but as my solutions got more sophisticated they got less reliable. In particular, I was outputting to an LCD, outputting to an LED, and checking (inputting from) a switch at the same time (using multiple timers). But at seemingly random times my application would crash. It makes sense that I should only do one thing with the device at a time, but all my solutions failed.

I tried suspending the conflicting timers as operations were done, but that only partially helped. Setting a global variable didn’t help at all. Toggling only the relevant bits (“WriteABit”) didn’t help either.

Of course I didn’t expect I was the first person to hit such an issue so I did some research and found that the solution for C# (and similar languages, I assume) is to use locks – an old concept for anyone using database servers, clusters, or other complex systems.

In particular, I changed my code to create a generic object:

object U421_lock = new object();

and then later whenever doing anything U421 related I wrapped the code with:

lock( U421_lock )
{
       // do stuff
}

 

Getting started using LCD displays from a Windows PC using the U421 October 10, 2010

Filed under: electronics,programming,U421 — Paul Thomsen @ 10:30 pm

Full function monitors are admittedly cheap these days (roughly $100 on the low end), so using an LCD display from a PC has little value. But you can build an LCD solution for $60 and the information on it will be visible even when your screensaver is active or the machine is locked. They also allow you to more immediately focus on the information you display on them (as opposed to the multiple windows on your monitors).

Assuming you have a need to use an LCD display from your Windows PC, here’s a solution that will work:

The strength of the U421 in this case is that it has LCD APIs, so you don’t have to worry about the subtleties of timing the data transfer and coordinating RS, R/W, and E.

The wiring is fairly simple:

  • B data lines to the LCD data lines (i.e. B0 to DB0, B1 to DB1, etc.)
  • A data lines to the RS/Rw/E lines (A0 to Rw, A1 to Rs, A2 to E)
  • power lines as appropriate, except nothing to VDD (unless you want to set up a control for contrast)

Preparation for the coding is:

  • copy the USBMicro USBm.dll into the bin\debug folder of the application
  • add “using System.Runtime.InteropServices” to the ‘using’ part of your code
  • add the public class USBm from the USBMicro sample code to your application

The key code is:

result = USBm.USBm_FindDevices();
Device = 0; // assuming there’s only one device
USBm.USBm_DirectionA(Device, 0xFF, 0xFF); // output only
USBm.USBm_DirectionB(Device, 0xFF, 0xFF);
result =
USBm.USBm_InitLCD(Device, 0x01, 0x12); // A.0 is the RW line, A.1 is the RS line; data port B, A.2 is the E line
result = USBm.USBm_LCDCmd(Device, 0x30 + 8); // function set: 8 bit; 2 lines, font 0
result = USBm.USBm_LCDCmd(Device, 0x04); // entry mode – not incrementing, in either direction
result = USBm.USBm_LCDCmd(Device, 0x0C); // display on; no cursor, no blinking
result = USBm.USBm_LCDCmd(Device, 0x01); // clear display

output[0] = “initialized”;
for (int i = 0; i < output[0].Length; i++)
    result =
USBm.USBm_LCDData(Device, Convert.ToByte(output[0][i]));

p.s. There’s a key difference between the LCD-20x4Y (black on green) and LCD-20x4BW (black on white) displays – the second line starts at a different DDRAM address. Use 64 rather than 40.

 

Using a combobox value in C#

Filed under: programming — Paul Thomsen @ 11:45 am

This is probably self evident to anyone that’s been programming in C# (and thus the .NET Framework) for any length of time, but for a newbie it’s harder than I would have expected.

I was expecting it be something like:

uint value;
value = (uint)combobox1.SelectedValue.Content;

Instead, it’s:

value = Convert.ToUint32( ((ComboBoxItem)comboBox1.SelectedValue).Content.ToString() );

That’s non-trivial. So what does it mean:

  • cast the combBox1.SelectedValue, which is a generic object, to a ComboBoxItem object. Then you can use the Content property. This is the main trick, and be sure you get the parentheses right.
  • IntelliSense is not intelligent enough to show you the options for a cast object so you have to know that Content is an available property for a ComboBoxItem. That’s not hard to find but it would be nice if IntelliSense helped here – I suspect I’m going to have this problem with other UI objects.
  • But the Content property is an object itself, so you have to use the ToString() method to get the value.
  • There’s no ToUint method, so you have to use the Convert object’s ToUint32() method

p.s. What’s worse is that I couldn’t find a sample on the Internet, despite quite a lot of searching. There’s plenty of examples of adding values to a combobox or changing the style, but nothing this basic. I had to revert to using a book (egad!) and even then the sample code only indirectly clarified it.