A book reader Mudah dan Menyenangkan Belajar Mikrokontroler asked as follows:
- In the program listing of digital clock with button (p. 91), please explain more about the use and calculation of Timer 0 Mode 2 so as to produce an accurate timer of 1 second.
- Please explain more about the workings of the listing of button data retrieval (p. 119).
- How do I use a variable declaration in listing with *.M51 extension such: ~byte, ~bit, ~word, and ~array?
- How to use the tool: Virtual LCD, Tombol, MXLED, in the simulator?
- Well, the next question may not relate to the book, but still about a microcontroller. How to make one button so it has two functions? For example we put the button at P3.0. If pressed once it will execute subprogram A and if pressed twice it will execute subprogram B.
- Mas, I tried to make the clock circuit on PCB and I filled it with the “debouncing 2.HEX” program. The result was running perfectly. But when the minutes or hours modifier button was pressed, the value on 7 segment did not changed directly but wait until the program completing one minutes. How to make changes instantly?
And here’s the answer:
Timer 1 sec accurate.
Actually, the information in the book is clear enough. First, the crystal used was 11.0592 MHz. Thus the speed of the machine cycle is 921.6 KHz (11.0592 MHz/12). Second, TH0 filled #0, which means timer0 interrupt will be called every 256 cycles of the machine. Remember that TH=256-interval or interval=256-TH. Thus the calling interval is 256 machine cycles.
From both the data above we can calculate the speed of the calling of timer0 interrupt is 3600Hz (921.6 KHz/256). Now, to obtain a second, we must divide this interval again by 3600. So we do this in two steps, which is divided by 225 (3600/225 = 16), then we can simply divide the result by 16 to obtain a frequency of 1Hz. However, because we need to set the frequency to 2Hz to make the blink of the point, then we do not divide by 16 but by 8.
1Hz frequency is obtained with a flip-flop which is a 2 divider. Flip-flop is obtained by using the cpl command on a flag (Bendera1Detik). Hopefully quite clear.
Techniques to retrieve button data by debouncing.
Pieces of the program will retrieve multiple keys concurrently and has been carried out debouncing. So we can use any key and do not have to worry about bounce. How to use it is just call the procedure, ie “AmbilTombol”.
After a call to this procedure, Acc will contain the button data. For example, if the button mounted on the bit0 and bit1, then after calling this procedure, A will contain 0 if no keys are pressed, 1 if the button on bit0 is pressed, 2 if the button on bit1 is pressed, and 3 if both buttons are pressed.
Of course, the resulting value depends on where the buttons are mounted. Just look at the value of bits. Bit0 is 1 and bit1 is 2. About how it happens, try to look at the included flowchart. Learn the flowchart carefully. Hopefully quite clear.
Memory allocation for variables with a technique that easy to make any modification.
~byte, ~bit, ~word, and ~array variable are the relatively easy way to set the location of memory compared with the set of memory locations in a way such as:
1234Buffer <b>equ</b> 8 <i>;4 byte</i>Angka <b>equ</b> 12Puluhan <b>equ</b> 13Satuan <b>equ</b> 14It would be easier if we use:
12<b>~Array</b> 4 Buffer<b>~byte</b> Angka Puluhan SatuanWith the second way, we will put the Buffer at location 8, the Angka at 12 locations, Puluhan on the location 13, and Satuan on the location 14. And automatically there will be a constant that holds the highest position of the memory location that is not used. In this position should be stored for stack initiation. The constant is named SaveStack. Thus, we can initiate the stack with:
1<b>mov SP</b>,#SaveStackand it would have the same meaning with:
1<b>mov SP</b>,#14because the highest location used is 14.
If we use the first way and we want to change the memory requirement for the buffer to be only 2 bytes, we will set it to:
1234Buffer <b>equ</b> 8Angka <b>equ</b> 10Puluhan <b>equ</b> 11Satuan <b>equ</b> 12and the stack initiation becomes:
1<b>mov SP</b>,#12Well, we have to change all the numbers in its memory location setting. But if we use the second way, then we simply replace the number 4 to 2 in the ~array declaration such as:
12<b>~Array</b> 2 Buffer<b>~byte</b> Angka Puluhan Satuanor because the Buffer only requires 2 bytes, then we can replace it with the ~word type such as:
12<b>~word</b> Buffer<b>~byte</b> Angka Puluhan SatuanNote that we only change the Buffer declaration and no other parts that we change.
While the variable ~bit is a 1-bit variable that is placed on location 20H.0 to 3FH.7.
Placement of all these variables will always find a lowest location that is still unused starting from address 8 (the default) or can be moved to a specific position using ~basequ.
And the explanation about this is already in the appendix of the book. Hopefully quite clear.
“Virtual LCD” (LCD simulation), “MXLED” (LED Matrix simulation), and “Tombol” (Button simulation using keyboard).
- “Virtual LCD” can be used for experiments that require a lot of character display. This simulation can be linked with the real circuit using a serial connection. See Chapter 19 of “Easy and Fun Learning Microcontroller”. However, these simulations can also be used from the simulator with “Link Message”. To do this, select used Com to “Message”. If data about the keyboard keystrokes are sent to the simulator via the serial communication (received in SBUF), the “Message to send” must be filled by 1035. Whereas if the data desired to be received by the port, then the “Message to send” is 1034 and “lParam to send” filled with the port number, ie 0, 1, 2, or 3. Besides, “Handle” should point to the active simulator. We can use the “Capture Handle” then click on the window of the target simulator. In addition to settings on the “Virtual LCD”, simulator port must also be adjusted. If the simulator wants to send data to the “Virtual LCD” through a serial communication simulations, the Com should use the “Message” and set “Handle” to point to the desired “Virtual LCD” with “Message to send” in the form WM_USER (1024). Meanwhile, if the simulator was about to send its data through a port, then we have to set the port to use “Link Message” directed to the “Virtual LCD”.
- “MXLED” is a LED matrix simulation. This simulation also receive data through the “Link Message” from the port settings on the simulator. Because the explanation rather long, so I put in a separate post that is in the Showing Images on LED Matrix using Simulator.
- For “Tombol”, if the port settings on the simulator using the “Link Message”, then the keyboard keystrokes on the “Tombol” will be sent to the simulator. “Tombol” itself does not require setting, because automatically it will find the current simulator.
One button for many functions.
Such buttons programming requires a fairly precission timing. The first is to check whether the button is pressed or not. If yes, then the process of button detection began.
The detection of this button is basically the same as other debouncing techniques, ie wait until the button is released again, but if the button has been released does not directly assume that the button has been released. There is a minimum time so that the button has been released is really considered to has been released. If time is not reached, then the next keystrokes will still be considered as part of previous keystrokes.
Now if the button is eligible to be considered to has been released, meaning that the minimum waiting time (timeout debouncing) exceeded. Program does not directly perform a certain action but simply increase the variable that stating the number of keystrokes, then wait the next keystroke again. This is done by creating a flag stating that the program is waiting for more key presses. This waiting period we now call the “next press timeout”. If the button is pressed again before the waiting time is exceeded, then the keystroke will only increase the calculation of the amount of keystrokes. The program will only process the button press data only when “next press timeout” is exceeded.
You can download the example program which is a two-digit counter 0-99. Port0 used to transmit data segment, port1 to control digits, while Port3.0 used as a button. To increase the count, we must press the button once. As for decrease the count, we must press the button twice quickly. We can also directly reach a value of 99 by pressing the button four times quickly. As for the immediate return to 0, then we must press the button three times quickly.
From this example, we can use one button for a lot of actions. But remember, if you create a program and there is a function executed by pressing the button quickly to ten times, can certainly be uncomfortable in their use ๐
Revised example program of digital clock with buttons with debouncing.
After I recheck, there are something missing and some were wrong. Should the MainLoop structure is like:
123<b>mov R4</b>,#NoMode <i>;R4 =status mode</i>MainLoop0: <b>acall</b> IsiBufferMainLoop: <b>acall</b> AmbilTombolAnd each finished making changes to the data either minutes or hours, then the jump is toward MainLoop0 and not to the MainLoop. In addition, the constants for key presses should be:
123S1ditekan <b>equ</b> 00010000b <i>;original =00001000b</i>S2ditekan <b>equ</b> 00100000b <i>;original =00010000b</i>S1S2ditekan <b>equ</b> 00110000b <i>;original =00011000b</i>And the button filtering on “AmbilTombol” should:
12345AmbilTombol_:<b>mov A</b>,P3<b>cpl a</b> <i>;data inversion</i><b>anl A</b>,#00110000b <i>;original =00011000b</i><b>ret</b>These errors resulted in a mistaken reading of the buttons because the buttons position are at P3.4 and P3.5 instead of P3.3 and P3.4. But I have made a revised version with a little addition so that when the status mode is on ModeJam, then the hours digit will be blinking. Similarly, when the status mode is on ModeMenit, then the minutes digit will also be blinking. Thus the user knows what will happen if the button is pressed. Revised program listing can be downloaded here.
Which can be downloaded from this post:

Leave a Reply