All About Circuits

Digital Circuits

Microcontroller Principles


20 questions By Tony R. Kuphaldt

Page 7 of 7 0 of 20 answers revealed (0%)
  • Question 19 of 20

    Examine the following schematic diagram and program listing (written in “pseudocode” rather than a formal programming language) to determine what type of basic logic function is being implemented in this microcontroller unit:



    Pseudocode listing Declare Pin0 as an output

    Declare Pin1, Pin2, and Pin3 as inputs

    LOOP

    IF Pin1 is HIGH, set Pin0 HIGH

    ELSEIF Pin2 is HIGH, set Pin0 HIGH

    ELSEIF Pin3 is HIGH, set Pin0 HIGH

    ELSE set Pin0 LOW

    ENDIF

    ENDLOOP

    Reveal answer
  • Question 20 of 20

    A student builds a microcontroller circuit to turn on an LED once for every five actuations of the input switch. The circuit is simple, with the microcontroller using a conditional loop to increment a variable each time the switch is pressed:



    Pseudocode listing Declare Pin0 as an output

    Declare Pin1 as an input

    Declare X as an integer variable

    LOOP

    WHILE Pin1 is HIGH

    Add 1 to X (X=X 1)

    ENDWHILE

    IF X is equal to 5, set Pin0 HIGH and set X to 0

    ELSE set Pin0 LOW

    ENDIF

    ENDLOOP

    Unfortunately, the program does not execute as planned. Instead of the LED coming on once every five switch actuations, it seems to come on randomly when the switch is released. Sometimes the LED turns on after the first switch actuation, while other times it takes more than five pushes of the switch to get it to turn on.

    After some careful analysis, it occurs to the student that the problem lies in the WHILE loop. As the microcontroller is much faster than the human hand, that loop executes many times while the switch is being pressed rather than only once, meaning that the variable X counts from 0 to 5 many times around for each switch actuation. It is only by chance, then, that X will be equal to five after the WHILE loop exits.

    What the student needs is for the switch to increment by 1 only for an off-to-on switch transition: at the positive edge of the input pulse. The problem is how to do this using programming.

    Another student, when faced with the same problem, chose to solve it this way and it worked just fine:

    Pseudocode listing Declare Pin0 as an output

    Declare Pin1 as an input

    Declare Switch as a Boolean (0 or 1) variable

    Declare Last_Switch as a Boolean (0 or 1) variable

    Declare X as an integer variable

    LOOP

    Set Last_Switch equal to Switch

    Set Switch equal to Pin1

    IF Switch = 1 and Last_Switch = 0 THEN add 1 to X (X=X 1)

    ELSE do nothing to X

    ENDIF

    IF X is equal to 5, set Pin0 HIGH and set X to 0

    ELSE set Pin0 LOW

    ENDIF

    ENDLOOP

    Explain how this program successfully increments X only on each off-to-on transition of the pushbutton switch while the other program increments X rapidly during the entire duration the pushbutton switch is pressed.

    Reveal answer