Reduction = Increase

You can spend a couple of years doing something day in and day out and think that you have got the hang of it, and then the fact that you have completely misunderstood a concept comes and bites you on the nose.

In the PIC processors, being RISC, none of the instructions are completely straightforward. This is especially the case with bit operations. You have apparently simple instructions that let you set or reset a single bit at any data location, well at least on the face of it you do! In order to keep the processors simple, pretty much all of the instructions have a structure that fits neatly into the four step pipeline of 1-load instruction, 2-read data byte, 3-modify data byte, 4-write data byte back. This same structure is underlying the bit instructions so that to set a bit, the whole byte is read, a bit is set, and the byte is written back. This works really well on memory locations where you know that what you write is what you will read back, but on I/O ports it is a different matter when the output port address is the same as the input port, and some of the input ports cannot be read in some modes!

This of course makes the process of simply turning a single port pin on or off a lot more complex than just using a bit set instruction – except that most of the time what you put on the output port gets read back in to the input port, so that as long as you don’t do something silly like short a pin or something the simple set instructions appear to work, and it has done for me pretty much up until now.

The answer as always with a RISC processor is that you have to use a few more instructions to do the simplest of tasks, and in this case you need to maintain an output buffer in memory that you apply the bitwise instructions to, before using a couple more instructions to move the buffer contents via the accumulator to the output port. In most cases the extra microsecond to do the extra instructions and the extra byte of memory won’t break the bank, but in the tight core of a PIC in a real time environment you may need a complete redesign to recover the resources.

Would it be easier in C? I don’t think so.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: