How Not to Trim?

Illustration

In order to play with electronics, one has to get some sweet parts. Ever since I moved to States my supplier was DigiKey. They have good part selection, decent prices, and while their interface is not really the newest thing out there, it is good enough. Yes, I do occasionally have an issue or two but when my orders arrive, everything is in perfect order. Usually.

Since I always have multiple projects in parallel, I got into habit of using Customer Reference field to the fullest. I always fill not only my name for the component but also project name (e.g. C 10nF /16V NP0 (0805) [Esp8266Plug A1452]). That way I can easily sort stuff when it arrives and this has worked for me for long time. But with latest order I got a few packages where Customer Reference text was Character Limit Exceeded and my component sorting got interesting.

It was obvious what happened. One of many components that deals with data entry and printing labels didn’t like the field length. Not ideal situation but nothing uncommon either. However, decision of handling this situation is really bad.

First of all, line has enough space for at least 72 characters. Why would you put software limit to 48? My best guess is that limit was decided some time long ago for completely different kind of label. They switched labels and simply forgot to update the length. Or there might be some legacy component in the middle that can handle only 48 characters. I think that would be also a good reason for limiting length so low. Most realistic reason is that somebody simply copy/pasted the same limit as defined for Description field. I can completely understand how that could happen.

But there is NO EXCUSE in deciding to drop the whole customer’s text and replace it with your own. Your system has limitations, and you obviously had them in mind during design. Perfect A in my opinion. But why wouldn’t you just WARN me when I enter that reference in the first place? There is data verification done on that page for the other fields. Why is this field so special that no verification can be performed?

