Microelectronics: SOFTWARE -- Input and output



Home | Forum | DAQ Fundamentals | DAQ Hardware | DAQ Software

Input Devices
| Data Loggers + Recorders | Books | Links + Resources


AMAZON multi-meters discounts AMAZON oscilloscope discounts


A single-bit input or output is all that is needed in many control systems. Assembler and BASIC programs for microcontroller systems, microprocessor systems, and PCs, using single-bit input and output, are presented and discussed. Delay routines are introduced. Two routines suitable for microelectronic control systems are described, with programs in assembler and BASIC.

A 1-bit input or a set of 1-bit inputs is all that is needed in many control applications. In Fig 1.3, one of the two proximity sensors (7 and 8) has a high output (logic 1) when the sliding valve is at one end of its track. At other times their outputs are logic 0.

Similarly, an output may need only one bit to perform a vital task. It takes only a 1-bit output to sound a fire alarm. Although we are mainly concerned with control applications in this SECTION, it must be realised that there are many instances of 1-bit input and output in measuring, communications and commercial microelectronic systems.

One-bit input

A 1-bit input is either 0, or 1. The simplest way of inputting this is to use a switch or push-button, as in Fig. 2.6. This is connected to one of the lines of an I/0 port, for example to the LSB line (D0). Before an input can be accepted, this line must be configured as an input. If only this line is being used, the remaining lines could be inputs or outputs.

However, it is preferable to configure all unused lines as outputs. If they are inputs and unconnected, it can happen that they pick up stray charge and can appear to have an input of '1'.

Another point to consider is that if we run the program and then we operate the switch or button, the program runs so fast that it is likely to have ended before the switch is closed. The input routine must wait for the switch to be closed.

Here is an assembler input routine:

;One-bit input routine

.equ ddrb = $17 ;ddrb address.

.equ portb = $18 ;port B address.

.equ bit0 = 1 ;set a constant.

ldi r20,254 ;binary 11111110.

out ddrb,r20 ;register 0 as

;input, rest as

;outputs.

start: in r16,portb ;read port.

andi r16,bit0 ;mask bit 0.

brne start ;if Z=0, back to start.

ldi r17,255 ;if Z=1, continue.

We will discuss this program in a little more detail than there is room for in the comments:

• The first stage in this program is to allocate labels to two addresses in the I/O registers. The data direction register of port B, is at address $17, and this address is now conveniently labeled ddrb. The actual I/O registers of port B are at $18, which is now labeled portb.

• As well as labeling addresses, the .equ directive also labels constants. The example here, called bit0, has bit 0 high and the rest low.

• With the '1200', a register is set as an output by setting its direction register to 1. If the direction register is reset to 0, the register is an input. Some processors may work the other way round to this. The program begins by making Port B register 0 an input.

• The waiting routine begins with a label for the CPU to jump back to as it cycles round the next three lines. It first reads the port values into register r16. Then the program uses a technique known as bit masking. When the port is read, we are interested only in the LSB. If we AND r16 with 1, or more specifically with 00000001, the result will be 0 if the LSB is 0 and 1 if the LSB is 1. The values of the other bits will not appear in the result, as zero ANDed with

0 or with 1 always gives a zero result. The zero flag is set if the result is 0. Then we use BRNE to test the state of the zero flag. If the result is 1 (Z = 0), it shows that the switch has not been closed, so the CPU jumps back to the start label and continues from there. It cycles round the three lines, jumping back repeatedly until the switch is closed and the bit changes to 0. Then Z = 1 and the program can continue.

• The last line gives the CPU something definite to do when the switch is closed, so that, when we are debugging the program, we can tell when it has reached the end.

===========

Masking

This is a technique for picking out one or more bits of a byte to see whether they are 0 or 1. It uses the logical AND operation.

Example:

The input at a port is 01011001 and we want to know whether bit 4 is 0 or 1. We do this by ANDing it with a byte in which bit 4 is 1 and the rest 0:

Input is 01011001 AND with 00010000

Result 00010000

The result is positive so the zero flag is 0, meaning that bit 4 is 1. This is tested and the program continues accordingly.

If bit 4 is 0, masking produces a zero result:

Input is 01001001 AND with 00010000 Result 00000000

The zero flag is 1, meaning that bit 4 is 0.

Masking can be used to pick out more than one bit at a time.

