Tuesday, December 4, 2007

Microchip PIC?

At home lately I’ve been trying to learn about electronics and simple analog circuits towards understanding microcontrollers from the bottom up rather than from the top down. In the past, I’ve relied on the kindness of others and/or good books or websites to handle all the component work and stuck to microcontroller programming in C. In C, microcontroller programming is just a bunch of bit operations, shifting, loops, polling, etc. But every time things failed to work like you think they would in “normal” programming (microprocessors) it invariably came down to the hardware, or the interface between the micro controller and the hardware---not the C language implementation or the code itself.

So in working from the electrons up, I’m finally to the point where I’m programming again now that I have a working knowledge of how resistors, capacitors, diodes, transistors, inductors and voltage regulators come into play. But the programming is in assembly, and my only previous assembly experience is on PowerPC architecture (some experience) and 680x0 architecture (a fair amount of experience a long long time ago.)

There are basically two ubiquitous chip families in simple 8 bit microcontroller programming. There are a lot more, but these two are far and away the most popular and often-used. They’re the PIC processors from Microchip and the AVR family from Atmel. The PIC’s seem the most popular overseas and the AVR’s seem most popular in the United states, though the AVR’s are (rightfully) starting to overtake PIC’s.

PIC is an abomination.

Here is a list of things I’ve come to loathe:

1. Program space is organized into 14-bit (yes, that’s FOURTEEN BIT) words.
2. There’s only one register, called W, and it only supports a tiny set of addressing modes.
3. Accessing data is done via the “file registers” which is just a fancy way of describing more 14-bit words in program space.
4. There are no conditional branches, you test condition codes as bit operations with the condition code register and there are only “skip the next instruction” kinds of branching.

I have to stop here and offer an example of how fucked up this is. You have to test the OPPOSITE case you want, with a operand that conditionally SKIPS the next instruction.

;
; I want to see if bit 4 is set in my file register
;
BTFSC SomeFileRegister, 4 ; so I test to see if the bit ISN’T set “Bit Test File and Skip if Clear”
GOTO _TheBitWasSetLabel
.
.
_TheBitWasntSetLabel:
; continue normal execution
.
.

[ elsewhere in the code ]

_TheBitWasSetLabel:
; the condition was met
.
.

Two instructions to do a conditional branch, and you have to test the INVERSE CASE.

Jesus.

What the hell were they thinking?

5. The same thing applies for loop counters, where you do the test at the decrement or increment instruction and SKIP the next statement (which is always a GOTO for conditional branches).
6. It’s an 8-bit architecture, so the working values are all 8 bit, but stored in 14 bit file registers (program memory.) That’s 6 bits wasted when storing data.
7. Storing a string in program space requires building a table of return-literal-value instructions, and then jumping the program counter to the appropriate line by adding to the PC file register. WTF??
8. There’s no stack register. You get 8 nested CALL instructions or INTERRUPTS and you’re out of space.

AVR, fortunately, is a much more sane set of machine instructions, and the C compilers available for the PIC chips handle all this madness for you. Soon I’ll be comfortable enough with the architecture to let the C compiler take care of it for me. Until then I have to boggle at the decision decisions!

20070927112615868_1.gif

Back to the drawing board...



After some critiquing by a few N&V forum members more knowledgeable about power supplies than myself (which is likely to be all of them) I've decided I need to do some redesign on the PSU.

I reworked the design a little to reflect my current implementation (the direct battery line for the DTV's screen) and after some charging and testing I've determined:

1) the 3.3v regulator needs to run off the battery directly, not one of the 7805's... it generates a load for the 7805 which heats it up something fierce.
2) my current NiCd cells are probably shot. They don't hold a charge for very long at all.
3) my R1 resistor is too large, as I calculated the charge current incorrectly.
4) I would get a lot more efficiency using a switching battery charger controller like some of the ones offered by Linear.

20070917101640778_1.gif

DTV PSU

