Sunday, November 30, 2014

Bitwise Operators, and the Conclusion of Shadow Registers

The purpose of using a shadow register is to avoid directly accessing an input/output port, since doing so can cause stability issues with the program.  We use a shadow register to copy a desired value for a bit or bits to the I/O register.  This however, is a byte oriented process, meaning that our shadow register is a representation of the entire 8-bit I/O register.  How then, would we manipulate individual bits of a register without accessing them directly?  With Bitwise Operators, and a shadow register.

BITWISE OPERATORS

What is a bitwise operator?  Bitwise operators are used to manipulate one or more bits of operands. Our variables and port registers are operands, and things like +, -, and = are operators.  Here is an example of using an operator:

00010101 + 10110110 = 11001011

In this example, we have taken two bytes and added them together using the "plus" operator to produce a result.  Let's think about our last project, where we flashed a LED on and off by manipulating a single port register bit (GP0).  How can we change the state of only GP0 on the port register without accessing it directly?  Using a bitwise operator would do the trick.

Figure 1.
The bitwise operator compares the input of two bytes, makes manipulations one bit at a time, and outputs a single byte with the result of the comparisons.  These comparisons are made based on the state of each bit, meaning whether the bit is true (1) or false (0).  This allows the programmer to easily change the state of individual bits while still performing the operations using entire bytes.  The comparisons of these bit-wise true/false states are represented using what are called Truth Tables (figure 2.).  A bitwise operation looks at the true/false relationship between two bits at the same position (i.e. the 2nd bit of each variable), and produces a true/false result based on what bitwise operator was used.  Let's look at the truth tables now, and see how each bitwise operator works.

Figure 2.
With the exception of "NOT", each bitwise operator compares two inputs and produces one output.

AND

The bitwise operator AND, represented by the ampersand symbol "&" compares the two inputs and says that if input 1 AND input 2 are true, the output is true.  Let's look at an example where we have two variables, A and B.

A is equal to 10011010
B is equal to 00110111       If we perform a bitwise AND operation on these two variables:
AND result    00010010

Both bits must be true for the result to be true, otherwise the result is false.  Here is how this looks in the C language:

A & B = result

OR

The bitwise operator OR, represented by the vertical line symbol "|" or sometimes called a pipe, says that if bit 1 OR bit 2 are true, the result is true.  If neither bit is true, the result is false.

10011010     A
00110111     B
10111111     Result

A | B = result

EXCLUSIVE OR

The bitwise operator XOR (Exclusive OR) represented by the carrot symbol "^" says that if either A OR B but not both is true, the result is true, otherwise the result is false.  This is the same as saying the output is on if the inputs are different.

10011010     A
00110111     B
10101101     Result

A ^ B = result

NOT

Let's look at the bitwise operator NOT real quick, because the last two operators that follow are combinations of NOT and other bitwise operators.  NOT, represented by a tilde "~" is nothing more than an inverse and does not compare.  It simply makes a true a false, and a false a true, and has only one input and one output.

10011010     A
01100101     NOT A

~A = result

NOT AND

The bitwise operator NAND is a combination of NOT and AND "&~".  It says if, and only if both inputs are true, the output will be false, otherwise the result is true.  It is the direct inverse of a regular AND operation.  The result is thus the inverse of AND.

10011010     A
00110111     B
11101101     Result

A &~ B = result

NOT OR

Finally, the bitwise operator NOR (NOT OR) "|~" is a combination of NOT and OR, which is the direct inverse of the OR operation.  It says that if any input is true, the output is false, thus producing a result which is the inverse to a regular OR.

10011010     A
00110111     B
01000000     Result

A |~ B = result

USING BITWISE OPERATORS

Using bitwise operators is the easiest way to compare every bit across two bytes.  We can make a change to just one bit of a port without changing the other bits and without directly accessing the bit, such as in GP0=1.  Let's look at some ways we can do this using the bitwise operators.

