The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by angelinavivianaharry, 2023-01-17 21:28:08

NOTA KULIAH 01-C1

NOTA KULIAH 01-C1

NOTA KULIAH 1 SEMESTER SEMESTER 2 DVM SESI 2/2022 JABATAN JABATAN TEKNOLOGI ELEKTRIK & ELEKTRONIK PROGRAM DIPLOMA TEKNOLOGI ELEKTRONIK KOD / KURSUS DEB2323 BASIC MICROCONTROLLER KOMPETENSI 1.0 CLASSIFY BASIC MICROCONTROLLERS KOMPETENSI UNIT 1.1 Discuss basic architectures of microcontrollers 1.2 Perform processor memory structures and types 1.3 Organize data memory HASIL PEMBELAJARAN Upon completion of the course, students will be able to: 1. Interpret the concept and main features of microcontroller according to internal architecture. (C3, PLO1) 2. Construct analogue and digital interfacing methods based on basic concepts and terminology. (P3, PLO2). 3. Implement the application of microcontroller using digital devices. (A2,PLO10) NAMA PELAJAR MUKA 01 DARIPADA 33 NO KOD DEB2323/NK01/NK01-05 KOLEJ VOKASIONAL MALAYSIA BAHAGIAN PENDIDIKAN DAN LATIHAN TEKNIKAL VOKASIONAL KEMENTERIAN PENDIDIKAN MALAYSIA ARAS 5 & 6, BLOK E14, KOMPLEKS E, PUSAT PENTADBIRAN KERAJAAN PERSEKUTUAN 62604 PUTRAJAYA


1.0 ELEMEN ASAS SISTEM MIKROKOMPUTER Satu sistem mikrokomputer mempunyai komponen-komponen berikut: i. Unit Pemprosesan Pusat (CPU – Central Processing Unit) ii. Masukan (Input) iii. Keluaran (Output) iv. Peranti simpanan (Unit Ungatan) i. UNIT PEMPROSESAN PUSAT (CPU) Ia merupakan unit yang utama dalam sistem mikrokomputer. Ia merupakan unit yang menerima, memproses dan memaparkan data-data. Ia boleh melakukan kerja-kerja seperti aritmetik, logik dan membuat keputusan. ii. INPUT Ia merupakan peranti yang digunakan untuk menyediakan data-data atau maklumat untuk diproses oleh pusat pemprosesan. Contoh: Joystick, Keyboard, Sensor, Scanner iii. OUTPUT Ia merupakan peranti yang digunakan untuk memaparkan data-data atau maklumat yang telah diproses oleh pusat pemprosesan. Contoh: Monitor, Pencetak, Speaker. iv. PERANTI SIMPANAN Ia merupakan peranti yang digunakan untuk menyimpan data-data atau maklumat sebelum/selepas diproses oleh pusat pemprosesan. Contoh: Hardisk, CD-ROM, RAM, ROM, Pen-drive 1.1 SAMBUNGAN ASAS SISTEM MIKROKOMPUTER Address Bus (Bas Alamat): Talian wayar yang digunakan untuk menyalurkan data-data yang mengandungi maklumat alamat (lokasi) bagi data yang hendak diproses atau data yang hendak disimpan. Data Bus (Bas Data): Talian wayar yang digunakan untuk menyalurkan data-data yang akan diproses (dari peranti ingatan ke CPU) atau data yang sudah diproses (dari CPU ke ingatan/output). Control Bus (Bas Kawalan): Talian wayar yang digunakan untuk menghantar isyarat kawalan (seperti MEMW, MEMR, IOR, IOW, interrupt and DMA) antara CPU dengan peranti ingatan dan peranti Input/Output.


2.0 PENGENALAN KEPADA MIKRO PENGAWAL (MICROCONTROLLER - MC) Ia merupakan sebuah komputer yang kecil. Di dalam MC terdapat komponen-komponen yang diperlukan untuk sebuah system mikrokomputer. Ini bererti di dalamnya terdapat komponen-komponen seperti CPU, RAM, ROM, peranti input/output (I/O). Mikro pengawal dianggap sebagai "komputer dalam cip". Satu pakej tunggal mikro pengawal mengandungi: i. satu unit pemprosesan pusat (CPU), ii. Ingatan semikonduktor, RAM ingatan untuk data ROM untuk ingatan program1 iii. penjana jam (pemasa / pembilang) iv. input / output ports. Ingatan baca sahaja (read-only memory) digunakan untuk menyimpan aturcara dalam mikro pengawal yang boleh dalam bentuk ROM, EPROM, EEPROM atau flash EEPROM. Kebaikan mikro pengawal adalah kos yang rendah, diprogramkan, peranti elektronik yang boleh dibenamkan dalam peralatan yang murah. Istilah "mikropengawal" sesuai dengan tugasnya yang melaksanakan fungsi kawalan. Mikro Pengawal tidak digunakan sebagai komputer serba guna. Mikro pengawal banyak digunakan dalam produk sistem terbenam (embedded system) iaitu bermaksud pemproses dan aplikasi digabungkan di dalam satu sistem. Mikro pengawal terbenam (embedded ) banyak dalam produk pengguna sehari-hari seperti kereta, mainan, TV, VCR, ketuhar gelombang mikro dan papan kekunci PC. Sebagai contoh, sebuah kereta moden mungkin mengandungi 10 hingga 30 mikro pengawal. MIKROPENGAWAL BERBANDING MIKROPEMPROSES ~ Mikropengawal tidak sama dengan mikropemproses dalam pelbagai cara. Perbezaan pertama dan yang paling penting ialah fungsinya. Supaya mikropemproses boleh digunakan, komponen lain seperti ingatan mesti ditambah kepadanya. Walaupun mikro pemproses dianggap sebagai mesin pengkomputeran berkuasa, tetapi kelemahannya ialah ia tidak diselaraskan untuk berkomunikasi dengan peranti persisian. ~ Hanya, dalam usaha untuk berkomunikasi dengan persekitaran persisian, mikropemproses mesti menggunakan litar khusus yang ditambah sebagai cip luaran. Mikropemproses adalah hati atau jantung bagi komputer. Ini adalah sepertimana pada permulaan dan masih yang sama pada masa kini. ~ Sebaliknya, mikro pengawal direka untuk menjadi semua itu dalam satu. Tiada komponen luar yang lain yang khusus diperlukan kerana semua litar lain yang perlu dalam peralatan telah dibina ke dalamnya. Ia menjimatkan masa dan ruang yang diperlukan untuk mereka bentuk peranti.