===========

Using a simulator and single stepping through the program, we can see the CPU repeatedly jumping back to start. The closing of the switch is simulated as follows. Step as far as breq start and edit the I/O register to make Port B equal to 1 (= switch closed). Resume single-stepping. The CPU jumps back to start once more, then jumps out of the loop at breq the next time round.

============

Programming in BASIC

This program is for running on a PC. It requires a switch or button (Fig 2.6) connected to line D0 of the 4-BIT I/O (pin 1 of the connector, see Fig. 5.14) . Connect also to one of the ground pins. Make sure that the voltage of the power supply is 5 V DC. Assuming the base address of the parallel port is $0378 (see p.95), the program is:

10 REM 1-bit input routine 20 OUT &H037A,&H04 30 state = INP(&H037A) 40 IF state = &H04 THEN 20 50 PRINT "Switch closed" Lines are numbered in tens, as is conventional. On the PC port, input is more complicated than output. First we have to make all outputs high, then rely on the input data to pull one or more low. Line 20 uses the command:

OUT address, value The address is the address of the 4-bit I/O register. The value written is 00000100 in binary. The top four bits are not stored. Bit D2 is high, to make the output high. Bits 0, 1, and 3 are low because they are inverted and the outputs will be high. This preliminary setting of the bits is required with this register.

Line 20 is the actual input line, using:

state = INP(address) This assigns the reading at the port to variable state.

The following line displays the value of A on the monitor.

The value that is obtained depends on whether the switch is open or closed. If the switch is open the value is &H04, and the program loops back to line 20. If the switch is pressed, state = &H06 and "Switch closed" is displayed at line 40.

============

Programming in PBASIC

This input routine can be tested by connecting a switch to pin P0, as shown in Fig 2.6. The supply voltage must not be more than 5 V DC. It is best to use the Stamp's own regulated supply available at pin 21.

Here is the One-bit input program, written in PBASIC for the Stamp2:

' One-bit input routine input 0 ' Make PO an input.

start:

debug "Running" if in0 = 0 then start ' Jump to start if 0.

debug "Done" ' Print "Done" if 1.

The first line configures pin P0 as an input (the push button switch is connected to that pin). The waiting routine consists only of the start label and an if … then statement, in which in0 is the input read from pin P0.

There are two debug statements to monitor the action of the program. In this program, debug is being used to pass messages back to the programmer that certain stages of the program have been reached. Each time the CPU reaches a debug statement, the message is displayed on the screen in the debug panel.

When the program is run, the CPU goes though the loop repeatedly, causing "Running" to be displayed each time.

The word "Running" repeatedly scrolls up the debug panel at high speed. When the button is pressed the word

"Done" is displayed just once and the program stops.

The debug statements are deleted when the program has been found to operate correctly, leaving a very short and understandable program. Its three lines are equivalent to eight lines of assembler.

===============

===============

EXERCISE 1 -- One-bit input

If you have not already done so, read the introduction to EXERCISE 9.1.

1. Adapt the one-bit input program to the system you are using.

2. Write new versions of the input program to make it do something slightly different. For example, revise the program so that it responds to a switch connected to bit 3 of Port B instead of to bit 0.

3. Write a program that responds only after the switch has been closed and then opened again. [Hint: it has to read a '0', then a '1'].

4. Write a program that responds only after the switch has been closed twice.

5. A very simple security system has two input buttons. To gain access, the user must press button 2, then button 1.

Pressing them in the order 1, 2, or pressing both at the same time is not allowed. The user has only one chance to press them in the right order. Write a program that functions in this way.

6. Expand the previous system to three buttons. Decide on the order of keying and write the program.

7. A security system has three buttons. Write a simple combination lock program that responds only when a selected pair of the buttons are pressed at the same time.

=============

One-bit output


FIG. 1 An LED is a useful device for indicating the state of an output port or pin. In (a) the LED is driven directly by the output from a microcontroller. The circuits in (b) and (c) take far less current and are therefore more suitable for outputs taken directly from a microprocessor.

One of the simplest output devices is a lamp switched by a transistor (Fig. 2.7). An even simpler output circuit is a light emitting diode (LED) with a series resistor to limit the flow of current. The circuit of Fig. 10.1a requires about 10 mA, so this is suitable for a microcontroller output pin, which can usually supply up to 20 mA.

