Pic 16f
Pic 16f
of ECE RSET
The microcontroller is simply a computer on a chip. It is one of the most important developments in electronics since the invention of the microprocessor itself. It is essential for the operation of devices such as mobile phones, DVD players, video cameras, and most self-contained electronic systems
The microcontroller contains the same main elements as any computer system: Processor Memory Input/output Block diagram of a basic microprocessor system
In a PC, these are provided as separate chips, linked together via bus connections on a printed circuit board, but under the control of the microprocessor (CPU).
In the microcontroller, all these elements are on one chip. In any given circuit, the microcontroller also tends to have a single dedicated function (in contrast to the PC).
The processor cannot operate without a program, which is a list of instructions that is held in memory. The program consists of a sequence of binary codes that are fetched from memory by the CPU in sequence, and executed.
There are two types of memory: volatile and non-volatile. Volatile memory loses its data when switched off, but can be written by the CPU to store current data; this is RAM (Random Access Memory). ROM (Read Only Memory) is non-volatile, and retains its data when switched off. Flash ROM, as used in memory sticks and MP3 players, is closest to the ideal, having the advantages of being nonvolatile and rewritable. This is why it is used as program memory in microcontrollers which need to be reprogrammed, such as the PIC 16F877.
Without some means of getting information and signals in and out, a data processing or digital control system would not be very useful. Ports are based on a data register, and set of control registers, which pass the data in and out in a controlled manner, often according to a standard protocol (method of communication). Parallel and serial data ports: (a) parallel; (b) serial
PIC stands for Peripheral Interface Controller A family of Harvard architecture microcontrollers made by Microchip Technology Derived from the PIC1650 originally developed by General Instrument Microelectronics Division. The name PIC was originally an acronym for "Programmable Intelligent Computer".
10
PIC follows a Harvard Architecture It also has a RISC (Reduced Instruction Set Computer) CPU.
11
12bit cores with 33 instructions: 12C50x, 16C5x 14bit cores with 35 instructions: 12C67x,16Cxxx 16bit cores with 58 instructions: 17C4x,17C7xx Enhanced 16bit cores with 77 instructions: 18Cxxx PIC 16F877 is now used widely as a more advanced teaching device. chip covers most of the features that higher level students need for project work with microcontrollers
Get PICing!
Rajagiri School of Engineering & Technology 8th Feb 2013 12
The chip can be obtained in different packages, such as conventional 40pin DIP (Dual In-Line Package),square surface mount or socket format. The DIP version is recommended for prototyping, and is shown in Figure
13
The chip has two pairs of power pins (VDD5 V nominal and Vss 0 V), and either pair can be used. The chip can actually work down to about 2 V supply, for battery and power-saving operation. A low-frequency clock circuit using only a capacitor and resistor to set the frequency can be connected to CLKIN, or a crystal oscillator circuit can be connected across CLKIN and CLKOUT. MCLR is the reset input; when cleared to 0, the MCU stops, and restarts when MCLR 1. This input must be tied high allowing the chip to run if an external reset circuit is not connected, but it is usually a good idea to incorporate a manual reset button in all but the most trivial applications
14
15
The main program memory is flash ROM, which stores a list of 14-bits instructions. These are fed to the execution unit, and used to modify the RAM file registers. These include special control registers, the port registers and a set of general purpose registers which can be used to store data temporarily. A separate working register (W) is used with the ALU (Arithmetic Logic Unit) to process data. Various special peripheral modules provide a range of I/O options
16
There are 512 RAM File Register addresses (01FFh), which are organized in 4 banks (03), each bank containing 128 addresses. The default (selected on power up) Bank 0 is numbered from 0 to 7Fh, Bank 1 from 80h to FFh and so on. These contain both Special Function Registers (SFRs), which have a dedicated purpose, and the General Purpose Registers (GPRs). Deducting the SFRs from the total number of RAM locations, and allowing for some registers which are repeated in more than one bank, leaves 368 bytes of GPR (data) registers.
Rajagiri School of Engineering & Technology
17
18
MPLAB Assembler-MPASM
19
_CONFIG(address 2007h) 1. Code Protection 2.In circuit debugging 3.Low voltage programming 5.Power up timer 6.Brown out timer 7. watchdog timer 8.RC Oscillator 9. Crystal Oscillator
Rajagiri School of Engineering & Technology
20
21
Status Register
The status register records the result of certain operations, MCU power status and includes the bank selection bits.
22
ZERO FLAG (Z) This is set when the result of a register operation is zero, and cleared when it is not zero. CARRY FLAG (C) This flag is only affected by add, subtract and rotate instructions. If the result of an add operation generates a carry out, this flag is set; that is, when two 8-bitnumbers give a 9-bit sum.
23
24
DIGIT CARRY (DC) The digit carry records a carry from the most significant bit of the low nibble (bit 3). POWER STATUS BITS There are two read only bits in the status register which indicate the overall MCU status. The Power Down (PD) bit is clear to zero when SLEEP mode is entered. The Time Out (TO) bit is cleared when a watchdog time out has occurred.
25
26
4 cycles per instruction on the PIC 16F87x microcontrollers. T1 Clock Read Instruction Decode Instruction Execute Instruction Store Result Oscillator circuits generate signal. Rajagiri Schoola of clock Engineering &
Technology
T2
T3
T4
27
For byte-oriented instructions, f represents a file register designator and d represents a destination designator. For bit-oriented instructions, b represents a bit field designator while f represents the address of the file in which the bit is located. For literal and control operations, k represents an eight or eleven-bit constant
29
30
31
32
33
DATA TRANSFER REGISTER INSTRUCTIONS ARITHMETIC INSTRUCTIONS LOGICAL INSTRUCTIONS BRANCH INSTRUCTIONS CONTROL INSTRUCTIONS
34
Move data from File register - MOVF f, d Move data from W to F - MOVWF f Move literal into W - MOVLW 02
35
list p=16f877a ; list directive to define processor #include <p16f877a.inc> ; processor specific variable definitions X EQU 20H Y EQU 21H TEMP EQU 22H ORG 00H MOVF X, 0 MOVWF TEMP MOVF Y, W MOVWF X MOVF TEMP, W MOVWF Y END
Rajagiri School of Engineering & Technology 8th Feb 2013 36
Clear W (reset all bits and value to 0) Clear F (reset all bits and value to 0) Decrement F (reduce by 1) Increment F (increase by 1) Swap the upper and lower four bits in F Complement F value (invert all bits) Rotate bits Left through carry flag Rotate bits Right through carry flag Clear ( 0) the bit specified Set ( 1) the bit specified
- CLRW - CLRF f - DECF f,d - INCF f,d - SWAPF f - COMF f,d - RLF f,d - RRF f,d - BCF f, b - BSF f, b
37
list p=16f877a ; list directive to define processor #include <p16f877a.inc> ; processor specific variable definitions
38
39
list p=16f877a ; list directive to define processor #include <p16f877a.inc> ; processor specific variable definitions A EQU 20H B EQU 21H RESULT EQU 23H ORG 00H MOVF A,0 ADDWF B,0 MOVWF RESULT END
Rajagiri School of Engineering & Technology 8th Feb 2013 40
41
- ANDWF f,d
- ANDLW k - IORWF f,d - IORLW k
Exclusive OR the bits of W and F, result in W - XORWF f,d Exclusive OR the bits of L and W - XORLW k
42
43
Go to a labelled line in the program Jump to the label at the start of a subroutine
-RETURN
-RETLW k
-RETFIE
44
No Operation - delay for 1 cycle - NOP Go into standby mode to save power -SLEEP
Clear watchdog timer to prevent automatic reset -CLRWDT
45
list p=16f877a ; list directive to define processor #include <p16f877a.inc> ; processor specific variable definitions
COUNT EQU 25H ; Count be the file register at 25H RESULT EQU 26H ; Result be the file register at 26H ORG 00H MOVLW d10 ;WREG =10 MOVWF COUNT ; Load the counter MOVLW 0 ;WREG=0 AGAIN ADDLW 3 ;WREG=WREG+3 DECF COUNT,1 ;Decrement counter BTFSS STATUS,Z ;Check whether COUNT =0 GOTO AGAIN MOVWF RESULT ;Sum at RESULT END
Rajagiri School of Engineering & Technology 8th Feb 2013 46
NUM EQU 24H EVEN EQU 25H ODD EQU 26H MOVF NUM,0 RRF NUM,1 BTFSC STATUS C GOTO LOOP1 GO TO LOOP2 MOVWF ODD GO TO LOOP3 MOVWF EVEN NOP
Rajagiri School of Engineering & Technology 8th Feb 2013 47
list p=16f877a ; list directive to define processor #include <p16f877a.inc> ; processor specific variable definitions
CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF &_PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF ORG 00 ; Program begins at 00 R1 EQU 0x20 ; R1 as file register at 20h R2 EQU 0x21 ; R2 as file register at 21h NUM EQU 0x22 ; NUM as file register at 22h MOVF R1,0 ; WREG= R1 SUBWF R2,0 ; WREG= WREG-R2 BTFSC STATUS, C ;Is C flag is clear skip the next instruction GOTO LOOP ;Unconditional branch MOVF R1,0 ;WREG=R1,Biggest number MOVWF NUM ;NUM =WREG GOTO LP ; Unconditional branch LOOP MOVF R2,0 ;WREG=R2,Biggest number MOVWF NUM ;NUM =WREG LP NOP END Rajagiri School of Engineering &
Technology
48
list p=16f877a ; list directive to define processor #include <p16f877a.inc> ; processor specific variable definitions _CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF ORG 00 MULTIPLIER EQU 0x20 MULTIPLICAND EQU 0x21 RESULT EQU 0x22 MOVLW 0 AGAIN ADDWF MULTIPLICAND,0 DECF MULTIPLIER,1 BNZ AGAIN MOVWF RESULT END
Rajagiri School of Engineering & Technology
49
list p=16f877a ; list directive to define processor #include <p16f877a.inc> ; processor specific variable definitions __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF ORG 00 NUME EQU D'4' DENO EQU D'3' NUM EQU 0x20 QUOTIENT EQU 0x22 REMAINDER EQU 0x23 CLRF QUOTIENT ; CLEAR QUOTIENT MOVF NUME,0 ;WREG=4 MOVWF NUM ;NUMERATOR =4 MOVF DENO,0 ;WREG =3 INCF QUOTIENT,1 ; INCREMENT QUOTIENT SUBWF NUM,1 ;NUM= WREG-NUM BTFSS STATUS,C GOTO B1 GOTO AGAIN DECF QUOTIENT,1 Rajagiri School of Engineering & ADDWF NUM,1;TO GET REMAINDER
Technology
AGAIN
B1 END
50
list p=16f877a ; list directive to define processor #include <p16f877a.inc> ; processor specific variable definitions __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF ORG 00 NUM EQU 0x20 RESULT EQU 0x21 COUNT EQU 0x22 MOVF NUM,0 AGAIN RLF NUM,1 BTFSC STATUS,C GOTO LOOP1 GOTO LOOP LOOP1 INCF RESULT,1 LOOP DECF COUNT,1 BTFSS STATUS,Z GOTO AGAIN NOP END
Rajagiri School of Engineering & Technology 8th Feb 2013 51
The PIC 16F877 file register RAM is divided into four banks of 128 locations, banks 03 At power on reset, bank 0 is selected by default. To access the others, these register bank select bits must be changed, as shown in Table
52
The register banks are selected by setting and clearing the bits RP0 and RP1in the status register. More conveniently, the pseudo-operation BANKSEL can be used instead. The operand for BANKSEL is any register in that bank, or its label. In effect, BANKSEL detects the bank bits in the register address and copies them to the status register bank select bits.
53
There are five parallel ports in the PIC 16F877, labeled AE. All pins can be used as bit- or byte-oriented digital input or output. Their alternate functions are summarized in Table
54
PORTA is a 6-bit wide, bidirectional port. The corresponding data direction register is TRISA. Setting a TRISA bit (= 1) will make the corresponding PORTA pin an input Clearing a TRISA bit (= 0) will make the corresponding PORTA pin an output SUMMARY OF REGISTERS ASSOCIATED WITH PORTA
55
PORTB is an 8-bit wide, bidirectional port. The corresponding data direction register is TRISB.
SUMMARY OF REGISTERS ASSOCIATED WITH PORTB
56
PORTC is an 8-bit wide, bidirectional port. The corresponding data direction register is TRISC.
SUMMARY OF REGISTERS ASSOCIATED WITH PORTC
57
PORTD is an 8-bit port. PORTD can be configured as an 8-bit wide microprocessor port (Parallel Slave Port) by setting control bit, PSPMODE (TRISE<4>). SUMMARY OF REGISTERS ASSOCIATED WITH PORTD
58
PORTE has three pins (RE0/RD/AN5, RE1/WR/AN6 and RE2/CS/AN7) which are individually configurable as inputs or outputs.
SUMMARY OF REGISTERS ASSOCIATED WITH PORTE
59
60
list p=16f877a ; list directive to define processor #include <p16f877a.inc> ; processor specific variable definitions __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF R0 EQU 20H R1 EQU 21H COUNT0 EQU 22H COUNT1 EQU 23H ORG 00H CLRW BANKSEL TRISB MOVWF TRISB
BANKSEL PORTB AGAIN1 MOVLW 0FFH MOVWF PORTB CALL DELAY COMF PORTB CALL DELAY GOTO AGAIN1
62
DELAY MOVLW 0ffh MOVWF COUNT0 FLOOP NOP NOP MOVLW 0ffh MOVWF COUNT1 NOP F1LOOP DECFSZ COUNT1,F GOTO F1LOOP DECFSZ COUNT0,F GOTO FLOOP RETURN END
63
Thank You
64