All of our projects to this point have involved turning on and off a LED using the output of one of our I/O pins.  We know ahead of time what the state of all the GPIO pins will be when we're working with a simple project like the LED, as we have cleared all the pins at the beginning of our program (made them all 0).  So GPIO will equal 00000000.  When we want to turn on the LED we need to make GP0 equal to 1, and up to this point we have been accomplishing this task by writing GP0 = 1. But we know now that directly accessing the port pins is not advised, so we need to do the same thing using a bitwise operator.

A very common practice in programming is using what is called a mask, where we define the position of a bit we want to manipulate.  A mask for our LED connected to port 0 would look like this: #define LEDmask 0b00000001.  The # define preprocessor macro tells the compiler that every time it sees the label "LEDmask", it is replaced with the binary value 00000001 (remembering that binary values are preceded by "0b").

We now have two operands, the GPIO port register "00000000" and the LEDmask "00000001".  If we want to perform a bitwise operation on these two operands to make GP0 turn on, how could we do that?  Let's perform a bitwise OR operation!

GPIO 00000000
mask 00000001
result 00000001

Each bit has been compared with "if input 1 OR input 2 is true, result is true", and the result is that bit 0 is true.  In order to use this result to make GPIO equal to the result, we will combine operators and tell the program, perform this OR operation and make GPIO equal to the result.

GPIO |= LEDmask;

Now the OR operation has been performed and the result has been copied to the GPIO register.

What if we don't know the state of the GPIO pins, except for the bit we want to change? Our LED on pin GP0 is off, but the other pins are performing other tasks and could be 1 or 0 at any given time, will this mask still work?

GPIO 10011010
mask 00000001
result 10011011

Yes! None of the other GPIO pins have been changed from their original state except for the bit we wanted to change.  How cool!

Now we want to turn off the LED, how can we do that?  We return now to knowing the state of our pins, GPIO is equal to 00000001 and our led mask is equal to 00000001.  What bitwise operator will change GPIO to 00000000?  We know that if we have 0 and 0, we want the result to be 0, and if we have 1 and 1, we want that result to be 0 as well.  We definitely don't want any result to be 1 because we don't want to turn on any pins.  What truth table matches up to what we want to do?  Exclusive OR (XOR) looks good.

GPIO 00000001
mask 00000001
result 00000000

That worked out good!  Now what if our pins are unknown state, will XOR still work?

GPIO 10011011
mask 00000001
result 10011010

That worked too!  Because the XOR operator produces a false result when both pins are the same (0 or 1) but a true result when they're different, all pins will stay in the same state except the one our mask indicates.  Pretty neat huh?

Let's look at one more way to turn off the LED using bitwise operators.  What if we inverted the LED mask using the NOT operator. Then 00000001 becomes 11111110.  Now if we AND together GPIO with GP0 turned on, and the inverted mask:

GPIO 00000001
mask 11111110
result 00000000

GP0 is now cleared and thus turned off.  The code that would fully perform this function looks like this:

GPIO &= ~LEDmask;

This says GPIO ANDED together with the inverse of LEDmask and made equal to the result.  All of these bitwise operators have a use, and the more you write programs in C and want to manipulate bits, the more useful you will find them.

So far in our examples we have still been directly manipulating the GPIO port register.  Even writing GPIO &= LEDmask; is directly manipulating the bits of the GPIO register because the operation is performed one bit at a time.  There is one more thing we must do to avoid doing this, and it will conclude our discussion on shadow registers.  We have already seen the use of a variable named GPIOinit, which is a copy of the GPIO port register to define the initialization state.  To fully avoid direct manipulation of the register, we must declare another GPIO variable that we will make all of our changes to. I like to use GPIOimg as my variable name, which indicates that the variable is an image of GPIO but you could easily name this variable something like GPIO_copy.  It should be an 8-bit variable, so the unsigned char type would work great.

Now when we use the bitwise operators, we perform them on the variable instead of the actual register so that the bit-by-bit operation is not done on the actual register.  Then we copy the variable to the actual register which takes place as a byte-size operation. Here is how that looks:

GPIOimg &= LEDmask;
GPIO = GPIOimg;

