Monday, September 1, 2014

Correcting my Error

Figure 1.


I have previously made an error in this blog which I will correct from here forward.  This error is more of a "good practices" error in code writing.  In figure 1 above we see a code example from a previous lesson.  Within the main program we set the data direction using TRISIO=0x8; where 0x8 is considered a "literal" using hexadecimal.  This is bad practice.  Later on when code gets longer, and things begin to get more cluttered and harder to find, you will always find it helpful that you have written code in such a way that going back and making revisions is easier.

Bitmapped registers, or registers that contain multiple bit-oriented values such as the TRISIO, GPIO, and OPTION registers, should always be expressed in binary rather than a literal.  Doing this makes the code easier to read and understand since we know off-hand that in binary a "0" is false and a "1" is true.  There is no need to convert from hex code into binary while scanning through and/or writing our program.

Making the correction to our program, we will change the line TRISIO=0x8; to TRISIO=0b00001000; (Figure 2.)  A string of binary is preceded by a zero and a lowercase b.

Figure 2.


November 24, 2014: Edited this post by removing a section on Shadow Registers. This topic is covered in better detail later and was removed from here to avoid confusion and for simplicity of lesson flow.