{"id":2086,"date":"2011-03-28T20:31:01","date_gmt":"2011-03-29T00:31:01","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=2086"},"modified":"2012-09-07T10:16:16","modified_gmt":"2012-09-07T14:16:16","slug":"lab-11-multiplexing-seven-segment-led-displays","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/lab-11-multiplexing-seven-segment-led-displays\/","title":{"rendered":"Lab 11: Multiplexing seven segment LED displays"},"content":{"rendered":"<p>In <strong><a href=\"http:\/\/embedded-lab.com\/blog\/?p=971\" target=\"_blank\">Lab 6<\/a><\/strong>, we discussed about interfacing a seven segment LED display to a PIC microcontroller. The seven segments were driven individually through separate I\/O pins of the microcontroller. If we do just like that then for 4 seven segment LED displays, 28 I\/O pins will be required, which is quite a bit of resources and is not affordable by mid-range PIC microcontrollers. That&#8217;s why a multiplexing technique is used for driving multiple seven segment displays. This tutorial shows how to multiplex 4 common anode type seven segment LED displays with a PIC16F628A microcontroller.<\/p>\n<div id=\"attachment_2195\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/03\/TitleImage.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2195\" class=\"size-full wp-image-2195\" title=\"TitleImage\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/03\/TitleImage.jpg\" alt=\"\" width=\"550\" height=\"377\" \/><\/a><p id=\"caption-attachment-2195\" class=\"wp-caption-text\">Multiplexing 4 common anode seven segment LED displays<\/p><\/div>\n<p><!--more--><\/p>\n<p><strong>Circuit Diagram<\/strong><\/p>\n<p>The theory behind the multiplexing technique is simple. All the similar segments of multiple LED displays are connected together and driven through a single I\/O pin. In the circuit below, the seven segments are connected to PORTB through current limiting resistors Rs. A particular segment is active when the corresponding PORTB pin is low. However, it will not glow until it&#8217;s anode is connected to Vcc. You can see the anodes of the four LED displays are not directly connected to Vcc. Instead, 4 PNP transistors are used as switches to connect or disconnect the anode terminals from Vcc. When the base of the PNP transistor is low, the transistor conducts and corresponding digit&#8217;s common anode is connected to Vcc. Therefore, the transistor selects which displays is active. The conduction of the transistors are controlled by RA0 through RA3 pins of PORTA. Suppose, if we want to display 7 in the units digit place, then segments a, b, and c should be turned on first (which means RB0, RB1, RB2 are 0 and RB3-RB6 are 1) and then RA0 should be pulled low (while keeping RA1-RA3 high) so that only units digit display will be active. In order to display all 4 digits, each seven-segment display is activated\u00a0sequentially using an appropriate refresh frequency so that it will appear that all the them are turned on at the same time.<\/p>\n<div id=\"attachment_2093\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/03\/Lab11_Circuit_SevenSegmentMultiplexing.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2093\" class=\"size-medium wp-image-2093\" title=\"Lab11_Circuit_SevenSegmentMultiplexing\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/03\/Lab11_Circuit_SevenSegmentMultiplexing-550x345.jpg\" alt=\"\" width=\"550\" height=\"345\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/03\/Lab11_Circuit_SevenSegmentMultiplexing-550x345.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/03\/Lab11_Circuit_SevenSegmentMultiplexing.jpg 907w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><p id=\"caption-attachment-2093\" class=\"wp-caption-text\">Driving four 7-segment LED displays using multiplexing technique<\/p><\/div>\n<div id=\"attachment_2088\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/03\/MultiplexedSevenSegmentSetup.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2088\" class=\"size-medium wp-image-2088\" title=\"MultiplexedSevenSegmentSetup\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/03\/MultiplexedSevenSegmentSetup-550x415.jpg\" alt=\"\" width=\"550\" height=\"415\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/03\/MultiplexedSevenSegmentSetup-550x415.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/03\/MultiplexedSevenSegmentSetup.jpg 800w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><p id=\"caption-attachment-2088\" class=\"wp-caption-text\">Circuit setup for multiplexed seven segment LED displays<\/p><\/div>\n<p><strong>Software<\/strong><\/p>\n<p style=\"text-align: left;\">A sample program for a 4-digit up counter is developed using the MikroC compiler. The counter starts with 0, increments every second up to 9999, and reset to zero. The value of the counter is displayed on the four 7-segment LED displays. The complete program is described here. The function <em><strong>mask()<\/strong><\/em> takes in a numeric digit from 0-9 and returns the value for PORTB that will turn the selected LEDs on to display that particular number. For example, if the digit to be displayed is 1, then it requires segments b and c to be turned on. So the function <em><strong>mask()<\/strong><\/em> will return value 0xF9 (0b11111001) for PORTB, that will turn on the LED segments b and c. Remember that the return values of\u00a0<em><strong>mask()<\/strong><\/em> function would be different (complement of the current values) if the LED display is common cathode type.<\/p>\n<pre>\/*\r\n * Project name:\r\n    Decimal UP Counter with Four 7-Segment Display Multiplexing\r\n * Copyright:\r\n     (c) Rajendra Bhatt, 2011.\r\n * Description:\r\n     This code is an example of multiplexed Seven Segment Displays.\r\n     MCU:  PIC16F628A, 4.0 MHz external clock, MCLR Enabled\r\n     The common anodes of four seven segment dispalys are\r\n     connected to RA0, RA1, RA2 and RA3, whereas the seven\r\n     segments are driven through PORTB pins\r\n*\/\r\n\r\nunsigned short i, DD0, DD1, DD2, DD3;\r\nunsigned int Count;\r\n\/\/------ Function to Return mask for common anode 7-seg. display\r\nunsigned short mask(unsigned short num) {\r\n switch (num) {\r\n case 0 : return 0xC0;\r\n case 1 : return 0xF9;\r\n case 2 : return 0xA4;\r\n case 3 : return 0xB0;\r\n case 4 : return 0x99;\r\n case 5 : return 0x92;\r\n case 6 : return 0x82;\r\n case 7 : return 0xF8;\r\n case 8 : return 0x80;\r\n case 9 : return 0x90;\r\n } \/\/case end\r\n}\r\nvoid main() {\r\n  CMCON  |= 7;        \/\/ Disable Comparators\r\n  TRISB = 0x00; \/\/ Set PORTB direction to be output\r\n  PORTB = 0xff;    \/\/ Turn OFF LEDs on PORTB\r\n  TRISA = 0b00100000; \/\/ RA5 is input only\r\n  Count   =    0;  \/\/ Initial Value of Counter\r\n\r\n  do {\r\n  DD0 = Count%10;  \/\/ Extract Ones Digit\r\n  DD0 = mask(DD0);\r\n  DD1 = (Count\/10)%10; \/\/ Extract Tens Digit\r\n  DD1 = mask(DD1);\r\n  DD2 = (Count\/100)%10; \/\/ Extract Hundreds Digit\r\n  DD2 = mask(DD2);\r\n  DD3 = (Count\/1000);  \/\/ Extract Thousands Digit\r\n  DD3 = mask(DD3);\r\n\r\n  for (i = 0; i&lt;=50; i++) {\r\n      PORTB = DD0;\r\n      RA0_bit = 0;      \/\/ Select Ones Digit\r\n      RA1_bit = 1;\r\n      RA2_bit = 1;\r\n      RA3_bit = 1;\r\n      delay_ms(5);\r\n      PORTB = DD1;\r\n      RA0_bit = 1;\r\n      RA1_bit = 0;     \/\/ Select Tens Digit\r\n      RA2_bit = 1;\r\n      RA3_bit = 1;\r\n      delay_ms(5);\r\n      PORTB = DD2;\r\n      RA0_bit = 1;\r\n      RA1_bit = 1;\r\n      RA2_bit = 0;     \/\/ Select Hundreds Digit\r\n      RA3_bit = 1;\r\n      delay_ms(5);\r\n      PORTB   = DD3;\r\n      RA0_bit = 1;\r\n      RA1_bit = 1;\r\n      RA2_bit = 1;\r\n      RA3_bit = 0;     \/\/ Select Thousands Digit\r\n      delay_ms(5);\r\n      }\r\n      Count = Count + 1 ;\r\n      if (Count &gt; 9999) Count = 0;\r\n  } while(1);          \/\/ endless loop\r\n}<\/pre>\n<p style=\"text-align: left;\"><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/PIC16F628A\/MultiplexedSevenSegmentDisplay.zip\">Download the project source (MikroC) and HEX files<\/a><\/p>\n<p>After the program is loaded in to the microcontroller, power the circuit and watch the 4-digit up-counter running.<\/p>\n<div id=\"attachment_2087\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/03\/MultiplexedSevenSegmentDisplay.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2087\" class=\"size-full wp-image-2087\" title=\"MultiplexedSevenSegmentDisplay\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/03\/MultiplexedSevenSegmentDisplay.jpg\" alt=\"\" width=\"550\" height=\"437\" \/><\/a><p id=\"caption-attachment-2087\" class=\"wp-caption-text\">4-Digit Up-counter running<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Lab 6, we discussed about interfacing a seven segment LED display to a PIC microcontroller. The seven segments were driven individually through separate I\/O pins of the microcontroller. If we do just like that then for 4 seven segment LED displays, 28 I\/O pins will be required, which is quite a bit of resources and is not affordable by mid-range PIC microcontrollers. That&#8217;s why a multiplexing technique is used for driving multiple seven segment displays. This tutorial shows how to multiplex 4 common anode type seven segment LED displays with a PIC16F628A microcontroller.<\/p>\n","protected":false},"author":1,"featured_media":2087,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,29,38,183],"tags":[88,237,96,236],"class_list":["post-2086","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-labs","category-embedded-lessons","category-pic-tutorials","category-tips-and-tricks","tag-counter","tag-multiplexing","tag-pic16f628a","tag-seven-segment-led"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/2086","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=2086"}],"version-history":[{"count":15,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/2086\/revisions"}],"predecessor-version":[{"id":2198,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/2086\/revisions\/2198"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/2087"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=2086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=2086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=2086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}