Binary Counter

Binary Counter

A binary counter may not have many practical uses, however it’s a useful educational tool as it allows the student to see in action what the lecturer expects them to visualize. In the long run and when time is spent coding timers and interrupts, understanding how a binary timer functions will be necessary.

Binary?

Binary is a number system, much like the decimal system is a number system. The binary number system has only two digits 0 and 1 and can be called a base 2 numbering system. Although we don’t see this in action, all forms of computerized technology use binary and it is the basis for computerized decision making, memory, data storage and so on.

Theory

The PIC micro-controller is an 8 bit system and uses registers which contain 8 bits or 2^8. This means that the highest value which can be held by a single register is 255 or b11111111.
When displaying values, a bit set to one or high means that the output pin is on. Therefore, when looking at that value it’s apparent that each bit can be used to represent a bit set high on an input / output register for example PORTB. With all the bits configured as outputs, we have a visual display for a counter.

The physical display would use LEDs, an illuminated LED indicates a value of ‘1’.
Building on the Hello World project, The Hello World project illustrated how a flashing LED can be used to demonstrate that the silicon is doing something. To build on that experience the Binary counter will take that same code and demonstrate how with a few changes, a new project emerges.

For the Binary counter to work, we need 1 register which will be incremented once every second. The new value of the register will be then written to the display port or PORTB to display the new value.Our counter register will be called counter for convenience sake, and the code will change to the following:

copyright (c) 2011, ZarDynamix: An embedded solutions company

Binary Counter Code Sample for ProtoDev

*/

void bincount(){    

    Delay_ms(1000);  /* second pause */
    count = count++;    
    PORTB=count;

    }

void main(){ 

    ANSEL  = 0;            // Configure AN pins as digital
    ANSELH = 0;
    C1ON_bit = 0;          // Disable comparators
    C2ON_bit = 0;

    TRISB=0x00;           //All Bits to Output
    PORTB=0x00;           // Clear the output
    int count =0;         // Declare the variable 

    while (1) {           // Endless loop
              bincount();
               }
     }
}

Hardware changes

For the project to function, you need to upgrade your PCB’s hardware to include 8 LEDs all connected to PORTB. As shown in the diagram and the adjacent picture, this is one suggestion of how your project can be upgraded.

Conclusion

Once your PCB is good to go, you will have noticed that your LEDs illuminated, counted up to 255 and then rolled back to 0. The reason for this is that although technically the count went to 2^9 the register does not have enough bits to contain a value of that size. So for our purposes, the register rolled back to 0.

[E]