ee345: introduction to microcontrollers the programmable interface controller (pic): pic 16f84

38
EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84 Prof. Ahmad Abu-El-Haija

Upload: amaris

Post on 19-Mar-2016

36 views

Category:

Documents


1 download

DESCRIPTION

EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84. Prof. Ahmad Abu-El-Haija. Outline. Definition of microcontroller Architecture Registers Instructions Applications Programming Examples. Microcontrollers versus Microprocessors. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

Prof. Ahmad Abu-El-Haija

Page 2: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

2

Outline

Definition of microcontroller Architecture Registers Instructions Applications Programming Examples

Page 3: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

3

Microcontrollers versus Microprocessors

Microcontroller differs from a microprocessor in many ways. First and the most important is its functionality. In order for a microprocessor to be used, other components such as memory, or components for receiving and sending data must be added to it. In short that means that microprocessor is the very heart of the computer.

On the other hand, microcontroller is designed to be all of that in one. No other external components are needed for its application because all necessary peripherals are already built into it. Thus, we save the time and space needed to construct devices

Page 4: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

4

Architecture – Typical PIC

Page 5: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

5

PIC16F84 belongs to a class of 8-bit microcontrollers of RISC architecture. Its general structure is shown here representing basic blocks.

Since PIC16F84 is a RISC microcontroller, that means that it has a reduced set of instructions, more precisely 35 instructions. (ex. Intel's and Motorola's microcontrollers have over hundred instructions). All of these instructions are executed in one cycle except for jump and branch instructions.

Architecture – PIC 16F84

Page 6: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

6

Pins of PIC 16F84

PIC16F84 has a total of 18 pins. It is most frequently found in a DIP18 type of case but can also be found in SMD case which is smaller than a DIP. DIP is an abbreviation for Dual In Package. SMD is an abbreviation for Surface Mount Devices suggesting that holes for pins to go through when mounting, aren't necessary in soldering this type of a component.

Page 7: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

Pins on PIC16F84 microcontroller have the following meanings:Pin no.1 RA2 Second pin on port A. Pin no.2 RA3 Third pin on port A. Pin no.3 RA4 Fourth pin on port A. TOCK1 which functions as a timer is also found on this pin Pin no.4 MCLR Reset input and Vpp programming voltage of a microcontrollerPin no.5 Vss Ground of power supply.Pin no.6 RB0 Zero pin on port B. Interrupt input is an additional function.Pin no.7 RB1 First pin on port B. Pin no.8 RB2 Second pin on port B. Pin no.9 RB3 Third pin on port B. Pin no.10 RB4 Fourth pin on port B. Pin no.11 RB5 Fifth pin on port B. Pin no.12 RB6 Sixth pin on port B. 'Clock' line in program mode.Pin no.13 RB7 Seventh pin on port B. 'Data' line in program mode.Pin no.14 Vdd Positive power supply pole.Pin no.15 OSC2 Pin assigned for connecting with an oscillatorPin no.16 OSC1 Pin assigned for connecting with an oscillatorPin no.17 RA2 Second pin on port A. Pin no.18 RA1 First pin on port A.

April 24, 2023EE345 - Introduction to Microcontrollers 7

Pins of PIC 16F84

Page 8: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

8

Crystal (XT) oscillator

Crystal oscillator is kept in metal housing with two pins where you have written down the frequency at which crystal oscillates. One ceramic capacitor of 30pF whose other end is connected to the ground needs to be connected with each pin. Oscillator and capacitors can be packed in joint case with three pins. Such element is called ceramic resonator and is represented in charts like the one shown. Center pins of the element is the ground, while end pins are connected with OSC1 and OSC2 pins on the microcontroller. When designing a device, the rule is to place an oscillator nearer a microcontroller, so as to avoid any interference on lines on which microcontroller is receiving a clock.

Page 9: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

9

Reset (Master Clear) Reset is used for putting the microcontroller into a 'known' condition. That

