Saturday, November 27, 2010

Pengenalan PIC 16F877A

Mikrokontroler PIC16F877A merupakan salah satu mikrokontroler dari keluarga PICmicro yang popular digunakan sekarang ini, mulai dari pemula hingga para profesional. Hal tersebut karena PIC16F877A sangat praktis dan menggunakan teknologi FLASH memori sehingga dapat di program-hapus hingga seribu kali. Keunggulan mikrokontroler jenis RISC ini dibanding dengan mikrokontroler 8-bit lain dikelasnya terutama terletak pada kecepatan dan kompresi kodenya. Selain itu, PIC116F877A juga tergolong praktis dan ringkas karena memiliki kemasan 40 pin dengan 33 jalur I/O.

Anggota keluarga PICmicro buatan Microchip Inc. cukup banyak. Ada yang menggunakan FLASH memori dan ada pula yang jenis OTP (One Time Programmable). Mikrontroler dari keluarga PICmicro yang popular, antara lain PIC2C08, PIC16C54, PIC16F84. Agar lebih mengenal PIC16F877A, berikut ini diberikan fitur-fitur penting yang terdapat pada PIC16F877A.



Gambar Mikrokontroler PIC16F877A

Fitur-Fitur PIC16F877A

Sebenarnya, PIC16F877A bukanlah mikrokontroler yang istimewa dalam keluarga PICmicro. Namun demikian, PIC16F877A cukup mudah dipelajari dan dapat di bilang memiliki kemampuan yang handal sebagai mikrokontroler yang memiliki 40 pin.

Fitur-fitur pada PIC16F877A antara lain :

1. RISC CPU yang mempunyai performance tinggi
2. Hanya 35 jenis instruksi yang perlu dipelajari
3. Semua instrujsi mempunyai siklus tunggal kecuali untuk instruksi percabangan.
4. Kecepatan Instruksi: DC – 20 MHz clock input DC – 200 ns instruction cycle
5. 8K x 14 words of FLASH Program Memory, 368 x 8 bytes of Data Memory (RAM) , 256 x 8 bytes of EEPROM Data Memory
6. Pinout compatible dengan PIC16C73B/74B/76/77
7. Interrupt (14 sumber interrupt)
8. Delapan level hardware stack
9. Direct, indirect dan relative addressing modes
10. Power-on Reset (POR)
11. Power-up Timer (PWRT) dan Oscillator Start-up Timer (OST)
12. Watchdog Timer (WDT) dengan on-chip RC oscillator
13. Programmable code protection
14. Power saving SLEEP mode
15. Selectable oscillator options
16. Low power, high speed CMOS FLASH/EEPROM technology
17. Fully static design
18. In-Circuit Serial Programming (ICSP) hanya dengan dua pin
19. Single 5V In-Circuit Serial Programming capability
20. Processor read/write access to program memory
21. Wide operating voltage range: 2.0V to 5.5V
22. High Sink/Source Current: 25 mA
23. Commercial, Industrial and Extended temperature ranges

Peripheral Features:

1. Timer0: 8-bit timer/counter dengan 8-bit prescaler
2. Timer1: 16-bit timer/counter dengan prescaler, dapat di-increment selama proses SLEEP dengan external crystal/clock
3. Timer2: 8-bit timer/counter dengan 8-bit period register, prescaler dan postscaler
4. Dua Capture, Compare, PWM modules (Capture is 16-bit, max. resolution is 12.5 ns , Compare is 16-bit, max. resolution is 200 ns , PWM max. resolution is 10-bit)
5. 10-bit multi-channel Analog-to-Digital converter (ADC)
6. Synchronous Serial Port (SSP) dengan SPI (Master mode) dan I2C (Master/Slave)
7. Universal Synchronous Asynchronous Receiver Transmitter (USART/SCI) dengan 9-bit address detection
8. Parallel Slave Port (PSP) 8-bits wide, dengan external RD, WR and CS controls (40/44-pin only)
9. Brown-out detection circuitry untuk Brown-out Reset (BOR)

Deskripsi Pin-Pin

Mikrokontroler PIC16F877A di produksi dalam kemasan 40 pin PDIP (Plastik Dual In Line) maupun 40 pin SO (Small Outline). Namun yang banyak terdapat dipasaran adalah kemasan PDIP. Pin-pin untuk I/O sebanyak 33 pin, yang terdiri atas 6 pada Port A, 8 pada Port B, 8 pada Port C, 8 pada Port D, 3 pada Port E. Ada pula beberapa Pin pada mikrokontroler yang memiliki fungsi ganda.


Gambar Pin Mikrokontroler PIC16F877A

Organisasi Memori