4 PERBEZAAN DI ANTARA MIKROPEMPROSES DENGAN MIKRO PENGAWAL Mikropemproses Mikro Pengawal (MC) 1. CPU , Peranti ingatan (ROM dan RAM), timer dan peranti I/O adalah dalam unit yang berasingan. 1. CPU, RAM, ROM, timer dan peranti I/O berada dalam satu chip tunggal (Embedded). 2.Pereka sistem boleh menentukan bilangan 2. Kapasiti RAM,ROM dan Port untuk I/O kapasiti untuk RAM, ROM dan I/O. telah ditetapkan semasa difabrikasikan. 3. Kapasiti ingatan dan I/O boleh 3. Kapasiti ingatan dan port I/O tidak boleh ditambahkan (expansive) pada bila-bila masa. ditambah pada bila-bila masa. 4. Untuk penggunaan am (General-purpose) 4. Untuk penggunaan yang spesifik sahaja. 5. Sesuai untuk projek yang mempunyai 5. Sesuai untuk projek yang mempunyai ruang ruang yang besar dan boleh berkembang. yang terhad. PEMILIHAN MIKROPENGAWAL Terdapat beberapa pengeluar utama mikropengawal iaitu Motorola, Intel, ATMEL, Zilog dan Microchip Technology. Setiap mikropengawal di atas mempunyai set arahan dan set daftar yang unik. Mereka tidak serasi di antara satu sama lain. Ini bermaksud aturcara yang ditulis untuk satu mikropengawal tidak boleh dilaksanakan pada mikropengawal yang lain. Mikropengawal dicipta dalam bentuk 8 bit, 16 bit dan 32 bit. KRITERIA PEMILIHAN MIKROPENGAWAL i. Memenuhi kehendak tugas yang diperlukan dengan cekap dan berkesan iaitu : - Kepantasan (speed). Apakah kelajuan terpantas yang boleh dilaksanakan. - Pembungkusan (packaging). Adakah dalam bentuk 40-pin DIP (dual inline package) atau QFP (quad flat package). - Penggunaan kuasa. Ini amat kritikal bagi sistem yang menggunakan bateri sebagai sumber bekalan kuasa. - Jumlah ingatan RAM dan ROM di dalam cip. - Bilangan pin I/O dan pemasa (timer) di dalam cip. - Keupayaan untuk di naiktaraf untuk prestasi yang lebih tinggi dan penggunaan kuasa yang rendah. - Kos setiap unit terutama untuk pembelian dalam kuantiti yang besar. ii. Mudah diperolehi pembangunan perisiannya seperti compiler (penyusun), assemblers (penghimpun) dan debugger (penyahpepijat). iii. Kebolehan dan kebolehpercayaan contohnya seperti mudah diperolehi sebanyak mana kuantiti yang diperlukan.


5 Microchip PIC Resol u Clock tion of Family ROM RAM A/D [Kbytes] [bytes] Pins Freq. Inputs A/D [MHz] Conve rter Base-Line 8 - bit architecture, 12-bit Instruction Word Length PIC10FXXX 0.375 - 16 -24 6 -8 4 -8 0 -2 8 0.75 PIC12FXXX 0.75 - 1.5 25 - 38 8 4 - 8 0 - 3 8 14 - PIC16FXXX 0.75 - 3 25 - 44 20 0 - 3 8 134 PIC16HVXXX 1.5 25 18 - 20 20 - - Mid-Range 8 - bit architecture, 14-bit Instruction World Length PIC12FXXX 1.75 - 64 - 3.5 128 8 20 0 - 4 10 PIC12HVXXX 1.75 64 8 20 0 - 4 10 14 - PIC16FXXX 1.75 - 14 64 - 64 20 0 - 13 8 or 10368 14 - 1.75 - 64 - 20 20 0 - 12 10 3.5 128 High-End 8 - bit architecture, 16-bit Instruction Word Length PIC18FXXX 4 - 128 256 - 18 - 48 4 - 16 10 or 32 - 12 396 80 40 - PIC18FXXJXX 8 - 128 1024 - 28 - 48 10 - 16 10 396 10 28 - PIC18FXXKXX 8 - 64 768 - 44 64 10 - 13 10 3936 1 x 1 6 P I C 1 6 H V X X X Com parator s 0 - 1 0 - 1 0 - 2 - 1 1 0 - 2 2 0 - 3 8/1 6 – bit Timers 1 x 8 1 x 8 1 x 8 1 x 8 1 - 2 x 8 1 x 16 1 - 2 x 8 1 x 16 1 - 2 x 8 2 x 8 1 x 16 0 - 2 x 8 2 - 3 x 16 0 - 2 x 8 Serial Comm. - - - - - - USA RT I2C SPI USA RT I2C SPI USB 2.0 CAN 2.0 USA RT I2C PW M Out p uts - - - - 0 - 1 0 - 1 0 - 3 - 0 - 5 2 - 5 Oth er s - EEPR OM EEPR OM Vdd = 15V EEPR OM - - - - - - PIC adalah keluarga mikropengawal dari seni bina Harvard dibuat oleh Microchip Technology. Nama PIC merujuk kepada " Peripheral Interface Controller ". Mikro pengawal PIC adalah mikro pengawal yang pertama RISC (reduced instruction set computing - dikurangkan set arahan pengkomputeran). Mikropengawal 8-bit: PIC10 PIC12 PIC14 PIC16 PIC17 PIC18 Mikropengawal 16-bit: PIC24F PIC24H Mikropengawal 32-bit: PIC32 Pengawal isyarat berdigit 16-bit: dsPIC30 dsPIC33F Keluarga Mikropengawal PIC


