Paul Thomsen – Technology & Science

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

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.

 

Getting electronics right (or not) May 31, 2010

Filed under: electronics — Paul Thomsen @ 8:43 pm
As stated earlier in this blog, I like the FT232R – it’s simple and yet flexible. But it turns out it does have limits. In particular, it seems it can’t drive what I would call ‘synchronous devices’ (or smart devices that require synchronization to work properly). An example is any kind of HD44780-driven LCD display.
 
More to the point is that for any kind of interesting electronics, a lot of things have to come together just right. Examples:
 
  • breadboard wiring – this might seem simple, but it’s easy to get the alignment wrong, especially if you’re connecting wire to wire on the breadboard
  • voltage – is VSS, VEE, VCC, or similar variances positive, negative, or ground? I’m sure professionals always know the answer, but what about the rest of us?
  • E, RS, R/W, etc. – which ones are important, what do they mean? To some degree it depends on what you’re doing – for example, are you doing any reading amongst the writing?
  • timing – should you toggle the data before or after E goes high (or low)? How soon before? How fast should the transition be made?
  • pull-down-resistors and grounding unused pins – how important are they? I’m not convinced on these two. They might help with the speed or cleanness of the transitions, but I haven’t seen the data or results to prove their importance. This is an open question for me.
  • programming commands (including sequence) – are they working or not? You know they’re working if the output is what you expect, but if you’re not getting the right output: is it the commands (or their sequence), or is it one of the above elements? Unlike traditional programming, you can’t (usually) run a debugger on the destination device to ensure the data is getting through
  • soldering, if need be

How to minimize these problems:

  • double and triple check the wiring, including using a magnifying glass, and ensure the wiring is properly aligned, vertically or horizontally
  • put LEDs in place of the circuit to ensure the lines at least get toggled as much as you expect, and in the patterns you expect (randomly, lots at the beginning of the command sequence, lots at the end, etc.)
  • try variations. This is easiest with the programming but can apply to the other points. Even if something surprising happens, that could be a clue as to what’s wrong
  • read the documentation carefully. On the other hand, don’t be surprised if there are errors. Most specs are relatively obscure documents that don’t go through a lot of professional editing or peer review
  • use a voltmeter to ensure that pins are isolated from each other as much as they should
 

On the importance of FT232 SetBitMode April 7, 2010

Filed under: electronics,FT232R — Paul Thomsen @ 9:30 pm

I had a nice working FT232 program that was doing well for days. I restarted it often, rewired, reconnected the device, etc. and it was working great. Then I came back a week later and it didn’t work! What could have happened? Loose wires would seem obvious, but that didn’t check out. Fried chip? I hoped not but swapped them out. Bad breadboard? That was a longshot but I was getting desperate. Forgotten last minute code change? I couldn’t see it. What could the issue be?

Strangely, other programs worked fine with the same or similar components. Then I minimalized the circuit and code and it still behaved strangely (wouldn’t even toggle a single bit). Finally it occurred to me that my code no longer set the bit mode before each write. At one point I had assumed that could be rarely done and the code would work, and maybe that would speed things a bit. And generally that seems to be true, as evidenced by my earlier experience. But under some circumstances (I’m not sure what, but something to do with running other programs, I suppose), this detail can become important.

So the conclusion is: to be safe, set the bit mode before your writes:

SM.bitBang_SetBitMode(ftHandle, 15, 0x1); // use the first 4 bits of the D bus

 

What electronics can you add to your PC using the FT232? March 27, 2010

Filed under: electronics,FT232R — Paul Thomsen @ 6:34 am

Your imagination is probably the most limiting constraint, but here’s some ideas I have:

  • pretty lights
    • flashing LEDs, in multiple colors. Maybe in strobing patterns, etc.
    • Ok, this is not particularly beneficial, but it is fun and will impress your friends
  • readable displays
    • one 7 segment digit – ok, this is quite limiting
    • two 7 segment digits – temperature, CPU performance, etc…
    • four 7 segment digits – time of day, delta time, stock price, etc…
    • simple LCD display – status of your presentation, new e-mails or RSS postings, server status, etc…
    • note that the benefit of a display attached to your PC is that you can use your PC display for its primary purpose (doing a presentation, for example)
  • sensors
    • compass – what direction is your laptop pointed in?
    • accelerometer – control a 3D display, point at things, ….
    • temperature – especially from the outside while you’re inside. Or to determine if you’re overheating your house, …
  • input devices
    • switches could be used to monitor the state of doors, windows, etc.
  • interfaces
    • get input from some old but useful device that isn’t compatible with your current PC
 

Why program electronics from Windows? March 26, 2010

Filed under: electronics — Paul Thomsen @ 10:05 pm

That’s a fair question – if you need a device with certain features then chances are you can buy it. The Windows market is huge and thus practically everything has been addressed by some vendor, or soon will be. But there are a few counter-arguments:

  • it’s fun and good to learn programming, but a challenge can be to find fun things to program. The world is full of games, web apps, gadgets, etc. so you can easily search for something that will almost certainly do a better job of whatever you might try to program. So it’s hard to feel ‘instant gratification’ from normal programming efforts. But if your program controls something physical, like an external display, accelerometer, other sensor, or robot, you really feel like you’ve done something special
  • for some reason most computers don’t include sensors or other hardware that even smartphones have. So you might feel like you’re PC is missing out even though it’s amazingly easier to use and more powerful. So why not supplement your hardware and program the PC to use that supplemental hardware?
  • you might invent something useful! To start with you’ll be pleased to toggle an LED, but you can soon evolve to more interesting things. Eventually you might combine an interesting set of sensors and/or displays and/or interfaces to do something actually practical. And since it works with Windows it can amplify the value of the great programs and scenarios that are used with Windows
  • electronics are amazingly powerful and cheap these days. You can do some cool stuff for very little cost
  • anyone can program a cute window, but how many programmers can get their programs to interact with the physical world?
  • if you love electronics and want to do sophisticated stuff with them you would normally have to use microcontrollers and microcontroller programming techniques. That’s great if you’re going to be an electronics engineer but otherwise you’ll spend most of your time developing arcane skills that aren’t beneficial beyond your hobby. However, if you can program the electronics from Windows then you get all the benefits of the electronics but also learn lots of general-purpose Windows programming skills (in my case .NET Framework, related languages, WPF, Silverlight, etc.). So you’ve got skills that you can use at work or elsewhere as well.
 