Memori pada PIC16F877A dapat dipisahkan menjadi dua blok memori, satu untuk memori program dan satu untuk memori data. Memori EEPROM dan register GPR didalam RAM merupakan memori data, sedangkan memori FLASH merupakan memori program.

Memori Program

Memori program direalisasikan dalam teknologi FLASH memori yang memungkinkan pem-program melakukan program-hapus hingga seribu kali. Pemrograman PIC16F877A dilakukan sebelum dipasang pada rangkaian aplikasi, atau ketika sistem sudah terpasang namun dikehendaki adanya up-dating pada program didalamnya. Pemrograman berulang biasanya dilakukan pada saat pengembangan dan penyempurnaan sistem. Ukuran memori program untuk PIC16F877A adalah 8K lokasi dengan lebar kata 14 words.


Gambar Alokasi Memori Program Mikrokontroler PIC16F877A

Memori Data

Memori data terbagi di dalam beberapa ruang (semacam halaman/bank) yang memuat register yang mempunyai fungsi-fungsi umum dan khusus yang tersendiri. Bit RP1 (STATUS<6>) dan RP0 (STATUS<5>) adalah bit yang menunjukan letak ruang yang dimaksud.

Setiap ruang mempunyai kapasitas di atas 7Fh (128 bytes). Lokasi paling bawah dari setiap ruang ditujukan untuk register yang mempunyai fungsi spesial.


Gambar Alokasi Memori Data Mikrokontroler PIC16F877A

Mode Pengalamatan

Lokasi memori RAM dapat di akses secara langsung atau tidak langsung :

Pengalamatan langsung

Pengalamatan langsung dilakukan melalui alamat 9 bit. Alamat ini merupakan rangkaian dari 7 bit langsung dari instruksi dan 2 bit dari RP0 dan RP1 pada register STATUS. Contoh pengalamatan langsung adalah pengaksesan register FSR

Pengalamatan tidak langsung

Berbeda dengan pengalamatan langsung, pengalamatan tidak langsung tidak mengambil alamat dari instruksi, tetapi menggunakan bit ke 7 (IRP) dari register status dan semua bit dari register FSR. Lokasi alamat di akses melalui register INDF yang didalamnya berisi alamat yang ditunjuk oleh FSR.

CPU (Central Processing Unit) PIC16F877A

CPU berperan sebagai otak dari mikrokontroler. Bagian ini bertanggung jawab untuk mengambil instruksi, melakukan decode, dan mengeksekusi instruksi. CPU terhubung ke semua bagian pada mikrokontroler. Fungsi terpenting dari CPU adalah melakukan dekode dan mengeksekusi suatu instruksi. Instruksi-instruksi dalam bahasa assembly terdiri atas opcode dan operan. Opcode menyatakan proses yang harus dilakukan mikrokontroler. Sedangkan operan adalah bagian yang dioperasikan pada aritmatika maupun logika. Agar mikrokontroler dapat mengerti perintah opcode, maka instruksi harus diterjemahkan ke dalam urutan biner dengan kode “0” dan “1”. Tugas untuk menterjemahkan instruksi dari bahasa asembly ke bahasa mesin (bahasa yang di mengerti oleh mikrokontroler) dilakukan oleh translator (software assembler atau compiler).



Gambar CPUMikrokontroler PIC16F877A

//=======================================================

Sumber : http://iddhien.com/index.php?option=com_content&task=view&id=35&Itemid=108

Penulisan Program Dalam PIC

1. Arahan Pengendalian

#DEFINE

Sintaks:

#define []

Penjelasan:
Arahan ini menukar suatu bahagian teks dengan yang lain. Setiap kali muncul di dalam program, ia akan di ganti dengan .

Contoh:

#define led_on 1

#define led_off 0

#INCLUDE

Sintaks:

#include

#include “nama_file”

Penjelasan:

Arahan ini digunakan untuk memasukkan file program lain ke dalam program yang bersangkutan. Misalnya, file p16f877A.inc merupakan file header untuk PIC16F877A yag memuat deklarasi register maupun bit-bit di dalam arsitektur PIC16F877A. dengan memasukkan file tersebut ke dalam program, kita dapat langsung menggunakan variabel-variabel(pemboleubah-pembolehubah) yang dinyatakan di dalamnya tanpa harus mendeklarasikan(mengistiharkan) kembali.


Contoh:

#include

#include “subprog.asm”

#CONSTAN

Sintaks:

Constant =

Penjelasan:

Apabila nama konstanta(pemalar) muncul dalam program, akan diganti dengan nilainya.

Contoh:

constant Max=100

constant panjang=45

#VARIABEL

Sintaks:

variable =

Penjelasan:

Apabila nama variabel(pembolehubah) muncul dalam program, akan digantikan dengan nilai sementara dari variabel(pembolehubah) tersebut. Hal ini tentu saja berbeza dengan CONSTAN. Nilai konstanta selalu tetap, namun nilai variabel dapat berubah oleh adanya proses yang terjadi.

Contoh:

variable waktu = 10

variable level = 6

#SET

Sintaks:

set

Penjelasan:

Arahan ini digunakan untuk mendefinisikan variabel(pemboleubah) assembler. SET mirip dengan EQU, tetapi dengan pengarah SET, nama variabel(pemboleubah) didefinisikan semula pada definisi berikutnya.

Contoh:

waktu set 10

level set 6

#EQU

Sintaks:

equ

Penjelasan:

Arahan EQU digunakan untuk mendefinisikan konstanta, variabel assembler.

Contoh:

lima equ 5

akhir equ 8

#ORG

Sintaks:

Sunday, April 5, 2009

Core architecture

The PIC architecture is distinctively minimalist. It is characterized by the following features:

* Separate code and data spaces (Harvard architecture)
* A small number of fixed length instructions
* Most instructions are single cycle execution (4 clock cycles), with single delay cycles upon branches and skips
* A single accumulator (W), the use of which (as source operand) is implied (i.e. is not encoded in the opcode)
* All RAM locations function as registers as both source and/or destination of math and other functions.[1]
* A hardware stack for storing return addresses
* A fairly small amount of addressable data space (typically 256 bytes), extended through banking
* Data space mapped CPU, port, and peripheral registers
* The program counter is also mapped into the data space and writable (this is used to implement indirect jumps).

Unlike most other CPUs, there is no distinction between memory space and register space because the RAM serves the job of both memory and registers, and the RAM is usually just referred to as the register file or simply as the registers.

Data space (RAM)

PICs have a set of registers that function as general purpose RAM. Special purpose control registers for on-chip hardware resources are also mapped into the data space. The addressability of memory varies depending on device series, and all PIC devices have some banking mechanism to extend the addressing to additional memory. Later series of devices feature move instructions which can cover the whole addressable space, independent of the selected bank. In earlier devices (i.e., the baseline and mid-range cores), any register move had to be achieved via the accumulator.

To implement indirect addressing, a "file select register" (FSR) and "indirect register" (INDF) are used: A register number is written to the FSR, after which reads from or writes to INDF will actually be to or from the register pointed to by FSR. Later devices extended this concept with post- and pre- increment/decrement for greater efficiency in accessing sequentially stored data. This also allows FSR to be treated almost like a stack pointer.

External data memory is not directly addressable except in some high pin count PIC18 devices.

Code space

All PICs feature Harvard architecture, so the code space and the data space are separate. PIC code space is generally implemented as EPROM, ROM, or flash ROM.

In general, external code memory is not directly addressable due to the lack of an external memory interface. The exceptions are PIC17 and select high pin count PIC18 devices.

Word size

The word size of PICs can be a source of confusion. All PICs handle (and address) data in 8-bit chunks, so they should be called 8-bit microcontrollers. However, the unit of addressability of the code space is not generally the same as the data space. For example, PICs in the baseline and mid-range families have program memory addressable in the same wordsize as the instruction width, ie. 12 or 14 bits respectively. In contrast, in the PIC18 series, the program memory is addressed in 8-bit increments (bytes), which differs from the instruction width of 16 bits.


In order to be clear, the program memory capacity is usually stated in number of (single word) instructions, rather than in bytes.

Stacks

PICs have a hardware call stack, which is used to save return addresses. The hardware stack is not software accessible on earlier devices, but this changed with the 18 series devices.

Hardware support for a general purpose parameter stack was lacking in early series, but this greatly improved in the 18 series, making the 18 series architecture more friendly to high level language compilers.

Instruction set

A PIC's instructions vary from about 35 instructions for the low-end PICs to over 80 instructions for the high-end PICs. The instruction set includes instructions to perform a variety of operations on registers directly, the accumulator and a literal constant or the accumulator and a register, as well as for conditional execution, and program branching.

Some operations, such as bit setting and testing, can be performed on any numbered register, but bi-operand arithmetic operations always involve W; writing the result back to either W or the other operand register. To load a constant, it is necessary to load it into W before it can be moved into another register. On the older cores, all register moves needed to pass through W, but this changed on the "high end" cores.

PIC cores have skip instructions which are used for conditional execution and branching. The skip instructions are: 'skip if bit set', and, 'skip if bit not set'. Because cores before PIC18 had only unconditional branch instructions, conditional jumps are implemented by a conditional skip (with the opposite condition) followed by an unconditional branch. Skips are also of utility for conditional execution of any immediate single following instruction.