The bitwise operation took place using the image, then the image is copied to the register.  No direct access to the GPIO bits is allowed using this setup, which completely eliminates the read/modify/write issues discussed earlier.  Our code is stable, and we are happy programmers!

PUTTING THIS ALL TOGETHER

We have learned quite a lot in this lesson and your head may be reeling, so let's put this lesson to practice and rewrite our last program using the bitwise operators.  Figure 3 shows the updated program, which flashes the LED for the number of times our count variable is equal to.  This time however, we are using bitwise operators and shadow registers.

Figure 3.
This program is all the same as the previous program except that on line 24, we have define our LED mask, and I have replaced the use of GP0=1 and GP0=0 with the bitwise operator routines.  The comments following each line explains the purpose of the line.

The large blank section in this program between lines 28 and 42 was code for the next lesson and can be ignored in this lesson, thus it was blanked out to avoid confusion.  This program will operate exactly the same as in the last lesson, the only difference being that this program is considered good, stable code.  You can copy this program into your MPLab software and try it for yourself.

I hope that at this point you are more familiar with the full use of shadow registers, and have a beginner's grasp on using bitwise operators. Please feel free to comment.

Tuesday, November 25, 2014

Variables, Counting Numbers, and the For Loop

At the end of the last lesson, we talked about using shadow registers to manipulate a port register and avoid manipulating individual bits of the port register because of Read/Modify/Write issues.  You also may have noticed I defined a variable, but I have only briefly talked about variables in an earlier lesson.  We are going to expand further variables in this lesson, and on shadow registers in the next lesson. This lesson will also introduce the "for" loop.

VARIABLES

A variable in C is basically a name which we assign to a memory location, which holds a number we are working with.  The compiler selects the next available memory location for us automatically.  In math, we use variables to represent a number which may change, such as T for time, or to represent a number which is a constant, such as the letter G for the gravitational constant. When we are writing a C program, we use variables as a placeholder for numbers to help make the code more manageable. Most C programs use many variables.

In our project, we have been looking at how to turn on and off an LED.  In our lesson about generating time delays, we looked at making the LED flash on and off for as long as we hold down our button.  In a new project, we will output the count of a variable to our LED so that we can visually see the variable in use. For this simple task, we are going to need two variables, but first, let's look at the types of variables, and the rules for naming them.

VARIABLE TYPES

Figure 1.

In one of our first lessons, A Crash Course Introduction to C, we saw the same table as in figure 1.  This table shows the different Variable Types, how much memory space they require, and what their decimal range is (what size number they can hold).  You will notice that unsigned Data Types refer to numbers who's values are only positive, while signed Data Types can be either positive or negative numbers.  Negative numbers in C would be used for things such as negative voltage values, temperatures, in calculator type programs, or even to determine what direction a motor is turning.  I have only ever used positive numbers in my C programs, so we will stick with those here as I don't want to give false information on a subject I'm not familiar enough with.

The size of the variable types refers to how much memory space they require. A bit requires one bit of memory space, where as a Character requires 8 bits, or 1 byte.  Decimal range tells us how far that Data Type can count.  We can tell one bit to be either on (1) or off (0).  We can tell a byte to be any combination of ones and zeros across 8 bits (00110100).  As it happens, one byte can be 256 combinations, thus we can use 1 byte to count from 0 to 255 (256 combinations if you include 0).  The mathematical formula for determining this range is: 2 possible states (0 or 1) across 8 bits, or 2^8 (two to the power of 8) = 256.  2^16 (for two bytes) = 65536.

If we want a variable which can count to 5, we would need to use one byte of memory space, since this is the smallest type which can count to 5.  Thus, we will select the "unsigned char" data type.  We could select something larger than this to accomplish the same thing, as using 2 bytes also can count to 5, but we would be wasting memory space.

