{"id":164,"date":"2010-10-10T01:51:03","date_gmt":"2010-10-10T05:51:03","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=164"},"modified":"2012-07-30T09:29:33","modified_gmt":"2012-07-30T13:29:33","slug":"lab-2-basic-digital-input-and-output","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/lab-2-basic-digital-input-and-output\/","title":{"rendered":"Lab 2: Basic digital input and output"},"content":{"rendered":"<p><strong>Description<\/strong><\/p>\n<p>Today we will learn how to read digital inputs from a push button switch. A digital input has only two values: 1 and 0. The configuration of the push button switch is same as that of the reset switch except it goes to a different port pin. The status of the switch will be read through RC1 and every time when it is pressed, an LED connected to RC0 will be toggled ON and OFF.<\/p>\n<p><strong>Required Theory<\/strong><\/p>\n<p>You must be familiar with the digital I\/O ports of PIC16F688 and their direction settings. If you are not, read <em><a href=\"http:\/\/embedded-lab.com\/blog\/?p=238\"><strong>Digital I\/O Ports in PIC16F688<\/strong><\/a>.<\/em><\/p>\n<p><strong>Circuit Diagram<\/strong><\/p>\n<p>To our last setup (see <em><strong><a href=\"http:\/\/embedded-lab.com\/blog\/?p=148\">Lab 1: Flashing an LED<\/a><\/strong><\/em>), connect a push button switch to RC1 as shown below. The input ports of a PIC microcontroller have very high input impedance, and therefore, during normal condition when the switch is open, the RC1 pin is pulled &#8216;High&#8217; through the external 10K resistor. When the switch is pressed, RC1 is pulled to ground and the microcontroller will read it as logic &#8216;Low&#8217;. In this way, a simple push button switch with a pull-up resistor can be used to provide digital inputs (1 and 0) to the microcontroller.<!--more--><\/p>\n<p style=\"text-align: center;\"><a rel=\"attachment wp-att-740\" href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=740\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-740 aligncenter\" title=\"Circuit_BasicIO_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Circuit_BasicIO_PIC16F688-550x383.jpg\" alt=\"\" width=\"550\" height=\"383\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Circuit_BasicIO_PIC16F688-550x383.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Circuit_BasicIO_PIC16F688.jpg 674w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">Circuit for reading digital inputs.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_Digital_IO_PIC16F688.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-711 aligncenter\" title=\"Image_Digital_IO_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_Digital_IO_PIC16F688.jpg\" alt=\"\" width=\"580\" height=\"450\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_Digital_IO_PIC16F688.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_Digital_IO_PIC16F688-550x426.jpg 550w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_Digital_IO_PGM_PIC16F688.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-712 aligncenter\" title=\"Image_Digital_IO_PGM_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_Digital_IO_PGM_PIC16F688.jpg\" alt=\"\" width=\"580\" height=\"487\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_Digital_IO_PGM_PIC16F688.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_Digital_IO_PGM_PIC16F688-535x450.jpg 535w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">Prototyped circuit on the breadboard<\/p>\n<p style=\"text-align: left;\"><strong>Software<\/strong><\/p>\n<div id=\"_mcePaste\">\n<div id=\"_mcePaste\">\n<p id=\"_mcePaste\">Since the switch is connected to RC1, this port pin must be defined as input. This is done by setting the corresponding bit in the TRISC register. Again, don&#8217;t forget to disable the comparators and ADC channels. The code for this experiment is provided below. You should compile it with the MikroC compiler and load the output hex file inside the PICMicro. The MikroC compiler has an in-built library routine (named\u00a0Button) for reading digital inputs from push button switches. The syntax is,<\/p>\n<div style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/ButtonLib.jpg\"><\/a><\/div>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-310\" title=\"ButtonLib\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/ButtonLib-550x381.jpg\" alt=\"\" width=\"550\" height=\"381\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/ButtonLib-550x381.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/ButtonLib.jpg 851w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n<p>You can see that the Button command has debounce features built in.<\/p>\n<div>\n<table width=\"95%\" align=\"center\">\n<tbody>\n<tr>\n<td style=\"border: 1px solid #800000; padding: 6px; text-align: left; vertical-align: middle; background-color: #ffffff; letter-spacing: 0pt; word-spacing: 0pt;\"><strong>Debouncing a switch<\/strong><\/p>\n<p>When a mechanical switch (like a push button) is pressed or released to make or break an electrical contact, the switch tends to make multiple transitions between &#8216;open&#8217; and &#8216;close&#8217; before coming to the stable final state. This is called switch bounce and this activity may go on for tens of milliseconds. We cannot see this bouncing but a microcontroller is relatively fast as compared to the action of the switch, and is, therefore, able to recognize this as multiple button presses and respond accordingly. Therefore, the switch must be debounced before it&#8217;s status is read by a microcontroller. Debouncing circuits require extra hardware and thus raises the cost of the embedded system. The easiest solution is to implement debouncing in software. The simplest logic is to wait for a few milliseconds (5-20 ms) after the key press or release is detected, and read the final state of the switch again to make sure it is a valid button press or release.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Note that the configuration bits setting is same as in Lab 1.<\/p>\n<pre> \/*\r\n  Lab 2: Toggle LED with a Tact Switch Input\r\n  Internal Oscillator @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF\r\n  Copyright @ Rajendra Bhatt\r\n  Oct 8, 2010\r\n *\/\r\n \/\/ Define LED @ RC0 and Tact switch @ RC1\r\n sbit LED at RC0_bit;\r\n sbit Switch at RC1_bit;\r\n #define Switch_Pin 1\r\n #define Switch_Port PORTC\r\n #define Debounce_Time 20\r\n void main() {\r\n ANSEL = 0b00000000; \/\/All I\/O pins are configured as digital\r\n CMCON0 = 0x07 ; \/\/ Disbale comparators\r\n TRISC = 0b00000010; \/\/ PORTC All Outputs\r\n TRISA = 0b00001000; \/\/ PORTA All Outputs, Except RA3\r\n LED = 0;\r\n do {\r\n  if (Button(&amp;Switch_Port, Switch_Pin, Debounce_Time, 0)) {\r\n    if (!Switch) {\r\n    LED = ~LED;\r\n    }\r\n    while (!Switch); \/\/ Wait for release of the button\r\n  }\r\n } while(1);  \/\/ Infinite Loop\r\n}<\/pre>\n<p style=\"text-align: right;\"><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/PIC16F688\/TactSwitch.zip\">Download the complete mikroC project files<\/a><\/p>\n<p><strong>Output<\/strong><\/p>\n<\/div>\n<\/div>\n<p>Every time the switch is pressed, the LED toggles ON and OFF. Once the switch is pressed, the microcontroller will wait for its release before it detects another press.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_Digital_IO_OP_PIC16F688.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-713 aligncenter\" title=\"Image_Digital_IO_OP_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_Digital_IO_OP_PIC16F688.jpg\" alt=\"\" width=\"580\" height=\"450\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_Digital_IO_OP_PIC16F688.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_Digital_IO_OP_PIC16F688-550x426.jpg 550w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">\n","protected":false},"excerpt":{"rendered":"<p>Description Today we will learn how to read digital inputs from a push button switch. A digital input has only two values: 1 and 0. The configuration of the push button switch is same as that of the reset switch except it goes to a different port pin. The status of the switch will be read through RC1 and every time when it is pressed, an LED connected to RC0 will be toggled ON and OFF. Required Theory You must be familiar with the digital I\/O ports of PIC16F688 and their direction settings. If you are not, read Digital I\/O<\/p>\n","protected":false},"author":2,"featured_media":310,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,29,38],"tags":[20,41,40],"class_list":["post-164","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-labs","category-embedded-lessons","category-pic-tutorials","tag-pic16f688","tag-switch","tag-tact"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/164","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=164"}],"version-history":[{"count":87,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"predecessor-version":[{"id":338,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/164\/revisions\/338"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/310"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}