And, if you really have to do anything, don’t replace MY text - trim it. What would help customer better: Character Limit Exceeded or Q MOSFET, P-channel (SOT23-3) [ElectroPiggy A...? I am not sure what was going through the head of person who made that particular decision but they definitely didn’t think of customer.

I guess my script for ordering the parts will get another update…

Detecting Watchdog Reset in XC8

Illustration

Watchdog is probably one of the best PIC microcontroller features. Yes, it would be great if you would write perfect code and program would not get stuck in the first place, but let’s face it: mistakes are going to be made. And really, are you going to write timeout code around every communication routine just so you can detect one in million error?

In 99% of cases, you will just turn on WDT timer in your config bits and count on it to reset microcontroller if something goes wrong. If watchdog times out, program will start from scratch and all is good. For all practical purposes, watchdog-initiated restart is same as normal start. Or is it?

Most PIC microcontrollers clear their ~TO bit in STATUS register as a way to detect watchdog restart. However, if you try to read that from your XC8 compiled C program it will seem that every microcontroller start is normal. It will report any timeout. Cause? Because your compiler is clearing STATUS register before your program starts running.

If you are interested in watchdog timeouts, first you need to tell your compiler to preserve startup status for you. This can be done by going into project properties, selecting XC8 linker category and checking Backup reset condition flags item. Now your compiler will give you access to two special variables: __timeout and __powerdown. These variables will be copy of ~TO and ~PD states respectively.

If you read datasheet carefully you will notice that ~TO will be 0 if timeout has occurred. Thus code to check for it would look something as:

void main() {
    ...

    if (^^__timeout == 0^^) { //value 0 means there was a timeout
        //this restart is due to the watchdog timeout - do something here
    }

    ...
}

PS: Procedure described here is specific to XC8 compiler. But same principle applies with most other compilers too.

Jelly Bean PICs

It used to be easier years ago when I started playing with Microchip microcontrollers. You pretty much had only PIC16F84 readily available and that was the PIC you used for anything. Today situation is very different. There is probably a different microcontroller for any purpose you can imagine. However, with time, I more-less standardized on a few of them.

Theme is common. I work mostly with 5 V power supply so anything that can work directly of that is highly desirable. Pretty mandatory is at least one UART port because that is something I use more often than not. I2C and SPI are also high on the list but they sort of go hand-in-hand with aforementioned USART so I rarely specifically search for them.

For most of things I do speed is rarely of concern and any PIC with internal oscillator (keeping component count low) will usually do. Exception is when it comes to USB and CAN bus that are really finicky in that respect.

Quite often my PIC of choice has multiple family members so I can easily go between memory sizes and/or pin counts. Also high on the list are old and proven designs everybody else uses. Why do development around a new and/or unknown PIC if you cannot buy it 90% of the time.

Since most of my soldering is done by hand any PIC that cannot be obtained in SSOP or similar package is automatically out of picture. Having it in DIP format is a small plus but I personally don’t really care about it.

Without a further ado, here is my list of PIC chips I always have available:

PIC16F1826

If I don’t have any special needs I will most probably end up using this little gem. It is rather small in size with only 20 pins SSOP (18-pin DIP), 16 of which can be used for I/O (not all models are this pin-efficient). Alongside 256 bytes of RAM, it also has 256 bytes of data EEPROM available. Basic model has only 2K of programming space but upgrade to 4K model (PIC16F1827) is completely painless.

Features include 10-bit ADC (12 channels), specialized capacitive touch module (12 channels), good UART, and support for both I2C and SPI. It goes up to 32 MHz, all that on internal oscillator module. Only thing you need to connect one of these is 1.8-5.5 V power supply and a small decoupling capacitor.

Price (in quantities of 1) is always less than $2.

PIC16F1934

If I need to connect PIC to LCD, I keep going back to this one 40-pin beast. With total of 36 I/O lines controlling 3 digit LCDs is trivial and that is not even maximum it can support. This basic model has similar (to PIC16F1826) memory configuration. However, next two models (PIC16F1937 and PIC16F1939) double the RAM and program memory each, all the while keeping full hardware and software compatibility.

Due to its bigger size, it has 14-channel 10-bit ADC and 16-channel capacitive touch module. It goes without saying that UART, I2C, and SPI are all supported. Internal oscillator is same respectable 32 MHz as is the need for 1.8-5.5 V power supply. A bit more careful decoupling is going to require three capacitors.

This family has also a smaller PIC16F1933, PIC16F1936, and PIC16F1938 members I sometime use. From software perspective they are the same as their bigger brothers but, since they have lower pin count, you cannot just drop them instead of larger device.

Price for one is $2.50.

PIC18F25K80

This microcontroller is a beast. In its small package (28-pin) it has whooping (for a microcontroller) 32K of a programming memory acompanied with 1K of a data EEPROM and 3.5K of RAM. Its hardware-compatible upgrade (PIC18F26K80) improves program memory to 64K. This family also has bigger members in 40 and 64-pin configuration but they are not drop-in replacements.

ADC is 12-bit (8 channels) which is pretty much the best you can find in any microcontroller and there is a support for two UART devices at the same time. Of course that it also supports I2C and SPI but my main reason for using it lies in its CAN bus support. Just add a CAN bus driver and you are good to go.

This is still 1.8-5.5 V device but one has to take a bit bigger care with decoupling since Vddcore needs a bit larger capacitor than standard 100 nF we’ve come to expect. Also notice that CAN bus functionality pretty much requires you to have external oscillator so getting everything setup is a bit more work.

For one you’ll pay about $3.50 but it is worth it.

PIC18F26J50

Whenever I need to play with USB, this is my go-to chip. This 28-pin device has 64K of program memory, 3.5K of RAM and unfortunately no data EEPROM. But fear not, you can use program memory for settings. If you don’t need a lot of program memory, you can use smaller PIC18F25J50 and PIC18F24J50 but I’ve found that anything USB related usually wants 64K as a minimum. Of couse, you can also go to higher pin count with PIC18F46J50 if that is what you need.

You also get standard 10-bit ADC (10 channels), 2 UART modules and, of course, I2C and SPI support. Internal oscillator goes up to 48 MHz and it is precise enough to allow for USB interaction. This device also has a really configurable pinout so it’s great for a size-constrained designs. Unfortunately its power supply has to be 2.0-3.6 V, it needs pull-up resistor for reset control, and multiple decoupling capacitor sizes require you to have quite a few supporting components. It gets expensive and crowded pretty fast.

There is a promising PIC16F1459 which does offer a bit more modern architecture and a simpler 1.8-5.5 V life but I find 8K programming memory really restrictive when dealing with anything USB.

One PIC will cost you slightly less than $4.50.

No Drilling, Please

Illustration

As I managed to do create a holly grail of electronics - double sided PCB without any vias, only remaining step was to get it made.

Usually this would consists of uploading board gerbers to OSH Park and getting payment sorted out. However, this time I was greeted with error: “I can’t find a drills file”. Mind you, this was expected since I really didn’t have a drill file, but unfortunately this wasn’t a warning I could skip. So I contacted support.

I really didn’t expect any quick answer since I raised ticket on December 31st after 16h PST. Surprisingly I got the solution almost immediately. It wasn’t possible to upload design without drills but Dan (yep, Laen doesn’t handle support any more) suggested a file with drill locations all falling outside of board. Their filtering process would then remove those as invalid and my drill-less board would be ready for manufacturing.

While this solution was acceptable for one-off job, it got me thinking whether there was something a bit more elegant and less error-prone if I create some bigger board in the future. To solve it properly I had to have a drill file without any drills. To the Excellon specification!

With a bit of testing, I got to the minimum of content that OSH Park parser would still consider a valid drill file. It is essentially just defining one drill tool, selecting it for work and then finishing script without ever drilling a hole:

M48
INCH
T01C0.0394
%
T01
T00
M30

Empty drill file is available for download.

In the Year 2014

Illustration

As always, first post of a year is reserved for a bit of statistics.

This year I published only 49 posts. I would like to tell it’s because they are of a higher quality but truth is that I got busy with other stuff and this blog got ignored in the progress. I highly doubt I’ll return to a new post every three days as it was when I started writing but once a week should be doable.

Since programming posts take most effort to do properly, their percentage has dropped to only 20%. About the same number of posts was related to Windows. Next up with 10% was Linux and another 10% was dealing with electronics. Rest is mishmash that I won’t even bother to categorize.

There was a slight decrease in amount of traffic site has been getting but that was to be expected due to a drop in posting frequency. As it became usual, about 65% of traffic is coming from unknown place so I’ll just ignore them. Out of known locations, United States were again most common source with about 35% of all visits and Germany came far second with 15%. India was third with 10%.

Same as last year, Chrome was the first browser of choice for viewing with 40% of traffic coming via it. Internet Explorer and Firefox basically share second place with about 25% each. Fourth honorable mention goes to Safari with 5%. Similar to last year, 95% of those visits were done via desktop and not mobile phone.

This year I also did a major site improvement - it became https-only. While average visitor probably doesn’t care and hopefully saw no difference, those more security conscious will appreciate this change. It is more of a philosophical move than anything else.

This year will also be remembered for me as the first year a page of my site was intentionally blocked by a search engine. Post in question was Installing Windows 8.1 (or 8) without a product key and it was DMCA’d due to comments as far as I can deduce. It is really difficult to know what exactly happened because every questions seems to get a black hole treatment.

That’s it - regular program will continue with the next post.