F dalam nama umumnya menunjukkan mikropengawal PIC menggunakan ingatan flash (flash memory) dan boleh dipadamkan secara elektronik. C umumnya bermakna ia hanya boleh dipadamkan dengan mendedahkan ‘die’ kepada cahaya ultraungu (yang hanya boleh dilakukan jika jenis pakej windowed digunakan). PIC16F887 adalah salah satu produk terbaru dari Microchip. Ia mempunyai semua komponen yang biasanya dimiliki oleh mikro pengawal moden. Harganya yang rendah, pelbagai aplikasi, berkualiti tinggi dan ketersediaan mudah, ia adalah penyelesaian yang ideal dalam aplikasi seperti: kawalan proses yang berbeza dalam industri, alat-alat kawalan mesin, pengukuran nilai-nilai yang berbeza dan lain-lain. PIC 16F887 .


7 CIRI-CIRI UTAMA MIKROPENGAWAL PIC 16F887 RISC architecture (reduced instruction set computing) o Mempunyai hanya 35 arahan. o All single-cycle instructions except branches. Operating frequency 0-20 MHz Precision internal oscillator o Factory calibrated o Software selectable frequency range of 8MHz to 31KHz Power supply voltage 2.0-5.5V o Consumption: 220uA (2.0V, 4MHz), 11uA (2.0 V, 32 KHz) 50nA (stand-by mode) Power-Saving Sleep Mode Brown-out Reset (BOR) with software control option 35 input/output pins o High current source/sink for direct LED drive o software and individually programmable pull-up resistor o Interrupt-on-Change pin 8K ROM memory in FLASH technology o Chip can be reprogrammed up to 100,000 times In-Circuit Serial Programming Option o Chip can be programmed even embedded in the target device 256 bytes EEPROM memory o Data can be written more than 1,000,000 times 368 bytes RAM memory A/D converter: o 14-channels o 10-bit resolution 3 independent timers/counters Watch-dog timer (WDT) Analogue comparator module with o Two analogue comparators o Fixed voltage reference (0.6V) o Programmable on-chip voltage reference PWM output steering control Enhanced USART module o Supports RS-485, RS-232 and LIN2.0 o Auto-Baud Detect Master Synchronous Serial Port (MSSP) o supports SPI and I2C mode


Fig. 1-1 PIC16F887 PDIP 40 Microcontroller Fig. 1-2 PIC16F887 QFN 44 Microcontroller


Penerangan pin Rajah. 1-1 di atas menunjukkan kebanyakan pin adalah pelbagai fungsi. Sebagai contoh, designator RA3 / AN3 / Vref + / + C1IN untuk pin kelima menyatakan fungsi-fungsi berikut: RA3 Port A input / output digital yang ketiga. AN3 input analog ketiga rujukan voltan Vref + Positif C1IN + input comparator C1positive Ini sering digunakan kerana ia membuatkan pakej mikropengawal lebih padat tanpa menjejaskan fungsinya. Pin pelbagai fungsi tidak boleh digunakan pada masa yang sama, tetapi boleh diubah pada bila-bila masa semasa beroperasi. Jadual berikut, merujuk kepada PDIP 40 mikro pengawal. Fig. 1-3 PIC16F887 Block Diagram Table 1-1 Pin Assignment


10


11 Unit Pemproses Pusat (CPU) CPU yang dihasilkan dengan teknologi RISC yang merupakan faktor penting apabila membuat keputusan mikropemproses untuk digunakan. RISC Set Arahan Komputer dikurangkan, memberikan PIC16F887 dua kelebihan: CPU hanya mengenali 35 arahan mudah (Terdapat beberapa mikro pengawal yang lain perlu untuk tahu lebih daripada 200 arahan). Masa pelaksanaan adalah sama untuk semua arahan kecuali kitar jam kedua dan kitar 4 jam terakhir (frekuensi pengayun stabil oleh kristal kuarza). Arahan Jump dan Branch masa pelaksanaan adalah 2 pusingan jam. Ini bermakna bahawa jika kelajuan operasi mikro pengawal adalah 20MHz, masa pelaksanaan setiap arahan ialah 200ns, iaitu program yang akan dilaksanakan pada kelajuan 5 juta arahan sesaat! CENTRAL PROCESSING UNIT (CPU) As its name suggests, this is a unit which monitors and controls all processes within the microcontroller. It consists of several subunits, of which the most important are: Instruction Decoder is a part of electronics which decodes program instructions and runs other circuits on the basis of that. The ‘instruction set’ which is different for each microcontroller family expresses the abilities of this circuit; Arithmetical Logical Unit (ALU) performs all mathematical and logical operations upon data; and Accumulator is an SFR closely related to the operation of the ALU. It is a kind of working desk used for storing all data upon which some operation should be performed (addition, shift/move etc.). It also stores results ready for use in further processing. One of the SFRs, called a Status Register (PSW), is closely related to the accumulator. It shows at any given time the ‘status’ of a number stored in the accumulator (number is larger or less than zero etc.). Accumulator is also called working register and is marked as W register or just W, therefore. Fig. 1-4 CPU


1 2 1 3 Ingatan Mikro pengawal ini mempunyai tiga jenis ingatan iaitu ROM, RAM dan EEPROM. Ingatan ROM Memori ROM digunakan untuk menyimpan secara kekal program yang sedang dilaksanakan. Inilah sebabnya mengapa ia sering dipanggil "Memori program". PIC16F887 mempunyai 8Kb ROM (dalam sejumlah 8192 lokasi). Sejak ROM ini dibuat dengan teknologi FLASH, kandungannya boleh diubah dengan menyediakan voltan pengaturcaraan khas (13V). Data yang disimpan dalam ROM tidak akan hilang apabila bekalan diputuskan. Digunakan untuk menyimpan aturcara sistem yang ditulis secara kekal dan tidak boleh diubah. Fig. 1-5 ROM Memory Concept EEPROM Memory Sama seperti program memori, kandungan EEPROM disimpan secara kekal, walaupun kuasa terpadam. Walau bagaimanapun, tidak seperti ROM, kandungan EEPROM boleh ditukar semasa operasi mikropengawal. Sebab itulah ingatan ini (256 lokasi) menyimpan keputusan yang dibuat selamalamanya dan digunakan semasa operasi. EEPROM boleh diaturcarakan semula tanpa menanggalkan cip daripada sistem komputer. Ingatan RAM Daftar (register) atau unit ingatan ialah litar elektronik yang boleh mengingat keadaan satu bait (8 bit). Ini adalah bahagian yang paling kompleks bagi ingatan mikropengawal. Dalam kes ini, ia terdiri daripada dua bahagian: i. General-purpose registers ii. Special-function registers (SFR). Digunakan sebagai storan sementara untuk menyimpan data, arahan dan hasil pengaturcaraan.