Microprocessor outputs can not usually supply as much current, so a transistor switch is needed, as in Fig. 10.1b. Assume in the following program that the LED is connected to pin 1 of Port B. The program begins in the same way as the previous one, but then goes on to light a lamp or LED after loading a value into r17.

; One-bit input/output routine

.equ ddrb = $17 ;ddrb address.

.equ portb = $18 ;port B address.

.equ bit0 = 1 ;set a constant.

ldi r20,254 ;binary 11111110.

out ddrb, r20 ;register 0 as

;input,rest as

;outputs.

out portb,r20 ;outputs all low.

start:in r16,portb ;read port.

andi r16, bit0 ;mask with bit 0.

brne start ;if Z=0, back to start.

ldi r17, 252 ;if Z=1. Put binary

;11111100 in r17.

out portb, r17 ;light the LED.

A detailed analysis of the program shows that:

• The program begins the in the same way as before. An extra line before the waiting routine makes all outputs low by storing 1 in each register that is an output.

• The waiting routine is as before.

• The input is masked to find the state of bit 0 (see box).

• In the final stage, when the switch is closed, r17 is loaded with a value that has zero at bit 1. A 0 in the output register makes the output go high, so lighting the LED. This is the other way round from many microcontrollers in which 0 = low output and 1 = high output.

==============

Programming in BASIC

The PC has a switch or button connected to bit D0 (Pin 1) of the 4-bit I/O register and an LED connected to bit D0 of the 8-bit register..

10 REM 1-bit input/output routine 20 OUT &H037A,&H04 :REM Set up inputs 30 state = INP(&H037A) 40 IF state = &H04 THEN 20 50 OUT &H0378,&H01 Lines 20 to 40 are the same as before. Note the reminder to make the outputs high in the 4-bit I/O register, so that pressing the button can pull D0 down. The processor loops around lines 20 to 40 until the button is pressed.

Then it drops out of the loop and goes to line 50. There an OUT statement sends a byte to the 8-bit output port, making D0 output high and turning on the LED.

=============

Programming in PBASIC

A push-button or switch is connected to pin P0, as before.

An LED is connected to pin P1, as in Fig. 10.1 The BASIC version of the one-bit input/output program is as follows:

' One-bit input/output routine input 0 output 1 'pin P1 is an output out1 = 0 'LED off start:

debug "Running" if in0 = 0 then start out1 = 1 'make P1 high Two additional lines are needed to switch on the LED connected to pin P1. The second debug statement of the previous program is not needed now.

On running the program the word "Running" is repeatedly scrolled up the debug panel. This stops when the button is pressed, at which point the LED comes on.

The debug line is deleted when the program has been tested.

==============

==============

EXERCISE 2 -- One-bit input/output

If you have not already done so, read the introduction to Section 9 / EXERCISE 1.

1. Adapt the example of a one-bit input/output program to the system you are using.

2. Write new versions of the input/output program to make it do something slightly different. For example, revise the program so that it responds to a switch connected to bit 3 of Port B instead of to bit 0. Change the output pin to bit 5.

3. Start with the LED on at the beginning of the run and let closing the switch turn it off.

4. Make the user close the switch (or press a button) twice to light the LED.

5. Program a toggle action. As the switch is repeatedly closed, the LED goes on, then off, indefinitely.

6. Install two switches on two input pins and program the system so that closing either of these turns the LED on.

7. Install two switches on two input pins and program the system so that the LED lights only when both switches are closed.

8. Install two switches and two LEDs. Write a program to turn one LED on with one of the switches and to turn the other LED on with the other switch.

==============

Delays

Processors work so fast that a short program such as the input/output program seems to run instantaneously. The LED seems to come on as soon as we press the button. The speed of action is advantageous, but sometimes we want to hold the CPU back a little. If the command that switches the LED on is followed immediately by a command to switch it off, the action takes place too quickly for us to see the LED flash. To be able to see the flash, we need to introduce a delay between switching it on and switching it off again. In this way, a delay is useful to hold the CPU back for a short time, to let the human operator keep up with it. A delay may sometimes be used to make an action repeat at regular intervals.

One way to produce a pause in a program without actually stopping it is to keep the CPU busy doing something that has no apparent effect.

In this program we set it the task of decrementing a number until the number becomes zero:

;delay routine

;ldi r16,255 ;the number to work on.

