A book reader Mudah dan Menyenangkan Belajar Mikrokontroler asked as follows:

  1. 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.
  2. Please explain more about the workings of the listing of button data retrieval (p. 119).
  3. How do I use a variable declaration in listing with *.M51 extension such: ~byte, ~bit, ~word, and ~array?
  4. How to use the tool: Virtual LCD, Tombol, MXLED, in the simulator?
  5. 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.
  6. 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:

  1. 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.

    Back to top

  2. 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.

    Back to top

  3. 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:

      Buffer  equ 8  ;4 byte
      Angka   equ 12
      Puluhan equ 13
      Satuan  equ 14

    It would be easier if we use:

      ~Array 4 Buffer
      ~byte Angka Puluhan Satuan

    With 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:

      mov SP,#SaveStack

    and it would have the same meaning with:

      mov SP,#14

    because 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:

      Buffer  equ 8
      Angka   equ 10
      Puluhan equ 11
      Satuan  equ 12

    and the stack initiation becomes:

      mov SP,#12

    Well, 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:

      ~Array 2 Buffer
      ~byte Angka Puluhan Satuan

    or because the Buffer only requires 2 bytes, then we can replace it with the ~word type such as:

      ~word Buffer
      ~byte Angka Puluhan Satuan

    Note 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.

    Back to top

  4. “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.

    Back to top

  5. 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 🙁

    Back to top

  6. 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:

                  mov   R4,#NoMode     ;R4 =status mode
       MainLoop0: acall IsiBuffer
       MainLoop:  acall AmbilTombol

    And 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:

       S1ditekan   equ   00010000b ;original =00001000b
       S2ditekan   equ   00100000b ;original =00010000b
       S1S2ditekan equ   00110000b ;original =00011000b

    And the button filtering on “AmbilTombol” should:

       AmbilTombol_:
                   mov   A,P3
                   cpl   a            ;data inversion
                   anl   A,#00110000b ;original =00011000b
                   ret

    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.

    Back to top

Which can be downloaded from this post:

  1. Examples of one button many function programs
  2. “Debouncing 2” program revision

38 Comments

Time limit is exhausted. Please reload the CAPTCHA.

