{"id":659,"date":"2010-11-08T12:33:52","date_gmt":"2010-11-08T17:33:52","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=659"},"modified":"2012-07-30T09:29:31","modified_gmt":"2012-07-30T13:29:31","slug":"lab-3-a-4-bit-binary-counter","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/lab-3-a-4-bit-binary-counter\/","title":{"rendered":"Lab 3: Four bit binary counter"},"content":{"rendered":"<p><strong>Description<\/strong><\/p>\n<p>Today&#8217;s lab session is about binary counting LEDs. The binary 1 and 0 will be represented by turning LEDs on and off. You will make a 4-bit binary counter (using 4 LEDs) that counts from 0 to 15 (0000-1111 binary). The four LEDs are connected to RC0 through RC3 port pins of PIC16F688 with current limiting resistors (470? each) in series. A push button switch is connected to pin RC4 to provide input for the counter. The counter starts from 0, and increase by 1 every time the button is pressed. When the counter reaches 15 (all LEDs on), it will reset to 0 on the next press of the button.<\/p>\n<p><strong>Required Theory<\/strong><\/p>\n<p>You should be familiar with the digital I\/O ports of PIC16F688 and their direction settings. If you are not, read <em><a href=\"..\/?p=238\"><strong>Digital I\/O Ports in PIC16F688<\/strong><\/a>. <\/em>Read previous lab session (<a href=\"http:\/\/embedded-lab.com\/blog\/?p=164\" target=\"_blank\"><strong><em>Lab 2: Basic digital input and output<\/em><\/strong><\/a>) to learn about reading inputs from a push button.<br \/>\n<!--more--><br \/>\n<strong>Circuit Diagram<\/strong><\/p>\n<p>The circuit diagram and its construction on the breadboard are shown in figures below. The PIC16F688 microcontroller uses its internal clock at 4.0 MHz.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=743\" rel=\"attachment wp-att-743\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-743 aligncenter\" title=\"Circuit_PIC16F688_4bitCounter\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Circuit_PIC16F688_4bitCounter-550x418.jpg\" alt=\"\" width=\"550\" height=\"418\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Circuit_PIC16F688_4bitCounter-550x418.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Circuit_PIC16F688_4bitCounter.jpg 726w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_4bitCounter_PIC16F688.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-720 aligncenter\" title=\"Img_4bitCounter_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_4bitCounter_PIC16F688-550x412.jpg\" alt=\"\" width=\"550\" height=\"412\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_4bitCounter_PIC16F688-550x412.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_4bitCounter_PIC16F688.jpg 580w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_4bitCounterPGM_PIC16F688.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-721 aligncenter\" title=\"Img_4bitCounterPGM_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_4bitCounterPGM_PIC16F688-550x389.jpg\" alt=\"\" width=\"550\" height=\"389\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_4bitCounterPGM_PIC16F688-550x389.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_4bitCounterPGM_PIC16F688.jpg 580w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p><strong>Software<\/strong><\/p>\n<p>Define PORTC pins RC0-RC3 as output, and the pin RC4 as an input. Disable comparators (CMCON0=7), and configure all I\/O pins as digital (ANSEL=0). Use Button() function to read input from the push button switch.<\/p>\n<pre><span style=\"color: #003366;\">\/*<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> Lab 3: 4-bit up counter<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> Internal Clock @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> Copyright @ Rajendra Bhatt<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> Nov 6, 2010<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\">*\/<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\">\/\/ Define Tact switch @ RC4<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> sbit Switch at RC4_bit;<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\">\/\/ Define button Switch parameters<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> #define Switch_Pin 4<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> #define Switch_Port PORTC<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> #define Debounce_Time 20 \u00a0\/\/ Switch Debounce time 20ms<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> unsigned short count;<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> void main() {<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> ANSEL = 0b00000000; \/\/All I\/O pins are configured as digital<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> CMCON0 = 0x07 ; \/\/ Disbale comparators<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> TRISC = 0b00010000; \/\/ PORTC all output except RC4<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> TRISA = 0b00001000; \/\/ PORTA All Outputs, Except RA3<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> count = 0;<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> PORTC = count;<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> do {<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> if (Button(&amp;Switch_Port, Switch_Pin, Debounce_Time, 0)) {<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> if (!Switch) {<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> count ++;<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> if (count ==16) count =0;<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> PORTC = count;<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> }<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> while (!Switch); \/\/ Wait till the button is \u00a0released<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> }<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\"> } while(1); \u00a0\/\/ Infinite Loop<\/span><\/pre>\n<pre id=\"_mcePaste\"><span style=\"color: #003366;\">}<\/span><\/pre>\n<p style=\"text-align: right;\"><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/PIC16F688\/4bit_counter.zip\" target=\"_blank\">Download HEX file<\/a><\/p>\n<p><strong>Output<\/strong><\/p>\n<p>The counter starts from 0 (all LEDs off), and is incremented by 1 on every button press. After it reaches 15, it overflows and takes the next value 0. This repeats forever.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_4bitCounterOP_PIC16F6881.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-723 aligncenter\" title=\"Img_4bitCounterOP_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_4bitCounterOP_PIC16F6881-550x417.jpg\" alt=\"\" width=\"550\" height=\"417\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_4bitCounterOP_PIC16F6881-550x417.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/11\/Img_4bitCounterOP_PIC16F6881.jpg 580w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description Today&#8217;s lab session is about binary counting LEDs. The binary 1 and 0 will be represented by turning LEDs on and off. You will make a 4-bit binary counter (using 4 LEDs) that counts from 0 to 15 (0000-1111 binary). The four LEDs are connected to RC0 through RC3 port pins of PIC16F688 with current limiting resistors (470? each) in series. A push button switch is connected to pin RC4 to provide input for the counter. The counter starts from 0, and increase by 1 every time the button is pressed. When the counter reaches 15 (all LEDs on),<\/p>\n","protected":false},"author":2,"featured_media":720,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,4,38],"tags":[80,81,20],"class_list":["post-659","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-labs","category-pic-projects","category-pic-tutorials","tag-binary-counter","tag-pic-lesson","tag-pic16f688"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=659"}],"version-history":[{"count":18,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/659\/revisions"}],"predecessor-version":[{"id":759,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/659\/revisions\/759"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/720"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}