In praise of the FT232

Filed under: electronics,FT232R — Paul Thomsen @ 9:45 pm

Do you like programming? Do you like electronics? Do you like Windows? Wouldn’t it be nice to combine all three?! That’s what I was thinking when I went looking for a solution that would maximize my motivation for evolving my Windows programming skills.

You might think that should be easy enough to manage – there’s plenty of electronics in the world, and plenty of programming options, and Windows is almost universal, so how hard could it be? But the problem with the world of electronics is that they seem to assume you’re doing it on a large scale and almost certainly have electronic engineering training. Either that or you’re a kid and thus are impressed by a few LEDs, resisters, and the simplest of integrated circuits.

So you can find lots of simple kits, and lots of standalone electronics solutions (especially based on microcontrollers). If Windows is involved it’s to help design printed circuit boards or to download microcontroller programs to the microcontrollers. Windows programming isn’t an option.

A further complication is that modern computers only have USB ports (not RS-232 or parallel ports), and thus electronics solutions built more than 5 years ago are almost impossible to use. And you’ll need a power supply, which means more wires and/or batteries, which only messes things up.

So go ahead and search for good solutions. You’ll find various close alternatives, but I only found one that really meets the above key requirements. Fortunately I stumbled upon http://www.smileymicros.com/, which has what they call the “USB thingie” or “BBUSB” (breadboard USB). It’s the FTDI FT232R chip mounted on a little breadboard-compatible circuit board and a USB connector. By default it offers RS-232 output (for those old solutions) but also an 8-bit “D” bus and a 4-bit “C” bus. And it has 5 volt and 3.3 volt power for your components. And it’s compatible with lots of operating systems. So you’re all set!

SmileyMicros is clearly a small company, and my impression is that they built a good set of products a few years ago but haven’t evolved them since then. I do suggest you consider their products but be prepared to do a little work to make them work as you’ll probably expect. Hopefully this blog and similar resources will make that practical.

In my case I considered all the SmileyMicros products carefully and went with the “USB Virtual Serial Port Cookbook – Parts Kit and Source Code CD”. That includes 2
“thingies”, a breadboard, some simple electronics, wiring, source code, and a book. So far I’ve only used one “thingie”, but someday I’ll need another so I’m glad to have two. I now have 3 breadboards and need more so it was good to add one. The electronics and wiring are definitely handy (I got more of each later). The source code is critical to getting started but in reality is a bit buggy and a bit crude – I’ll go into more details later. The book wasn’t as helpful as I had hoped, but has a little added value. In the end I think that product was the right choice for me but if dollars are tight then you could realistically just get one “thingie” and the rest everywhere else (if you’re willing to do some legwork). But in the end that won’t save you a lot.

The “thingie” is primarily based on a chip called “FT232” (particularly the FT232R) from a company called FTDI (http://www.ftdichip.com/). (Actually, I wonder if it isn’t OEMed by them). I’ve been thoroughly impressed by FTDI – their drivers, tools, and documentation are all first-rate, and completely up-to-date. Most of the times when I couldn’t figure something else from the SmileyMicros materials I could figure it out from the FTDI materials. So you’ll definitely want to check them out.

Because the “thingie” is based on the FT232, and the FT232 really does all the real work, that’s what I actually prefer to call it. There’s also at least a few other solutions out there based on the FT232, and much of what I post on the topic should be applicable to them.

And now that I’m into it, I realize there’s one more reason why Windows and electronics don’t seem to go together – Windows is not a real-time operating system! In fact, user-oriented operating systems are not normally real-time, and don’t need to be. But electronics are generally VERY fast, and thus real-time interfacing is important. And as it turns out, the brilliant FTDI guys handled this for us as well. The FT232 is not simply a USB-to-bus (or USB-to-RS232) interface – it actually has some smarts on it. I’m not sure how smart it is (just yet) but at the very least it will take not just straight USB input (one byte at a time), but in fact whole buffers. So you might only be able to send bytes to it once every 20 milliseconds or so, but you can send thousands of bytes at a time. So now you’re at well below millisecond speeds on a bytes/second basis. We can do interesting electronics stuff with it as we’ll see in this blog later.

 

Welcome to my computer technology/science blog

Filed under: electronics,industry observations,science — Paul Thomsen @ 8:54 pm

And by that I mean all things in the world of computer technology that aren’t related to computer management, and science generally. For my computer management (SMS/ConfigMgr/SCCM, etc.) blog postings, see http://www.myitforum.com/cs2/pthomsen. Or for my misc. trivia including mountaineering, see my Facebook account.

As a long-time technology geek I can’t help but to be fascinated by all things related to computer technology and science, including electronics and programming. I encourage your feedback on my thoughts in these areas. Together we can distinguish reality from the hype, and maybe even build a useful solution or two.

I expect this blog will primarily focus on industry issues, electronics, and science. I don’t pretend to be an expert in any of those so I’ll generally start from basic principals, but not necessarily first principals. Generally they’ll be in time sequence, so if you don’t understand a particular posting then check the earlier ones for relevant background. In the case of electronics you will have to read a basic book or two if you’re completely new to electronics.