start:

dec r16 ;decrementing.

brne start ;and again, nop ;until Z = 1

The branch instruction brne is the opposite of breq. It means 'branch as long as the number is not equal to zero'. In other words branch as long as Z = 0. To give the CPU something to jump to when it has finished running the loop, we use the mnemonic nop, which means 'no operation'. The CPU does nothing at this instruction.

Inserting one or more nop commands in a program can be used to create very short delays, but only of a few microseconds.

The program above loads r16 with 255 and then decrements it by 1 each time round the loop until it reaches zero. It takes one machine cycle to decrement r16, and two cycles to fetch Z and check whether or not it is 1. This makes 3 cycles for the whole loop, and the CPU runs round the loop 255 times. This takes 3 × 255 = 765 cycles. If the system is running at 1 MHz, this takes 765 µs. This delay is too short for operations such as flashing LEDs, but it could be useful when waiting for the output voltage levels of an ADC to settle before sampling its output.

For a shorter delay, load r16 with a smaller number. For a longer delay, we need to load a larger number. Like many other microcontrollers, '1200' has only 8-bit registers. If your microcontroller has 16-bit registers, it can produce delays of up to 65536/3 = 21845 µs, or 22ms.

With only 8-bit registers to work with, we adopt another strategy. We use two registers, and program two loops, one inside the other. These are known as nested loops.

;ldi r16,255 ;the first number.

startouter: ;start of outer loop.

startinner: ;start of inner loop.

ldi r17,255 ;the second number.

dec r17 ;dec. second number.

brne startinner ;end of inner loop.

dec r16 ;dec. first number.

brne startouter ;end of outer loop.

nop ;do nothing more.

The loop in which r16 is counted down remains as before, except that its label has been renamed startouter. The CPU will run this loop 255 times, as before. The difference is that, within this outer loop, there is an inner loop. It starts at startimer and is based on decrementing r17. Each time the CPU runs the outer loop, it comes to the beginning of the inner loop. Then it has to decrement r17 from 255 to 0 before it can continue with the outer loop.

It takes 756 cycles to decrement r17 to zero and it has to do this 255 times. Decrementing r19 takes 3 cycles, so the outer loop takes 759 cycles. Running it 255 times takes 255 × 759 = 193545 cycles. At 1 MHz this takes 0.193545, or approximately 0.2 s. This is a reasonable delay for flashing an LED, though some times we may want a delay that is longer.

There are several other ways of producing delays. Some microcontrollers have built-in timers for this purpose. Timers are sometimes included on the same chip as another device such as an I/O port. The 68230 PIT is an example of this.

============

Programming in BASIC

BASIC provides two ways of creating delays. One of these is to make the processor run a loop many times. With interpreted BASIC, which has to interpret the program each time round the loop, quite long delays can be obtained. For example:

100 FOR j = 1 TO 1E7:NEXT 110 PRINT "Finished"

This takes about 18 s on a PC with a 300 MHz clock. The delay is ten times longer if we increase the '7' to '8', and even longer if we make it larger.

The other technique is to make use of time$. This is a string variable with the format:

hh:mm:ss It automatically registers the time since it was reset, in hours, minutes and seconds. At any time in a program, time$ can be set to any chosen value. Conversely, we can read its value at any time. This routine gives a delay of approximately 12s:

100 time$ = " 00:00:00" :REM Resetting 110 IF VAL(RIGHT$(TIME$,2))<12 THEN 110 120 PRINT "Finished"

Line 110 evaluates the last two characters in time$ and sends the CPU back to the beginning of the line until the value exceeds 12. Very long delays can be obtained by using time$, and the length of time does not depend on the frequency of the system clock. There are two disadvantages of time$. One is that, being a string variable, it is more difficult to extract the information you need, as demonstrated by line 110 above. The other disadvantage is that resetting time$ in a BASIC program resets the clock in the Windows Date and Time routine.

There are ways of using time$ without making this happen.

============

Programming in PBASIC

Timing in PBASIC has a useful PAUSE instruction. For example:

PAUSE 1000 This gives a delay of 1000 ms, equal to 1 s. The pause can be up to 65535 ms long, or just over 65 s.

If a longer delay is required, PAUSE may be put in a loop.

FOR j = 1 to 60 PAUSE 1000 NEXT This repeats a delay of 1 second sixty times, giving a total delay of approximately 1 minute.

