15
9/4/2013 [email protected] | :) EE-346 COURSE PIC16F84A TUTORIAL COMPOSED BY ENG.AHMED AKOUR

PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

  • Upload
    others

  • View
    18

  • Download
    1

Embed Size (px)

Citation preview

Page 1: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

9/4/2013

[email protected] | :)

EE-346

COURSE

PIC16F84A TUTORIAL

COMPOSED BY ENG.AHMED AKOUR

Page 2: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

Microcontroller (µC) Vs microprocessor (µP).

Figure below shows the internal architecture of a typical µC system, and its equivalent

µP system, note that in the µC side, all devices are packaged in the same chip, in contrast of

µP system that each block is considered a stand-alone device.

Page 3: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

PIC16 identification: PIC is generally assumed to mean Peripheral Interface Controller, it comes with a

variety of families; PIC10 and PIC12 (Base-line), PIC16 (Mid-range), PIC17 (High-end),

PIC18 (enhancement), Finally PIC24 and dsPIC. Here we will deeply look at PIC16

family. PIC16F84A is our interest MCU in this family.

PIC16F84A: Is an 18 pin chip, operating at a frequency up to 20MHz, it has two I/O ports

A(5),B(8) mapping to 13 pins, the following points highlight the most important

features:

1- 1Kbytes x 14bit Flash Program memory space.

2- 68 x 8 Bytes of DATA Memory (RAM).

3- 64 x 8 Bytes of EEPROM DATA Memory.

4- Two I/O PORTS.

5- Wide operating frequency DC-20MHz.

6- Wide operating voltage 2.0v – 5.5v.

7- TMR0: 8-bit timer/counter with 8-bit programmable prescaler.

8- 10,000 erase/write cycles Enhanced FLASH Program memory typical.

9- 10,000,000 typical erase/write cycles EEPROM Data memory typical.

10- EEPROM Data Retention > 40 years.

11- current sink/source : - 25 mA sink max. per pin.

- 25 mA source max. per pin.

11-Four interrupt sources:

- External RB0/INT pin

- TMR0 timer overflow

- PORTB<7:4> interrupt-on-change

- Data EEPROM write complete

Page 4: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

MEMORY ORGANIZATION: There are two memory blocks in the PIC16F84A.These are the program memory and the data memory. Each block has its own bus, so that access to each block can occur during the same oscillator cycle.

Program memory:

The PIC16F84A has a 13-bit program counter capable of addressing a 1K x 14

program memory space. The RESET vector is at 0000h and the interrupt vector is at

0004h .

Program Counter (PC): is CPU register where adresses of instructions are

stored.every time that the CPU looks for an instruction in the memory,the PC is

increased,pointing to the following instruction.in an instant of time the PC

contains the adress of the instruction that will be excuted next.

Page 5: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

Data Memory(RAM):

Data Memory is devided into two pages called Banks(Bank0,Bank1).Each Bank

has 128 locations (addresses or registers) .the range of addresses for each

bank is from 00h to 7fh.

Data Memory registers can be classified as special function registers (SFRs) or

general purpose registers(GPRs).

SFRs are used to control PIC and access its peripheral modules.GPRs make up

the data memory available to the user .

Selecting Banks: there is one bit used to select banks ,this bit called RP0

that is bit number 5 in the special function register called STATUS

register . setting this bit to 0 selecting bank0 while setting the bit to 1

selecting bank1.

Note that the GPRs addresses start from address 0Ch.

Page 6: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

Program memory Data memory EEPROM

Data bus 14 bit 8 bit 8 bit

Address bus 13 bit 7 bit 8 bit

STATUS REGISTER: The STATUS register contains the arithmetic status of the ALU, the RESET status And the bank select bit for data memory.

Page 7: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

OPTION REGISTER The OPTION register is a readable and writable register which contains various control bits to configure the TMR0/WDT prescaler, the external INT interrupt, TMR0, and the weak pull-ups on PORTB.

Page 8: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

PINOUT:

Pic16F84A has 18 pins:

13 as i/o pins.

5 pins for biase ,oscillator and reset pin.

MCLR# (reset pin): pin # 4 ,this pin used to

manually reset the program execution when

it goes low (logicaly zero).

Connection:this pin is active low so it is usually connected to 5 Volt

(Vcc/Vdd) through a resistor (typical 10KΩ) to prevent any unwanted reset

