Practical_Electronics-May_2019

([email protected]) #1

flash. To do this we use the command pair: DO and LOOP. DO
needs to be added after the SETPIN line of code and marks the
start of the loop; and LOOP goes at the end of the program so
that the program jumps back to the line of code immediately
after the DO command.
Go to the Editor (F4) and position the cursor at the end of
the first line of code. Note: you can use the End key to position
the cursor at the end of the current line of code; and a second
press of the End key will position the cursor at the end of the
very last line of code. Likewise, with the Home key; one press
to go to the start of the current line, and a second press to go
to the start of the first line of code. Alternatively, use the four
arrow keys to move the cursor. You cannot use the mouse to
scroll, or to position the cursor – this is a limitation of TeraTerm.
So, with the cursor at the end of the first line of code (the
line containing the SETPIN command), press Enter and a new
blank line is inserted. Now type DO and then press the End key
twice to go to the end of your program and press Enter, and
then type LOOP. Your program should now look something
similar to that shown in the User Manual on page 12 (but
with a different pin number, and with different PAUSE values).
It is worth indenting the lines of code that are between DO
and LOOP (as shown in the program listing on page 12). You
can use the tab key to do this, or use the spacebar. Indenting
makes it much easier to read the code (and has zero impact
on performance because the Micromite just ignores spaces at
the start of a line).
Press F2 and this should result in the LED turning on for half
a second, then off for half a second, and then continuing to flash
like this until you either remove power, reset the Micromite,
or press Ctrl-C.
Experiment with changing the values of the two PAUSE
commands to vary the flash rate (they don’t need to be the same
as each other). This is also good practice for switching between
the three Micromite modes, while seeing the effects of changing
values in your code.
We recommend you read the rest of page 12 (Setting the
AUTORUN option). In summary, at the command prompt, type
OPTION AUTORUN ON and then reset the Micromite with the
reset button on the DM. You will see the start-up message, and
then the program will automatically run (LED flashes). Ctrl-C
will stop the program running and return you to the command
prompt. OPTION AUTORUN OFF will prevent the program in
the Micromite from running automatically at power up.
Referring back to Fig.5 i) and ii), point A is controlled by the
command PIN(x)=1 (or 0 ) to turn the LED on (or off) – with x
replaced by the relevant pin number. Now consider connecting
point B to an I/O pin (instead of connecting it to 0V). Let’s
connect it to pin y (ie, not the same pin as point A). You may
ask why do this, but bear with us. The LED will only come on
when PIN(x)=1 and PIN(y)=0. If PIN(y)=1, then the LED is
off, irrespective of PIN(x) setting. If this sounds complex, just
liken it to point B being connected to +V. You will not be able
to turn the LED on while point B is at +V.
The reason for explaining this is that we can do a neat trick
if we connect two LEDs back to back (to a single resistor) as
shown in Fig.5 iii). Assume that point C is connected to I/O
pin x, and point D is connect to I/O pin y. Now we have four
combinations of outcome depending on how the Micromite
sets PIN(x) and PIN(y):
PIN(x)=0 and PIN(y)=0 – both LEDs off
PIN(x)=1 and PIN(y)=0 – LED D1 on, D2 off
PIN(x)=0 and PIN(y)=1 – LED D2 on, D1 off
PIN(x)=1 and PIN(y)=1 – both LEDs off


At first glance this may not seem very useful, but now consider
what happens if we rapidly switch between PIN(x)=1 with
PIN(y)=0 and then switch to PIN(x)=0 with PIN(y)=1. It

will result in both
LEDs being on;
albeit they will be
slightly dimmer in
appearance due to
the rapid switching
between on and off
(but our persistence
of vision will make
it appear as if both
LEDs are on).
So with just two
I/O pins, and one
resistor, we can alter our code to result in any combination of
LEDs being turned on and/or off.
Don’t worry if this seems confusing; it is mentioned here
because it is a technique that will be used in next month’s
Electronic Dice program, making it possible to control seven
LEDs with just three I/O pins (and only four resistors).

Digital inputs
A digital input pin allows MMBASIC to be able to monitor a
device that is in one of only two states. A switch or button is a
great example of a digital input device – it can be either open
or closed. Referring to Fig.6 i), this is a typical connection of
how we can detect if a button is being pushed or not. When the
button is not being pressed, point A will be at +V (regarded as
a high logic level) due to the pull-up resistor. However, when
the button is pressed, point A is then directly connected to 0V,
which is regarded as a low logic level (point A is ‘active low’).
A resistor value in the region of 10kΩ will ensure that when the
button is pressed, then only a small amount of current flows
between +V and 0V rather than shorting it out.
Fig.6 ii) is similar to i), but the logic levels are now working
in reverse. Point B is at a low logic level when the button is not
being pressed (thanks to the pull-down resistor), and at a high
logic level when the button is pressed (point B is ‘active high’).
To demonstrate the above, build the circuit shown in Fig.6 i)
and use it to control the flashing LED program from the previous
section. You need a button or switch, and a resistor with a
value between 470Ω and 100kΩ. If you don’t have a button or
switch, then simply use two jumper wires and you can touch
them together to simulate a button press. Use a breadboard, a
small piece of stripboard, or even just the leads of the resistor
to assemble the circuit shown in Fig.6 i). Connect the +V side
of the pull-up resistor to the middle 4-way socket row, J8 (this
is at a 3V3 level). Connect one side of the button to 0V (J7, the
left hand 4-way socket row), and point A to I/O pin 14.
Now insert the following three lines of code to the very
beginning of your program:
SETPIN(14),DIN ' make I/O pin 14 a digital input
waiting: ' this is a label we are defining
IF PIN(14) = 1 THEN GOTO waiting ' test High (1)

Before you press F2 to run this, there are three new things
introduced here that briefly need explaining. The SETPIN
command is configuring the I/O pin 14 as a digital input with the
use of DIN (as opposed to DOUT). More information is available
in the User Manual on page 39 (Using the I/O pins).
A label called waiting: has been defined and simply gives
us a point in our code that can be referenced should we wish to
jump to that part of the code. Remember, program flow normally
goes from top to bottom, but we can jump to other parts should
we need to. Labels give us this ability and need to be a word
that is not the same as an MMBASIC command. It also needs
to have a colon character immediately after the last character.
Finally, the IF...THEN... is an example of a ‘test condition’.
What this is doing is quite straightforward. It is testing the state of

Fig.6. Two different ways to connect a
button to a digital input pin: i) active low,
ii) active high.

RPullup

+V

0V

A B

0V

+V

RL RPulldown

i) ii)
Free download pdf