The precision of the PAUSE routine is not high. The ceramic resonator in the timing oscillator has a precision of only ±1% and there are slight delays in reading the interpreted PBASIC.

=============

Programming in PBASIC

A PBASIC version of the delay program uses the PAUSE command to flash an LED once every 0.4 s.

'delay output 1 ;pin P1 as output again:

out1 = 0 ;LED off pause 200 ;0.2 s delay out1 = 1 ;LED on pause 200 ;0.2 s delay goto again ; to repeat the flash.

The LED is connected to pin P1. This routine is easily expanded to flash at different rates, and to flash more than one LED.

==========

EXERCISE 4 -- Delays

1. Adapt the delay program to the system you are using.

2. Write new versions of the delay program to make it do something slightly different. For example, revise the program so that it produces delay of 0.1 s or 0.15 s.

3. Use the delay routine to turn an LED on for 0.2s, then off.

4. Write a program to turn an LED on for 0.1 s, then off for 0.2 s, repeating. Try varying the on and off periods.

5. Install four LEDs. Write a program to light them for 0.2 s, one at a time, in order.

6. Install two switches or push buttons. Start flashing an LED when one switch is briefly closed (or a button is pressed) and stop the flashing when the other switch is closed.

7. Install two switches and an LED. Take the two inputs to be coded in binary, equivalent to 00, 01, 10 and 11. Depending on the input, flash an LED 1,2, 3, or 4 times.

8. Install two switches and four LEDs. Depending on the input flash the selected one of the LEDs.

9. Write a delay routine that uses INC instead of DEC.

==============

Temperature control system

The elementary input/output programs in this SECTION can easily be adapted for simple control functions. For input and output we need:

• A sensor that gives a logic 0 or 1 output, depending on whether the temperature is below or above a set level.

• An actuator that is switched on or off by a logical 0 or 1 signal and something appropriate to temperature regulation.


FIG. 2 This sensor circuit gives a logic high output when the temperature is above the set point. The supply voltage must not exceed the supply voltage of the processor.


FIG. 3 A transistor switch can be used to drive a wide range of DC actuators. The actuator may be an LED, a lamp, a solid-state buzzer, a relay, a solenoid or a low-voltage DC motor. Include the diode when the actuator is inductive.

As an example, a cooling system is built up from a temperature sensor, a microcontroller and a motor-driven fan. Fig. 2 is the circuit for a temperature sensor, based on a thermistor. The set point of the switching circuit depends on the setting of the variable resistor. If the temperature is below the set point, the output of the buffer gate is low (logic 0). If the temperature is above the set point, the output is logic 1.

The actuator, is a small low-voltage DC motor, connected as in Fig. 3. The transistor must be rated to pass the current required by the motor. The motor is an inductive load so a protective diode must be connected across the motor, as shown by the dashed lines. The temperature sensor is connected to pin 0 of port B of the microcontroller. The motor is connected to pin 1 of Port B and turns a small fan. The program switches the motor on when the temperature is higher than the set point and switches it off again when the temperature falls below the set point.

;Cooling system control

.equ ddrb = $17 ;the first 5 lines are

.equ portb = $18 ;the same as in the

.equ bit0 = 1 ;input/output program.

ldi r20,254 out ddrb, r20 sample:

in r16, portb ;reading bit B0.

andi r16, bit0 ;masking.

breq cold ;cold or hot? ldi r17,252 ;hot, so turn out portb, r17 ;fan on.

rjump sample ;to sample again.

cold:

ldi r17, 254 ;cold, so turn out portb, r17 ;fan off.

rjmp sample ;to sample again.

The main points about this program are:

• The initial stages are the same as for the input/output routine, to set up B0 as an input and B1(and the rest of Port B) as an output.

• r20 is not output at the beginning as we do not want to turn on the fan until the temperature has been sampled.

• The sampling routine uses bit masking as in the input/output program.

• If the sample is 0 (= low temperature), the bit is equal zero (Z = 1) and the program jumps on to cold to make the output low. If the sample is 1 (= high temperature) the program runs on to the next line.

• Decimal 252 is binary 11111100, which puts a 0 in the output register. In the '1200', a 0 in the register produces a high output, and turns the fan motor on for cooling.