I worked out a power supply for my DTV system. I'm still working on getting it online as a portable. Right now I've got a good screen for it, and the whole schebang housed in a butt-ugly RadioShack enclosure with all the ports exposed out the side. PS/2, IEC (DIN-6) and Joystick (DB-9) are all mounted.

The DTV portable is going to need a 7.0v to 9.0v line for the screen, a 5v line for the PS/2 keyboard, and a 3.3v line for the DTV itself. Using some scavenged parts, some free samples from National, and a little elbow grease, I worked up a schematic:

20070911212844634_2.gif

The battery I'm using is seven 1.2v NiCd cells I scavenged from next door when Spatialight moved its R&D operation to Korea and let the whole building come in and pick over their trash. I made out like a bandit, scoring a ton of great parts and connectors and stuff.

Anyways, after burning my fingers for a few hours, I finally ended up with:

20070911212844634_1.jpg

The outputs are on the right. The top is black, that's ground. Below it is green, that's 3.3v. Below that is red, that's 5.0v, and the purple line is unregulated 8.0v-ish straight off the battery. The LCD screen I'm using has its own voltage regulators, so I'm going to feed the battery directly to the screen for now. ·

OneStation NOAC

The "OneStation" is a Made-in-China handheld that contains a NOAC (Nintendo On A Chip, or NES On A Chip) that you can buy mailorder (www.DealExtreme.com) for like $30.



It's styled after the GameBoy Micro, and actually has NOAC and what would normally be considered the "motherboard" of the chip on the cartridge instead of in the main unit. The main unit is simply an audio circuit, a b/w composite video out circuit, the game pad buttons, and the LCD connector for the main OLED display.

20070829112336620_1.jpg

Since all of the "brains" of this thing are essentially inside the cart, it makes the cart an incredibly compact NES-on-a-chip.

20070829112336620_2.jpg

More Scope Research

I've been looking for a way to look at my circuits without spending large sums of money on a reasonably capable digital sampling oscilloscope.

I want to do analog circuits, mixed signal circuits, and microcontroller work. I want to drive LCD displays and eventually learn how to generate VGA and NTSC signals for displays.

I have big goals, and big dreams, but I'm a husband and a father of young girls and I have a full time day job and I can't afford all this damn electronics equipment.

So I made it my mission to find cheap-ass solutions to expensive test equipment. So far, I'm having only limited success. This post is to try and consolidate the information I've gathered and relate some of my experiences.

Here's what I've got so far:

Parallel Port Oscilloscopes

Scope2k4

http://home.planet.nl/~m.f.hajer/scope2k.htm

I built this, and it works. I don't have real scope probes, but I cannibalized a $10 radio shack multimeter I had retired and mounted everything in that enclosure. This works really well for low speed digital signals. The resolution isn't so great, and the software is pretty simple (I use the free version.)

Sound Card Scopes

Zelscope

http://www.zelscope.com/

with this hardware:

http://xoscope.sourceforge.net/hardware/hardware.html (or bought from http://www.jaycarelectronics.com/productView.asp?ID=KA1811)

Virtins Sound Card Multi-Instrument

http://www.virtins.com/

I built the virtins sound card input protection circuit and I use it with the Zelscope software. The circuit is just some additional impedance on the line and some overload protection:

http://www.virtins.com/Virtins_Sound_Card_MultiInstrument_Manual.pdf

Serial Port Scope

I also discovered, in my surfing around, a serial-port based FPGA-based idea:

http://www.fpga4fun.com/Hands-on_Flashy.html

which shows some promise, though I haven't done any kind of FPGA work in the past and this would be another huge learning experience. The high-speed aspect of it looks promising, but the software seems feature-sparse.

USB Scope