Merupakan ingatan jenis meruap di mana data yang disimpan pada RAM akan hilang apabila bekalan kuasa diputuskan. Fig. 1-6 SFR and General Purpose Registers General-Purpose Registers (GPR) General-Purpose Registers digunakan untuk menyimpan data sementara dan keputusan yang dibuat semasa operasi. Sebagai contoh, jika program yang melaksanakan pengiraan (contohnya, mengira produk pada barisan pemasangan), ia adalah perlu untuk mempunyai suatu daftar yang bermaksud tambah. Jika mikropengawal tidak kreatif, adalah perlu untuk menyatakan alamat beberapa GPR dan sediakan satu fungsi baru. Satu program yang mudah harus diwujudkan untuk kenaikan nilai daftar ini sebanyak 1, selepas setiap produk melalui sensor. Oleh itu, mikro pengawal boleh melaksanakan program itu kerana ia kini tahu apa dan di mana jumlah yang mesti ditokok. Begitu juga dengan contoh mudah ini, setiap pemboleh ubah program mesti preassigned beberapa general-purpose register. SFR Registers Daftar SFR adalah juga lokasi memori RAM, tetapi tidak seperti daftar GPR, tujuan mereka adalah ditentukan terlebih dahulu dalam proses pembuatan dan tidak boleh diubah. Bila bit mereka disambung kepada litar tertentu pada cip (A / D converter, modul komunikasi siri, dan lain-lain), apa-apa perubahan kandungan mereka secara langsung memberi kesan kepada operasi mikro pengawal atau kepada beberapa litar. Sebagai contoh, dengan menukar daftar TRISA itu, fungsi setiap pin port boleh diubah dengan cara ia bertindak sebagai input atau output. Satu lagi ciri lokasi memori adalah bahawa mereka mempunyai nama-nama mereka (daftar dan bit mereka), yang ketara memudahkan untuk menulis program. Oleh kerana bahasa pengaturcaraan peringkat tinggi boleh menggunakan senarai semua daftar dengan tepat alamat mereka, ia sudah cukup untuk menentukan nama dalam daftar ini untuk membaca atau menukar kandungannya. 1 4


MAKSUD ISTILAH DATA. i. Bit - Unit data terkecil - 1 bit menyimpan satu digit perduaan iaitu ‘1’ atau ‘0’ ii. Nibble iaitu 4 bit sebagai 1 unit. iii. Byte iaitu 8 bit sebagai 1 unit iv. Word iaitu 16 bit sebagai 1 unit v. Long word iaitu 32 bit sebagai 1unit. KAPASITI INGATAN Merupakan keupayaan ingatan yang diukur dengan mengira jumlah bit yang disimpan oleh sesuatau ingatan. Kapasiti ingatan akan menentukan berapa besar program yang boleh dijalankan dan jumlah data yang boleh disimpan di dalam ingatan tersebut. Ingatan ditentukan berdasarkan jumlah bit alamat yang ada pada cip yang digunakan. Unit matrik kapasiti: • 1K = 2 10 = 1024 bit • 1M = 2 20 = 1,048,576 bit • 1G = 2 30 = 1,073,741,824 bit Contoh: Jika diberi satu cip 4K. cip tersebut boleh menyimpan data sebanyak = 4K = 4 x 1024 = 4096 bit


Ingatan data dibahagikan kepada empat bank. Sebelum mengakses beberapa daftar semasa program ditulis (untuk membaca atau menukar kandungannya), adalah perlu untuk memilih bank yang mengandungi daftar itu. Dua bit pada daftar STATUS digunakan untuk memilih bank, yang akan dibincangkan kemudian. Bagi memudahkan operasi, SFRs yang paling biasa digunakan mempunyai alamat yang sama di semua bankbank yang membolehkan mereka untuk diakses dengan mudah. BANK INGATAN RAM


1 6


18 Sebahagian RAM yang digunakan untuk tindanan itu terdiri daripada daftar lapan 13-bit. Sebelum mikropengawal mula melaksanakan satu subrutin (CALLinstruction) atau apabila sampukan berlaku, alamat arahan pertama seterusnya (address of first next instruction) yang sedang dilaksanakan akan ditolak ke penindan (stack), iaitu ke salah satu daftar. Dengan cara itu, apabila subrutin atau pelaksanaan sampukan, mikropengawal tahu dari mana untuk meneruskan pelaksanaan program yang sedang dilaksanakan. Alamat ini dibersihkan (cleared) setelah kembali ke program utama kerana tidak ada keperluan untuk menyimpannya lebih lama, dan salah satu lokasi tindanan adalah secara automatik boleh digunakan lagi. Ia adalah penting untuk memahami bahawa data sentiasa ditolak ke dalam tindanan secara putaran. Ini bermakna bahawa selepas tindanan telah ditolak lapan kali, tolakan kesembilan akan menggantikan nilai yang telah disimpan dengan tolakan pertama. Gesaan kesepuluh akan overwrite tolakan kedua dan sebagainya. Data ditulis ganti dengan cara ini tidak boleh diperolehi semula. Di samping itu, pengaturcara tidak boleh mengakses daftar ini untuk menulis atau membaca dan tidak ada bit Status untuk menunjukkan keadaan tindanan limpahan atau stack underflow conditions. Atas sebab itu, seseorang itu perlu berhati-hati semasa menulis program. Sistem sampukan Perkara pertama yang mikropengawal buat apabila permintaan ada sampukan adalah untuk melaksanakan arahan semasa dan kemudian berhenti pelaksanaan program biasa. Sejurus selepas itu, alamat ingatan program semasa secara automatik ditolak ke dalam tindanann dan default address (dipratakrifkan oleh pengilang) ditulis ke pembilang program ini. Lokasi itu dari mana program itu berterusan pelaksanaan dipanggil vektor sampukan. Untuk mikropengawal PIC16F887, alamat ini adalah 0004h. Seperti yang dapat dilihat dalam Rajah. 1-7 bawah, lokasi mengandungi vektor sampukan melewati semasa pelaksanaan program biasa. Sebahagian daripada program yang diaktifkan apabila permintaan sampukan tiba dipanggil rutin sampukan. Arahan pertama terletak pada vektor sampukan. Berapa lama subrutin ini akan menjadi dan apa yang ia akan menjadi bergantung kepada kemahiran pengaturcara serta sumber sampukan itu sendiri. Terdapat mikro pengawal mempunyai lebih vektor sampukan ( setiap permintaan sampukan akan mempunyai vektor), tetapi dalam kes ini hanya ada satu. Oleh itu, bahagian pertama daripada rutin sampukan terdiri dalam pengiktirafan sumber sampukan. TINDANAN - STACK