• In the cold routine, decimal 254 is binary 11111110, which puts a 1 in the output register, turning the motor off.

============

Programming in BASIC

The sensor is connected to D0 in the 4-bit I/O port (pin 1).

The fan motor is connected (through a transistor, see Fig. 2) to D0 (pin 2) in the 8-bit output register.

10 REM Cooling system control

20 OUT &H0378,0

30 OUT &H037A,&H04

40 state = INP(&H037A)

50 IF state = &H04 THEN OUT &H0378,&H01 ELSE OUT &H0378,&H01 60 GOTO 30

Detailed notes:

• Line 20 makes all outputs low at the start of the routine. In a full-length program, this routine could be preceded by a routine which left the register with high levels on some of its lines. Line 20 sets all the outputs to low, ready for this routine.

• Line 30 sets up the 4-bit I/O port in the usual way, with all high outputs.

• Line 40 reads the port and assigns the state of the port to the variable state.

• Line 50 tests state and turns the motor on or off accordingly. If state = &H04, it means that the temperature is high (the equivalent input to 'button not pressed' in the previous programs). The fan is turned on by sending a '1' to the output port.

Otherwise, temperature must be low, so a '0' is sent to turn the fan off. The use of IF...THEN...ELSE makes the program shorter and simpler.

• Line 60 sends the CPU back to line 30 to re-initialize the I/O port. If this is not done, the output at D1 stays low indefinitely and the system latches with the fan off.

============

Programming in PBASIC

The PBASIC version of the cooling system program is as follows:

' Cooling system control input 0 'first two lines are output 1 'the same as the I/O 'program.

sample:

if in0 = 0 then cold debug "hot " out1 = 1 'make P1 high to turn goto sample 'on the fan.

cold:

debug "cold " out1 = 0 'make P1 low to turn goto sample 'off the fan.

=============

Security system

To keep the programming simple, this system has been limited to two inputs and one output. The light sensor is a photodiode. A beam from a lamp shines on this. If an intruder comes between the lamp and the photodiode, the beam is broken and a siren is sounded.

There is a reset button, which resets the system, silencing the siren and putting the system on the alert for the next intruder. In real life, it would be best to hide the reset button but in this demonstration system the button is simply connected to pin B2. It is wired as in Fig 2.6 to give an active low output.

The light sensor (Fig .4) gives a low output when the beam is broken and is connected to pin B0. The siren is a low-voltage solid state type, driven by a circuit such as Fig. 3 (omitting the diode) and connected to B1.

The listing for the '1200' microcontroller is as follows:

;Security routine

.equ ddrb = $17

.equ portb = $18

.equ bit0 = 1 ;Sensor input.

.equ bit2 = 4 ;Button input.

ldi r20,250 out ddrb,r20 out portb, r20 ;Siren off.

waiting: in r16,portb ;Waiting for intruder.

andi r16,bit0 brne waiting ldi r17,248 out portb,r17 ;Siren on.

alarm: in r16,portb ;Siren sounding until andi r16,bit2 ;button is pressed.

brne alarm ldi r17,250 out portb,r17 ;Siren off.

rjmp waiting

Note that after initializing labels and constants (which takes only a few milliseconds) the first action is to turn off the siren. This is to guard against the output going high when power is applied and switching the siren on. This precaution may be unnecessary but it takes care of a possible glitch.


FIG. 4 The light sensitive circuit has a high output when light is shining on the LDR. It goes low when the LDR is shaded.

=============

Programming in PBASIC

Connect the light sensor (FIG. 4) to pin P0 of the Stamp and a siren switched by a transistor to P1. The first version of the security program is similar to the I/O routine on:

' Security system V.1 input 0 output 1 out1 = 0 'ensure LED off.

onalert:

debug "On alert " if in0 = 0 then onalert debug "Intruder!" out1 = 1 'switch on siren If a delay is added, it makes the system insensitive to short breakages of the beam, and so avoids false alarms:

' Security system V.2 input 0 output 1 out1 = 0 'ensure LED off.

onalert:

if in0 = 0 then onalert pause 500 if in0 = 0 then onalert out1 = 1 'switch on siren The debug statements have been deleted, as they should no longer be necessary. Notice how the beam has to be broken for at least half a second to trigger the siren.

The program can be reset to silence the siren by pressing the restart button on the Stamp, but it may be more convenient to install a pushbutton. Connect a button to pin P2, as in Fig 2.6.

