
{"id":288,"date":"2018-09-28T16:23:46","date_gmt":"2018-09-28T16:23:46","guid":{"rendered":"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/?page_id=288"},"modified":"2019-09-12T13:53:22","modified_gmt":"2019-09-12T13:53:22","slug":"topic-3-digital-inputs-and-multiple-outputs","status":"publish","type":"page","link":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/microcontrollers\/mbed-os-2\/courses\/embedded-systems-in-context-level-4\/topic-3-digital-inputs-and-multiple-outputs\/","title":{"rendered":"Topic 3 &#8211; Digital Inputs and Multiple Outputs"},"content":{"rendered":"<p><a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/embedded-systems-in-context-level-4\/\">Back to ToC<\/a><\/p>\n<p>In this section, we will look at digital inputs, and how we might use them to control digital outputs.\u00a0We will be using a simple \u201c<a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/push-to-make-switch-glossary-entry\/\" target=\"_blank\" rel=\"noopener\"><b>push-to-make switch<\/b><\/a>\u201d to generate a single digital bit of data.<\/p>\n<h1><b>Intended Learning Outcomes<\/b><\/h1>\n<ol>\n<li>Configure an digital input with a push to make switch<\/li>\n<li>Use a pull up\/down resistor to avoid floating inputs<\/li>\n<li>Write software to read a switch state<\/li>\n<li>Use programming methods to avoid switch bounce<\/li>\n<li>Control multiple digital outputs with binary numbers<\/li>\n<li>Use binary operators to manipulate binary numbers<\/li>\n<\/ol>\n<hr \/>\n<h1>Activity 3.1 &#8211; Simple Switch Input<\/h1>\n<figure id=\"attachment_291\" aria-describedby=\"caption-attachment-291\" style=\"width: 150px\" class=\"wp-caption alignright\"><img loading=\"lazy\" decoding=\"async\" class=\"size-thumbnail wp-image-291\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/SPST_photo-150x150.png\" alt=\"\" width=\"150\" height=\"150\" \/><figcaption id=\"caption-attachment-291\" class=\"wp-caption-text\">Push to Make Switch<\/figcaption><\/figure>\n<figure id=\"attachment_290\" aria-describedby=\"caption-attachment-290\" style=\"width: 150px\" class=\"wp-caption alignright\"><img loading=\"lazy\" decoding=\"async\" class=\"size-thumbnail wp-image-290\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/pushtomake_switch-150x70.png\" alt=\"\" width=\"150\" height=\"70\" \/><figcaption id=\"caption-attachment-290\" class=\"wp-caption-text\">Schematic Symbol for a Push-to-Make Switch<\/figcaption><\/figure>\n<p>Remember that a single digital signal is either ON or OFF. Numerically, we represent this as 1 or 0 (a single binary digit).\u00a0The simplest way to generate a single bit input signal is with a push switch.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The schematic to generate a digital input is as follows.<\/p>\n<figure id=\"attachment_294\" aria-describedby=\"caption-attachment-294\" style=\"width: 215px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-294 size-medium\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Activity31-SW1-215x300.png\" alt=\"\" width=\"215\" height=\"300\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Activity31-SW1-215x300.png 215w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Activity31-SW1.png 335w\" sizes=\"auto, (max-width: 215px) 100vw, 215px\" \/><figcaption id=\"caption-attachment-294\" class=\"wp-caption-text\">Schematic for a single push switch input<\/figcaption><\/figure>\n<p>In addition to the switch, wire up 3 LED\u2019s as shown here.<\/p>\n<figure id=\"attachment_295\" aria-describedby=\"caption-attachment-295\" style=\"width: 247px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-295\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Activity31-AllSwitches.png\" alt=\"\" width=\"247\" height=\"750\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Activity31-AllSwitches.png 427w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Activity31-AllSwitches-99x300.png 99w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Activity31-AllSwitches-329x999.png 329w\" sizes=\"auto, (max-width: 247px) 100vw, 247px\" \/><figcaption id=\"caption-attachment-295\" class=\"wp-caption-text\">Schematic for the LED outputs<\/figcaption><\/figure>\n<p><span style=\"letter-spacing: 0.05em\">These will be used for outputs.A suggested prototype board layout is shown below:<\/span><\/p>\n<figure id=\"attachment_297\" aria-describedby=\"caption-attachment-297\" style=\"width: 1808px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-297 size-full\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/LEDS-and-Switch-F429ZI.png\" alt=\"\" width=\"1808\" height=\"1833\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/LEDS-and-Switch-F429ZI.png 1808w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/LEDS-and-Switch-F429ZI-296x300.png 296w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/LEDS-and-Switch-F429ZI-768x779.png 768w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/LEDS-and-Switch-F429ZI-1010x1024.png 1010w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/LEDS-and-Switch-F429ZI-985x999.png 985w\" sizes=\"auto, (max-width: 1808px) 100vw, 1808px\" \/><figcaption id=\"caption-attachment-297\" class=\"wp-caption-text\">Prototype board layout for the three LEDs and Push Switch<\/figcaption><\/figure>\n<h2>Task 3.1.1<\/h2>\n<p>In the first task, we are simply going to observe the digital input with a DVM when the =button is pressed and released.<\/p>\n<table style=\"border-collapse: collapse;border: 1px solid black\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"middle\">Using the digital volt meter, measure the voltage across R4 when the button is pressed, and when it is released<span class=\"Apple-converted-space\">\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Tip &#8211; this can be tricky, so get someone to press and release the button for you while you hold the DVM leads.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<figure id=\"attachment_299\" aria-describedby=\"caption-attachment-299\" style=\"width: 200px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-299\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/DVM-measurement-points-528x1024.png\" alt=\"Setting your Digital Volt Meter (DVM) to Voltage mode, measure the potential difference between these two points.\" width=\"200\" height=\"388\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/DVM-measurement-points-528x1024.png 528w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/DVM-measurement-points-155x300.png 155w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/DVM-measurement-points-515x999.png 515w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/DVM-measurement-points.png 666w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><figcaption id=\"caption-attachment-299\" class=\"wp-caption-text\">Setting your Digital Volt Meter (DVM) to DC Voltage mode, measure the potential difference between these two points<\/figcaption><\/figure>\n<hr \/>\n<h1>Activity 3.2 &#8211; Reading Digital Inputs<\/h1>\n<p>For this task, we are going to read the digital input using software. The objective is as follows:<\/p>\n<ul>\n<li>When the user depresses a button, the red LED switches ON and stays ON (until the system is reset).<\/li>\n<\/ul>\n<p>Remember &#8211; the input is connected to D4.<span class=\"Apple-converted-space\">\u00a0<\/span>Maybe unsurprisingly, we use a component called a <span class=\"lang:default highlight:0 decode:true crayon-inline \">DigitalIn<\/span>\u00a0.<\/p>\n<h2>Task 3.2.1<\/h2>\n<table style=\"border-collapse: collapse;border: 1px solid black\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"middle\">Create a new project and paste in the code below. Build and run to test.<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">When the yellow and green lights come on, press and release SW1.<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Press the black RESET button on the Nucleo board to run again.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"theme:xcode lang:c++ decode:true \">#include \"mbed.h\"\r\n\r\nDigitalOut  red_led(D7);\r\nDigitalOut  yellow_led(D6);\r\nDigitalOut  green_led(D5);\r\nDigitalIn   SW1(D4);\r\n\r\nint main() {\r\n\r\n    \/\/Switch on Yellow and Green to indicate\r\n    \/\/that the code is running\r\n    yellow_led = 1;\r\n    green_led = 1;\r\n    red_led = 0; \/\/Set RED LED to OFF\r\n    \r\n    \/\/ Wait for SW1 to be pressed\r\n    while (SW1 == 0) { }\r\n    \r\n    red_led = 1;\t\/\/Turn ON LED\r\n    \r\n    while (1) { }\t\t\/\/Repeat forever\r\n}<\/pre>\n<p>Once again, we use of the the component from Mbed to make interfacing with hardware simple.<\/p>\n<p style=\"padding-left: 30px\"><span class=\"lang:c++ decode:true crayon-inline \">DigitalIn SW1(D4);<\/span><\/p>\n<p>where <span class=\"lang:default highlight:0 decode:true crayon-inline \">SW1<\/span>\u00a0<b>\u00a0<\/b>is an instance of <span class=\"lang:default highlight:0 decode:true crayon-inline \">DigitalIn<\/span>\u00a0, which can read pin <span class=\"lang:default highlight:0 decode:true crayon-inline \">D4<\/span>\u00a0.\u00a0A critical line in this code is as follows:<\/p>\n<p style=\"padding-left: 30px\"><span class=\"lang:c++ decode:true crayon-inline \">while (SW1 == 0) { }<\/span><\/p>\n<p>While the digital input <span class=\"lang:default highlight:0 decode:true crayon-inline \">SW1<\/span>\u00a0 is equal to zero, run the (empty) code block between the curly braces and then repeat.<\/p>\n<p><b>NOTE<\/b>:<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p style=\"padding-left: 30px\">the operator <span class=\"lang:c++ highlight:0 decode:true crayon-inline \">==<\/span>\u00a0 is used to <b>test<\/b> for equality.<\/p>\n<p style=\"padding-left: 30px\">the operator <span class=\"lang:c++ decode:true crayon-inline \">=<\/span>\u00a0 is used to <b>assign<\/b> a value (from right to left)<\/p>\n<p><i>It is very easy to get these confused!<\/i><\/p>\n<p>Once the switch is pressed, an attempt to read <span class=\"lang:c++ highlight:0 decode:true crayon-inline\">SW1<\/span>\u00a0 will return a 1. The condition <span class=\"lang:c++ decode:true crayon-inline \">SW==0<\/span>\u00a0 will now be <b>false<\/b>, so the while loop will exit and run to the next statement. The next statement switches on the red LED.<\/p>\n<p style=\"padding-left: 30px\"><span class=\"lang:c++ decode:true crayon-inline\">red_led = 1;<\/span><\/p>\n<p>Let\u2019s recap the syntax of a <a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/while-loop-glossary-entry\/\" target=\"_blank\" rel=\"noopener\"><b>while loop<\/b><\/a>.<\/p>\n<pre class=\"lang:c++ decode:true \">while (&lt;condition to enter the loop&gt;) {\r\n\r\n    \/\/ Loop body\r\n\r\n}\r\n\r\n<\/pre>\n<p><span style=\"letter-spacing: 0.05em\">In words:<\/span><\/p>\n<blockquote><p>\u201dIf the condition is met, execute the code in the loop body and then repeat, otherwise exit and continue executing the next statement.\u201d<\/p><\/blockquote>\n<p>A graphical depiction of this this is given in the <a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/while-loop-glossary-entry\/\" target=\"_blank\" rel=\"noopener\">glossary<\/a>.<\/p>\n<h2>Task 3.2.2<\/h2>\n<table style=\"border-collapse: collapse;border: 1px solid black\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"middle\">Modify the code so that the red LED only comes on after the SW is pressed <b>and<\/b> then released<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Hint: follow the flow chart below<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Test your solution. Is it entirely reliable? Can you improve on it?<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<figure id=\"attachment_301\" aria-describedby=\"caption-attachment-301\" style=\"width: 200px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-301\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Task322FlowChart.png\" alt=\"\" width=\"200\" height=\"558\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Task322FlowChart.png 329w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Task322FlowChart-108x300.png 108w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><figcaption id=\"caption-attachment-301\" class=\"wp-caption-text\">Flow chart depicting an algorithm to wait for the press and release of a switch.<\/figcaption><\/figure>\n<p>You <em>probably<\/em> found this to be an unreliable solution. In the next section, you will learn about \u201cswitch bounce\u201d and how to avoid it.<\/p>\n<h1><b>Switch Bounce<\/b><\/h1>\n<p>The reality is that mechanical switches are not perfect. Most switches suffer switch bounce to some extent. An ideal switch signal might be assumed to be a step waveform, as depicted below.<\/p>\n<figure id=\"attachment_306\" aria-describedby=\"caption-attachment-306\" style=\"width: 594px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-306\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/IdealSwitchSignal.png\" alt=\"\" width=\"594\" height=\"300\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/IdealSwitchSignal.png 1394w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/IdealSwitchSignal-300x152.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/IdealSwitchSignal-768x388.png 768w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/IdealSwitchSignal-1024x517.png 1024w\" sizes=\"auto, (max-width: 594px) 100vw, 594px\" \/><figcaption id=\"caption-attachment-306\" class=\"wp-caption-text\">Ideal model of a digital signal generated from a mechanical switch<\/figcaption><\/figure>\n<p>In reality, this is never the case. It is impossible for a signal to change instantaneously. Furthermore, at the moment switch contacts make or break, partial connections and disconnections may occur, resulting in a phenomena known as switch bounce. This is depicted in the figure below:<\/p>\n<figure id=\"attachment_307\" aria-describedby=\"caption-attachment-307\" style=\"width: 608px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-307\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/RealSwitchSignal.png\" alt=\"\" width=\"608\" height=\"300\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/RealSwitchSignal.png 1386w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/RealSwitchSignal-300x148.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/RealSwitchSignal-768x379.png 768w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/RealSwitchSignal-1024x505.png 1024w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><figcaption id=\"caption-attachment-307\" class=\"wp-caption-text\">Depicting switch bounce from a mechanical switch which could be registered as 3 events: (1)ON,(2)OFF, (3)ON.<\/figcaption><\/figure>\n<p>As you can see in the second diagram, the waveform is not a perfect step like the ideal.<\/p>\n<p>There are a number of additional phenomena we much consider:<\/p>\n<ul>\n<li>Noise &#8211; random signals superimposed on top of the waveform. This can originate from internal sources, including the electronic components in your circuit and external sources, such as strip lighting, radio transmitters and other nearby electronic devices.<\/li>\n<li>Reactive effects &#8211; if you\u2019ve not studied reactance yet, it should suffice to say that perfect step waveforms are almost impossible &#8211; signals often rise as an exponential curve (although it might be very rapid) and can even &#8220;ring&#8221; (overshoot) due to stray intrinsic reactive effects of components, connectors and electronic components.<\/li>\n<\/ul>\n<p>This reinforces the points that electronics is ultimately analog and signals are rarely ideal.<\/p>\n<p>A problem with such phenomena is that their occurrence is stochastic (random, probability based). An engineer must be mindful of such issues, and design systems to minimize the probability of such events occurring or causing malfunction, especially in safety critical systems.<\/p>\n<p>In the case of switch bounce, the exact nature is highly unpredictable.<\/p>\n<h2>Task 3.2.3<\/h2>\n<p>Consider the following revised flow-chart below. This has added a delay after the first press is detected.<\/p>\n<figure id=\"attachment_309\" aria-describedby=\"caption-attachment-309\" style=\"width: 200px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-309\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/FloeChartForSwitchBounce.png\" alt=\"\" width=\"200\" height=\"586\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/FloeChartForSwitchBounce.png 339w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/FloeChartForSwitchBounce-102x300.png 102w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><figcaption id=\"caption-attachment-309\" class=\"wp-caption-text\">Flow chart showing the introduction of a small delay to combat the effects of switch bounce<\/figcaption><\/figure>\n<table style=\"border-collapse: collapse;border: 1px solid black\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"middle\">Modify the code to add this delay<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Does it impact on the user experience? Is the delay long enough?<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">What would happen if you made the delay too long (e.g. 5s)?<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\"><i>hint:<\/i> use the wait function that we used yesterday<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Adding the delay adds what is often known as <b>latency<\/b>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>This task illustrates a few key points:<\/p>\n<ul>\n<li>electronic signals are rarely \u201cideal\u201d<\/li>\n<li>switch inputs suffer \u201cswitch bounce\u201d<\/li>\n<li>we sometimes have to make trade-offs, such as reliability and latency<span class=\"Apple-converted-space\">\u00a0<\/span><\/li>\n<\/ul>\n<hr \/>\n<h1>Activity 3.3 &#8211; Multiple Outputs<\/h1>\n<p>The <a href=\"https:\/\/os.mbed.com\" target=\"_blank\" rel=\"noopener\">mbed.com<\/a>\u00a0website contains a lot of useful reference and tutorial information. A great place to start is the online documentation <a href=\"https:\/\/os.mbed.com\/docs\/latest\" target=\"_blank\" rel=\"noopener\">https:\/\/os.mbed.com\/docs\/latest<\/a><\/p>\n<p>Much of what we have used to far can be found under the section on Driver APIs\u00a0<a href=\"https:\/\/os.mbed.com\/docs\/latest\/apis\/drivers.html\" target=\"_blank\" rel=\"noopener\">https:\/\/os.mbed.com\/docs\/latest\/apis\/drivers.html<\/a><\/p>\n<p>So far, we have only read from a single input pin or asserted a single output pin at a time. We often want to read groups of pins at the same time*<\/p>\n<p>*remember that integer values can be represented in binary as groups of bits (also known as a bus).<\/p>\n<p>Consider all 3-bit binary numbers in the table below.<\/p>\n<figure id=\"attachment_311\" aria-describedby=\"caption-attachment-311\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-311\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Binary2Decimal-3bitTable.png\" alt=\"\" width=\"300\" height=\"313\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Binary2Decimal-3bitTable.png 910w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Binary2Decimal-3bitTable-287x300.png 287w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Binary2Decimal-3bitTable-768x802.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption id=\"caption-attachment-311\" class=\"wp-caption-text\">Showing the 3-bit unsigned binary representation of an unsigned decimal<\/figcaption><\/figure>\n<p>In the next task, we are going to convert decimal numbers to binary using the LEDs as an output display.\u00a0The red LED will be the <b>most significant bit<\/b> (BIT2) and the greed LED the <b>least significant bit<\/b> (BIT0).<\/p>\n<figure id=\"attachment_312\" aria-describedby=\"caption-attachment-312\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-312\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Binary2Decimal-LEDs.png\" alt=\"\" width=\"300\" height=\"124\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Binary2Decimal-LEDs.png 926w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Binary2Decimal-LEDs-300x124.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Binary2Decimal-LEDs-768x318.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption id=\"caption-attachment-312\" class=\"wp-caption-text\">Pin assignments for the LEDs<\/figcaption><\/figure>\n<h2>Task 3.3.1<\/h2>\n<p>Follow the steps in the following table<\/p>\n<table style=\"border-collapse: collapse;border: 1px solid black\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"middle\">Create a new project<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Paste in the code below<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Try setting <span class=\"lang:default decode:true crayon-inline \">binaryOutput<\/span>\u00a0 to different values (0..7)<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\"><i>Do you see the binary equivalent displayed in the LEDs? If not, ask the tutor to explain (this is rather critical!)<\/i><\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\"><i>What happens if you set binaryOutput to a value greater than 7? (hint : work out what the binary equivalent, then compare)<\/i><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"theme:xcode lang:c++ decode:true \">#include \"mbed.h\"\r\n\r\n\/\/lsb first\r\nBusOut binaryOutput(D5, D6, D7);\r\n\r\nint main() {\r\n\r\n    \/\/Try different values\r\n    binaryOutput = 3;\r\n    \r\n    while (1) { }\r\n}<\/pre>\n<p>The new component is <span class=\"lang:default decode:true crayon-inline \">BusOut<\/span>\u00a0 which takes a variable number of parameters. These parameters specify (lsb first) which pins to use for the binary output.<\/p>\n<p>Assigning a decimal to the instance \u201c<span class=\"lang:default decode:true crayon-inline\">binaryOutput<\/span>\u201d converts the decimal to binary, and sets the pins accordingly.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Exhaustively testing all values between 0 and 7 is fairly time consuming, especially if you needed to repeat the tests.<\/p>\n<p>Next we are going to use variables and while loops to cycle through the numbers 0..7 automatically.<\/p>\n<h2>Task 3.3.2<\/h2>\n<p>In the code below we use a <a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/do-while-loop-glossary-entry\/\" target=\"_blank\" rel=\"noopener\"><b>do-while loop<\/b><\/a>. See the <a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/do-while-loop-glossary-entry\/\" target=\"_blank\" rel=\"noopener\">glossary<\/a> \/ lecture slides for a reminder on how do-while loops work.<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true \">#include \"mbed.h\"\r\n\r\nBusOut binaryOutput(D5, D6, D7);\r\n\/\/DigitalIn SW1(D4);\r\n\r\nint main() {\r\n\r\n    unsigned int uiCount = 0;\r\n\r\n    while(1) {\r\n        \r\n        do {\r\n            \/\/Write decimal to the output\r\n            binaryOutput = uiCount;\r\n\t\t\t\r\n\t    \/\/Increment the count using +\r\n            uiCount = uiCount + 1;  \r\n            wait(1.0);              \/\/Delay for 1s\r\n\r\n\t         \/\/Condition to repeat\r\n        } while (uiCount &lt; 8);    \r\n\t\t\t\t\t\t\t\r\n        \/\/Reset the count\r\n        uiCount = 0;\r\n        \r\n    } \/\/end while(1)\r\n} \/\/end main\r\n<\/pre>\n<table style=\"border-collapse: collapse;border: 1px solid black\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"middle\">Create a new project<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Paste in the code code above<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Run the code and observe the outputs<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Modify the code to count down from 7..0<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\"><i>hints:<\/i> you will need to change the condition to re-enter the loop; note that <span class=\"lang:c++ decode:true crayon-inline \">uiCount<\/span>\u00a0 is unsigned, so cannot be negative.\u00a0If you get stuck, you can \u2018peek\u2019 at the <a href=\"https:\/\/os.mbed.com\/teams\/Students-Plymouth-University-UK-SoCEM\/code\/Task332Solution\/file\/7b9a83230461\/main.cpp\/\" target=\"_blank\" rel=\"noopener\">solution<\/a>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1><\/h1>\n<hr \/>\n<h1>Activity 3.4 Bitwise Operators<\/h1>\n<p>A very important skill is to be able to manipulate individual bits in an integer variable. This includes:<\/p>\n<ul>\n<li><b>Setting<\/b> &#8211; refers to setting a specified bit (or group of bits) to 1 without affecting the others.<\/li>\n<li><b>Resetting<\/b> &#8211; Same as setting, only setting to 0 instead of 1<\/li>\n<li><b>Toggling<\/b> &#8211; refers to flipping a specified bit (or group of bits) to their opposite state (0 to 1 or 1 to 0), again without affecting the others.<\/li>\n<li><a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/logical-shift-glossary-entry\/\" target=\"_blank\" rel=\"noopener\"><b>Logical Shift left<\/b><\/a> &#8211; all bits move one position to the left, a zero is pushed in from the right and the most significant bit(s) is lost<\/li>\n<li><a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/logical-shift-glossary-entry\/\" target=\"_blank\" rel=\"noopener\"><b>Logical Shift right<\/b><\/a> &#8211; all bits move to the right, the least significant bit(s) is lost and a zero is pushed in from the left<\/li>\n<\/ul>\n<p>These operations can be performed using \u201clogical operators\u201d. These are summarised below (see lecture slides for examples).<\/p>\n<figure id=\"attachment_318\" aria-describedby=\"caption-attachment-318\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-318\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitManipulationOperators.png\" alt=\"\" width=\"300\" height=\"128\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitManipulationOperators.png 1346w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitManipulationOperators-300x128.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitManipulationOperators-768x328.png 768w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitManipulationOperators-1024x437.png 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption id=\"caption-attachment-318\" class=\"wp-caption-text\">Bit Manipulation Operators<\/figcaption><\/figure>\n<h2><b>Setting A BIT<\/b><\/h2>\n<p>Consider the example where the unsigned integer variable <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-39adccca45c7a41f32459e2bbcfa5e37_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#117;\" title=\"Rendered by QuickLaTeX.com\" height=\"8\" width=\"10\" style=\"vertical-align: 0px;\"\/>\u00a0is equal to binary <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-ac99a9929b0637ffabe5cbfe52941998_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#49;&#48;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"26\" style=\"vertical-align: 0px;\"\/>. In decimal this is equivalent to <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-dcceaa6d0bfed1c680b553a9c328fe0c_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#49;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#52;&#32;&#43;&#32;&#48;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#50;&#32;&#43;&#32;&#48;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#49;&#32;&#61;&#32;&#52;\" title=\"Rendered by QuickLaTeX.com\" height=\"14\" width=\"167\" style=\"vertical-align: -2px;\"\/>.<span class=\"Apple-converted-space\">\u00a0<\/span>If we SET bit 0 to a 1, we get binary <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-f1b0eb3071d256b5202d92bfd17e9da8_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#49;&#48;&#49;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"25\" style=\"vertical-align: 0px;\"\/>, which is equal to decimal 5.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-319\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitSettingExample.png\" alt=\"\" width=\"300\" height=\"106\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitSettingExample.png 1084w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitSettingExample-300x106.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitSettingExample-768x271.png 768w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitSettingExample-1024x361.png 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Note how only bit 0 is changed, and the others are left unaffected. How is this done in code? We use the logical<a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/logical-or-glossary-entry\/\" target=\"_blank\" rel=\"noopener\"><b> OR <\/b><\/a>operator <span class=\"lang:default decode:true crayon-inline \">|<\/span>\u00a0 , which looks like a vertical bar (<a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/logical-or-glossary-entry\/\" target=\"_blank\" rel=\"noopener\">see the glossary<\/a> for a reminder).<\/p>\n<h2>Task 3.4.1<\/h2>\n<p>Complete the following:<\/p>\n<table style=\"border-collapse: collapse;border: 1px solid black\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"middle\">Create a new project<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Paste in the code code below<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Read the comments and complete the code<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"theme:xcode lang:c++ decode:true \">#include \"mbed.h\"\r\n\r\n\/\/Global objects\r\nBusOut binaryOutput(D5, D6, D7);\r\nDigitalIn SW1(D4);\r\n\r\n\/\/Function prototypes\r\nvoid waitForButtonPress();\r\n\r\n\/\/Main function\r\nint main() {\r\n    \r\n    \/\/Create a variable to hold the bit pattern\r\n    unsigned int u;\r\n    \r\n    \/\/Flash LED's to indicate the code is running\r\n    binaryOutput = 7;\r\n    wait(0.5);\r\n    binaryOutput = 0;\r\n    \r\n    \/\/Main Loop\r\n    while(1) {\r\n        \r\n        u = 0;             \/\/Set initial value\r\n        binaryOutput = u;  \/\/Show binary on LED's\r\n        waitForButtonPress();   \/\/Call function\r\n        \r\n        \/\/Here is the first - use | to set bit 1\r\n        u = u | 2;        \/\/OR with binary 010\r\n        binaryOutput = u;\r\n        waitForButtonPress();\r\n        \r\n        \/\/Modify u with the | to set bit 2\r\n        \/\/WRITE CODE HERE\r\n        \r\n        binaryOutput = u;\r\n        waitForButtonPress();\r\n        \r\n        \r\n        \/\/Modify u with the | to set bit 0\r\n        \/\/WRITE CODE HERE\r\n        \r\n        binaryOutput = u;\r\n        waitForButtonPress();\r\n        \r\n    } \/\/end while(1)\r\n} \/\/end main\r\n\r\n\r\n\r\n\/\/This is known as a C function.\r\n\/\/This saves a lot of code repetition\r\nvoid waitForButtonPress() {\r\n    while (SW1 == 0);\r\n    wait(0.25);\r\n    while (SW1 == 1);\r\n}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-348\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Task341-LEDTable.png\" alt=\"\" width=\"200\" height=\"116\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Task341-LEDTable.png 678w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Task341-LEDTable-300x173.png 300w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/>Remember the green LED is the least significant bit (bit 0).\u00a0You can <a href=\"https:\/\/os.mbed.com\/teams\/Students-Plymouth-University-UK-SoCEM\/code\/Task341Solution\/file\/9de80b65dfc5\/main.cpp\" target=\"_blank\" rel=\"noopener\">look at the solution<\/a> if you are stuck.<\/p>\n<h2><b>Resetting a bit<\/b><\/h2>\n<p>Consider the example where the unsigned integer variable <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-39adccca45c7a41f32459e2bbcfa5e37_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#117;\" title=\"Rendered by QuickLaTeX.com\" height=\"8\" width=\"10\" style=\"vertical-align: 0px;\"\/>\u00a0is equal to binary 110. In decimal this is equivalent to <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-666f338bfe8fbe474c853d387e6566f0_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#49;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#52;&#32;&#43;&#32;&#49;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#50;&#32;&#43;&#32;&#48;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#49;&#61;&#54;\" title=\"Rendered by QuickLaTeX.com\" height=\"14\" width=\"167\" style=\"vertical-align: -2px;\"\/><\/p>\n<p>If we RESET bit 1 to a 0, we get binary 100, which is equal to decimal 4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-349\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Task341-Table.png\" alt=\"\" width=\"400\" height=\"145\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Task341-Table.png 906w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Task341-Table-300x109.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Task341-Table-768x278.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Note again how only a single bit (bit 1) is changed, and the others are left unaffected. This time we use the logical<a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/logical-and-glossary-entry\/\" target=\"_blank\" rel=\"noopener\"><b> AND <\/b><\/a>operator <span class=\"lang:default decode:true crayon-inline \">&amp;<\/span>\u00a0 (see the <a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/logical-and-glossary-entry\/\" target=\"_blank\" rel=\"noopener\">glossary<\/a>\/lecture slides for a reminder).<\/p>\n<h2>Task 3.4.2<\/h2>\n<p>Perform the following:<\/p>\n<table style=\"border-collapse: collapse;border: 1px solid black\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"middle\">Create a new project<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Paste in the code (see below)<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Read the comments and complete the code. If you are stuck or wish to check your answer, a solution is <a href=\"https:\/\/os.mbed.com\/teams\/Students-Plymouth-University-UK-SoCEM\/code\/Task342Solution\/file\/cdb59b84bd58\/main.cpp\" target=\"_blank\" rel=\"noopener\">here<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"theme:xcode lang:c++ decode:true \">#include \"mbed.h\"\r\n\r\n\/\/Global objects\r\nBusOut binaryOutput(D5, D6, D7);\r\nDigitalIn SW1(D4);\r\n\r\n\/\/Function prototypes\r\nvoid waitForButtonPress();\r\n\r\n\/\/Main function\r\nint main() {\r\n    \r\n    \/\/Create a variable to hold the bit pattern\r\n    unsigned int u;\r\n    \r\n    \/\/Flash LED's to indicate the code is running\r\n    binaryOutput = 7;\r\n    wait(0.5);\r\n    binaryOutput = 0;\r\n    \r\n    while(1) {\r\n        \r\n        u = 7;             \/\/Set initial value 111\r\n        binaryOutput = u;  \/\/Show binary on LED's\r\n        waitForButtonPress(); \/\/Call function\r\n        \r\n        \/\/Use &amp; to reset bit 1\r\n        u = u &amp; 5;          \/\/AND with binary 101\r\n        binaryOutput = u;\r\n        waitForButtonPress();\r\n        \r\n        \/\/Modify u with &amp; to reset bit 2 to a 0\r\n        \/\/WRITE CODE HERE\r\n        binaryOutput = u;\r\n        waitForButtonPress();\r\n        \r\n        \r\n        \/\/Modify u with &amp; to reset bit 0 to a 0\r\n        \/\/WRITE CODE HERE\r\n        binaryOutput = u;\r\n        waitForButtonPress();\r\n        \r\n    } \/\/end while(1)\r\n} \/\/end main\r\n\r\n\r\nvoid waitForButtonPress() {\r\n    while (SW1 == 0);\r\n    wait(0.25);\r\n    while (SW1 == 1);\r\n}<\/pre>\n<h2><b>Bit Toggling<\/b><\/h2>\n<p>When a bit is flipped to the opposite state, we call this toggling.<\/p>\n<p>Consider the example where the unsigned integer variable <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-39adccca45c7a41f32459e2bbcfa5e37_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#117;\" title=\"Rendered by QuickLaTeX.com\" height=\"8\" width=\"10\" style=\"vertical-align: 0px;\"\/>\u00a0is equal to binary 100. In decimal this is equivalent to <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-dcceaa6d0bfed1c680b553a9c328fe0c_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#49;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#52;&#32;&#43;&#32;&#48;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#50;&#32;&#43;&#32;&#48;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#49;&#32;&#61;&#32;&#52;\" title=\"Rendered by QuickLaTeX.com\" height=\"14\" width=\"167\" style=\"vertical-align: -2px;\"\/>.<span class=\"Apple-converted-space\">\u00a0<\/span>If we TOGGLE bit 1, we get binary 110, which is equal to decimal 6. If we repeat the process, we get back to binary 100 again.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-353\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitToggleTable.png\" alt=\"\" width=\"350\" height=\"183\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitToggleTable.png 910w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitToggleTable-300x157.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/BitToggleTable-768x402.png 768w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>Again we are only affecting a specific bit (bit 1), with the others are left unaffected. This time we use the logical<a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/logical-xor-glossary-entry\/\" target=\"_blank\" rel=\"noopener\"><b> XOR <\/b><\/a>operator ^ (see the <a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/logical-xor-glossary-entry\/\" target=\"_blank\" rel=\"noopener\">glossary<\/a> for a reminder).<\/p>\n<h2>Task 3.4.3<\/h2>\n<p>Perform the following:<\/p>\n<table style=\"border-collapse: collapse;border: 1px solid black\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"middle\">Create a new project<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Paste in the code (see below)<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Read the comments and complete the code. A <a href=\"https:\/\/os.mbed.com\/teams\/Students-Plymouth-University-UK-SoCEM\/code\/Task343Solution\/file\/807cc79e50f0\/main.cpp\" target=\"_blank\" rel=\"noopener\">solution is available here.<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"theme:xcode lang:c++ decode:true \">#include \"mbed.h\"\r\n\r\n\/\/Global objects\r\nBusOut binaryOutput(D5, D6, D7);\r\nDigitalIn SW1(D4);\r\n\r\n\/\/Function prototypes\r\nvoid waitForButtonPress();\r\n\r\n\/\/Main function\r\nint main() {\r\n    \r\n    \/\/Create a variable to hold the bit pattern\r\n    unsigned int u;\r\n    \r\n    \/\/Flash LED's to indicate the code is running\r\n    binaryOutput = 7;\r\n    wait(0.5);\r\n    binaryOutput = 0;\r\n    \r\n    while(1) {\r\n        \r\n        u = 0;             \/\/Set initial value 0\r\n        binaryOutput = u;  \/\/Show binary on LED's\r\n        waitForButtonPress(); \/\/Call function\r\n        \r\n        \/\/Use &amp; to toggle bit 0\r\n        u = u ^ 1;          \/\/XOR with binary 001\r\n        binaryOutput = u;\r\n        waitForButtonPress();\r\n        \r\n        \/\/Toggle bit 1\r\n        \/\/WRITE CODE HERE\r\n        binaryOutput = u;\r\n        waitForButtonPress();\r\n        \r\n        \r\n        \/\/Toggle bit 2\r\n        \/\/WRITE CODE HERE\r\n        binaryOutput = u;\r\n        waitForButtonPress();\r\n        \r\n        \/\/Toggle bits 0 and 2\r\n        \/\/WRITE CODE HERE\r\n        binaryOutput = u;\r\n        waitForButtonPress();\r\n        \r\n        \/\/Toggle all bits\r\n        \/\/WRITE CODE HERE\r\n        binaryOutput = u;\r\n        waitForButtonPress();\r\n        \r\n        \/\/Toggle all bits\r\n        \/\/WRITE CODE HERE\r\n        binaryOutput = u;\r\n        waitForButtonPress();\r\n        \r\n    } \/\/end while(1)\r\n} \/\/end main\r\n\r\n\r\nvoid waitForButtonPress() {\r\n    while (SW1 == 0);\r\n    wait(0.25);\r\n    while (SW1 == 1);\r\n}<\/pre>\n<h2><b>Logical Inverse (NOT)<\/b><\/h2>\n<p>In the last example, you may have observed that all bits could be toggled when you XOR a 1 with ALL bits. A simpler and (computationally) faster option is to use a Logical Inverse.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Consider the example where the unsigned integer variable <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-39adccca45c7a41f32459e2bbcfa5e37_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#117;\" title=\"Rendered by QuickLaTeX.com\" height=\"8\" width=\"10\" style=\"vertical-align: 0px;\"\/>\u00a0is equal to binary 010. In decimal this is equivalent to <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-f3cf739a009bb91e0d27ae5bd7023c68_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#48;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#52;&#32;&#43;&#32;&#49;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#50;&#32;&#43;&#32;&#48;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#49;&#32;&#61;&#32;&#50;\" title=\"Rendered by QuickLaTeX.com\" height=\"14\" width=\"167\" style=\"vertical-align: -2px;\"\/>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>If we INVERT <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-39adccca45c7a41f32459e2bbcfa5e37_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#117;\" title=\"Rendered by QuickLaTeX.com\" height=\"8\" width=\"10\" style=\"vertical-align: 0px;\"\/>, we get binary 101, which is equal to decimal 5. If we repeat the process, we get back to binary 010 again.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-355\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Not-table.png\" alt=\"\" width=\"350\" height=\"184\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Not-table.png 1084w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Not-table-300x158.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Not-table-768x404.png 768w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/09\/Not-table-1024x538.png 1024w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>Unlike previous examples, the inverse affects ALL bits. This time we use the logical<a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/logical-not-glossary-entry\/\" target=\"_blank\" rel=\"noopener\"><b> NOT <\/b><\/a>operator ~ (see the <a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/logical-not-glossary-entry\/\" target=\"_blank\" rel=\"noopener\">glossary<\/a> for a reminder).<\/p>\n<p><b>Logical shift<\/b><\/p>\n<p>Other useful and commonly used operators are the logical shift right and left operators.<\/p>\n<p>Consider the example where the unsigned integer variable <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-39adccca45c7a41f32459e2bbcfa5e37_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#117;\" title=\"Rendered by QuickLaTeX.com\" height=\"8\" width=\"10\" style=\"vertical-align: 0px;\"\/>\u00a0is equal to binary 001. In decimal this is equivalent to <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-8b692aea13cd61dd98917a65bd5ea032_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#48;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#52;&#32;&#43;&#32;&#48;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#50;&#32;&#43;&#32;&#49;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#49;&#32;&#61;&#32;&#49;\" title=\"Rendered by QuickLaTeX.com\" height=\"14\" width=\"167\" style=\"vertical-align: -2px;\"\/>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>If we LEFT SHIFT <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-39adccca45c7a41f32459e2bbcfa5e37_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#117;\" title=\"Rendered by QuickLaTeX.com\" height=\"8\" width=\"10\" style=\"vertical-align: 0px;\"\/>, we get 010. LEFT shift again, and we get 100. Now right shift, and we get 010 again etc.<\/p>\n<ul>\n<li>For a 1-bit LEFT SHIFT, the bits are moved one position to the left and a zero is pushed into the least significant bit position.<span class=\"Apple-converted-space\">\u00a0<\/span><\/li>\n<li>For a 1-bit RIGHT SHIFT, the bits move once position to the right and a zero is pushed into the most significant bit position.<\/li>\n<\/ul>\n<p>It is also possible to shift multiple positions at a time. The operations described here are known as a <a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/logical-shift-glossary-entry\/\" target=\"_blank\" rel=\"noopener\"><b>logical shift<\/b><\/a>. There is another type, known as an <a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/glossary-2\/arithmetic-shift-glossary-entry\/\" target=\"_blank\" rel=\"noopener\"><b>arithmetic shift<\/b><\/a>, but until we cover 2\u2019s compliment arithmetic (signed numbers), discussion of this will be deferred.<\/p>\n<h2>Task 3.4.4<\/h2>\n<p>Perform the following:<\/p>\n<table style=\"border-collapse: collapse;border: 1px solid black\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"middle\">Create a new project<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Paste in the code (see below)<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Read the comments and complete the code<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote><p>Hint: You could use the right shift, but actually you can do this much more simply. Think about the pattern \/ states:<\/p>\n<p>G Y R Y (now repeat)<\/p>\n<p>Some of this is already covered. You just need to add one more state.<\/p>\n<p><a href=\"https:\/\/os.mbed.com\/teams\/Students-Plymouth-University-UK-SoCEM\/code\/Task344Solution\/file\/eebd090fb610\/main.cpp\" target=\"_blank\" rel=\"noopener\">A solution is available<\/a><\/p><\/blockquote>\n<pre class=\"theme:xcode lang:c++ decode:true\">#include \"mbed.h\"\r\n\r\n\/\/Global objects\r\nBusOut binaryOutput(D5, D6, D7);\r\n\r\n\/\/Main function\r\nint main() {\r\n    \r\n    \/\/Create a variable to hold the bit pattern\r\n    unsigned int u;\r\n    \r\n    while(1) {\r\n        \r\n        u = 1;             \/\/Set initial value 0\r\n        int count = 0;\r\n        while (count++ &lt; 3) {\r\n            binaryOutput = u; \/\/Write to LEDs\r\n            u = u &lt;&lt; 1;       \/\/Shift left 1 bit\r\n            wait(0.25);       \/\/Wait\r\n        }\r\n        \r\n\/\/ TODO: Make the pattern shift in the opposite \/\/ direction\r\n\/\/ (also known as the Knight Rider pattern)\r\n        \r\n        \r\n    } \/\/end while(1)\r\n} \/\/end main<\/pre>\n<h1><\/h1>\n<hr \/>\n<h1>Activity 3.5 &#8211; Review<\/h1>\n<p>In this section, there are some additional advanced tasks and a set of review questions.\u00a0In some cases, you might want to write some code to check your answers.\u00a0In other cases, you might need to refer back to the lecture slides.<\/p>\n<h2>Additional Task 3.5.1<\/h2>\n<p>There are bugs in the code below. Can you find and correct it?<\/p>\n<p>A <a href=\"https:\/\/os.mbed.com\/teams\/Students-Plymouth-University-UK-SoCEM\/code\/Task351Solution\/file\/6858ff5fba1f\/main.cpp\/\" target=\"_blank\" rel=\"noopener\">solution is provided here<\/a>.<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true \">#include \"mbed.h\"\r\n\r\nBusOut binaryOutput(D5, D6, D7);\r\n\r\n\/*\r\n **************************************************************************\r\n CAN YOU FIND THE BUGS IN THIS CODE?\r\n One problem only shows itself when you run the sequence the first time ;o)\r\n **************************************************************************\r\n *\/\r\n\r\nint main() {\r\n    \r\n    int iCount = 7;\r\n    \r\n    \/\/Repeat this program forever\r\n    while(1) {\r\n        \r\n        do {\r\n            binaryOutput = --iCount;  \/\/Write decimal to the output and decrement\r\n            wait(1.00);             \/\/Delay for 500ms\r\n        } while (iCount &gt; 0);       \/\/Condition to repeat\r\n        \r\n        \/\/Reset the count\r\n        iCount = 7;\r\n    }\r\n}<\/pre>\n<h2>Additional Task 3.5.2<\/h2>\n<p>(Do this task if you have spare time)<\/p>\n<p>This task is a continuation from 3.5.1.<\/p>\n<table style=\"border-collapse: collapse;border: 1px solid black\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"middle\">Wire up an additional switch and connect to D3<span class=\"Apple-converted-space\">\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Create a project and paste in the code below<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Build and run the code, and try pressing different switch combinations<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Q1. With both switches pressed, what is delay between the LEDs changing state?<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Q2. Which logical operator is performed using the ^ operator?<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Q3. Which input (D3 or D4) is the least significant bit in the input binaryInput?<\/td>\n<\/tr>\n<tr>\n<td valign=\"middle\">Q4. Could you use two DigitalIn objects instead of a BusIn? If so, how? ( <a href=\"https:\/\/os.mbed.com\/teams\/Students-Plymouth-University-UK-SoCEM\/code\/Task352Solution-FZ429\/file\/facc2bd1d423\/main.cpp\" target=\"_blank\" rel=\"noopener\">a solution is given here<\/a> ). <a href=\"https:\/\/os.mbed.com\/teams\/Students-Plymouth-University-UK-SoCEM\/code\/Task352Solution-FZ429\/file\/facc2bd1d423\/README.txt\" target=\"_blank\" rel=\"noopener\">Answers to Q1-3 are here<\/a>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"theme:xcode lang:c++ decode:true \">#include \"mbed.h\"\r\n\r\n\/\/Global objects\r\nBusOut binaryOutput(D5, D6, D7); \/\/Outputs as an integer\r\nBusIn binaryInput(D3, D4); \/\/Inputs as an integer\r\n\r\n\r\n\/\/Main function\r\nint main() {\r\n    \r\n    \/\/Create a variable to hold the bit pattern\r\n    unsigned int u = 7;\r\n    \r\n    while(1) {\r\n        \r\n        binaryOutput = u;    \/\/Write to LEDs\r\n        \r\n        \/\/TOGGLE all 3 bits in u\r\n        u = u ^ 7;\r\n        \r\n        \/\/Calculate the delay\r\n        double delay = (double)(binaryInput+1);\r\n        wait(delay * 0.25);          \/\/Wait\r\n        \r\n    } \/\/end while(1)\r\n} \/\/end main<\/pre>\n<p><a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/mbed-os-2\/courses\/embedded-systems-in-context-level-4\/\">Back to ToC<\/a><\/p>\n<hr \/>\n<h1>Feedback<\/h1>\n<p>If you wish to leave feedback or have a question, please use the form below.<\/p>\n<p>[contact-form][contact-field label=&#8221;Name&#8221; type=&#8221;name&#8221; required=&#8221;true&#8221; \/][contact-field label=&#8221;Email&#8221; type=&#8221;email&#8221; required=&#8221;true&#8221; \/][contact-field label=&#8221;Website&#8221; type=&#8221;url&#8221; \/][contact-field label=&#8221;Message&#8221; type=&#8221;textarea&#8221; \/][\/contact-form]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Back to ToC In this section, we will look at digital inputs, and how we might use them to control digital outputs.\u00a0We will be using a simple \u201cpush-to-make switch\u201d to generate a single digital bit of data. Intended Learning Outcomes Configure an digital input with a push to make switch Use a pull up\/down resistor&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/microcontrollers\/mbed-os-2\/courses\/embedded-systems-in-context-level-4\/topic-3-digital-inputs-and-multiple-outputs\/\">Continue reading <span class=\"screen-reader-text\">Topic 3 &#8211; Digital Inputs and Multiple Outputs<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":130,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-288","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/comments?post=288"}],"version-history":[{"count":30,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/288\/revisions"}],"predecessor-version":[{"id":494,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/288\/revisions\/494"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/130"}],"wp:attachment":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/media?parent=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}