Bagaimana untuk menggunakan daftar SFR Terdapat senarai daftar SFR dengan semua bit. Setiap daripada mereka mengawal beberapa proses. Ia kelihatan seperti sebuah meja besar dengan kawalan banyak instrumen dan suis. Terpulang kepada kita untuk memilih daftar SFR yang hendak diguna. Selepas itu, apabila anda benar-benar memahami operasi perkakasan, kaji daftar SFR yang mengawal operasi perkakasan yang kita pilih. Yang penting, semasa menulis program dan sebelum menukar beberapa bit daftar ini, jangan lupa untuk memilih bank yang sesuai. Inilah sebabnya mengapa ia disenaraikan dalam jadual di atas. Semua mikro pengawal yang dinaik taraf menggunakan salah satu daripada dua model reka bentuk asas iaitu: Mereka mewakili dua cara yang berbeza pertukaran data antara CPU dan ingatan. Pengawal mikro menggunakan senibina von Neumann-hanya mempunyai satu blok ingatan dan satu bas data 8-bit. Semua data ditukar melalui 8 baris ini menjadikan bas yang sarat dan komunikasi adalah sangat lambat dan tidak cekap. CPU sama ada boleh membaca arahan atau membaca data / tulis dari / ke memori. Kedua-dua tidak boleh berlaku pada masa yang sama kerana arahan-arahan dan data menggunakan bas yang sama. Bas data yang sama digunakan untuk semua operasi perantaraan i. Reka bentuk Harvard ii. Reka bentuk Von Neumann. VON-NEUMANN ARCHITECTURE Fig.1-7 Interrupt System SENI BINA DALAMAN


HARVARD ARCHITECTURE Mikropengawal yang menggunakan seni bina Harvard mempunyai dua buah bas data yang berbeza. Satu adalah 8 bit dan menghubungkan CPU kepada RAM. Yang lain terdiri daripada 12, 14 atau 16 talian dan menghubungkan CPU ke ROM. Oleh itu, CPU boleh membaca arahan dan capaian ingatan data pada masa yang sama. Oleh kerana daftar memori RAM adalah lebar 8 bit, semua data yang ditukar adalah pada lebar yang sama. Semasa proses menulis program, hanya 8-bit data akan dipertimbangkan. Dalam erti kata lain, semua yang anda boleh mengubah dari dalam program ini dan semua yang anda boleh gunakan adalah lebar data 8 bit. Semua program yang ditulis untuk pengawal ini akan disimpan dalam mikropengawal ROM dalaman selepas disusun kepada kod mesin. Walau bagaimanapun, lokasi memori ROM tidak mempunyai 8, tetapi 12, 14 atau 16 bit. Selebihnya bit 4, 6 atau 8 mewakili arahan yang menyatakan untuk CPU, apa yang perlu dilakukan dengan data 8 bit. Kelebihan reka bentuk Harvard Semua data dalam program ini adalah lebar satu bait (8 bit). Sebagai bas data yang digunakan untuk membaca program mempunyai 12, 14 atau 16 baris, kedua-dua arahan dan data boleh dibaca pada masa yang sama dengan menggunakan bit lebihan. Atas sebab ini, semua arahan adalah arahan kitaran tunggal, kecuali arahan lompatan (jump) memerlukan arahan dua kitaran. Disebabkan oleh program (ROM) dan data sementara (RAM) adalah berasingan, CPU boleh melaksanakan dua arahan pada satu masa. Ringkasnya, semasa RAM sedang membaca atau menulis (akhir satu arahan), arahan program seterusnya dibaca melalui bas lain. Apabila menggunakan pengawal mikro dengan senibina Von-Neumann, kita tidak tahu berapa banyak memori diduduki oleh program ini. Pada asasnya, arahan program yang menduduki dua lokasi ingatan (satu mengandungi maklumat mengenai APA yang perlu dilakukan, manakala yang lain mengandungi maklumat MANA yang perlu dilakukan). Dalam pengawal mikro dengan seni bina Harvard, bas perkataan program adalah lebih luas daripada satu bait, yang membolehkan setiap perkataan program terdiri daripada arahan dan data, iaitu satu lokasi memori – satu arahan program. Pemproses Micro-coded dan Hard-coded Pelaksanaan seni bina komputer boleh dicapai secara meluas dalam dua cara. Komputer adalah sebuah litar digital berurutan kompleks dengan kedua-dua gabungan dan komponen litar berurutan. Dalam pemproses mikro-kod, setiap arahan direalisasikan oleh beberapa langkah-langkah yang dilaksanakan menggunakan subrutin kecil. Subrutin ini dipanggil mikro-kod disimpan dalam unit arahan decode. Oleh itu, pemproses mikro berkod boleh dipanggil pemproses dalam pemproses.


Pemproses Mikro-kod Fig.3.1 Architecture of a Micro-Coded Processor Let us take an example. The instruction "Move Acc, Reg" can be executed in the following steps. 1. Output address to the data memory 2. Configure the internal bus for data memory value to be stored in accumulator. 3. Enable bus read. 4. Store the data into the accumulator. 5. Compare data read with zero or any other important condition and set bits in the STATUS register. 6. Disable data bus. Each step of the instruction is realized by a subroutine (micro-code). A set of bits in the instruction points to the memory where the micro-code for the instruction is located. Advantages: 1. Kemudahan fabrikasi (Easy of fabrication) 2. Mudah untuk nyahpepijat (easy to debug) Disadvantage: - Program execution takes longer time. Pemproses Hard-coded Setiap arahan direalisasikan oleh litar digital gabungan dan / atau berurutan. Reka bentuk yang kompleks, sukar untuk debug. Walau bagaimanapun, pelaksanaan program ini adalah lebih cepat.