Notify me of followup comments via e-mail. You can also subscribe without commenting.

  • 1.Apakah penambahan perintah pada alamat Timer 0 menyebabkan perhitungan waktu menjadi tidak akurat? Dalam Hal ini saya memodifikasi listing Debounching.A51 yang terdapat di buku karangan anda dengan menambah kan beberapa perintah pada alamat timer 0 hasilnya perhitungan waktu 1 detik menjadi lebih cepat dari sebelumnya.
    2. Sensor Optocoupler itu yang Bagaimana?, Yang Kaki nya 4 Menyerupai IC 4pin bukan? Terus sinyal apa yang dihasilkan (0 atau 1) dari Optocoupler tersebut?
    3. Bagaimana cara mengeser karakter Pada LCD 2×16 tepat nya pada listing program jam_LCD.A51. trus kenapa pas saya coba di simulator Programnya tidak berjalan dengan baik

    1. Penambahan listing pada alamat Timer0 sangat mungkin menyebabkan perhitungan waktu menjadi tidak akurat, terutama kalau perintah yang diberikan membuat variabel perhitungan waktu menjadi kacau. Disamping itu, panjang total yang diijinkan untuk ditambahkan di situ, tidak boleh lebih panjang dari 256 siklus mesin, karena hal tersebut berarti seluruh waktu mikrokontroler akan dihabiskan oleh timer0.

      Optocoupler itu pasangan antara pemancar sinar dan penerima sinar (sangat dianjurkan yang infra red). Ada yang memang sudah dalam bentuk pasangan, bisa juga membuatnya dari dua buah LED infra red. LED yang satu digunakan sebagai pemancar, dan LED yang lainnya digunakan sebagai penerima. Pada contoh di buku, yang digunakan sebagai penerima adalah transistor foto. Tentu saja sinyal yang dihasilkan harus bisa diartikan menjadi 0 (L) atau 1 (H) agar bisa dimengerti oleh mikrokontroler.

      JAM_LDC.A51 tidak dirancang untuk dijalankan oleh simulator, tetapi untuk LCD yang sesungguhnya. Virtual LCD memiliki protokol yang berbeda dengan LCD yang sesungguhnya. Jadi tidak kompatibel. Kalau mau mencoba dengan simulator, pakaliah program JAM_LCD_SERIAL.A51 🙂

  • Terimakasih Bapak atas solusi permasalahan program jam sudah selesai, tapi ada pertanyaan yang lain yang tidak ada hubungannya dengan program, tapi masih seputar jam digital.
    Saya membuat display dari LED yang saya susun menyerupai 7 segmen, saya sudah menggunakan LED super bright, dan power suply saya kira sudah cukup, tetapi nyala LED tidak begitu terang. Apakah ada cara atau trik agar LED yang saya fungsikan sebagai display bisa terang.

    Terima kasih

    1. Mr. Supriyono >>
      Batas max arus untuk P1-P3 adalah 15 mA. Bright LED membutuhkan arus yang relatif besar sehingga harus ditambah penguat dengan transistor.

    2. Terimakasih Pak Sulhan atas jawabannya, tapi saya masih bingung untuk membuat penguat untuk menyalakan & segmen supaya lebih terang. Apakah menggunakan 8 transistor untuk 7 segmen. Berarti, arus yang lewat 7 segmen melewati 2 transistor, yang satu untuk commmon dan dan yang satu penguat yg baru ini.Yang paling baik tipe transistornya apa ?

      Terimakasih

    3. Ya, benar. Jadi baik port yang mengendalikan digit, maupun port yang mengendalikan segment, sama-sama menggunakan transistor. Tentu saja arus yang melalui transistor untuk pengendali digit akan sekitar 8x arus yang melalui transistor pengendali segment.
      Transistor yang paling baik untuk digunakan? ya.. yang penting cukup kuat untuk men-supply arus. Sedangkan masalah kecepatan tidak terlalu penting karena kecepatan switchingnya juga hanya lambat saja. Hitung saja kebutuhan arus kolektornya, trus perhitungkan juga dengan kemampuan port untuk menyupply basisnya. Kalo kebutuhan arus kolektor memang cukup besar, mungkin juga perlu untuk di-darlington.

  • Assalamu ‘alaikum,
    Bagaimana memodifikasi program jam digital agar efek blink, saat setting jam, bisa kembali kekondisi normal apabila tidak ada switch yang ditekan selama waktu tertentu ( misal 30 detik ). Jadi saat akan setting jam/menit, dan tidak jadi maka dibiarkan saja akan kembali normal.
    Terimaksih atas jawabannya.

    Salam hormat,
    Supriyono

    1. Itu gampang sekali. Kita lihat bahwa pengaturan jam atau menit ditentukan oleh kondisi R4. Jika R4=#NoMode, maka keadaan adalah normal. Oleh karena itu, untuk mengembalikan ke keadaan normal, kita cukup mengisi R4 dengan #NoMode.

      Sedangkan untuk membuat mekanisme timeout, kita membutuhkan dua variabel lagi, misalkan KeyTimeOut, yaitu untuk perhitungan timeout, dan Detik0, yaitu untuk mengetahui apakah detik telah berubah atau belum.

      Setiap terjadi perubahan detik, maka jika KeyTimeOut belum nol, maka harus dikurangi dengan 1. Sebaliknya jika sudah nol, maka tidak ada yang perlu dilakukan lagi. Akan tetapi pada saat pertama kali KeyTimeOut berubah menjadi nol, maka kita harus mengisi R4 dengan #NoMode karena perubahan ini menunjukkan bahwa telah terjadi timeout.

      KeyTimeOut sendiri diisi setiap kali ada penekanan tombol, yaitu saat kembali dari AmbilTombol dan hasilnya tidak nol.

      Perubahan listing program (dari Debouncing2rev.A51) adalah sebagai berikut:

                    .....
      KeyTimeOut    data  20
      Detik0        data  21
      TimeOutConst  equ   30 ;30 detik
      
                    mov   R4,#NoMode
      MainLoop0:    acall IsiBuffer
      MainLoop:     mov   a,Detik
                    xrl   a,Detik0
                    jz    CekTombol
                    mov   Detik0,Detik
                    mov   a,KeyTimeOut
                    jz    CekTombol
                    djnz  KeyTimeOut,CekTombol
                    mov   R4,#NoMode
      CekTombol:    acall AmbilTombol
                    jz    MainLoop
                    mov   KeyTimeOut,#TimeOutConst
      
                    cjne  A,#S1S2ditekan,maybeS1
                    .....
      

  • Assalamu alaikum wr. wb.
    Gimana kabarnya, kok jarang online. Aku juga akhir2 ini jarang online, lagi agak repot.

    Iya kayaknya menyerah deh Pak Sulhan tentang PR nya, masih belum ngatasi ilmunya.

    Mohon pencerahan.

    Salam hormat

  • ass wr wb

    Mohon tambahan list program untuk menambah detik satuan dan puluhan, terimah kasih.

  • saya ingin download debouncing2 rev A51 tapi tidak dapat gimana caranya?
    trims

  • Hallo,, mw tanya donk.. bisa buatin program untuk Lan tester menggunakan Mikon ga?? yang terhubung dengan lcd..dan led,,
    jadi klo kabel’ya straight maka akan tampil tulisan di LCD =”STRAIGHT”; klo cross tulisan di LCD = “CROSS”..

    Terimakasih sebelumnya.. ^_^

  • Mas mau tanya,
    Pada skema rangkaian Bab “kendali motor DC”,
    Rangkaian DAC yang digunakan dar bab sebelumnya ya??
    sensor optocoupler gimana maksudnya??

    1. @Fandy: Bener, pake DAC dari percobaan sebelumnya. Optocoupler itu pasangan antara pemancar infrared dan penerima infrared. Kalau tidak ada yg sudah jadi, bisa jg dibuat sendiri menggunakan 2 buah IRLED. yang satu dipake sebagai pemancar, dan yang satunya lagi digunakan sebagai penerima. Akan tetapi bagian penerima harus dikuatkan lg dg sebuah transistor.

    2. Ada 2 mas, yg r-2r atau satu lagi??
      Karena say masih menggunakan Proteus terlebih dahulu,
      Ada Opto yg sudah jadi, tp saya gak tau Opto-npn atau MOCxxx yang kita gunakan mas..
      Tolong pencerahannya 🙂

    3. yang R-2R. klo yg satu lg kan adanya di bab sesudahnya. MOC? itu kan optoisolator? ato ada yg optocoupler ya?

    4. hehe..
      kurang tau jg mas, makanya saya tanya.
      Yg DAC akan saya coba, makasih ya mas..
      Oia, DAC tu ada IC nya g mas??

    5. optoisolator ya optocoupler jg sih, tp maksudku optocoupler seperti yg ada di mouse yg masih pake bola. DAC bentuk IC ya ada lah, itu di bab 18 kan ada DAC0808.

    1. @Aris: maksudnya rangkaian seven segmen 2 tu yg di bab 8? ya kurang lebih begitu lah, tp klo untuk bab-bab selanjutnya, listing programnya dirancang untuk jalan di kristal 11,0592MHz

    2. Ooo.. begitu ya mas…

      jadi, untuk seven segmen 2 yg di bab 8, cuma mnghidupkan 4 led seven segmen ya mas?

      pas saya running simulasi dgn mmakai listing program seven segmen keseluruhan pada bab 8, yang tampil pada simulasi hanya menghidupkan keempat seven segmen dengan angka 0.

    3. @ Aris
      Sebenarnya itu menyalakan 7-segment untuk menampilkan menit dan jam. Akan tetapi perubahan menitnya bukan berdasarkan menit yg sesungguhnya, tetapi berdasarkan delay. Jika ingin melihat perubahannya secara lebih cepat, ganti
      mov R7,#255
      pada baris 121 menjadi
      mov R7,#2

    1. @ Fandy
      Memang LED infra merah tidak kelihatan menyala. Penempatan optocoupler lihat buku hal. 146 – 147.

  • Salam Kenal Pak Sulhan Setiawan,

    saya ifhan dri Papua,

    saya sudah membeli buku anda “MUDAH DAN MENYENANGKAN BELAJAR MIKROKONTROLER” yang ingin saya tanyakan

    1. untuk rangkaian LED pd halaman 8 (LED1)

    sekarang saya mau masukkan/imput Program k dalam Chip 89c501, nah.. saya harus membuat rangkaian baru untuk Rangkaian Serial..??

    2. apakah kita harus membuat 2 rangkaian dimana rangkaian pertama adalah rangkaian Downloader dan rangkaian ke 2 adalah rangkaian LED1.? setelah di downloader programnya ke Chip, kita pindahkan CHIp ke rangkaian LED1, begitu ya..???

    1. salam kenal ifhan. downloader, atau sering jg disebut programmer itu sebuah alat tersendiri. untuk memasukan kode program ke dalam chip, maka chip tersebut langsung dipasang pada downloader dan dilakukan pengisiran sesuai prosedur dari downloader yg digunakan. ada banyak toko online yg menjual perangkat ini. bisa jg browsing2 untuk mencari rangkaian yg mungkin bisa dibuat sendiri. tetapi kalau ifhan menggunakan komputer yg tersedia port paralelnya (bukan laptop), dan chip yg digunakan adalah AT89S51/AT89S52 (bukan AT89Cxx), maka ifhan bisa melakukan download program dg cara yg mudah seperti yg ditunjukkan pada help di Microcontroller Project. Buruan download programnya ❗

  • Buku cuma 50rb,yg didpt banyak sekali.trimakasih

  • rangkaian di buku pak sulhn yg ad ic lm 741 itu rangkaian downloader ya…?

    1. Semua rangkaian yang ada di buku adalah rangkaian untuk percobaan pada bab yang bersangkutan. Tidak ada rangkaian downloader dalam buku.

  • as wr wb,oya pak ,kami mau pesan bukunya gmn ya caranya,soal nya kami tinggal di aceh,

  • pak dalam buku semuanya pake at89c5..
    trus caranya buat donlodernya gimana?
    saya udah browsing2 semuanya harus punya dulu ic master(ic yg sudah terisi program)
    buat donlodernya lewat paralel.
    http://www.sulhansetiawan.com/sdownldr (ini untuk 89s5..)
    hampir seperti itu tapi untuk at89c5..

    1. pak saya udah beli bukunya. isinya mantap.. tapi ic 89c51 ditempat saya susah carinya. mau praktek jadi gak bisa. 🙁
      gimana kalau bikin buku lagi tapi untuk komponen2 nya yg banyak dijual dipasaran ex: 89s51,52 dsb. dan donlodernya mudah dibuat.

    2. Joko, Abe. AT89S51 adalah versi yang lebih baru dari AT89C51 yang kompatibel dengan AT89C51. Jadi, semua percobaan pada buku bisa langsung diterapkan untuk AT89S51 tanpa ada persoalan.