practically means that microcontroller can behave rather inaccurately under certain undesirable conditions. In order to continue its proper functioning it has to be reset, meaning all registers would be placed in a starting position. Reset is not only used when microcontroller doesn't behave the way we want it to, but can also be used when trying out a device as an interrupt in program execution, or to get a microcontroller ready when loading a program.

In order to prevent from bringing a logical zero to MCLR pin accidentally (line above it means that reset is activated by a logical zero), MCLR has to be connected via resistor to the positive supply pole. Resistor should be between 5 and 10K. This kind of resistor whose function is to keep a certain line on a logical one as a preventive, is called a pull up.

Page 10: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

10

CPU

Central processing unit (CPU) is the brain of a microcontroller. That part is responsible for finding and fetching the right instruction which needs to be executed, for decoding that instruction, and finally for its execution.

Arithmetic logic unit is responsible for performing operations of adding, subtracting, moving (left or right within a register) and logic operations. Moving data inside a register is also known as 'shifting'. PIC16F84 contains an 8-bit arithmetic logic unit and 8-bit work registers.

Depending on which instruction is being executed, ALU can affect values of Carry (C), Digit Carry (DC), and Zero (Z) bits in STATUS register.

Page 11: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

11

Page 12: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

12

PORTB and TRISB

PORTB has adjoined 8 pins. The appropriate register for data direction is TRISB. Setting a bit in TRISB register defines the corresponding port pin as input, and resetting a bit in TRISB register defines the corresponding port pin as output.

Page 13: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

13

PORTA and TRISA

PORTA has 5 adjoining pins. The corresponding register for data direction is TRISA at address 85h. Like with port B, setting a bit in TRISA register defines also the corresponding port pin as input, and clearing a bit in TRISA register defines the corresponding port pin as output.

Page 14: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

14

Memory Organization

PIC16F84 has two separate memory blocks, one for data and the other for program. EEPROM memory with GPR and SFR registers in RAM memory make up the data block, while FLASH memory makes up the program block. 

Program memoryProgram memory has been carried out in FLASH technology which makes it possible to program a microcontroller many times before it's installed into a device, and even after its installment if eventual changes in program or process parameters should occur. The size of program memory is 1024 locations with 14 bits width where locations zero and four are reserved for reset and interrupt vector.

Page 15: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

15

Memory and Registers

Data memoryData memory consists of EEPROM and RAM memories. EEPROM memory consists of 64 eight bit locations whose contents is not lost during loosing of power supply. EEPROM is not  directly addressable, but is accessed indirectly through EEADR and EEDATA registers. As EEPROM memory, there is a strict procedure for writing in EEPROM which must be followed in order to avoid accidental writing.

GPR registersLocations of RAM memory are also called GPR registers which is an abbreviation for General Purpose Registers. GPR registers can be accessed regardless of which bank is selected at the moment. 

SFR registersRegisters which take up first 12 locations in banks 0 and 1 are registers of specialized function assigned with certain blocks of the microcontroller. These are called Special Function Registers.

Page 16: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

16

Page 17: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

17

Some instructions

BSF: bit set in file register BSF PORTA,3 BSF TRISA,2 ; RA2 is input

BCF: bit clear in file register BCF TRISB,6 ; RB6 is output

MOVLW: move literal (immediate number) to register W MOVLW 0x6D ; 6D (in hex) W ADDLW: ADD (or AND) literal to register W ADDLW 0x56 ; W+56(in hex) W ADDLW D’56 ; W+56(decimal) W ANDLW B’00011000’ ; W AND B’00011000’ W

Page 18: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

18

Some instructions

MOVWF f: move W to a file register f MOVWF PORTA ; W PORTA MOVWF TRISA ; W TRISA

ADDWF f,d: add W & f, W+fd (destination) ADDWF PORTA,w ; porta+ww ADDWF PORTA,f ; porta+wporta

INCF PORTA,f ; porta+1porta

DECF PORTB,w ; portb-1w

Page 19: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

19

Bit test, indirect mode

BTFSS PORTA,3 GOTO label BTFSC INTCON,5

Page 20: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

20