during program execution.

Page 9: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

Bias: any IC needs bias to operate. PIC 16F84A has two pins for biasing these

pins are Vdd (Pin #14 connected to 5 Volt) and

Vss (Pin #5 connected to GND).

Connection:

Page 10: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

Oscillator: the µC needs oscillator to generate the signal necessary to

synchronize all internal operation.

PIC16F84A has two pins for oscillator these pins are pin #16 (OSC1/CLKIN)

and pin #15 (OSC2/CLKOUT).

Connection:

The main oscillator in PIC Microcontroller can be internal or External

clock.

Internal Oscillator/Clock: Some of microcontrollers have an internal RC

oscillator.

- frequency up to 4 MHz.

- error 1.5%.

- 12xxx,10xxx,some of 16xxx and 18xxx series.

Page 11: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

External Oscillators/Clock:either RC Oscillator or (crystal/cyramic)

Oscillators.

o RC Oscillator :

- Frequency up to 1MHz.

- Error 30%.

- Timing insensetive.

- Low Cost.

o Ceramic Oscillator:

- Error 0.5%.

- Robust.

- Used in remote control.

- Frequency up to 4Mhz.

o Crystal Oscillator:

- Error (0.02-0.1)%.

- Best used.

Speed Mode in Software: The type of oscillator can be selected by the

configuration bits.these also select specific modes of operation:

- LP mode: selects oscillator frequencies between (32KHz-200KHz).

- XT mode: selects oscillator frequencies between (100KHz-4MHz).

- HS mode: selects oscillator frequencies between (8MHz-20MHz).

- RC mode: when we use an external RC network as an oscillator.

Page 12: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

I/O Pins: Pic 16F84A has 13 pins can be used as input or output. These pins are

distributed on two Ports PortA (5 Pins) and PortB (8 Pins).

Port Name Number Of Pins

PORTA 5

PORTB 8

TOTAL 13

Configure i/o pins as Input or Output:

Each port needs two special function register:

Data direction register called TRIS (8 bit register): this

register responsible for determining which pin in the port either input

or output.

Setting any bit in this register to 1 will make the pin corresponding to

that pin is input ,while setting any bit in the register to 0 will make

the pin corresponding to that pin is output.

Eg. We want to load the following value in TrisB register.

Loading 0x03(hexadecimal) == 0b00000011(binary).

- Loading 0x03 into TRISB make pin RB0 & RB1 as an input pins and the

other pins (RB2,RB3,RB4,RB5,RB6,RB7) as an output.

- Similarly loading 0x01==0b00000001 into TRISA making the bit RA0

(bit #0) as an input pin and other pins (RA1,RA2,RA3,RA4) as an

output.

RA4 RA3 RA2 RA1 RA0

0 0 0 0 1

RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0 0 0 0 0 0 0 1 1

TRISB REGISTER

TRISA REGISTER

Page 13: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

Data Register Called Port (8 bit register): this register responsible for writing to

the output pins or reading from input pins.

Configuration Bits: these bits allow the programmer to specify certain

aspects of microcontroller to better fit the intended application.

- These bits must be selected at the beginning of the program.

- These bits can't be changed during programming.

- These bits like fuses.

- These bits are responsible for specifying :

i. Oscillator mode.

ii. Reset parameter (watchdog timer on or off, brown out).

iii. Program memory protection.

iv. Protection of EEPROM data.

v. Power mode (3v or 5v).

Selecting the configuration bits in software:

The configuration bits must be selected at the beginning of the program by writing

the following statement

__CONFIG _XT_OSC &_LVP_OFF &_WDT_OFF &_PWRTE_ON &_CP_OFF

Page 14: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

References:

1. Microcontroller fundamentals and applications with PIC, Fernando E.Valdes.

2. PIC MICROCONTROLLER,A simple Approach ,Ahmed Ihmeid.

3. PIC 16F84A datasheet ,microchip.

Page 15: PIC16F84A Tutorial Composed By Eng.Ahmed Akourahmedakour.weebly.com/uploads/5/9/5/4/5954364/pic16f84a_tutoria… · PIC16F84A Tutorial Composed By Eng.Ahmed Akour Author:) Subject:

PIC16F84A Tutorial Composed By Eng.Ahmed Akour

GOOD LUCK

ENG AHMED AKOUR