GETTING STARTED WITHPICs
THE LATEST IN PROGRAMMING MICROCONTROLLERS
■ BY CHUCK HELLEBUYCK
C LANGUAGE INTRODUCTION
IN MY APRIL COLUMN, I DID A SPEED-TEST comparison of various PIC®
microcontrollers (MCUs) that were programmable in Basic, and then compared
a Basic and C compiler. I made a mistake that affected the results. I claimed
that microEngineering Labs’ PICBasic Pro compiler outperformed HI-TECH’s
PICC-Lite compiler for speed, and I was wrong. PICC-Lite compiled code runs
faster. I’ll explain how I goofed a little later.
The response to the speed-test
article was amazing. I got more
emails from that article than any
article prior. Several knowledgeable
C language people pointed out my
mistake. I also had a few people point
out that Revolution Education’s
PICAXE programming system could
be “overclocked,” meaning it could be
run with an 8 MHz or 16 MHz
resonator in place of the 4 MHz
resonator I used in the speed test. At 16
MHz, the PICAXE would run almost as
fast as Basic Micro’s Basic Atom.
This was great feedback, since it
indicates that readers of this column
include programmers from various
experience levels and are reading the
details. In other words, they aren’t
falling asleep reading my stuff. Now
let me explain the error.
CODE ERROR EXPLAINED
If you missed the April column
speed test, I just did a simple For-Next
loop that continued for 255 iterations,
and within the loop I processed a
16
■ FIGURE 1
June 2007
simple math equation to increment a
variable. I toggled a pin outside the
loop so I could measure the processing
time on an oscilloscope, as shown in
Figure 1. In the article, I made one mistake that several C programmers pointed out. In the section where I compared
PICBasic Pro to the PICC-Lite compiler,
I declared two “int” variables in the C
compiler version of the speed-test code.
This was a huge mistake in a
speed test program, since “int” creates
a 16 bit variable in the C program —
but in all the Basic versions of the
code I declared byte variables. This
greatly affected the speed results. You
see, the PIC MCUs I used in the
article were eight bit microcontrollers,
meaning they have an eight bit wide
data bus. To process a 16 bit variable,
the program has to perform the math
function in two steps: low byte then
high byte. This takes twice as long.
Therefore, it incorrectly showed the C
compiler slower than PICBasic Pro.
To add to the confusion, I didn’t
even realize until much later that I also
sent the final version of the article with
a typo in the C code
embedded in the text. The
C code in the published
column included the For-Loop variable changing
from 0 to less than 256;
when in reality I ran it 0 to
less than 255 because I
knew a byte variable would
always be less than 256.
Changing from 256 to 255 should have
tipped me off to the “int” variable error,
but I completely missed it. You see, a
byte variable will roll over to 0 after
reaching 255 — since the 256 decimal
is 100000000 binary. As this shows, it
takes nine bits to store the 256 decimal
and the lower eight bits (or byte) are
zeros. So, I was thinking byte variable in
my corrected code, but left the 16 bit
“int” variable declaration in the code. If
I wasn’t focusing on a speed test, it
wouldn’t have been an issue.
I even admitted I was shocked
by the results of the PICBasic Pro
compiler being faster than C. I tested
it on another processor, but got the
same results since I made the same
mistake. If an operating system
supplier finds a bug, then they release
an upgrade. This article is my upgrade!
I’m correcting my mistake and clearly
pointing out that the speed of the
PICBasic Pro was fast, but not as fast
as the corrected version of the C code
compiled by the PICC-Lite compiler.
After a very nicely written email
from Langue Rodriguez pointed out
my mistake, I went back and declared
the variable in the C program as an
unsigned “char” variable (which made
it an eight bit variable) and re-ran the
speed test. The corrected code is
shown in Listing 1. This re-test showed
the PICC-Lite C program was faster
than PICBasic Pro by 460 microseconds, with the setup running at 4
MHz. The PICBasic Pro program took