Other instructions CLRW CLRF MOVF …

Next we see how typical instructions are explained in the data sheet …

Page 21: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

21

INCFSZ      Increment f, skip if=0 DECFSZ

Page 22: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

22

Page 23: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

23

Assembly language programming

"Assembly language" and "assembler" are two different notions. The first represents a set of rules used in writing a program for a microcontroller, and the other is a program on the personal computer which translates assembly language into a language of zeros and ones. A program that is translated into  "zeros" and "ones" is also called "machine language".

Page 24: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

24

Assembly language programming

In order to function properly, we must define several microcontroller parameters such as:  - type of oscillator,- whether watchdog timer is turned on, and- whether internal reset circuit is enabled.All this is defined by the following directive:

_CONFIG _CP_OFF&_WDT_OFF&PWRTE_ON&XT_OSC

Page 25: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

25

Applications

Buttons are mechanical devices used to execute a break or make connection between two points. They come in different sizes and with different purposes. Buttons that are used here are also called "dip-buttons". They are soldered directly onto a printed board and are common in electronics. They have four pins (two for each contact) which give them mechanical stability.

Page 26: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

Example 1 – Single LED Circuit

April 24, 2023EE345 - Introduction to Microcontrollers 26

This circuit consists of a single LED lamp wired to Port B, line 0, as shown below. 

Page 27: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

A program to turn on the LED on Port-B, line 0, requires a few but essential processing operations. Code must perform the following operations:

1. Define and select processor (in this case 16F84A).2. Link-in the corresponding include file (p16f84A.inc).3. Select the oscillator type (here external resonator, _XT

type).4. Direct execution to the main label.5. Initialize Port-B for output.6. Set line 0 in Port-B high.

April 24, 2023EE345 - Introduction to Microcontrollers 27

Example 1 – Single LED Circuit

Page 28: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

The program is as follows:  ; File: LEDOn.asm; Date: June 1, 2006; Author: Julio Sanchez; Processor: 16F84A; Description:; Turn on LED wired to Port-B, line 0;===========================; switches;===========================; Switches used in __config directive:; _CP_ON Code protection ON/OFF; * _CP_OFF; * _PWRTE_ON Power-up timer ON/OFF; _PWRTE_ON; _WDT_ON Watchdog timer ON/OFF; * _WDT_OFF; _LP_OSC Low power crystal oscillator; * _XT_OSC External parallel resonator/crystal oscillator; |_____ * indicates setup valuesprocessor 16f84Ainclude <p16f84A.inc>__config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

April 24, 2023EE345 - Introduction to Microcontrollers 28

Single LED Circuit – program

Page 29: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

; variables in PIC RAM; None used; main program

org 0 ; start at address 0goto main

; space for interrupt handlerorg 0x04

; main programmain; Initialize all lines in Port-B for output

bsf STATUS, RP0 ; Bank 1movlw B’00000000’ ; w = 00000000 binarymovwf TRISB ; Set up Port-B for output

; Note: this can also be accomplished via: tris PORTB ; Turn on line 0 in Port-B. All others remain off

bcf STATUS, RP0movlw B’00000001’movwf PORTB ; Line 0 ON, all others OFF

; Endless loop intentionally hangs up programwait goto wait

end

April 24, 2023EE345 - Introduction to Microcontrollers 29

Single LED Circuit – program cont’d

Page 30: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

Example 2 – LED Flasher A different program makes the LED in the above circuit flash on and

off. All that is necessary is a delay loop using a file register counter. The logic turns on the LED and counts down to zero. Then it turns the LED off and counts down again.

The counter routine demonstrates the creation of a procedure in PIC programming. In fact, a procedure is nothing more than a routine called by a label at its entry point and terminated with a return statement. The procedure is executed by a call statement to its initial label, as follows:

call delay ; Call to procedure.; Elsewhere in the programdelay:; procedure instructions go herereturn ; End of procedure

April 24, 2023EE345 - Introduction to Microcontrollers 30

Page 31: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

delay:movlw .200 ; w = 200 decimalmovwf j ; j = w

