Key-Programmable Control Microcontrollers are everywhere: in household devices, in devices of entertainment electronics, in meters and even in unmanned space vehicles. Everywhere, they do things that a programme tells them. It is very exciting to generate simple control programmes yourself as well. The first step always is choosing a microcontroller or processor that matches your desired task as precisely as possible. You can choose between any number of types from different companies.
Table of Contents Key-Programmable Control 3 Table of Contents 5 1 Introduction 7 2 Alternating Flash 10 3 Binary Counter and PWM Output 11 4 Analogue-Digital Converter 15 5 Random Generator 17 6 Impulse Length Measurement 19 7 Reading Programmes 21 8 Entering Programmes 23 9 Recovery of the Example Programmes 25 10 TPS Basic Commands 25 11 Computing with Variables 28 12 Jumps and Branches 30 13 Command Overview 32 14 Counting Loops 33 15 Comparisons 34 16 A
21 6 Appendix 41
1 Introduction The principle of the TPS controller is simple. You have four digital inputs E1 to E4 and four digital outputs A1 to A4. There are also two analogue inputs AD1 and AD2 and a quasianalogue PWM-output. A reset input with a connected reset button resets a programme to the start. The controller is supplied with three AA cells with approx. 4.5 V and can work in a range of 2.2 V to 5.5 V. Technical Data: Microcontroller: HT46F47 Clock frequency: 2 MHz Internal EEPROM: 128 Bytes Power supply VCC: 2.
Fig. 1: Basic circuit of the system Fig. 2: Standard setup with pushbuttons Some basic programmes are already present in the delivery condition of the TPS-controller (default) and can be started directly.
step by step. First, familiarise yourself with the hardware functions and start your own programmes only after this. In the first test, you will start small programmes that are already finished in the controller. The associated programme lists provide an initial impression of the options. They are only briefly explained. The precise explanation of the individual commands follows in the next chapter.
under all circumstances, since it may destroy the controller. Install a brief wire piece as tension relief. Once the voltage connection has been established, it should remain connected continually if possible. To deactivate it, take one of the batteries from the compartment. Fig. 4: Minimum application with LEDs Already use the reset button additionally and connectfour LEDs to dropping resistors of 2.2 kO. They are needed for the first hardware tests. Observe the order.
Address Command Data Comment 20 1 1 LED 1 21 2 8 Wait for 500 ms 22 1 8 LED 8 23 2 8 Wait for 500 ms 24 3 4 Jump –4 Listing 1: Alternating flash If the desired result does not occur, check proper polarity of the LEDs first. It is also helpful to measure some voltages. Always use, e.g., a digital multimeter in the 20 V range and leave the minus connection at GND. All voltages are thus measured against GND: VCC: 4.5 V Reset: 4.5 V Osc1: 1.5 V E1 to E4: 4.
Fig. 5: Use of the PWM-LED Fig. 6: Starting the binary counter Apply E1 to GND. This starts the second example programme after a reset. It counts up the output conditions binarily.
gone through. The programme uses variable A for a simple addition and for output to the digital outputs as well as the PWM-output. Commands 7 and 5 have subfunctions that are written as data. Address Command Data Comment 25 7 1 A=A+1 26 5 4 Port = A 27 5 9 PWM = A 28 2 6 Wait for 100 ms 29 3 4 Jump –4 Listing 2: Binary counter with LEWD and PWM output The counting programme can be used as a practice programme for reading binary counters that you must master for your own programming.
A1: 200 ms A2: 400 ms A3: 800 ms A4: 1,600 ms Additionally, the rising numerical values are also output to the PWM output (pulse width modulation) . The PWM signal is a rectangular signal with a frequency of approx. 16 kHz. The pulse length is controlled so that the pulse-pause ratio determines the average activation duration and thus the brightness of the LED. The brightness of the connected LED is controlled in 15 levels between zero and full brightness.
Fig. 8: Smoothed PWM output voltage 4 Analogue-Digital Converter A connection E2 to GND and pushing the reset button will start a small example programme for the analogue-digital converter (AD-converter). The analogue voltage at the analogue input AD1 is measured and converted to a digital numeric value. Because the TPS controller works with 4bit values continually, the result of the analogue-digital conversion is a number from 0 to 15.
Fig. 9: Connection of the light sensor Fig.
The example programme is very similar to the programme from the last section because of the output to the digital outputs and the PWM-output. The first line has the command to convert an analogue value, however. Address Command Data Comment 2A 6 9 A = AD1 2B 5 4 Port = A 2C 5 9 PWM = A 2D 2 6 Wait for 100 ms 2E 3 4 Jump –4 Listing 3: AD converter and PWM output Test the programme with different sensor light exposure. The more light on the LDR, the lower the voltage at AD1.
Fig. 11: Start of the random switch Fig. 12: Jumper between E3 and GND The programme uses a conditional jump command. When the S1 input condition is one, the following command is skipped.
the variable A. This leads to quick counting up of the starting condition. When releasing, the last counter reading is retained. Due to the high counting speed, you have no influence on the result, which therefore is random. Address Command Data Comment 30 5 4 Port = A 31 C E S1 = 1? 32 7 1 A=A+1 33 3 3 Jump –3 Listing 4: Random generator Briefly push the button to reactivate a new random result. Test the random function by generating a statistic of the results.
Fig. 13: E4 at GND Fig.
A time measurement runs in condition S1 = 0, i.e. with the button pushed. Another approx. 5 ms are added to the waiting time of 5 ms for execution of a total of five commands in the counting loop. Therefore, the time unit of the measurement is 10 ms.
Fig. 15: S1 and S2 for programming mode Fig.
• • • • • • • Display off, 300 ms Show command Second push of button S2 Show data Third push of button S2 Display next address, 300 ms etc. If you want to view only a present programme with five steps, for example, without changing it, a total of ten pushes of S2 takes you to the end. Because the current address is displayed briefly, orientation is easy. You will always know if the display is currently showing a command or data.
display and uses it for programming the EEPROM. You know this from the car: When you activate the starter, the light and radio will go out for a brief moment. You can change the already-present programme in a single location as well. Use S2 to scroll to the desired location and change the command or data with S1, to save them with S2. For the first part, a programme with only two commands is entered. It switches on three LEDs and leads into an endless loop.
9 Recovery of the Example Programmes If you want to restore the controller's original condition after some time, this can be done by entering two bytes FF. In fact, this corresponds to the condition of the unwritten EEPROM. The firmware of the TPS controller contains a start function that initially reviews the first two addresses to recognise an empty memory. If two FF-bytes are read here, the controller assumes that no programme has been entered yet.
The waiting command 2 uses a parameter that contains the time in milliseconds and a graduation of 1-2-5. In spite of the low number scope from 0 to 15, this permits execution of delay times between one millisecond and one minute. You would have to wait even longer if you were to programme execution of the waiting command several times, e.g. in a counting loop. The return command 3 is particularly simple and is sufficient for any tasks where a process should be repeated endlessly.
Address Command Data Comment 08 3 8 Jump –8 11 28 12 28 14 28 18 28 38 Listing 10: Running light 1 Expand the programme with two more output patterns, so that the light point runs back and forth. Experiment with other output patterns and delay times as well.
11 Computing with Variables Until now, the parameters have used only consistent number values in the parameters of the individual commands. This is sensible when a programme is to run the same way every time. More complex programmes, however, work with variable data. E.g. a calculation such as A = A + B can be executed. Depending on the content of the variables A and B, something different will result each time. The result could control the LEDs at the outputs as follows, for example.
65: A = Din.0 66: A = Din.1 67: A = Din.2 68: A = Din.3 69: A = AD1 6A: A = AD2 71 –7A: A = Expression 1–10 71: A = A + 1 72: A = A – 1 73: A = A + B 74: A = A – B 75: A = A * B 76: A = A / B 77: A = A And B 78: A = A Or B 79: A = A Xor B 7A: A = Not A One example of using the variable A is found in the programme examples in chapter 3. The programme was set at the address Zero here and slightly expanded. Additionally, there is a defined start with the value 0 in the variable A.
Address Command Data Comment 00 6 9 A = AD1 01 5 4 Port = A 02 7 A A = Not A 03 5 9 PWM = A 04 2 6 Wait for 100 ms 05 3 5 Jump –5 69 54 7A 59 26 35 Listing 14: Invert 12 Jumps and Branches Up to now, there was only a simple jump back (command 3) that went back up to 15 addresses. Now we add an absolute jump. Since the jump target can only be indicated with 4 bit, there is an additional command that specifies the high nibble of the address.
C6: if Din.2 = 1 then Adr = Adr + 1 C7: if Din.3 = 1 then Adr = Adr + 1 C8: if Din.0 = 0 then Adr = Adr + 1 C9: if Din.1 = 0 then Adr = Adr + 1 CA: if Din.2 = 0 then Adr = Adr + 1 CB: if Din.
Enter the programme and test it. The reaction time is no much faster. The time unit is at approx. 5 ms. The original example programme is still in the memory from address 34h onwards, since only the lower addresses have been overwritten. Write a little programme that contains only a jump to this address. You first need to indicate page 3. The following absolute jump with the specified address 4 then actually targets the address 34.
9 9 1s 9 9 PWM = A A= AD1 A=A Xor B 9 9 9 Din.1 = 0 9 1 2 3 4 5 6 7 9 A B C D A 10 2s 10 10 A= AD2 A = Not A A A A Din.2 = 0 A B 11 5s 11 11 B B B Din.3 = 0 B C 12 10 s D 13 20 s 12 12 C C C S1 = 0 C 13 13 D D D S2 = 0 D E 14 F 15 30 s 14 14 E E E S1 = 1 E 60 s 15 15 F F F S2 = 1 F 8 E 14 Counting Loops A process is to be performed, e.g. five times. For this, a count loop is formed.
Change the count variable to value 4 and test the programme again. Now the LEDs will flash precisely five times. You can also use the count loop so that you will not jump back, but forwards. This time, the process is actually performed five times when C has been loaded with the value 5 in the beginning. The skipped address 04 contains a relative jump to itself and thus an endless loop that serves as the programme end.
02 8 0 AdrHi = 0 Address Command Data Comment 03 6 9 A = AD1 04 C 1 Skip if A>B 05 9 8 Adr 08 06 1 F LEDs 1111 07 3 4 Adr 03 08 1 0 LEDs 0000 09 3 6 Adr 03 45 51 80 69 C1 98 1F 34 10 36 Listing 19: Simple twilight switch Test the programme by shading the light senor with your hand more or less. You will find that the basic function is met. However, there is usually an unpleasant side effect. Exactly at the threshold between On and Off, the LEDs will flash uncontrolledly.
Address Command Data Comment 03 7 7 A = A And B 04 5 4 Port = A 05 3 5 Jump –5 64 51 43 77 54 35 Listing 20:Application of the AND function Change the programme and test other logical functions as well.
Subprogramme: Address Command Data Comment 08 7 2 A = A-1 09 E 0 Ret 80 D8 54 29 D8 54 28 37 72 E0 Listing 21: Subprogramme calls The result of the programme is a downwards counting binary counter with uneven time delays. Also test other commands in the subprogramme. There are several useful subprogrammes for general use among the example programmes in the delivery condition. They are listed completely in the Appendix.
18 Twilight Switch A twilight switch is to switch on the lamp when the ambient brightness drops below a certain limit. When the light grows brighter, the lamp is to go out again. It should be ensured that the light does not flicker on the threshold between light and dark. This is possible with a hysteresis, i.e. a certain distance between the activation and deactivation brightness.
In the core of the programme, the skip commands that are already known are used. If the respective button is not pushed, the associated command to increase or reduce the accumulator content is skipped. The problem is that this can usually lead to an overrun from 15 to 0 or from 0 to 15. Preventing this overrun requires somewhat more effort. For this, you have to query whether the lower end (0) or the upper end (15) has already been reached.
20 Number lock The number lock presented here switches on the PWM output if the user enters the correct number sequence. The number input should be precisely according to the pattern of programming via buttons S1 and S2. The following programme demonstrates input of a single number via the button S1. As when programming, the first push of the button leads to result 0000. Any subsequent push of S1 increases output by 1. Pushing S2 ends the input. In this case, the programme will end in an endless loop.
The PWM output is treated like a normal digital port in this example. This is necessary because all four outputs A1 to A4 are needed for number input. After each complete input, the four LEDs are deleted to give the observer as little information about the secret combination as possible.
02 4 E A = 1110 03 8 0 Page 0 04 C 3 A = B? 05 9 8 Jump 08 06 8 2 Page 2 07 9 5 Jump 25, "Count up" 08 4 D A = 1101 09 8 0 Page 0 0A C 3 A =B ? 0B 9 E Jump 0E 0C 8 2 Page 2 0D 9 A Jump 2A, "AD/PWM" 0E 4 B A = 1011 0F 8 1 Page 1 64 51 4E 80 C3 98 82 95 4D 80 C3 9E 82 9A 4B 81 Page 0: Selection and start of the example programmes Address Command Data Comment 10 C 3 A = B? 11 9 4 Jump 14 12 8 3 Page 3 13 9 0 Jump 30, "Random" 14 4 7
C3 94 83 90 47 81 C3 9A 83 94 43 82 C3 90 84 90 Page 1: Selection and start of the example programmes Address Command Data Comment 20 1 1 0001 "Alternating flash" 21 2 8 Wait for 500 ms 22 1 8 1000 23 2 8 Wait for 500 ms 24 3 4 Jump –4 25 7 1 A = A + 1 "Count up" 26 5 4 Port = A 27 5 9 PWM = A 28 2 6 Wait for 100 ms 29 3 4 Jump –4 2A 6 9 A = AD1 "AD/PWM" 2B 5 4 Port = A 2C 5 9 PWM = A 2D 2 6 Wait for 100 ms 2E 3 4 Jump –4 2F F F - 11 28 18
3C 3 4 Jump –4 3D 3 9 Jump –9 3E F F - 3F F F - 54 CE 71 33 22 CC 32 40 22 71 54 CE 34 39 FF FF Page 3: Example programmes: Random, stop watch S1 Address Command Data Comment 40 8 6 Jump 6, "Stop watch Start/Stop" 41 D 0 Call "Waiting S1" 42 4 0 A=0 43 7 1 A=A+1 44 5 4 Port = A 45 2 3 Wait for 10 ms 46 C D S2 = 0? 47 3 4 Jump –4 48 D 8 Call "Waiting S2" 49 4 0 A=0 4A 5 4 Port = A 4B 3 B Jump –11 Address Command Data Comment 4C F F -
56 2 1 2 ms 57 1 9 A4 = 1 58 1 1 A4 = 0 59 2 1 2 ms 5A 1 9 A4 = 1 5B 1 1 A4 = 0 5C 2 0 1 ms 5D B 4 D-times 04 5E 1 0 Dout 0 5F E 0 Return 4F 93 45 53 19 11 21 19 11 21 19 11 20 B4 10 E0 Page 5: Subprogramme sound output Address Command Data Comment 60 2 3 Wait 10 ms "Wait S1" 61 C E S1 = 1? 62 3 2 Jump –2 63 2 3 Wait for 10 ms 64 C C S1 = 0? 65 3 1 Jump –1 66 E 0 Return 67 F F - 68 2 3 Wait 10 ms "Wait S2" 69 C F S2 = 1? 6A
72 4 0 A=0 73 5 4 Port = A 74 2 3 Wait for 10 ms 75 C E S1 = 1? 76 3 2 Jump –2 77 C F S2 = 1? 78 E 0 Return 79 C C S1 = 0? 7A 3 3 Jump –3 7B 7 1 A=A+1 Address Command Data Comment 7C 2 3 Wait for 10 ms 7D C C S1 = 1? 7E 3 1 Jump – 1 7F 3 C Jump –12 CC 31 40 54 23 CE 32 CF 30 CC 33 71 23 CC 31 3C Page 7: Subprogramme button input Command table 1 2 Port = Wait 0 0 1 ms 0 0 1 1 2 ms 1 1 B=A A=B 2 2 5 ms 2 2 C=A A=C 3 3 10 ms
9 9 1s 9 9 PWM = A = AD1 A = A Xor A B 9 9 9 Din.1 = 0 9 A 10 2s 10 10 A = AD2 A = Not A A A A Din.2 = 0 A B 11 5s 11 11 B B B Din.
Imprint © 2012 Franzis Verlag GmbH, 85540 Haar www.elo-web.de Author: Burkhard Kainka ISBN 978-3-645-10104-2 Produced at the order of Conrad Electronic SE, Klaus-Conrad-Str. 1, 92240 Hirschau All rights reserved, even including photomechanical reproduction and storage on electrical media. Generation and distribution of copies on paper, data carriers or online, in particular as PDF, is only permissible with the express consent of the publisher and will be prosecuted under criminal law.
Electronic devices must not be disposed of in the domestic waste! At the end of its service life, dispose of the product according to the relevant statutory provisions. Collection points have been set up for return. You may dispose of your electrical devices there free of charge. Your community will inform you of where such collection points are located. This product complies with the relevant CE directives if used according to the included instructions.