Micro-Coded Processor Hard-Coded /Hardware Processor Flexible , easy to revise .Because changing the code in the memory will change the decode. Rigid Easy to debug Hard to debug Generation of control signals is generated from the data stored in the micro-coded memory.So its easy to change the decoding process by changing the data in the memory. Sometimes it reduces the execution time. Since the control signals are generated by the combinational logic circuit, so to change decoding total circuit on chip is to be changed. INPUT/OUTPUT PORTS One of the most important merits of the microcontroller is a number of input/output pins which enable it to be connected to peripheral modules. There are in total 35 general-purpose I/O pins provided on the PIC16F887, which is quite enough for most applications. In order to synchronize the operation of I/O ports with the internal 8-bit organization of the microcontroller, they are, similar to registers, grouped into five ports denoted by letters A, B, C, D and E. All I/O ports have several features in common: To save more space on the board, all I/O pins are multifunctional. However, they can be assigned only one function at the same time. Every port is accompanied by the corresponding TRIS register: TRISA, TRISB, TRISC etc. which determines the performance, but not the contents of the port bits. By clearing any bit of the TRIS register (bit=0), the corresponding port pin is configured as an output. Similarly, by setting any bit the TRIS register (bit=1), the corresponding port pin is configured as an input. This rule is easy to of remember 0 = Output, 1 = Input.


/ The PORTA.2 pin is configured as a digital input. // All other PORTA pins are digital outputs ANSEL = ANSELH = 0; // All I/O pins are configured as digital PORTA = 0; // All PORTA pins are cleared TRISA = 0b00000100; // All PORTA pins except PORTA.2 are configured as outputs Example programming in C PORTA and TRISA register Port PORTA is an 8-bit wide, bidirectional port. Bits of the TRISA register control the PORTA pins, i.e. whether they will act as digital inputs or outputs: PORTB and TRISB registers Port PORTB is an 8-bit wide, bidirectional port. Bits of the TRISB register determine the function of its pins. Similar to the TRISA register bits which determine which of the pins are to be configured as inputs and which ones as outputs, the appropriate bits of the ANSEL register determine whether PORTA pins are to be configured as analog inputs or digital inputs/outputs. (ANSEL = 0 for digital, ANSEL = 1 for analog) RA0 = AN0 (determined by the ANS0 bit of the ANSEL register) RA1 = AN1 (determined by the ANS1 bit of the ANSEL register) RA2 = AN2 (determined by the ANS2 bit of the ANSEL register) RA3 = AN3 (determined by the ANS3 bit of the ANSEL register) RA5 = AN4 (determined by the ANS4 bit of the ANSEL register) Each bit of this port is assigned an additional function related to some of the built-in peripheral modules.


2 4 Similar to port PORTA, a logic one (1) on the TRISB register bit configures the appropriate port PORTB pin as an input and vice versa. Six pins of this port can act as analog inputs (AN). Bits of the ANSELH register determine whether these pins are to be configured as analog inputs (ANSELH = 1) or digital inputs/outputs (ANSELH = 0) RB0 = AN12 (determined by the ANS12 bit of the ANSELH register) RB1 = AN10 (determined by the ANS10 bit of the ANSELH register) RB2 = AN8 (determined by the ANS8 bit of the ANSELH register) RB3 = AN9 (determined by the ANS9 bit of the ANSELH register) RB4 = AN11 (determined by the ANS11 bit of the ANSELH register) RB5 = AN13 (determined by the ANS13 bit of the ANSELH register) The following features makes this port different from other ports, hence its pins are commonly used: All the port PORTB pins have built in pull-up resistors, which makes them ideal for connecting to push buttons (keyboard), switches or optocouplers. In order to connect these resistors to the PORTB pins, the appropriate bit of the WPUB register should be set.* (Adress 95H SFR Bank 1) Having a high resistance (several tens of kiloohms), these ‘virtual’ resistors do not affect pins configured as outputs, but serve as a useful complement when they act as inputs. Otherwise, without these resistors attached the input pins would act as floating due to their high input resistance. Apart from the bits of the WPUB register, there is another bit affecting the installation of all pull-up resistors. It is the RBPU bit of the OPTION_REG register. Port PORTB can be used as an interrupt source. If enabled, each port PORTB bit configured as an input may cause an interrupt by changing its logic state. In order to enable pins to cause an interrupt, the appropriate bit of the IOCB register (Adress 96h SFR Bank 1) should be set.


Owing to these features, the port PORTB pins are commonly used for checking push buttons on the keyboard as they are able to unerringly register any button pressure. There is no need to ‘scan’ these inputs all the time, therefore. When the X, Y and Z pins are configured as outputs set to logic one (1), it is only necessary to wait for an interrupt request to arrive after pressing a button. By combining zeros and ones on these outputs it is checked which push button is pressed. Refer to figure below: Let's do it in mikroC... /* The PORTB.1 pin is configured as a digital input. Any change of its logic state will cause an .i.n.terrupt. It also has a pull-up resistor. All other PORTB pins are digital outputs.*/ ANS EL = ANSELH = 0; / / All I/O pins are configured as digital POR TB = 0; / / All PORTB pins are cleared TRI SB = 0b00000010 ; / / All PORTB pins except PORTB.1 are configured as outputs RBP U = 0; / / Pull-up resistors are enabled WPU B1 = 1; / / Pull-up resistor is connected to the PORTB.1 pin IOC B1 = 1; / / The PORTB.1 pin may cause an interrupt on logic state change RBI E = GIE = 1; / / Interrupt is enabled ... PIN RB0/INT The RB0/INT pin is the only ‘true’ external interrupt source. It can be configured to react to signal raising edge (zero-to-one transition) or signal falling edge (one-to-zero transition). The INTEDG bit of the OPTION_REG register selects the appropriate signal.