A note about variable overflow.  An overflow is when a value exceeds the allowed number span. For instance, using the unsigned char data type which can range from 0 to 255, if we asked the program to do the calculation 255 + 1 the result would equal 0, and not 256 because we would have exceeded the range of the unsigned char type and the variable has overflowed.  If our program used the variable to continue counting up from 0 and not stop, the unsigned char data type would continue to count from 0 to 255 over and over again.  If you wish to avoid this overflow, you would need to choose a different variable data type, that can cover the range you require.

For our example program, we will utilize the unsigned char variable type.  All we have to do now is tell the compiler what we want.  Let's see how to define a variable type (Figure 2).

Figure 2.

You can see this code snippet begins a new program.  After the usual comments, and inclusion of the #include and configuration word at the top of the program, I have dedicated a space to defining variables. Notice the continued use of comments to break up the program into sections, which continues to help keep our program neat and easy to read.  Line 13 shows the statement "unsigned char count = 1;".  Remember that all statements end with a semicolon.  I have used the C Keyword "unsigned char" to define this variable as a character data type with no sign (as in no negative sign).

After the C Keyword comes our variable name, which in this case is "count". A name for a variable is called a "Variable Identifier", and you can name your variables just about anything, as long as it's not a C keyword such as "main" or "char".  A list of reserved C keywords is shown in figure 3. A C Variable Identifier is a sequence of letters and digits (no spaces), including the underscore which is considered a letter. The identifier must start with a letter and not a number, and although you can start with an underscore, you should avoid doing so, as such identifiers are reserved for the compiler's use and should not be defined by your programs.  Two more rules about variable names: They are case-sensitive, so "count" is different from "Count", and your variable name cannot exceed 255 characters.

Figure 3.
After naming the variable I have given it a value, in this case a value of 1.  Not all variables require assigning a value but it is appropriate in this example as you will soon see.  You may have noticed that I defined this section as "Define Global Variables".  Within a program you can have Global Variables and Local Variables.

Global variables are known throughout the entire program, and can be called again and again by any function within the program.  They take up a permanent residence in the program memory and are defined outside any function, including the main function.

A local variable however, is specific to just one function in the program and is created by the function itself.  Local variables are a great way to keep memory space down, as a local variable is created and stored in the RAM for only as long as the function needs it. The memory location is then freed for other uses.  As stated, a local variable can only be called within the function that created it, and would not be recognized anywhere else in the program.  If the same function is called again in the program, that local variable would be created again, used, and then discarded.  We will see a local variable used in our program below.

The For Loop

As we know, a loop in a C program is executed repeatedly so long as a condition is true, such as the while(1) loop which is always true.  A for loop is a function which repeats a specified number of times, and evaluates an expression on each loop to test whether the condition is still true.  If we had a task for instance, that we wanted to repeat ten times in a row, we could just type the same statements ten times in a row which would get the job done, but that would be ridiculous.  Alternatively, we would use a for loop.

The basic structure of a for loop is:   for(variable initialization; evaluate condition of true/false; update variable).  Notice that the for loop contains three statements within the parenthesis, and each statement is separated by a semicolon.  If I wanted to flash our LED 10 times, I could type the following instructions 10 times:

GP0 = 1;
GP0 = 0;

This would be impractical though, rather we will use a for loop to accomplish the same result.  Let's assume we have a variable named "flashes" that we defined in our global variables section.  The for loop would initialize that variable, use a conditional statement to see if we have reached 10 iterations of the loop, and if not, update the variable to account for the 1 iteration of the loop the function is currently doing.  Here's how that would look:

for(flashes = 0; flashes < 10; flashes ++)

What this function is doing is making our variable flashes equal to zero, testing whether flashes is less than 10, and if it is, incrementing the flashes variable by 1.  Flashes ++ is the same as saying flashes + 1. Each time the loop completes, the variable has 1 added to it, and when the conditional statement flashes < 10 is no longer true (i.e. flashes is no longer less than 10), the loop stops.

We can further accomplish the same thing by counting backwards, or decrementing our variable.  We set the value of the variable to 10, test whether the variable is greater than zero, and if it is we subtract 1.  Here's how that looks:

for(flashes = 10; flashes > 0; flashes --)

