Thursday, December 31, 2015

The PIC

K9AY controller that doesn't
work.
For a year and a half, I've been working on a push-button controller for my K9AY loops. The loop remote box is pretty much as K9AY originally designed it -- two 6 volt DPDT relays, fed with 6 volt pulsating DC or AC that is coupled into the coaxial feed line. While some people used separate control lines for the relays because of hum problems, I never encountered this issue.

For years, I used a rotary switch to select the direction. Then I decided I wanted a push-button controller so I can immediately select the desired direction.

The push-button controller design was based on a 74LS175 Quad D-type flip flop and SPDT buttons. Pressing a button drives the clock line low and also raises the corresponding D input on the flip flop. Releasing the button causes the leading edge to latch the state of the button. The not-Q outputs of each flip flop drive the LED on the button, while the Q outputs feed to an VN10KN MOSFET that drives a SPST relay that supplies voltage. Six volts for NW, minus six volts for SE, six volts AC for SW, and no voltage (no relay) for NE. Simple.

Except it doesn't work.

The buttons are the problem. Like all mechanical switches, they bounce. These bounces are probably a handful of times within 2-10 ms, but that's enough to throw off the logic which switches within a handful of nanoseconds. Sometimes you press a button and nothing happens. Sometimes you press it, and more than one relay activates.

It's frustrating. The whole purpose of the push-button controller is to command the loops to go immediately to one direction, except the circuit isn't reliable.

I wanted to keep this circuit simple -- one 14-pin chip. Now it looks like I'd have to add a lot more logic to get this design to work.

As a software developer, I kept thinking -- if I only had a micro-controller, I could avoid the switch bounces with software. And I could fix other problem with the design. For example, if you accidentally pressed two switches at the same time, two LEDs would light. With software, I could make it so only the last switch press is selected -- only one direction selected at a time.

This made me wonder -- if I were going to use a micro-controller -- what would I use? I got started on my software career when I built a 6800-based computer in 1977. I was familiar with the 6802, 6801 and 6805. But these were relatively huge chips, and required a lot of support logic. What I needed as a micro-controller that would fit into a 14-pin or 16-pin chip, and allow for four digital inputs, and seven digital outputs without any support. What was available, and how much would it cost?

So, I went to mouser.com and started searching. Hey, these Microchip PIC micro-controllers seem like they would work. After all the Elecraft K2 has a bunch of them. There were also ATmel AVR chips, whatever they are. Seems to me those are used in the Arduino.

Actually, there was an abundance of these devices that might work. It got more interesting when I sorted them by price. I ended up selecting the Microchip PIC16F1503.

This is a pretty amazing device! Two kilowords of instruction memory, 128 bytes of RAM. Capable of up to 5 MIPS. Low power consumption, very flexible operating voltages. With a whole host of interesting I/O features: 12 input and 11 digital output channels, 10-bit A/D converter with eight external and three internal channels, 5-bit D/A converter, two comparators, three timers, 10-bit Pulse-Width Modulator, two configurable logic cells, and a 20-bit numerically controlled oscillator.

Of course, you can't use all of that all at the same time, but this seemed like a marvelously flexible device for all kinds of projects. The best part was the price -- 90 cents a piece. Naturally, I ordered 10 of them.

This device has plenty of power for my K9AY controller. The first step was to figure out how to use it. Microchip offers the free MPLAB X IDE software, which can be easily downloaded for Windows, Mac or Linux. The IDE alone will let you write software and run the simulator. The next question is -- how to get the program into the chips. Microchip also makes a device called the PICkit 3 -- a USB device that easily connects to PIC processors and allows for programming as well as some limited hardware debugging. There was also an end of year special, so I could get it for $36 plus shipping.

The circuitry to connect the PICkit 3 to the chips is dead simple. I looked into building a little board with a ZIF socket. However, ZIF sockets are like $10 apiece. Before I could submit my order for one, I found a little board on amazon.com from DIYMall that is exactly what I was thinking of building for less than $8.

It took me a couple of nights to figure out how to use the IDE. The PIC itself is mind-numbingly complicated. First step is to set the configuration bits, which tells the PIC things like where it's clock signal is coming from. Then you actually get to the start-up code, where you have to program the chips innards to reflect the configuration you wish to use -- which pins for I/O, and what function blocks are connected to those pins. The PIC16F1503 is complex enough that it would actually take a lot of time and code space to configure each block. But, again, you can't use all that stuff at once, so just focus on the items you need.

The configuration is done by writing to specific memory addresses. This would be easy if the chip allowed direct access to all of the data memory. However, because of the programming architecture, it really only has direct access to the first 128 bytes of memory. So, of that space, all but the first 32 bytes and last 16 bytes of that space are banked into 32 banks of memory. Interspersed in those banks are the configuration registers.

Confused yet? Well, the PIC instruction set is nothing to write home about either. There's only one accumulator (W) and no other implicit registers, other than those in low memory. The PIC16F1503 is at least a mid-range CPU, so it has some additional instructions to make things easier to program.

If you are not an assembly language programmer, you really ought to ask yourself what the heck you are doing writing micro-controller software, but there is a C compiler that can be used.

I had little problem writing a program on the simulator. Getting it into the chips was something more of an ordeal that took me about three days to figure out. I wrote a little program that takes all 6 bits of PORTC and outputs a counter on them. It increments the W register from 0 to 0 (256 times) between counts, so the results should be visible. I used a 32 kHz clock speed, so things ought to happen slowly enough to observe.

At first, I couldn't get MPLAB X to talk to the PICkit. MPLAB X seemed to know the PICKit was there, but when going to program, it just sat there, doing nothing. After digging around on the web, I found that you could reset the PICKit by holding down it's button while you plugged it into USB.

At that point, MPLAB X was now talking to the PICkit, but it still wouldn't program. The PICkit has an option to supply power for programming, but the default is not to. Once I turned the power on,  I was getting a warning that the PICkit was trying to supply 5 volts, but it was reading only 4.75 volts. Now, there's nothing on the DIYMall board other than a ZIF socket and a couple of jumpers. Setting it to 4.75 volts made the warning go away, but it couldn't find the chip.

It was about this time I started studying the DIYMall board more closely. It came with no documentation, and I couldn't find anything on-line about it either. When I turned it over, I found there were jumper indications. I moved the first jumper to the B position, which was correct for 14-pin chips.

No difference. This shouldn't be so hard!

Looking at the board some more, it turns out that the 14-pin chips aren't supposed to go in pin 1 of the ZIF socket, but way down around pin 30. There was a heavy white line on the backside of the board indicating this. That's the first time I've encountered documentation printed solely on the board itself. I figured I'd try it. Low and behold, it programmed!

Putting it in a board, it worked the first time. I've included a little video of the chip in action.

video