introduction to pic micro controller
TRANSCRIPT
Introduction to PIC Microcontroller
Electronic System
FunctionalBlock
(TR,IC,C,R etc)M
Voltage
Switch
Sensor
Lamp
Relay
Motor
I/F CKT
I/F CKT
Micro-Controller System
Electronic Circuit
Chip Resource
Programming
Input Output
Electronic System (Cont.)
M
Input Unit
Output Unit
Programmable Logic Controller
Microprocessor
Programming
Input Output
PC Parallel Port Control
I/O Port
PC Hardware
Programming
TransducerSwitch
LampMotor
KeyboardMouse
I/O Port
Display
What is computer?
The most essential feature of computer is that it can be programmed. A program is a set of instructions to the computer and it is stored in the computer itself. By replacing programs the computer can perform different tasks. It is called stored program architecture proposed by John Von Neumann in 1946. This architecture is used in all computers today .
What is the difference? micro-P & micro-C
Microprocessor Microcontroller
Main part of CPU without Memory
All in one chipCPU with memory +I/O
Use for computer
High performance
General purpose
Low cost, tiny chip
Why logic only?
Can use the newest technology.
Why logic & memory are mixed?
To reduce cost. Package is the main factor of cost.
What is the PIC ?
PIC(Peripheral Interface Controller) is a brand name of micro-C, manufactured by Microchip Technology Inc. It is intended to use as a peripheral control device.
The PIC uses the RISC(Reduced Instruction Set Computer).With this, the total number of instruction is 35 .
Compared to a human being, the brain is the main CPU and the PIC is equivalent to the autonomic nervous system.
RISC vs. CISC
Microcontrollers with Harvard architecture are also called "RISC microcontrollers". RISC stands for Reduced Instruction Set Computer. Microcontrollers with von-Neumann's architecture are called 'CISC microcontrollers'. Title CISC stands for Complex Instruction Set Computer.
Hardware of the PIC16F84A
Pin Diagram
(I/O pin)
(I/O pin)
(+2~5.5V)
(I/O pin)
(I/O pin)
(I/O pin)
(I/O pin)
(I/O pin)
(I/O pin)
(I/O pin)
(Ground, 0V)
(I/O pin)
(I/O pin)
(I/O pin)
(I/O pin)
Clock generator - oscillator
Crystal Oscillator RC Oscillator
For timing insensitive, slow speed application. R=5Kohm-100Kohm C>20pF
Precisely timing control, high speed application Ex. f=4MHz
Reset
In order to prevent from bringing a logical zero to MCLR pin accidentally MCLR has to be connected via resistor to the positive supply pole.
Set all parameters to initial value and start operation from address 0.a) When power onb) When MCLR set to 0
The PIC, like the CPU, has calculation functions and memory, and is controlled by the software.However, the throughput and the memory capacity are low. Depending on the kind of PIC, the maximum clock operating frequency is about 20 MHz and the memory capacity (to write the program) is about 1K to 4K words.
The PIC is the small computer
PIC16F84A Block Diagram
Flash Program Memory
Flash memory is used to store the program. One word is 14 bits long and 1024 words (1k words ) can be stored. Even if power is switched off the contents of the flash memory will not be lost. Flash memory can be written to using the writer, but the number of times it be rewritten is limited to 1000 times.
Reset Vector ( 0000h ) The program will start from this address at power on or reset.
Interrupt Vector ( 0004h ) The program will start from this address at interrupt.
Configuration word ( 2007h ) The basic configuration of the PIC is specified. This area can not be accessed by the program.
RAM (Random Access Memory) File Registers
50h – 7Fh D0h –FFhUnimplemented
SFR (Special Function Registers)
12 bytes/Bank
GPR (General Purpose Registers)
68 bytes
(68 bytes) Can be used to temporally store results and conditions while the program is running.
STATUS Register
bits 6:5 RP1:RP0 (Register Bank Select bits) 01 = first bank00 = zero bank
bit 2 Z (Zero bit) Indication of a zero result1 = result equals zero0 = result does not equal zero
bit 0 C (Carry) Transfer 1 = transfer occurred from the highest resulting bit 0 = transfer did not occur
Specification of the input-output ports
The PIC16F84A has the 13 input/output pins. Five sets are called A port which corresponds to the PORTA register and eight sets are called B port which corresponds to the PORTB register. Each register is composed of 8 bits and the input/output pin corresponds to each bit of register.
The mode (the input or the output) of each pin is specified by the TRISA register (for PORTA) and the TRISB register (for PORTB). The setting "0" is for the output and "1" is for the input. These mode setting can be set every pin.
MPASM Assembler
The MPASM assembler (the assembler) is a command-line or Windows-based PC application that provides a platform for developing assembly language code for Microchip's PICmicro microcontroller (MCU) families.
Source Code (.asm)
Labels Mnemonics Operands Comments
list p=18f452 #include p18f452.inc Dest equ 0x0B ;Define constant org 0x0000 ;Reset vector goto Start org 0x0040 ;Interrupt vector Start ;Begin program movlw 0x0A movwf Dest bcf Dest, 3 ;This line uses 2 operands goto Start end ;End of source cord
Instruction set of PIC16 seriesIn PIC16 series, RISC(Reduced Instruction Set Computer) is adopted and the number of the instructions to use is 35 kinds.
Mnemonic Op explanation code
ADDWF f, d Add W and f 00 0111 dfff ffff
ANDWF f, d AND W with f 00 0101 dfff ffff
CLRF f Clear f 00 0001 1fff ffff
CLRW - Clear W 00 0001 0xxx xxxx
COMF f, d Complement f 00 1001 dfff ffff
DECF f, d Decrement f 00 0011 dfff ffff
DECFSZ f, d Decrement f, Skip if 0 00 1011 dfff ffff
INCF f, d Increment f 00 1010 dfff ffff
INCFSZ f, d Increment f, Skip if 0 00 1111 dfff ffff
Byte-oriented file register operations
Instruction set of PIC16 series
Mnemonic Op explanation code
IORWF f, d Inclusive OR W with f 00 0100 dfff ffff
MOVF f, d Move f 00 1000 dfff ffff
MOVWF f Move W to f 00 0000 1fff ffff
NOP - No Operation 00 0000 0xx0 0000
RLF f, d Rotate Left f through Carry 00 1101 dfff ffff
RRF f, d Rotate Right f through Carry 00 1100 dfff ffff
SUBWF f, d Subtract W from f 00 0010 dfff ffff
SWAPF f, d Swap nibbles in f 00 1110 dfff ffff
XORWF f, d Exclusive OR W with f 00 0110 dfff ffff
Byte-oriented file register operations (Cont.)
Instruction set of PIC16 series
Mnemonic Op explanation code
BCF f, d Bit Clear f 01 00dd dfff ffff
BSF f, d Bit Set f 01 01dd dfff ffff
BTFSC f, d Bit Test f, Skip if Clear 01 10dd dfff ffff
BTFSS f, d Bit Test f, Skip if Set 01 11dd dfff ffff
Bit-oriented file register operations
Literal and control operations
Mnemonic Op explanation code
ADDLW k Add literal and W 11 111x kkkk kkkk
ANDLW k AND literal with W 11 1001 kkkk kkkk
CALL k Call subroutine 10 0kkk kkkk kkkk
CLRWDT - Clear Watchdog Timer 00 0000 0110 0100
Instruction set of PIC16 series
Literal and control operations (Cont.)
Mnemonic Op explanation code
GOTO k Go to address 10 1kkk kkkk kkkk
IORLW k Inclusive OR literal with W 11 1000 kkkk kkkk
MOVLW k Move literal to W 11 00xx kkkk kkkk
RETFIE - Return from interrupt 00 0000 0110 0100
RETLW k Return with literal in W 11 01xx kkkk kkkk
RETURN - Return from Subroutine 00 0000 0000 1000
SLEEP - Go into stanby mode 00 0000 0110 0011
SUBLW k Subtract W from literal 11 110x kkkk kkkk
XORLW k Exclusive OR literal with W 11 1010 kkkk kkkk
MOVF
Move f
Operation of Instructions
It moves (copy) the contents of the f register.d = 0 : store result in Wd = 1 : store result in f
MOVLW MOVWF
Move W to fMove literal to W
It sets literal data to the W register.
It moves (copy) the contents of the W register to the f register.
Example:movf abc,W (abc: valiable)
Example:movlw b’10101010’movlw 0xffmovlw abc
Example:movwf abcmovwf PORTB
DECFSZ
Decrement f, Skip if 0
Operation of Instruction
It subtracts 1 from the contents of the f register.In case the result is 0, skip the next instruction.
CLRF NOP
No OperationClear f
It clears f register. It is moved to the next instruction without processing anything.
Example:decfsz abc,F
Example:clrf abcclrf TRISB
Example:nop
BSF
Bit Set f
Operation of Instruction
It makes the specification bit of the contents of the f register 1.
BTFSS
Bit Test f, Skip if Set
It checks the specification bit of the f register. In case of 1, it skips the next instruction.
Example:decfsz abc,F
Example:btfss abc,7btfss PORTA,nbtfss STATUS,C
BCF
Bit Clear f
BTFSC
Bit Test f, Skip if Clear
GOTO
Go to address
Operation of Instructions
It jumps unconditionally to the address which the literal field shows.
CALL RETURN
Return from SubroutineCall subroutine
It jumps to the subroutine which the literal field shows.It stores the present PC value +1 in the stack.
It returns unconditionally from the subroutine.
Example: goto xyz (xyz: label)
Example: call xyzExample: return
bsf STATUS,RP0
bcf STATUS,RP0
Example of Listing File leddemo1.lst
0000 00028 org 0 ;Reset Vector0000 2804 00029 goto init0004 00030 org 4 ;Interrupt Vector 00031 00032 ;**************** Initial Process ****************0004 1683 00033 init bsf STATUS,RP0 ;Change to Bank1 0005 300F 00034 movlw 0x0f ;Input port assignment0006 0085 00035 movwf TRISA ;Set PORTA to Input mode0007 0186 00036 clrf TRISB ;Set PORTB to Output mode0008 1283 00037 bcf STATUS,RP0 ;Change to Bank00009 30FF 00038 movlw 0xff ;All LED OFF000A 0086 00039 movwf PORTB ; 1=OFF, 0=ON000B 018E 00040 clrf flags ;set stop000C 30FE 00041 movlw b'11111110'000D 0092 00042 movwf pattern ;set LED pattern000E 3001 00043 movlw 1000F 008C 00044 movwf actsw1 ;reset activate counter0010 008D 00045 movwf actsw2 ;reset activate counter0011 008F 00046 movwf mode ;set mode=1
Instruction address
Object code
Hex File Formats (.hex, .hxl, .hxh)
This file format is useful for transferring PICmicro MCU series code to Microchip programmers and third party PICmicro MCU programmers.
The MPASM assembler is capable of producing different hex file formats.
Format Name Format Type
File Extension
Use
Intel Hex Format
INHX8M .hex8-bit core device programmers
Intel Split Hex Format
INHX8S .hx1, .hxhOdd/even programmers
Intel Hex 32 Format
INHX32 .hex16-bit core device programmers
Intel Hex Format (INHX8M)
Each data record begins with a 9-character prefix and ends with a 2-character checksum. Each record has the following format:
:BBAAAATTHHHH....HHHCC
where: BB- A two digit hexadecimal byte count representing the number of data bytes that will appear on the line.
AAAA- A four digit hexadecimal address representing the starting address of the data record.
TT- A two digit record type that will always be '00' except for the end-of-file record, which will be '01'.
HH- A two digit hexadecimal data byte, presented in low byte/high byte combinations.
CC- A two digit hexadecimal checksum that is the two's complement of the sum of all preceding bytes in the record.
:020000040000FA:020000000428D2:0800080083160F30850086010C:100010008312FF3086008E01FE30920001308C008A:100020008D008F008C0B1B280C1485181B280130A9:100030008E06FF308C008D0B26280D1405192628FE:10004000FF308D008F0B262803308F008519122872:100050000E1C12280F1C3128920D121003181214B6:100060003528920C921303189217120886008F1CE1:100070001228482001308C008D0012286430900036:1000800000000000000000000000900B4028080065:0C009000053091003E20910B4A2808002A:02400E00F33F7E:00000001FF
Example of Hex File
leddemo1.hex
Writing ProgrammeClick Device -> MICROCHIP ->OK
MPU/MCU -> PIC16F84A ->Run
1) Click File-> Load File to Prog BufferLoad your hex file from PC
3) Insert PIC into the socket.Click Program -> Run !!!
2) Click Intel HEX or 8 Bits INHX8M -> OK
Writing Programme (Cont.)