The PIC architecture has no (or very meager) hardware support for automatically saving processor state when servicing interrupts. The 18 series improved this situation by implementing shadow registers which save several important registers during an interrupt.

In general, PIC instructions fall into 5 classes:

1. Operation on W with 8-bit immediate ("literal") operand. E.g. movlw (move literal to W), andlw (AND literal with W). One instruction peculiar to the PIC is retlw, load immediate into W and return, which is used with computed branches to produce lookup tables.
2. Operation with W and indexed register. The result can be written to either the W register (e.g. addwf reg,w). or the selected register (e.g. addwf reg,f).
3. Bit operations. These take a register number and a bit number, and perform one of 4 actions: set or clear a bit, and test and skip on set/clear. The latter are used to perform conditional branches. The usual ALU status flags are available in a numbered register so operations such as "branch on carry clear" are possible.
4. Control transfers. Other than the skip instructions previously mentioned, there are only two: goto and call.
5. A few miscellaneous zero-operand instructions, such as return from subroutine, and sleep to enter low-power mode.

Interrupt Latency

A very useful and unique property of PICs is that their interrupt latency is constant (it's also low: 3 instruction cycles). The delay is constant even though instructions can take one or two instruction cycles: a dead cycle is optionally inserted into the interrupt response sequence to make this true. External interrupts have to be synchronized with the four clock instruction cycle, otherwise there can be a one instruction cycle jitter. Internal interrupts are already synchronized.

The constant interrupt latency allows PICs to achieve interrupt driven low jitter timing sequences. An example of this is a video sync pulse generator. Other microcontrollers can do this in some cases, but it's awkward. The non-interrupt code has to anticipate the interrupt and enter into a sleep state before it arrives. On PICs, there is no need for this.

The three-cycle latency is increased in practice because the PIC does not store its registers when entering the interrupt routine. Typically, 4 instructions are needed to store the W-register, the status register and switch to a specific bank before starting the actual interrupt processing.

Limitations

The PIC architectures have several limitations:

* Only a single accumulator
* A small instruction set
* Operations and registers are not orthogonal; some instructions can address RAM and/or immediate constants, while others can only use the accumulator
* Memory must be directly referenced in arithmetic and logic operations, although indirect addressing is available via 2 additional registers
* Register-bank switching is required to access the entire RAM of many devices, making position-independent code complex and inefficient
* Conditional skip instructions are used instead of conditional branch instructions used by most other architectures

The following limitations have been addressed in the PIC18, but still apply to earlier cores:

* Indexed addressing mode is very rudimentary
* Stack:
o The hardware call stack is so small that program structure must often be flattened
o The hardware call stack is not addressable, so pre-emptive task switching cannot be implemented
o Software-implemented stacks are not efficient, so it is difficult to generate reentrant code and support local variables
* Program memory is not directly addressable, and thus space-inefficient and/or time-consuming to access. (This is true of most Harvard architecture microcontrollers.)

With paged program memory, there are two page sizes to worry about: one for CALL and GOTO and another for computed GOTO (typically used for table lookups). For example, on PIC16, CALL and GOTO have 11 bits of addressing, so the page size is 2KB. For computed GOTOs, where you add to PCL, the page size is 256 bytes. In both cases, the upper address bits are provided by the PCLATH register. This register must be changed every time control transfers between pages. PCLATH must also be preserved by any interrupt handler.[5]

Compiler development

These properties have made it difficult to develop compilers that target PIC microcontrollers. While several commercial compilers are available, in 2008, Microchip finally released their C compilers, C18, and C30 for their line of 18f 24f and 30/33f processors. By contrast, Atmel's AVR microcontrollers—which are competitive with PIC in terms of hardware capabilities and price, but feature a RISC instruction set—have long been supported by the GNU C Compiler.

Also, because of these properties, PIC assembly language code can be difficult to comprehend. Judicious use of simple macros can make PIC assembly language much more palatable, but at the cost of a reduction in performance. For example, the original Parallax PIC assembler "pasm" has macros which hide W and make the PIC look like a two-address machine. It has macro instructions like "mov b,a" (move the data from address a to address b) and "add b,a" (add data from address a to data in address b). It also hides the skip instructions by providing three operand branch macro instructions such as "cjne a,b,dest" (compare a with b and jump to dest if they are not equal).

PIC

PIC is a family of Harvard architecture microcontrollers made by Microchip Technology, derived from the PIC1640[1] originally developed by General Instrument's Microelectronics Division. The name PIC initially referred to "Peripheral Interface Controller".[2][3]

PICs are popular with developers and hobbyists alike due to their low cost, wide availability, large user base, extensive collection of application notes, availability of low cost or free development tools, and serial programming (and re-programming with flash memory) capability.

Microchip announced on February 2008 the shipment of its six billionth PIC processor.[4]