Longer than long

Sometimes you need to use really big numbers — and 32 bits just won’t cut it.

Fortunately, the gcc compiler has this covered; its “long long” type supports signed and unsigned 64-bit integers. The difference is huge — whereas unsigned 32-bit integers are limited to 0-4,294,967,295, 64-bit integers can go all the way up to 18,446,744,073,709,551,615.

These numbers can be used in C as shown in this example:

#include <stdio.h>
int main(){

//Variable declaration
unsigned long long int myNumber = 1;
int power;

//For loop
for(power = 0; power < 63; power++){
printf(“Power: %d      Number: %llu\n”,power,myNumber);
myNumber = myNumber * 2;
}//for

return(0);

}//main()

Posted in C, Coding, Linux, Math | Tagged , , , , , , | Leave a comment

PIC output characteristics

In order to automate several functions at home, I am in the process of designing a low-speed home automation network. Since one of my primary goals is to have individual nodes be as low-cost as possible, I have decided on a clocked multi-master serial bus interface. Since the specifics of the interface (pullup resistor strength and signaling speed, mostly) will depend on the output characteristics of the individual nodes, some testing of a representative PIC microcontroller (a 12F683) is needed.

My testing protocol (if I can even call it that) is informal and straightforward. A capacitance, representing a conservative estimate of the capacitance of a length of RJ12 cable, is connected to an output from a PIC microcontroller. Since the network will be open-drain to avoid contention issues, a 270 ohm resistor will be used (digital outputs on a PIC are rated to source or sink 20mA max, so the pull-up resistance should be no less than 250 ohms. Power consumption is not a major concern, and a value close to the lower limit will allow faster rise times and therefore faster signaling.)

The experimental setup…

The first experiment was at full speed (running on the internal 8MHz clock): a 500ns pulse, with the pin being actively driven high and low. No capacitance was added, and no pull-up resistor was used.

01_500ns_no_load_active

500ns pulse, no load, active high/low

Next, a 270 ohm pull-up resistor was added, and an open drain configuration was used (with the pin always either driven low or tristated, never driven high). This will prevent an overcurrent condition on the bus, should two devices assert different values at the same time.

03_500ns_270r_od

500ns pulse, 270 ohm to Vcc, active low / open drain

Next, 100pF capacitance was added. This had little or no effect on the actively-driven falling edge, but did start to affect the pulled-up rising edge.

500ns, 270 ohm pull-up, open-drain, 100pF load

Next, the capacitance was increased to 1nF. This resulted in the voltage taking most of the 500ns pulse time to recover.

500ns pulse, 270 ohm pull-up, open-drain, 1nF loading

Because of this, I decided to slow the pulses down. I first tried 5us pulse widths, but the capacitance still couldn’t be increased past 1nF or so.

5us pulse, 270 ohm pull-up to Vcc, active low / open drain, 1nF loading

5us pulse, 270 ohm pull-up to Vcc, active low / open drain, 3nF

Finally, I settled on a 50us pulse time. This allows data transmission at up to 20kb/sec with a reasonably clean signal, even with 10nF loading. 6-conductor flat satin cable is stated by Black Box to have a characteristic capacitance of 22pF per foot. In standard units, this is 22pF per 304.8m, or about 72.2nF per meter. Since I plan on having about 25m of cable in the network to start (and perhaps as much as 100m eventually), this should result in a total cable capacitance of between roughly 1.8nF and  7.22nF.

50us pulse, 270 ohm pull-up to Vcc, active low / open drain, 10nF loading

Here is a detail of the risetime at 10nF, showing a risetime of about 10us. By sampling past the halfway point of each bit period, the signal should be stable even with 10ns loading. 20kb/sec should therefore be fine. I’ll probably run it at 19,200 for nostalgia if no other reason.

50us pulse, 270 ohm pull-up to Vcc, active low / open drain, 10nF risetime (detail)

These risetimes could, of course, have been calculated in terms of time constants (tau = RC), but it’s always nice to see actual results produced by actual hardware. One particular concern that I had was whether the PIC’s tristate functionality could really be relied upon to work this way, rather than as a relatively slow way to set up pin functionality upon initialization. It does indeed work nicely — apparently just as fast as actively toggling bits (although controlling both tristate and pin value would require swapping memory banks, which takes a few cycles.) For this test, the software simply remains in the TRISIO bank.

 

Posted in Analog, Digital, Electronics, PIC Microcontrollers | Tagged , , , , , , , , , | Leave a comment

A Torrent of Knowledge

As it turns out, Wikipedia is not only free to use, but makes its entire database available for download, as well. I just downloaded the entire set of Wikipedia articles (not including revision history) in just over 25 minutes.

Here is a page with links to several BitTorrent trackers. Downloads seem to be possible at well over the 50Mb/sec that my cable connection can handle. Compressed, the files are still several GB, so you may not want to try this on a metered data connection.

Posted in Digital Citizenship, Internet, Resources | Leave a comment

A Method To The Madness

A quick look at the values of standard 5% resistors is puzzling. Instead of a nice, logical progression of values (say, 1.0k; 1.1k; 1.2k… 1.9k; 2.0k), the values seem to follow some arbitrary scheme, possibly chosen by a numerologist rather than an engineer.

There’s a reason for this, though — the standard 5% resistor values are chosen based on a specific set of “preferred numbers.” This specific sequence not only ensures continuous coverage of the whole order-of-magnitude sequence (1.0 to 9.9 times a power of ten), but helps cut down on manufacturing defects, as well. Here’s how.

The E24 series, showing complete coverage of the decade from 10-100 within 5% tolerance.

The E24 series, showing complete coverage of the decade from 10-100 within 5% tolerance.

The “E24 series” is chosen so that every possible value, from 1.0 to 9.9999, falls within 5% of one (or two) of the values in the series. This way, any possible desired resistor value can be substituted by a resistor whose value is within the 5% margin of error. More interestingly, resistors can be manufactured with no control whatsoever over the resistance — and as long as their resistance falls somewhere on the scale, they can be binned into a group. A resistor tests out as 992 ohms? Into the 1k bin it goes. Another is 450 ohms? Good enough to go in the 470 ohm bin. All possible values fall into one (or sometimes two) possible bins.

It only seems weird until you think about it.

Posted in Analog, Components, EET201, Electronics, Math | Leave a comment