There are many USB scope setups. Many of them way too expensive for me (I'm looking in the under-$200 range) but there are a few standouts.

Hobby Lab USB Scope

http://www.hobbylab.us/USBOscilloscope/Home.htm

Po.Labs Scope

http://po.labs.googlepages.com/

(Same device, different company?)

Hantek DSO-2090

http://www.beigly.com/catalog/product_info.php?products_id=96&osCsid=23bce8203ec673af5b0f959cd5c90642

Thoughts

The sound-card based scopes may be fine for some analog stuff, but I encountered strange behavior that I understand is related to capacitors on the input lines inside the sound card itself (noise reduction?) that make it very hard to use for digital circuits. I can't seem to see a nice square wave, I get drop-off towards 0 volts. I also don't ever see a real voltage level on the sound card scope... it always seems to taper. It's disconcerting, and has put me off of that solution.

The Parallel-port based Scope2k4 seems to work pretty well for digital circuits, but the sampling frequency is pretty low and low resolution (8 bit ADC, max of like 4kHz sampling rate?) Which makes me think it's impractical for any real microcontroller work.

Scope me, Scope me

I wish I had an oscilloscope. I wish it BAD. Why? Because just having a good multimeter really isn't enough to understand what's happening inside the circuits I'm building. Not even close.

It's not enough to know voltage between two points on the circuit, though that IS useful sometimes. And it's difficulty to measure current without disconnecting and reconnecting things. Those are both tools I have with my multimeter from college and I do use them.

But to actually see, say, what's happening to voltage along a line when you have a 1k ohm resistor charging a 220uF cap, versus a 10k ohm resistor charging a 220uF cap, you really need continuous voltage monitoring over time. The multimeter can tell me roughly what the average is, and I can see if it's fluctuating at all, but with a scope you can see actually what's occurring over time, and that's hella useful.

So I built a couple simple PC-based scopes. I wanted more detail, and I didn't want to spend the hundreds of dollars that it takes to get even a half-decent entry level standalone oscilloscope. I think I can probably find a decent 20 year old Tektronix one for like $350 on craigslist or eBay, but it's not even a storage scope, which is something I'd want to have for digital circuit work. Good new ones are in the multiple thousands of dollars, it's all very discouraging.

But a little searching the web turned up a couple of somewhat viable options. They don't sample at NEARLY the frequency you'd need for "serious" scope stuff (like video) though.

A soundcard based scope:

http://www.virtins.com/

A parallel port scope:

http://home.planet.nl/~m.f.hajer/scope2k4.html

Both of which cost actual money money for the software (which, I'm finding, is really important) but the barrier of entry is low. With the soundcard scope, it's just a couple of resistors and some diodes to make a small circuit to protect the LINE-IN input on the sound card from over-voltage. With the parallel port scope, a couple of cheap ($2?) 8 bit AD converter chips were necessary, but the circuit itself was just a voltage regulator, some resistors and those chips.

I built both of them. Why? Because the soundcard based scope has a fatal flaw. While it's great for analog stuff, there are some capacitors on the line-in on the sound card and they normalize the voltage. If you hook the probes up to a AAA battery, you'll see the 1.5v trace, but then it immediately tapers down to zero. This makes it unusable for any kind of digital work since 1's don't stay 1's for long (at least visually.)

So I built the parallel scope for digital work. It behaves exactly as you'd expect an oscilloscope to behave when you hook up a 1.5v AAA battery to the test leads---you see the 1.5v trace. I hope that this is useful for digital work, even though the scope's maximum sample frequency is 4kHz compared to 44kHz for the soundcard scope and 20Mhz for a very bottom run entry level professional scope. I guess time will tell.
·

Lucky's Buzzy Ball

My dog is almost totally blind now, and while doing laps in the pool is his favorite thing in life (and best, most reliable form of full body exercise) I miss the throw-and-catch from when he was a puppy. And I'm sure he does too---ball-obsessed as he is. He carries a ball with him wherever he goes.

So my solution is to make a beepy-ball. I've started investigating hobby electronics as a way of learning some things that I never really understood and getting enough knowledge to make a few little projects that I've "always wanted to do."

Here's the schematic of version 0.1:

20070809121254587_1.gif

Here's the sample board layout:

20070809121254587_2.gif

These are works in progress, and hopefully will be reduced. ·