Digital and Programmable Control PDF

Summary

This SQA past paper from 2022 covers digital electronics and programmable control topics. It includes questions on logic gates, truth tables, and other fundamental concepts in digital electronics.

Full Transcript

ENGINEERING SCIENCE HIGHER DIGITAL ELECTRONICS & MICROCONTROLLERS CHAPTER DIGITAL LOGIC PAGES 2 - 32 Click here PROGRAMMABLE CONTROL...

ENGINEERING SCIENCE HIGHER DIGITAL ELECTRONICS & MICROCONTROLLERS CHAPTER DIGITAL LOGIC PAGES 2 - 32 Click here PROGRAMMABLE CONTROL data b for your ookle t PAGES 32 - 50 Click he past p re for apers 1 DIGITAL ELECTRONICS & MICROCONTROLLERS CHAPTER ONE DIGITAL LOGIC Logic Gates There are seven different logic gates; these are the NOT, AND, OR, NAND, NOR, XOR and the XNOR. When drawing circuits containing logic gates it is common to use logic symbols, in most circuits these days the American Military Symbols (ANSI) symbols are used in this and most other countries worldwide, the older British Standard symbols may still be found but are far less common. You should recognise some of these from National 5. NOT AND NAND OR NOR Throughout this course the more common American symbols shown above will be used. These are also the symbols used by the SQA. 2 Truth Tables. The easiest way to represent how each gate behaves is to make use of Truth Tables. A Truth Table shows all possible combinations of inputs and outputs to a logic gate. Any study of electronics, either analogue or digital, is a study of the processing of electrical signals. INPUT PROCESS OUTPUT Input signals come from a variety of sources - a switch from a keyboard; a bar code reader; a temperature sensor; another part of a computer. Output signals can have a variety of destinations - a monitor; a modem; an alarm; another part of a computer. Digital signals can be at a HIGH voltage level or a LOW voltage level. In logic circuits a LOW signal is said to be at logic '0' a HIGH signal at logic ' 1'. The results can be recorded and used in a number of formats, the most common being shown below. INPUTS OUTPUTS INPUTS PROCESS OUTPUTS A B A LOGIC 0 0 0 B GATE 0 1 1 1 0 1 1 1 0 Results displayed in this way are known as TRUTH TABLES. 3 Consider the inputs to a logic gate as two switches. +Vcc INPUT A OUTPUT INPUT B Fig 4.4 It is possible for each switch to be in one of two positions, either, on (1) or off (0). These positions are known as INPUT STATES. For two inputs, there are only 4 possible combinations of input states: A-off & B-off; A-off & B-on; A-on & B-off; A-on & B-on. Input A Input B 0 0 0 1 1 0 1 1 For three inputs, there are 8 possible combinations. In general, for n inputs there are 2n possible combinations of input states, (and hence 2n lines in the truth table.) ASSIGNMENT 1 1. How many combinations of input states would there be for a 6 input system? 2. Write down the 8 possible combinations of input states for a 3 input system. 4 ASSIGNMENT 2 Truth Tables for Individual logic gates It is possible to use circuit simulation software such as ‘Yenka’ to investigate electric and electronic circuits, use this or another similar software package to determine the truth table for each of the following gates, use switches at the inputs and an indicator at the output. 1. NOT gate 2. AND gate Z is A and B A not s Zi 3. OR gate 4. NAND gate Z is not A and B Z is A or B 5. NOR gate 6. XOR gate Zi so nly A or B Z is not A orB 5 ASSIGNMENT 3 1. When is the output of an OR gate high ? 2. When is the output of an AND gate high ? 3. When is the output from an XOR gate high ? (‘X’ stands for exclusive) 4. Why is the NOT gate sometimes called an ‘INVERTER’ ? 5. The truth table below shows the output conditions for the various combinations of input conditions for AND, NAND, OR and NOR gates. Inputs AND NAND OR NOR 00 0 1 0 1 01 0 1 1 0 10 0 1 1 0 11 1 0 1 0 How does the output of the NAND gate compare with the output of the AND (and the output of the NOR compare with the OR) 6. The paper can be fed through a computer printer either by pressing the button on the printer (line feed) or by sending a signal from the computer. COMPUTER SIGNAL LOGIC PAPER GATE FEED PRINTER BUTTON Which logic gate should be used for this operation? 6 7. The motor in a washing machine should not operate until a high signal is sent from the control program and the water level in the drum is high enough. WATER LEVEL SENSOR LOGIC GATE MOTOR CONTROL PROGRAM Which logic gate should be used for this operation? 8. To avoid accidents at times of poor visibility, a warning indicator in a car operates if the light level is too low (logic level 0) and the headlamps are switched off. LIGHTLEVEL SENSOR LOGIC WARNING GATE INDICATOR HEADLAMPS SWITCH Which logic gate should be used for this operation? 9. In the maternity unit of a hospital, the temperature and pulse rate of premature babies has to be continually monitored. A warning alarm should sound if either the temperature or the pulse rate of the baby falls too LOW TEMPERATURE SENSOR LOGIC WARNIN GATE G PULSERATE SENSOR Which logic gate should be used for this operation? 7 Gate Networks. It is often necessary to use more than one gate to perform a decision process. For example, on some TV sets it is possible to change the channel either by pressing the channel select button on the TV or by pressing the channel select button on the remote control. The channel will only change however, if the TV set is switched on, i.e. the channel will change if the TV set button is pressed OR the remote button is pressed AND the TV set is on. A logic diagram for this is shown. REMOTE BUTTON TV SET CHANNEL BUTTON SELECT TV ON/OFF SWITCH Analysing a combinational logic system Combinational logic circuits are those in which the output at any time is determined entirely by the combination of input signals which is present at that time, i.e. the output state does not depend on the sequence of the input signals. In digital electronics we often encounter systems which will contain several logic gates that have been combined together. Suppose that you need to know how a particular network will behave for each possible combination of inputs. 8 If a logic diagram is given, for example: Fig 4.11 STEP 1 Label all the different points on the circuit, including A C inputs and outputs. B Notice that one input serves two gates, this is quite D (OUTPUT) common in logic circuits and both gates should be B labelled accordingly. STEP 2 Draw up a Truth Table for the circuit, with a different INPUT OUTPUT column for each letter. A B C D NB. determine the number of input conditions for the 0 0 truth table. 0 1 1 0 two input, therefore 22 = 4 1 1 i.e. 4 lines on the truth table. A STEP 3 B C Determine what goes into column C. To do this, consider the OR gate on its own, with inputs A and B, enter findings into column C. INPUT OUTPUT A B C D 0 0 0 0 1 1 1 0 1 1 1 1 9 STEP 5 Determine what goes into column D i.e. the output column. Consider the AND gate on its own with inputs from C and B enter findings into column D. INPUT OUTPUT C A B C D 0 0 0 D (OUTPUT) 1 1 1 B 0 1 0 1 1 1 STEP 6 Your final answer should look like this. INPUT OUTPUT A B 0 0 0 A more complicated system may need several stages 0 1 1 before you finish, but if you work systematically through 1 0 0 the circuit in the way shown, considering one gate at a time, you should arrive at the correct answer. 1 1 1 10 ASSIGNMENT 4 Complete a truth table for each of the combinations of gates shown below. Use Yenka to test the combinations and check your answers 1 2. 3. 4. 5. 11 NAND Equivalent Circuits. It is possible to make other logic gates (and circuits) by combining NAND gates. In practice, it is much easier and cheaper to make NAND gates than any others, indeed NAND gates were the first types of gate to be developed into Integrated Circuits (IC's). Frequently, a circuit will be made entirely from NAND gates. Although it is possible to use NOR gates as the basic logic unit, the industry standard is the NAND gate. The use of only one type of gate to make a circuit often allows us to simplify the wiring of a system and makes more efficient use of an Integrated Circuit. Most NAND logic gate IC's contain a number of gates - usually four - and cost as little as a few pence for each IC. Figure 4.20 shows a pin layout diagram of a NAND logic gate IC. This IC has 4 independent 2 input NAND gates +Vcc 14 13 12 11 10 9 8 1 2 3 4 5 6 7 Gnd (0V) 12 It is possible to use circuit simulation software such as ‘Yenka’ to investigate electric and electronic circuits. Collect a copy of the worksheet “NAND equivalent circuits”. ASSIGNMENT 5 Use ‘Yenka’ or another similar software package to determine the truth table for each of the following network of NAND gates. Compare the truth table you obtain with truth tables for the individual gates and decide which gate is the equivalent to the NAND network. 1. 2. 3. 4. 13 Simplification of Combinational Logic Circuits. As has previously been stated it is possible to make all logic circuits from NAND gates only. This section will examine a method for converting circuits that contain a number of different types of gates into one that uses NAND gates only. Consider the circuit shown. A C B Z D B The system is made from an AND gate an OR gate and a NOT gate. The problem is to design a system with the same Truth Table, but made from NAND gates only. STEP 1 Redraw the circuit, replacing each gate with its NAND gate equivalent. AND A C OR 1 2 3 6 Z B 4 D 5 NOT 14 STEP 2 Examine the new arrangement and look for adjacent pairs of NOT gates. In this circuit there are two such pairs. (2 & 3 and 4 & 5 are adjacent pairs). If you consider what happens when you feed a signal to a NOT gate then pass the signal on to another NOT gate you will find that the signal has been Double Inverted this in fact means that whenever you feed a signal to a pair of NOT gates you will get the same signal out. A C 1 2 3 6 Z B 4 D 5 Therefore pairs of NOT gates in series can be removed from the system without any effect. (This fact is of considerable importance when we come on to the next section on Boolean algebra.) STEP 3 Redraw the circuit with the NOT gates removed. A Z B The final circuit has only two gates whereas the original circuit started with three gates, each of a different type. There are obvious implications in terms of cost for manufactur- ers if they are able to reduce logic circuits to situations where there are fewer gates as well as enabling them to use one type of gate. The original circuit would have required three IC's and the final circuit would only require one IC. This method may not reduce the number of gates used on every occasion but it should reduce the number of IC's used. This method is not very elegant and can be very demanding in terms of paper use and does not always lead to a very efficient use of NAND gates. The next section on Boolean algebra should allow us to design circuits more effectively and use fewer gates. 15 ASSIGNMENT 6 The following logic diagrams are constructed from basic gates. Using the method shown, construct equivalent circuits using NAND gates only. 1. 2. 3. 4. a) Construct a truth table for the logic A circuit shown in figure 97. b) Redraw the circuit using NAND gates only. B c) Simplify the NAND circuit. C d) Construct a truth table for the finished NAND circuit. 16 Boolean Algebra. Boolean algebra is a special form of algebra that has been developed for binary systems. It was developed by George Boole in 1854 and can be very useful for simplifying and designing logic circuits. Variables G e o rg e. The most commonly used variables in logic circuit design are capital letters; such as A, B, C, Z and so on and are used to annotate inputs and outputs to systems. In digital electronics we consider situations where the variables can only have one of two possible values, i.e. 'Logical 0' or 'Logical 1'. The statement A = 1 means that the variable A has the value of Logic 1. Similarly, if B = 0 it means that variable B has the value of logic 0. Logical Operations: In Boolean algebra there are three logical operators, these are the AND operation, the OR operation and the Inversion. AND Operator The AND operation can be represented in Boolean notation by. A B=Z The dot between the A and the B is read as AND. OR Operator The OR operation can be represented in Boolean notation by A+B = Z The + between the A and the B is read as OR. 17 Inversion Operator The statement Z = A means that Z is not equal to A. The variable is usually read as NOT A it is also referred to as A Bar. The bar over the top of the variable changes its value, or inverts it. This is known as the NOT operation. Basic logic gates and their Boolean representations LOGIC SYMBOL BOOLEAN DESCRIPTION TRUTH TABLE EXPRESSION NOT A Z A=Z NOT A EQUALS Z 0 1 1 0 AND A B Z 0 0 0 A.B = Z A AND B EQUALS Z 0 1 0 1 0 0 1 1 1 OR A B Z 0 0 0 A+B = Z A OR B EQUALS Z 0 1 1 1 0 1 1 1 1 Fig 4.33 NAND gate The NAND gate is made up from a combination of an AND gate followed by a NOT gate. A C Z B The signal at point C would be A · B. This signal is then inverted by the NOT gate to give:. Z=A B This reads as output Z is equal to A NAND B 18 NOR gate. The NOR gate is made up from a combination of an OR gate followed by a NOT. A C Z B The signal at point C would be A+B. This signal is then inverted by the NOT gate to give: Z=A+B This reads as output Z is equal to A NOR B ASSIGNMENT 7 Write down the Boolean expression for each of the following logic gates. a) b) c) A A A Z Z Z B B B d) e) f) A A Z A Z B Z B C g) h) i) A A A B B B Z Z Z C C C D D Fig 4.36 19 Laws of Boolean Algebra The following is a summary of the basic laws of Boolean algebra. These are the most commonly used laws. Your teacher will assist you if you require further information. NB. It is essential to understand whilst undertaking analysis using Boolean that you must not confuse the symbols used for the Logical operators with those used in normal algebra. It is perhaps useful to know that the rules of common algebra are the same as those of Boolean. + represents logical operator OR · represents Logical operator AND A represents A bar i.e. NOT A ( the inverse of A) Deriving the Boolean expression for a circuit Consider the following circuit. The Boolean expression for the circuit can be derived as follows: STEP 1 Label the inputs on the left-hand side of A the diagram. (If one input serves more B than one gate ensure that the input is labelled accordingly). B 20 STEP 2 A.B Consider each gate in turn. Use the Boolean notation (operators) to give the output of each gate in terms of its input. Write on the B appropriate expression after each gate. B STEP 3 When outputs from other gates are inputs to a further gate, treat the expressions as you would A.B any other equation and make use of brackets (if (A.B) + B necessary). Then write on the appropriate B expression after the next gate and soon until you reach the final output. STEP 4 Write down the final Boolean expression for the network. (A.B) + B = Z. (Since normal rules of algebra hold, Z = B + ( A B ) would also be correct) 21 ASSIGNMENT 8 1. Derive the Boolean expression for each of the following circuits: a) b) c) d) A B C D e) f) A A B C B 2.. Draw a gating arrangement to illustrate Z= A (B + C) Draw a logic diagram to yield D = B + C Develop it to obtain Z = A.D Write a Boolean equation for the overall behaviour. 22 3. Derive the Boolean equation and the truth table the following arrangements a) b) A A B B C C 4. For each pair of circuits shown below: Write a Boolean expression for each of these circuits; By constructing a truth table for each of them, show that they are equivalent; Draw the equivalent arrangements using only 2-input NAND gates. a) A B Z A B b) A Z A B Z B c) Z A A Z B B C C Fig 4.43 23 Deriving the Boolean expression from a Truth Table Consider the Truth Table given A B Z 0 0 1 0 1 0 1 0 1 1 1 1 STEP 1 Note each combination that will give you a ‘1’ at the output and write the Boolean expression for this line at the side of the Truth Table, next to the line that it applies to. A B Z 0 0 1 A.B 0 1 0 1 0 1 A.B 1 1 1 A.B STEP 2 Join the equations by putting an OR sign between each to give the final Boolean expression. Z= (A. B) + (A. B ) + A. B 24 Designing a circuit from its Boolean equation Consider the following equation:... Z = (A B) + ( A B) + ( A B) STEP 1 Draw inputs A and B (and any other input variables) on the left-hand side of the Page. A B STEP 2 A. Start by considering the first term: (A B) A A.B It can been seen from the equation that both inputs require to be inverted. This is achieved by passing B signals from A and B through NOT gates. The output B from these signals are then used as inputs to an AND gate (as signified by the dot between the A and B in the Boolean expression). Write this on the output line of the AND gate. STEP 3. Consider the next term: (A B) A A A.B It can be seen from this term that input A does not require to be inverted but input B does. Since we B B already have both input A and B bar available we can amend the circuit diagram accordingly by feeding these inputs to a second AND gate. A.B A 25 A A STEP 4. Consider the final term: (A B) A.B B B Again both inputs are available to us. Neither of the inputs is inverted so the signals are taken from the original inputs and are fed to a third AND gate. A.B A A.B STEP 5 Finally we use the outputs from the three AND gates as inputs to a three input OR gate to arrive at the final solution. A A A.B B B A.B A.B + A.B + A.B A A.B 26 ASSIGNMENT 9 1. Write the Boolean equation for the following truth tables a) A B Z b) A B C Z 0 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 1 1 0 0 1 1 1 1 2. Develop a Boolean equation and draw a logic circuit diagram containing AND, OR and NOT gates to yield the truth table shown. A B C Z 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 0 0 1 1 0 1 0 1 1 0 0 1 1 1 0 3. For the following Boolean equations, draw the correct logic circuit arrangement. a. Z = A + (A B) b. Z = A + B + C. c. Z = (A B) + (A C)... d. Z = (A B) + (A C) + (B C).... e. Z = (A B C) + (D E) + (F G). 27 Combinational logic circuit design. As problems become more and more difficult it is not always possible to go from the question to the answer in one or two steps, when that is the case the following set of rules should be followed. When designing a system to suit a need you should proceed in the following order. 1. Describe the problem clearly in words. 2. Write out a Truth Table for the system. 3. Derive the Boolean expression from the Truth Table. 4. Simplify this expression if possible. 5. Draw a logic circuit diagram for the system using AND, OR and NOT gates. 6. Convert the circuit to NAND gates only. It is entirely possible that not every problem will require all of these steps to be followed, however this will be a useful guide for most. 28 WORKED EXAMPLE: The temperature in a manufacturing process is critical. A sensor is used to detect overheating. The sensor normally records a 0 logic level, but when it overheats it records a logic level 1. The signals are detected on a control panel. On the control panel, under normal conditions, a green light is on. If the temperature gets too high the green light goes off and a red light comes on and a warning bell sounds. The engineer has a switch to cut out the bell, but leaving the red light on. The switch is used when the engineer has noticed the fault on the panel. This system has two inputs (4 possible combinations) and three outputs (green light, red light and bell). 1. Describe the problem clearly in words. Let the temperature sensor be input A and the fault acknowledge switch as input B. Green light is on if: A is 0 AND B is 0 OR A is 0 AND B is 1 The second condition occurs if the engineer acknowledges a fault when there is no fault. The bell rings when: A is 1 AND B is 0 The red light is on when: A is 1 AND B is 0 OR A is 1 AND B is 1 2. Write out a Truth Table for the system. Inputs green red alarm A B light light bell 0 0 1 0 0 0 1 1 0 0 1 0 0 1 1 1 1 0 1 0 3. Derive the Boolean expression Inputs green red alarm A B light light bell 0 0 1 A·B 0 0 0 1 1 A·B 0 0 1 0 0 1 A· B 1 1 1 0 1 A· B 0 29 4. Simplify this expression if possible.. Green Light: Z = (A B) +(A B). It can be seen that the output does not depend on the state of B since the output is HIGH when B is either HIGH or NOT HIGH i.e. the state of the green light depends only on the state of A, hence the expression can be simplified to Green light: Z = A. Red light Z = (A B) + (A B). Similarly it can be seen that the red light is ON irrespective of the state of B the output only depends on the state of A hence this can be simplified to. Red light Z = A. Alarm: Z = A B (can’t be simplified further) 5.Draw a logic circuit diagram for the system using AND, OR and NOT gates. Start with the expression that has fewest terms/gates (in this case no Gates!) A A RED LIGHT A GREEN LIGHT A.B ALARM B B 30 6. Convert the circuit to NAND gates only. (Look for double inverted signals which can be cancelled out.) A RED GREEN ALARM B ASSIGNMENT10 1. An electric guillotine must be adequately guarded. In order to safeguard the operator the machine has two switches, A and B, set about one metre apart, both of which need to be pressed before the machine will operate. Design a logic circuit that will give a green light if, both switches are not pressed and both switches are pressed. If only one switch is pressed a red light should come on. Assume that the switches return a 0 when not pressed and a 1 when pressed. 2. A given logic circuit has two logic inputs A and B. It is required to produce two logic outputs X and Y according to the following rules: 1. X is to be at logic 1 if (A OR B) but NOT (A AND B) are at logic 1. 2. Y is to be at logic 1 if (A AND B) but NOT (A OR B) are at logic 1. Use the previous method to design an appropriate logic circuit. 3. A garage door is operated by a motor which is controlled by three switches. The motor runs either: when a pressure pad switch, A, in the drive is closed and a light dependent resistor switching circuit, B, is simultaneously activated by the car’s headlights or when the keyswitch, C, in the garage door is operated. a) Prepare a truth table for all possible combinations of switching conditions for switched A, B and C. Take switch open as logic 0. 31 b) From the truth table, prepare a logic diagram using the least number of gates. c) Redesign your circuit to use only 2-input NAND gates. 4. A domestic burglar alarm system is designed such that a bell will operate when the power switch is closed and a pressure switch under a carpet is closed or a switch is opened as a window is lifted. a) Assuming all switches to be logic state zero (0) when open i) Draw a logic diagram for the design, allocating capital letters to the inputs to each gate and to the output to the bell. ii) Prepare a truth table for the design. Your table must be headed by the appropriate letters b) Show by use of a logic diagrams how you would\modify or combine 3-input NAND gates to provide AND and OR gates. 5. The diagram below shows part of an industrial control system having three inputs A, B and C with an output G. D A F B G E C a) How many different input conditions are possible in this system? b) What is the function of gates 1 and 2? c) Complete a truth table including columns which show the states of D, E, F and G. d) Why is the design made up entirely of NAND gates e) Give an alternative design using 3-input AND and OR gates. 32 DIGITAL ELECTRONICS & MICROCONTROLLERS CHAPTER TWO PROGRAMMABLE CONTROL Introduction The use of programmable control systems is now common in a huge number of situations. From robotic systems in manufacturing plants, to smart phones, kitchen appliances, games controllers and beyond, they are part of everyday life and will continue to be so for a long time to come. In the National 5 course you were introduced to the concept of using flowcharts and coding to program microcontrollers to perform set tasks. Here is a reminder of what you should already be familiar with. Flow Charts This shape of box must be used at the start and end of a This is a decision box. It flow chart asks a question and points where you should go depending on whether the answer is yes or no. We use this box to switch things off and We use this box to pause on. the flow chart or to do some other internal processes 33 Coding Loops… Infinite loops will repeat a section of a flow chart forever. In the flowchart shown, the output will switch on and off continuously as it loops back to the start. The coding for this process is; main: high 0 ‘switch output 0 on pause 5000 ‘delay 5 seconds low 0 ‘switch output 0 off pause 5000 ‘delay 5 seconds goto main ‘return to main e rt od of c e s ta e is lin to th Th back ps loo We can create loops in two main ways. Finite loops will repeat a section of the flow chart a set number of times. This involves the use of a variable x to keep track of how many times the loop has been performed. In the flowchart shown, the output will switch on and off 5 times before the sequence stops. The coding for this process is called a For…Next Loop. This is how it would be written; Symbol counter = b0 ‘define b0 with the name counter Main: For counter = 1 to 5 ‘start a for…next loop high 7 ‘switch output 7 on e it od ts low 7 ‘switch output 7 off of c nd se next counter ‘end of for…next loop ine ra his l ounte tions end T c piti he e r t st o5r T s t a t start his line o there s the nex f code has b tl een 5 oop (unti repit l itions ) 34 Programming a Stamp Controller Port Addressing and the Data Direction Register Microcontrollers communicate with the outside world by input/output pins which are grouped together in 'ports'. The Stamp Controller has one input/output port, which contains eight input/output pins. Each pin can be addressed individually, or all eight pins in the port can be addressed simultaneously. In the PBASIC language the pins are labelled 0 to 7, and the whole port address is allocated the label 'pins'. For example, to switch pin 3 'high' individually the command would be: high B.3 To switch pin 3 'low' individually the command would be: low B.3 Each pin is referenced to by a single bit in the port address called 'pins'. input/output pin 7 6 5 4 3 2 1 0 bit of address 'pins' 7 6 5 4 3 2 1 0 decimal value 128 64 32 16 8 4 2 1 To switch all pins 'high' the command would be let pins = 255 or %11111111 To switch all pins 'low' the command would be let pins = 0 To switch pins 0-2 'high' and pins 3-7 'low' the command would be let pins = %00000111 or 7 Note how the use of the binary number system can be used on this occasion to clearly illustrate which pins are switched high (=1) or low (=0). 35 Each pin can be configured to be an output (to send digital signals) or an input (to receive digital signals). The Data Direction Register (DDR) is used to configure the ports, and in the PBASIC language the DDR is allocated the label 'dirsx' where x can equal ‘a’ or ‘b’ indicating port A or port B. If all the bits in the DDR are set high then all the pins will be set as outputs. If all the bits are set low each pin will be set as an input. For example, let dirsb = 255 ' set all pins as outputs let dirsb = 0 ' set all pins as inputs let dirsb = %11110000 ' set 0-3 inputs, 4-7 outputs Every PBASIC program listing should always begin with a 'let dirs =' statement to correctly setup the DDR. By default all pins are set to inputs when the Stamp Controller is reset. ASSIGNMENT11 1. Write pBASIC commands that will perform the following functions. a) switch on pins 0 and 1. _______________________________________________________________ b) switch on pins 7,6,5 and 4. _______________________________________________________________ c) switch on pins 7,5,3,and 1. _______________________________________________________________ 2. Write pBASIC commands for the following a) set pin 7 and 6 as outputs and the others as inputs. _______________________________________________________________ b) set pins 6,5 and 3 as outputs and the others as inputs. _______________________________________________________________ 36 Programming a T4 Board Other microcontrollers already have the inputs and outputs set, which means you do not need to include the let dirs = %100000000 code. A T4 Board has an PICAXE 18x microcontroller, which has a pin configuration as shown. Input (Push Switch) 7 Output (red) Input (Push Switch) 6 Output (green) 5 Output (red) 4 Output (yellow) 3 Output (green) Input (thermistor) 2 Output (red) Input (variable resistor) 1 Output (yellow) Input (LDR) 0 Output (green) ASSIGNMENT12 For the following tasks produce a working simulation in Yenka and a corresponding pBASIC program that could be tested on a T4 board. Copy the solutions into your jotter 1 : An LED must flash 10 times after a switch has been pressed. 37 2: A motor must spin forward when a switch is pressed and reverse when a second switch is pressed. pBASIC Coding The main programming commands that you should be familiar with are used in the program below. symbol red = 7 ' define pin 7 with the name "red" symbol counter = b0 ‘ define b0 with the name “counter” init: let dirs = %10000000 ' set up pin 7 as an output, all others as inputs main: if pin0 = 0 then main ' If input0 is not on then loop back to the label main for counter = 1 to 5 ' start a for...next loop high red ' switch pin 7 high pause 1000 ' wait for 1 second low red ' switch pin 7 low pause 1000 ' wait for 1 second next counter ' end of for...next loop end ' finish here. 38 3. Design a system that will operate the traffic light sequence at a pelican crossing. The system should remain in State 1 until a pedestrian presses a button. In State 4 the amber light should flash 5 times before returning to State 1. Input Pin Output Connection Connection 7 Red Light 6 Amber Light 5 Green Light 4 3 State 1 State 2 State 3 State 4 2 1 Start Switch 0 4. As part of a Christmas decoration, a lighting sequence is to be controlled by a microcontroller. The output connections are shown below. Input Pin Output Connection Connection 7 6 Yellow Light 5 Purple Light 4 3 Blue Light 2 Green Light 1 0 Red Light The red and green lights should come on together and stay on for 5 seconds. Then they both go off and the yellow and blue lights should come on together for 8 seconds. They then go off and the purple light flashes on and off 6 times (the 'on' and 'off' times being 0.5 seconds each). The sequence then repeats itself. Draw a flowchart and write a PBASIC program for this sequence. 39 Monitoring Multiple Inputs There are many situations where systems need to be able to check a number of inputs at the same time before performing a further function. An obvious case might be an automatic door to a building that would need to check for people entering and leaving a building. This type of monitoring can be done quite simply in the following way. The key part here is connecting successive decision boxes in a loop so that each can be checked in turn. This can be expanded for as many inputs as may be required. 40 ASSIGNMENT13 For each task create a working Yenka simulation and a pBASIC program for the given specification. 1. A climate control system for an office must; Spin a fan when the temperature rises above a set level (the motor must run off a 12V supply) Light a bank of three LEDs when it gets dark. 2. A control system is required for a race track for model dragsters. It is to be a 3-track version of the one shown below. The system must be activated by a starter pressing a switch Sensors at the end of each track must activate when that track’s dragster goes over them. A lamp must light to indicate the track that the winning dragster was 41 DC Motor Speed Control Pulse Width Modulation There are two main ways of controlling the speed of a dc motor. One is by changing the voltage applied to it but this is not simple to do with a digital system because the outputs of a microcontroller are either 5V or 0V, nothing inbetween. Therefore this solution is often unsatisfactory for controlling DC motors due to the undesirable loss of motor torque. Pulse Width Modulation (PWM) is a digital method which can be used to vary the motor speed. In this method the full voltage is applied to the motor, but it is rapidly pulsed on and off. By varying the on and off ratio of the pulses the speed of the motor can be varied. As the full voltage is applied to the motor during the 'on' pulses the torque of the motor remains high. V MARK t SPACE The graph shows how the technique is applied. The 'on' time for the motor is called the mark, the 'off' time is called the space. When the voltage is applied to the motor it accelerates to top speed. However before the top speed is reached the motor is switched off, thus slowing it down. By increasing the frequency of the pulses this acceleration/deceleration becomes negligible, and the motor rotates constantly at a slower speed. The PWM technique does have certain limitations. It cannot be used with mechanical relays, as the rapid switching would damage the mechanical contacts. The frequency of the pulses must also be carefully selected - if the frequency is too slow the motor will stall. 42 ASSIGNMENT14 Key in, download and run the program listed below. This program drives the motor at approximately half speed, as the space is twice the length of the mark. symbol mark = b1 symbol space = b2 symbol motor = 7 init: let mark = 10 ' set mark to 10ms let space = 20 ' set space to 20ms main: high motor ' output high pause mark ' pause for mark time low motor ' output low pause space ' pause for low time goto main ' loop Try out different speeds (by experiment) by altering the values of 'mark' and 'space'. 43 Arithmetic operations Finite loops can use arithmetic operations to test if a variable has reached a desired value. In the flowchart below this is done using the commands add 1 to x and x=5?. We can use a number of other operations to give us more control. The table below shows a number of commands and how they could be programmed on Yenka or in pBASIC. Command Yenka pBASIC Decrease the value of x let x = x-1 Divide the value of x by 3 (this will return a whole let x = x/3 number) Multiply the value of x by 3 let x = x*3 Check if x is more than 5 if x > 5 then label Set the value of x to half of let x = y/2 y 44 The system below shows how a warehouse could simply keep track of the number of items in stock. As one new item is brought in a sensor is activated and the value of x (a variable that indicates the number of items) is incremented. S second sensor is activated when an item is removed and sent to a customer. The final part of the flow chart shows how a warning could be issued when stock levels are low. ASSIGNMENT15 1. Build the system shown above and use the Monitor box to see how x reacts to the input from the sensors. Write a pBASIC program to perform the same function as the system shown. 2. Develop a system in Yenka and a pBASIC program to keep track of the number of cars in a car park. When there are 10 a barrier should close. 45 3. A system is required to control a lift in a building. It must; Count people coming in and out of the lift. If there are 10 or less in the lift when a 'close' button is pressed then a motor should close the door. If there are more than 10 people when the 'close' button is pressed an alarm must switch on. The alarm must stop when there are 10 people or less in the lift. (Someone needs to get out) Complete a Yenka simulation and pBASIC program to perform the required specification. 46 4. A drinks machine can take in the following coins—5p, 10p and 20p. There will be a separate slot for each type of coin. Create a system that will meet the following criteria. When 35p is put in the machine and a button is pressed a can must be given out. The machine must give out change in 5p pieces.(Use a lamp to indicate each coin being ejected) Complete the Yenka simulation shown below and a pBASIC program to perform the required specification. 47 Analogue inputs So far you have made use of digital inputs but in the real world there are numerous situations when an analogue input is received and must be processed. For example, a games console will need to take in very detailed information from a motion sensor to work out where a user is and what movements they are making. This would not be possible if the sensor only indicated if there was movement or not. Most microcontrollers will have internal analogue-to-digital converters (ADCs). They will convert the input analogue value to a binary number in the range of 0-255 (0 - %11111111). Analogue signals can be read by a control system in Yenka using; This will take a reading from an input pin that has been set as an analogue input and stores it in variable x. In pBASIC the same function can be performed with the command, readADC 0,x This works with microcontrollers that have inbuilt ADCs on 1 or more of their inputs. For the instruction above the 0 indicates that pin0 is an analogue input with an analogue-to-digital converter and that the analogue input value will be stored in variable x. 48 The STAMP controller has no internal ADC but does have one on the input module which can work with 2 inputs. To use analogue inputs we need to move the switch on the input module to select analogue inputs and use the commands such as let b0 = sensorA ‘store the value from the sensor in x let b0 = sensorB if sensorA > 50 then main ASSIGNMENT16 Try simulating the following system to monitor the two analogue inputs. 49 ASSIGNMENT17 1. Design a system, and produce a pBASIC program, that will perform the following function for a projector. When a start switch is pressed a lamp comes on. If the temperature rises above a desired level a fan is switched on. COMBINED TASKS ASSIGNMENT18 1. Write a pBASIC program to perform the following function; increase the speed of a motor when one switch is pressed decrease when a second switch it pressed stop the motor when a third is pressed 2. As the outside light level decreases 4 LEDs come on in succession to maintain a consistent light level in a room. Complete a Yenka simulation and a pBASIC program. 50 3. A wind turbine uses a potentiometer to sense the direction of the wind. It uses a second potentiometer to monitor which direction the turbine is facing. Design a control system in Yenka, and write a pBASIC program, that will make a motor turn forward or backward to move the turbine to turn into the wind. Extension – Adjust the speed of the motor so that it slows down as it gets closer to its desired position. 51

Use Quizgecko on...
Browser
Browser