The same number of loops is performed using both methods.

Remember earlier I said that when we only need a variable within one function, we can use a local variable to save memory space?  This is a great example of where we can use a local variable.  Rather than declaring "flashes" to be a global variable, we really only need it within the for loop, so we will remove the global variable and declare flashes as a local variable within the function.  We are only counting to 10, so we can use an unsigned char variable type.  Here's how it looks:

for(unsigned char flashes = 0; flashes < 10; flashes ++)

I have simply created the variable flashes right in the for function.  The variable is created, used, and discarded all within this one function

Putting it Together; Completing the Program

Our program will operate by counting a variable up from 1, and then flashing a LED an equal number of times to visually represent the value of the counter variable.  We will accomplish this by use of two variables, one global and one local. Each time we complete a loop cycle, the counter gets incremented, the value of the counter gets copied to the local variable, and the for loop flashes the LED that many times.  We make use of the delay macros to slow program operation down so that we can actually see the on/off cycles of the LED, with a longer pause after each count increment so we are able to discern the value of the counter.

Figure 4.

Our completed program is shown in figure 4. I have made use of comments to describe the program in lines 1 through 7, and included the xc.h header file on line 9. I have heavily used comments to break up the program code, as our programs are slowly growing lengthier and more complex, which keeps us in good practice of making our code easy to read.

The configuration word then follows, which is the same as in previous programs.  Line 16 defines our global variable "count", which will be our counter variable. I set the value to 1 here so that on the first pass of the for loop, the LED flashes 1 time rather than 0 times.

The code on lines 18 through 20 define the I/O map, which sets up our shadow registers for the databus and Input/Output. This also helps to keep the program code more portable to other devices, and easier to change for other uses. It is a good practice to follow.

Since we are using the delay macros we must define our crystal frequency; line 23 does this.

Our main program function begins on line 26. I have then copied the initialization values of TRISIO and GPIO to their respective registers to initialize data direction.

Our endless while loop begins on line 30.  Line 32 starts our for loop function, where we declare an unsigned char variable type named flashes, set the value of flashes to equal the value of count, test that flashes is greater than 0, and if so, we subtract 1 from the flashes variable.  The instructions within the for loop function brackets ({ and }) will be executed repeatedly until flashes is equal to 0, then the loop will stop.

Lines 34 through 39 contain the instructions within the for function. The LED is turned on for 100 milliseconds (0.1 seconds), then turned off for 100 milliseconds.

Line 40 has exited the for function, and we are now within the while(1) loop, so these instructions only get executed once the for loop is down counting flashes down to zero.  Line 40 is our slightly longer delay, which helps us to discern the number of flashes we had in the for loop. In this case, the delay is 500 milliseconds.  There is a typo in the comment following this instruction, this delay is 1/2 second, not 3/4 second.

Line 41 adds 1 to our "count" variable so that the next time flashes is made equal to count, we see the addition as an added flash of the LED. Our program wraps up by closing the brackets on the while(1) and main functions.

Note the comments at the bottom of this program.  There is still one more lesson we need to cover before we can fully utilize shadow registers without making the program confusing.  We will cover that topic in the next lesson.

You can now update your project by typing in the code in Figure 4 and flashing the .hex file to your microcontroller.  Use the same hardware as in the previous project, but simply remove the push button, as it is not needed.

I hope you have enjoyed this lesson and found it to be useful. Comments are always welcome.

Sunday, November 16, 2014

Introduction to Shadow Registers

One of my good friends in years past over at allaboutcircuits.com, RJ Oldenkamp was kind enough to help me out a lot with the code for a project I was working on.  I am very much a novice now but far much more then.  I had written my program which was 565 lines of code, thinking it was pretty good. Everything worked well but I wanted a second set of eyes on it to maybe point out some things I'd missed and give me some help on proper code writing. Well RJ got a hold of the project and after teaching me endless things I'd never even thought of, and the "proper" way to write embedded C, the same project was now 815 lines, but far more stable than I even dreamed of at the time.

