Practical_Electronics-May_2019

([email protected]) #1

that at any time, it
is only ever in one
of three modes. It
is either waiting
for an instruction
from you (command
prompt displayed),
or, you are entering a
program (full screen
editor displayed),
or, it is running the
program in memory.
These three modes are referred to as IMMEDIATE mode, EDIT
mode, and RUN mode, as shown in Fig.4.
To switch between modes, you either type a single command
(if at the command prompt), or you press a function key (F1, F2,
or F4), or Ctrl and C. There are other key-press combinations
based around the Ctrl key for those of you with keyboards lacking
function keys (refer to User Manual – Full Screen Editor section).
One important point mentioned in Part 1 – the Micromite
makes it extremely quick and easy to change a program (in
the Editor), and then immediately run the program to see the
result of the change. Should you need to make further changes
(you may be adjusting the speed of a flashing LED which is
controlled by a single parameter) then pressing the single
relevant function key switches modes immediately and you
can make the change as needed. Most other microcontrollers
require you to make the change on a PC, then compile your
code, then upload your code to the microcontroller, then run
the code. Any further changes require this whole sequence to
be repeated (which can take a considerable time).
Please take a moment to study Fig.4 carefully, and then for a
little practice performing switching modes, turn to the bottom
of page 11 in the User Manual (A Simple Program section) and
create, run, and edit the one-line ‘Hello World’ program shown.


Inputs and outputs
We will soon move on to controlling and sensing basic digital
inputs and outputs, but before that, let’s first examine an input
and an output that involves no components connected to the I/O
pins. I am talking about the console; ie, TeraTerm. The screen
is an output, and the keyboard is an input.
To get something displayed on the TeraTerm screen we
can use the command PRINT. Go ahead and try this. At the
command prompt type: PRINT 22/7 (Enter). You should
see 3.14286 displayed on the TeraTerm screen (along with
the cursor on the next line waiting for the next instruction
from you). In the Immediate mode, when you press the
Enter key, anything you type on the line will be acted upon
immediately. Press some random keys and then Enter and
see what happens. You should see an error message along
with a clue as to what the error is.
The TeraTerm keyboard can be used as an input with the
command INPUT. To quickly demonstrate, type the following
as a single line at the command prompt:
INPUT x : PRINT "Number entered was:"; x
When you press Enter you will see a question mark appear.
This is MMBASIC waiting for you to input a number. Type in
a number and then press the Enter key. You will then see an
‘output’ displayed (because of the PRINT command). This is
a very simple example of how to use the TeraTerm console as
an input (via the keyboard), and as an output (via the screen).


Digital outputs
A digital output device is something that is either ‘on’ or ‘off’.
An obvious component to demonstrate this is an LED; however,
it could just as easily be a motor, a buzzer, a relay... Note that the
device being controlled may well need some interface electronics


between it and the Micromite. For example, the Micromite has
not got the power to turn on a powerful mains motor, but this
could be done with a relay controlled by the Micromite (and
the mains motor connected to the relay contacts).
Back to controlling an LED; please now refer to Fig.5 i). To
turn the LED on, point A is connected to +V, and point B to 0V.
The resistor is required to limit the current flowing through
the LED (as is standard). However, to have the Micromite
control the LED we connect point A to an I/O pin (instead
of +V), and connect point B to 0V. This is exactly how the
LED test circuit works (as used when I/O pins on the MKC
and DM were tested).
Go ahead and connect a resistor and an LED to any one of the
19 available I/O pins (refer to Fig.2 for pin number/locations). It
doesn’t matter if you switch round the positions of the resistor
and the LED (as in Fig.5 (ii)) – the same logic still applies.
There are two commands we need to use – one will configure
the relevant pin to be a digital output, and the other command
will turn the pin on (+V), or off (0V).
At the command prompt, type SETPIN(2),DOUT, substituting
the value 2 with whatever pin number you are using. When
you press Enter, nothing will appear to happen; however, you
have now configured the pin for use as a digital output (DOUT).
Now type PIN(2)=1 again substituting the value 2 with
the relevant pin number. This time you should see something
happen – the LED will turn on. If not, check connections and
commands to ensure you have used the correct positions and
pin number value.
Now type PIN(2)=0 (using your pin number) and the LED will
turn off. That’s it, you now have software control of hardware
via a digital output pin.
To make it more interesting, we will now enter a small program
into the Editor and add a bit of intelligence. Note that while
entering a line of code (into the Editor), when you press Enter
the command is not acted upon as it would be at the command
prompt. The Editor is just a place to enter a program; and things
will only happen when you run your program.
At the command prompt, type NEW (to delete any code
currently in the Micromite) and then type EDIT (or press F4).
You should now have a blank screen with just a line of words
at the bottom. Go ahead and type the following (pressing Enter
to move to the next line down):
SETPIN(2),DOUT ' configure pin 2 as digital output
PIN(2)=1 ' switch pin 2 ON
PAUSE 500 ' delay for 500mS (1/2 second)
PIN(2)=0 ' switch pin 2 OFF
PAUSE 500 ' 0.5 second delay
Note that the command PAUSE just adds a delay. The Micromite
will sit there doing nothing for the time specified in milliseconds.
There are 1000 milliseconds (ms) in one second; so 500ms is
half a second.
An important aspect of the Editor is that comments can be
added to a program by using the ‘'’ character. Anything on a
line of code typed after the ‘'’ is ignored by the Micromite.
Now move your LED over to I/O pin 2 and press F2 to
automatically save and run your program. If everything works
correctly, then you will see the LED turn on for half a second,
and then turn off. Because the program was able to continue
and reach the end of the code, the Micromite automatically
returned back to the command prompt. However, if there was
an error of some kind, then a warning will be displayed on the
TeraTerm screen. If this happens, press F4 and the cursor will
be placed at the start of the offending line of code. Check you
entered things exactly as shown above, correcting any errors.
Then press F2 to run the program again.
To make things a bit more interesting we will now create
what is called a ‘loop’, allowing the LED to continually switch
on, then off. Put another way, it will make the LED continually

Fig.5. Various ways used to connect LEDs.


a
k

A

D1 D2

B

RL a
k

A

B

RL

i) ii)

a
k a

k

C

D

RL

iii)
Free download pdf