2 6 RB6 AND RB7 PINS The PIC16F887 does not have any special pins for programming (the process of writing a program to ROM). Port pins, normally available as general-purpose I/O pins, are used for this purpose. To be more precise, it is about port B pins used for clock (RB6) and data transfer (RB7) during program loading. Besides, it is necessary to apply power supply voltage Vdd (5V) as well as appropriate voltage Vpp (12-14V) for FLASH memory programming. During programming, Vpp voltage is applied to the MCLR pin. You don’t have to think of all details concerning this process, nor which one of these voltages is applied first since the programmer’s electronics is in charge of that. What is very important here is that the program may be loaded to the microcontroller even after soldering it onto the target device. Normally, the loaded program can also be changed in the same way. This function is called ICSP (In-Circuit Serial Programming). In order to use it properly, it is necessary to plan ahead. It is only necessary to install a miniature 5-pin connector onto the target device so as to provide the microcontroller with necessary programming voltages. In order to prevent these voltages from interfering with other device electronics connected to microcontroller pins, all additional peripheral devices should be disconnected during the process of programming using resistors or jumpers. As you can see, voltages applied to programmer's socket pins are the same as those used during ICSP programming. PORTC and TRISC register Port C is an 8-bit wide, bidirectional port. Bits of the TRISC register determine the function of its pins. Similar to other ports, a logic one (1) in the TRISC register configures the appropriate portC pin as an input.


PORTD and TRISD register Port D is an 8-bit wide, bidirectional port. Bits of the TRISD register determine the function of its pins. A logic one (1) in the TRISD register configures the appropriate portD pin as an input. PORTE and TRISE register Port E is a 4-bit wide, bidirectional port. The TRISE register’s bits determine the function of its pins. Similar to other ports, a logic one (1) in the TRISE register configures the appropriate portE pin as an input. The exception is the RE3 pin which is always configured as an input. Similar to ports A and B, three pins can be configured as analog inputs in this case. The ANSELH register bits determine whether a pin will act as an analog input (AN) or digital input/output: RE0 = AN5 (determined by the ANS5 bit of the ANSELregister); RE1 = AN6 (determined by the ANS6 bit of the ANSELregister); and RE2 = AN7 (determined by the ANS7 bit of the ANSELregister).


Let's do it in mikroC... /* The PORTE.0 pin is configured as an analog input while another three pins of the same port are configured as digital. */ The rule is: To configure a pin as an analog input, the appropriate bit of the ANSEL or ANSELH registers must be set (1). To configure a pin as a digital input/output, the appropriate bit must be cleared (0). The state of the ANSEL bits has no influence on digital output functions. The result of any attempt to read a port pin configured as an analog input will be 0. ... ANSEL = 0b00100000; // The PORTE.0 pin is configured as analog ANSELH = 0; // All other I/O pins are configured as digital TRISE = 0b00000001; // All PORTE pins except PORTE.0 are configured as outputs PORTE = 0; // All PORTE pins are cleared ANSEL and ANSELH register The ANSEL and ANSELH registers are used to configure the input mode of an I/O pin to analog or digital. ... DEB3343 (S3) 2019


2 9 In Short You will probably never write a program which doesn't use ports so the effort you make to learn all about them will definately pay off. Anyway, they are probaly the simplest modules within the microcontroller. This is how they are used: When designing a device, select a port through which the microcontroller will communicate to peripheral environment. If you use only digital inputs/outputs, select any port you want. If you intend to use some of the analog inputs, select the appropriate ports supporting such a pin configuration (AN0- AN13). Each port pin may be configured as either input or output. Bits of the TRISA, TRISB, TRISC, TRISD and TRISE registers determine how the appropriate port pins- PORTA, PORTB, PORTC, PORTD and PORTE will act. Simply... If you use some of the analog inputs, it is first necessary to set the appropriate bits of the ANSEL and ANSELH registers at the beginning of the program. If you use switches and push buttons as input signal source, connect them to port B pins because they have pull-up resistors. The use of these resistors is enabled by the RBPU bit of the OPTION_REG register, whereas the installation of individual resistors is enabled by bits of the WPUB register. It is usually necessary to respond as soon as input pins change their logic state. However, it is not necessary to write a program for checking pins’ logic state. It is far simpler to connect such inputs to the PORTB pins and enable an interrupt to occur on every voltage change. Bits of the IOCB and INTCON registers are in charge of that. DEB3343 (S3) 2019


3 0 INSTRUCTION MOVLW k MOVWF f MOVF f,d CLRW CLRF f SWAPF f,d ADDLW k ADDWF f,d SUBLW k SUBWF f,d ANDLW k ANDWF f,d ANDWF f,d IORLW k IORWF f,d XORLW k XORWF f,d INCF f,d DESCRIPTION OPERATION Data Transfer Instructions Move constant to W k -> w Move W to f W -> f Move fto d f -> d Clear W 0 ->W Clear f 0 ->f Swap nibbles in f f(7:4),(3:0) -> f(3:0),(7:4) Arithmetic-logic Instructions Add W and constant W+k -> W Add W and f W+f -> d Subtract W from constant k-W -> W Subtract W from f f-W -> d Logical AND with W with constant W AND k -> W Logical AND with W with f W AND f -> d Logical AND with W with f W AND f -> d Logical OR with W with constant W OR k -> W Logical OR with W with f W OR f -> d Logical exclusive OR with W with W XOR k -> W constant Logical exclusive OR with W with f W XOR f -> d Increment f by 1 f+1 -> f FLAG Z Z Z C, DC, Z C, DC ,Z C, DC, Z C, DC, Z Z Z Z Z Z Z Z Z CL K 1 1 1 1 1 1 1 1 1 1 1 1 * 1, 2 2 1, 2 1, 2 1, 2 1, 2 1, 2 1, 2 1, 2 1, It has been already mentioned that microcontrollers differs from other integrated circuits. Most of them are ready for installation into the target device just as they are, this is not the case with the microcontrollers. In order that the microcontroller may operate, it needs precise instructions on what to do. In other words, a program that the microcontroller should execute must be written and loaded into the microcontroller. This chapter covers the commands which the microcontroller "understands". The instruction set for the 16FXX includes 35 instructions in total. Such a small number of instructions is specific to the RISC microcontroller because they are well-optimized from the aspect of operating speed, simplicity in architecture and code compactness. The only disadvantage of RISC architecture is that the programmer is expected to cope with these instructions. INSTRUCTION SET DEB3343 (S3) 2019