This sequence of versions shows how a program may be developed stage by stage, testing each stage as it is added. This is not the 'Top down' approach to program writing so often recommended, but it is helpful to beginners to explore the capabilities of the CPU and the programming language in this way. Here is the third, though not necessarily the final, version of the security program:

' Security system V3 input 0 output 1 input 2 'add input for button out1 = 0 'ensure LED off.

onalert:

if in0 = 0 then onalert pause 500 if in0 = 0 then onalert out1 = 1 'switch on siren alarm:

if in2 = 1 then alarm 'keep siren sounding out1 = 0 'siren off goto start 'On the alert again

=============

EXERCISE 5 -- Projects with 1-bit I/O

The cooling system and security system that have just been described show that it is possible to build useful systems with only one or two sensors and one or two actuators. There are dozens of similar systems that can be built from 1-bit sensors and 1-bit actuators.

1. To begin with, build the cooler or security system, adapting the design and programming to suit the microcontroller, microprocessor or computer that you are using. Build it on a breadboard to start with, though you could transfer it to stripboard or make a PCD for it if it is successful. Try to think of improvements and additional facilities. One practical point: if you are working on the security system, you should use it to switch on an LED while you are developing it. This is less irritating to other people working in the same room. Later when the design is perfected and the program has been debugged, you can substitute a siren for the LED.

2. Write new versions of the cooler program to make it do something slightly different. For example, revise the program so that it responds to a sensor connected to bit 3 of Port B instead of to bit 0. Change the program to control a motor through an output at bit 7.

3. Adapt the cooler program to switch on a siren when the temperature rises above the set point. This could be a fire alarm system.

4. Adapt the cooler program to flash an LED repeatedly when the temperature falls below the set point. This could be a frost warning system.

5. Substitute other sensors for the thermistor in the circuit of Fig. 10.2. Suitable sensors include a light-dependent resistor, a vibration detector, a pressure mat, and a proximity switch. First test the sensor circuit without connecting it to the processor, and adjust resistance values if necessary. Then decide on a suitable application for the sensor and devise an appropriate actuator. Finally connect a sensor and an actuator to the system and write a program for the application.

6. Design and build a system with up to three 1-bit inputs and up to three 1-bit outputs. For inputs, use circuits sensitive to light, temperature, sound, position or magnetic fields. There are many ways in which switches, keys and buttons of different kinds may be used to provide input.

This includes the use of microswitches as limit switches, or to indicate whether doors are open or closed. For outputs use lamps, LEDs, sirens, buzzers, solenoids, electric motors (small ones working on 6V or less, DC), or relays.

Aim to control your system with a program not much longer than the programs in this SECTION. If it seems that the program needs to be longer, wait until you have studied the next SECTION before you write it.

It is intended that you should build the systems on a breadboard to begin with, though some of them will be suitable for transferring to a more permanent form such as stripboard or pcb.

================

Problems on input and output

1. List three different ways of providing input to a microelectronic system and describe one application of each.

2. List three output devices and describe examples of systems in which they could be used.

3. Which programming language do you prefer to use? Give reasons.

4. What is masking? Give an example of a program in which masking is used.

5. How can we make the CPU wait for a given input, or wait until a particular event has occurred?

6. Why is sometimes necessary to have a delay in a program? Describe how a delay of 10 s could be produced.

7. Choose one of the following tasks and outline the design of a microelectronic system to perform it, using only 1-bit inputs and outputs:

• a radio controlled mechanism for opening and closing a garage door.

• a system to switch on lights at the front of a house whenever someone approaches it, but only at night.

• an automatic coffee vending machine.

• a sliding valve with proximity detectors, as in Fig. 1.3.

• a task of your own choosing.

List the input and output devices that you would use. Outline in words how you would program the system to operate (you are not expected to write the actual program).

8. List the things that should be done at the beginning of a program.

9. Write a delay routine for use in BASIC programs, that uses time$, but does not upset the Windows date and time routine.

10. Basing your answer on the cooler system, design and program a system to control a heater to keep a room at constant temperature.

11. Extend the programming of the security system to switch the siren off after it has been sounding for 15 minutes.

Answers to questions

PREV. | NEXT

Related Articles -- Top of Page -- Home

Updated: Thursday, May 18, 2017 10:08 PST