jloop:movwf k ; k = w

kloop:decfsz k,f ; k = k-1, skip next if zerogoto kloopdecfsz j,f ; j = j-1, skip next if zerogoto jloopreturn

 Code assumes that two variables were created in the processor’s GPR space, as follows:

; Declare variables at 2 memory locationsj equ 0x0ck equ 0x0d

April 24, 2023EE345 - Introduction to Microcontrollers 31

LED Flasher – Delay Loop

Page 32: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

processor 16f84Ainclude <p16f84A.inc>__config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

;=====================================================; variables in PIC RAM;=====================================================; Declare variables at 2 memory locationsj equ 0x0ck equ 0x0d;=====================================================; m a i n p r o g r a m;=====================================================

org 0 ; start at address 0goto main

;=============================; space for interrupt handler;=============================

org 0x04;=============================

April 24, 2023EE345 - Introduction to Microcontrollers 32

LED Flasher – Program

Page 33: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

; main program;=============================main:; Initialize all line in Port-B for output

movlw B’00000000’ ; w = 00000000 binarytris PORTB ; Set up Port-B for output

;; Program loop to turn LED on and offLEDonoff:; Turn on line 0 in Port-B. All others remain off

movlw B’00000001’ ; LED ONmovwf PORTBcall delay ; Local delay routine

; Turn off line 0 in Port-B.movlw B’00000000’ ; LED OFFmovwf PORTBcall delaygoto LEDonoff

;================================

April 24, 2023EE345 - Introduction to Microcontrollers 33

LED Flasher – Program cont’d

Page 34: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

; delay subroutine;================================delay:

movlw .200 ; w = 200 decimalmovwf j ; j = w

jloop:movwf k ; k = w

kloop:decfsz k,f ; k = k-1, skip next if zerogoto kloopdecfsz j,f ; j = j-1, skip next if zerogoto jloopreturnEnd

April 24, 2023EE345 - Introduction to Microcontrollers 34

LED Flasher – Program cont’d

Page 35: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

Example 3 – LED/Pushbutton A slightly more complex circuit contains a pushbutton

switch. In this case, the program monitors the state of the pushbutton and lights the LED accordingly.

Next figure below shows one possible wiring for the LED/pushbutton circuit.

If a switch reports a zero bit when active, it is described as active-low. A switch that reports a one-bit when pressed is said to be active-high. The pushbutton switch on the figure is active-low.

In the same manner, an output device can be wired so that it is turned on with a logic 0 and off with logic 1 on the port pin.

April 24, 2023EE345 - Introduction to Microcontrollers 35

Page 36: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

April 24, 2023EE345 - Introduction to Microcontrollers 36

LED/Pushbutton Circuit

Page 37: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

; Configure as before main:; Initialize all lines in Port-B for output

movlw B’00000000’ ; w = 00000000 binarytris PORTB ; Set up Port-B for output

; Initialize Port-A, line 0, for inputmovlw B’00000001’ ; w = 00000001 binarytris PORTA ; Set up RA0 for input

; Program loop to test state of pushbutton switch; read PB switch stateLEDctrl:; Push button switch on demo board is wired to Port-A bit 0; Switch logic is active low

btfss PORTA,0 ; Test. Skip next line if bit is setgoto turnOFF ; Turn LED off routine

; At this point Port-A bit 0 is not set; Switch is pressed (active low action); Turn ON line 0 in Port-B

bsf PORTB,0 ; RB0 highgoto LEDctrl

turnOFF:; Routine to turn OFF LED

bcf PORTB,0 ; RB0 lowgoto LEDctrlEnd

April 24, 2023EE345 - Introduction to Microcontrollers 37

LED/Pushbutton – Program

Page 38: EE345: Introduction to Microcontrollers The Programmable Interface Controller (PIC): PIC 16F84

We hope material was clear & useful …. PIC is much more complex ….

You will spend all of next course studying it.GOOD LUCK !

April 24, 2023EE345 - Introduction to Microcontrollers 38