All of this transpired during the first year of me writing this blog, and in fact is a part of why I had been absent from it for so long. That and making a career change twice, moving half-way across the United States, and getting settled in a whole new routine.  The point of this story is that one of the things that RJ brought to my attention was the concept of Shadow Registers.

It is very difficult sometimes when explaining microprocessor things to beginners not to get too technical and start throwing terms that might confuse them; and the point of this blog after all, is to be truly beginner friendly, so I'm really trying to keep this as simple as possible.  After looking around, the best explanation I could find comes from Michael Rigby-Jones from Nortel Networks.

When you perform nearly any operation on a register, the PIC first reads the register, then it performs the operation on the register it just read, and finally the PIC writes the result back to the register. This is known as read/modify/write.  This process is fine when dealing with normal registers and most special function registers, but when you perform a read/modify/write on a port register such as TRISIO, PORTA, PORTB, etc or some timer/counter registers, it could cause problems.

When the microprocessor reads a port register, it reads the ACTUAL state of the pins on the port rather than the output latch, which can cause two problems.  If the pin is an input, then the input pin state will be read, the operation performed on it, and the result sent to the output latch. This may not immediately cause problems, but if that pin is made into an output later on, the state of the output latch may have changed from the time it was deliberately set by the code.

If the pin is defined as an output, the output latch and the actual pin should be in the same state, though in practice sometimes they aren't.  If you are controlling a capacitive load, for example, the pin may take some time to respond as it charges and discharges the capacitor.  A common problem occurs when using the bit set (GP1=1) or bit clear (GP1=0) function directly on a port.

Example, consider two lines of code:
GP4=0
GP4=1
If pin 4 is loaded in any way, such as being connected to a capacitive load, then it may not have time to respond to the first instruction before the second one is executed.  During the second instruction, the microcontroller reads the data port pins, sees that GP4 is set low (0), and during the write portion of the read/modify/write it writes GP4 low back into the output latch.  The result would be that GP4 never gets set.

Confused?  Think of it this way. A capacitor holds a charge. If you have a closed circuit with a battery, a capacitor, and an LED connected, the LED is on.  Think of the LED as our pin and your eyes as the microcontroller.  Turn on the pin (connect the battery), what is the state of the pin?  The LED is on right?  Now turn off the pin (disconnect the battery) and immediately determine the state of the pin.  Since the capacitor is discharging through the LED, we still see the LED on even though the battery is disconnected.  We have told the pin to turn off, but our eyes (the PIC) is seeing it as on.  It's the same kind of concept, and that's the best I can do to explain it.

So how do we fix it?  I have mentioned already that it is bad practice to use read/modify/write instructions directly on a port, so you use a shadow register.  We are taking one byte of RAM and using it to mimic the data port.  We perform all operations on the RAM location and then simply copy the RAM location to the port register.  This essentially works because rather than telling the MCU to write to just one I/O pin, we are writing to the entire register (TRISIO, PORTA, etc.).  What we are doing is creating a RAM variable (the shadow register) that enables us to control an individual GPIO pin without the risk of corrupting the settings of the other GPIO pins on the register.

Let's see this in practice.  We want to turn on our LED, which is connected to GP0.  We have been doing that by writing to the individual GP0 I/O port.
GP0=1;

Now we want to use a shadow register.  When we are setting aliases we will set a variable (more on this in the next lesson) to a RAM location to mimic the entire GP register. That is, rather than setting just GP0 to something, we will also set all the other pins at the same time.
unsigned char GPIOimg;   //I've just defined a RAM location as a GPIO Image.
GPIOimg = 00000001;    //I've just set the image of GPIO to set GP0 to on
GPIO = GPIOimg;           //I've just copied the image of GPIO to the actual GPIO port.

So rather than setting the single bit of GP0, I have written the entire port.  We are getting to a point in lessons where overlap is occurring, and as such, I'm having to use examples we haven't covered yet.  All will come together in the next lesson or two and it will make a lot more sense, and we will write another full program to put the lessons to constructive use.