3 1 DECF f,d RLF f,d RRF f,d COMF f,d BCF f,b BSF f,b BTFSC f,b BTFSS f,b DECFSZ f,d INCFSZ f,d GOTO k CALL k RETU RN RETL W k RETFI E NOP CLRWDT Decrement f by 1 f-1 -> f Rotate left f through CARRY bit Rotate right f through CARRY bit Complement f f -> d Bit-oriented Instructions Clear bit b in f 0 -> f(b) Set bit b in f 1 -> f(b) Program Control Instructions Test bit b of f. Skip the following Skip if f(b) = 0 instruction if clear. Test bit b of f. Skip the following Skip if f(b) = 1 instruction if set. Decrement f. Skip the following f-1 -> d skip if Z = 1 instruction if clear. Increment f. Skip the following f+1 -> d skip if Z = 0 instruction if set. Go to address k -> PC Cal subroutine PC -> TOS, k -> PC Return from subroutine TOS -> PC Return with constant in W k -> W, TOS -> PC Return from interrupt TOS -> PC, 1 -> GIE Other instructions No operation TOS -> PC, 1 -> GIE 0 -> WDT, 1 -> TO, 1 -> Clear watchdog timer P D 0 -> WDT, 1 -> TO, 0 -> Go into sleep mode P D Z C C Z TO, PD TO, PD 1 1 1 1 1 1 1 (2) 1 (2) 1 (2) 1 (2) 2 2 2 2 2 1 1 1 1 , 2 1 , 2 1 , 2 1 , 2 1,2 1,2 3 3 1, 2, 3 1, 2, 3 *1 When an I/O register is modified as a function of itself, the value used will be that value present on the pins themselves. *2 If the instruction is executed on the TMR register and if d=1, the prescaler will be cleared. *3 If the PC is modified or test result is logic one (1), the instruction requires two cycles. Table 9-1 16Fxx Instruction Set


Data Transfer Instructions Data Transfer within the microcontroller takes place between working register W (called accumulator) and a register which represents any location of internal RAM regardless of whether it is about special function or general purpose registers. First three instructions move literal to W register (MOVLW stands for move Literal to W), move data from W register to RAM and from RAM to W register (or to the same RAM location with change on flag Z only). Instruction CLRF clears f register, whereas CLRW clears W register. SWAPF instruction swaps nibbles within f register (one nibble contains four bits). Arithmetic-logic Instructions Similar to most microcontrollers, PIC supports only two arithmetic instructions- addition and subtraction. Flags C, DC, Z are automatically set depending on the results of addition or subtraction. The only exception is the flag C. Since subtraction is performed as addition with negative value, the flag C is inverted after subtraction. It means that the flag C is set if it is possible to perform operation and cleared if the larger number is subtracted from smaller one. Logic one (1) of the PIC is able to perform operations AND, OR, EX-OR, inverting (COMF) and rotation (RLF and RRF). Instructions which rotate a register actually rotate its bits through the flag C by one bit left (toward bit 7) or right (toward bit 0). The bit shifted from the register is moved to the flag C which is automatically moved to the bit on the opposite side of the register. Bit-oriented Instructions Instructions BCF and BSF clear or set any bit in memory. Although it seems to be a simple operation, it is not like that. CPU first reads the entire byte, changes one its bit and rewrites the whole byte to the same location. Program Control Instructions The PIC16F887 executes instructions GOTO, CALL, RETURN in the same way as all other microcontrollers do. A difference is that stack is independent from internal RAM and has 8 levels. The ‘RETLW k’ instruction is identical to RETURN instruction, with exception that a constant defined by instruction operand is written to the W register prior to return from subroutine. This instruction enables Lookup tables to be easily created by creating a table as a subroutine consisting of ‘RETLWk‘ instructions, where the literals ‘k’ belong to the table. The next step is to write the position of the literals k (0, 1, 2, 3...n) to W register and call the subroutine (table) using the CALL instruction. Table below consists of the following literals: k0, k1, k2...kn. Main movlw 2 ;write number 2 to accumulator call Lookup ;jump to the lookup table Lookup addwf PCL,f ;add accumulator and program current address (PCL) retlw k0 ;return from subroutine (accumulator contains k0) retlw k1 ;... retlw k2 ;... ... ;... ... ;... retlw kn ;return from subroutine (accumulator contains kn) The first line of the subroutine ( instruction ADDWF PCL,f )simply adds a literal "k" from W register and table start address which is stored in the PCL register. The result is real data address in program memory. Upon return from the subroutine, the W register will contain the addressed literal k. In this case, it is the "k2"


3 3 literal. RETFIE (RETurn From IntErrupt) represents a return from interrupt routine. In contrast to the RETURN instruction, it may automatically set the GIE bit (Global Interrupt Enable). When an interrupt occurs this bit is automatically cleared. Only the program counter is pushed to the stack, which means that there is no auto save of registers’ status and the current status either. The problem is solved by saving status of all important registers at the beginning of interrupt routine. These values are retrieved to these registers immediately before leaving the interrupt routine. Conditional jumps are executed by two instructions: BTFSC and BTFSS. Depending on the state of bit being tested in the ‘f’ register, the following instruction will be skipped or not. Instruction Execution Time All instructions are single-cycle instructions. The only exception may be conditional branch instructions (if condition is met) or instructions being executed upon the program counter. In both cases, two cycles are required for instruction execution where the second cycle is executed as a NOP (No Operation). A single-cycle instruction consists of four clock cycles. If 4MHz oscillator is used, a nominal time for instruction execution is 1µS. In case of jump, the instruction execution time is 2µS. Instructions Legend f - Any memory location (register); W - Working register (accumulator); b - Bit address within an 8-bit register; d - Destination bit; [label] - Set of 8 characters indicating start of particular address in the program; TOS - Top of stack; [] - Option; <> - bit field in register (several bit addresses); C - Carry/Borrow bit of the STATUS register; DC - Digit Carry bit of the STATUS register; and Z - Zero bit of the STATUS register. ADDLW - Add literal and W Syntax: [label] ADDLW k Description: The content of the register W is added to the 8-bit literal k. The result is stored in the W register. Operation: (W) + k -> W Operand: 0 :5 k :5 255 Status affected: C, DC, Z Number of cycles: 1 EXAMPLE: [label] ADDLW 0x15 Before instruction execution: W=0x10 After instruction: W=0x25 C=0 (the result is not greater than 0xFF, which means that Carry has not occurred). ADDWF - Add W and f Syntax: [label] ADDWF f, d Description: Add the contents of the W and f registers. If d = w or d = 0 the result is stored in the W register. If d = f or d = 1 the result is stored in register f.


Click to View FlipBook Version