introduction to pic micro controller

33
Introduction to PIC Microcontroller

Upload: jjizat

Post on 04-Apr-2015

175 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Introduction to PIC Micro Controller

Introduction to PIC Microcontroller

Page 2: Introduction to PIC Micro Controller

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

Page 3: Introduction to PIC Micro Controller

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

Page 4: Introduction to PIC Micro Controller

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 .

Page 5: Introduction to PIC Micro Controller

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.

Page 6: Introduction to PIC Micro Controller

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.

Page 7: Introduction to PIC Micro Controller

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.

Page 8: Introduction to PIC Micro Controller

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)

Page 9: Introduction to PIC Micro Controller

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

Page 10: Introduction to PIC Micro Controller

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

Page 11: Introduction to PIC Micro Controller

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

Page 12: Introduction to PIC Micro Controller

PIC16F84A Block Diagram

Page 13: Introduction to PIC Micro Controller

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.

Page 14: Introduction to PIC Micro Controller

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.

Page 15: Introduction to PIC Micro Controller

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

Page 16: Introduction to PIC Micro Controller

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.

Page 17: Introduction to PIC Micro Controller

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.

Page 18: Introduction to PIC Micro Controller

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

Page 19: Introduction to PIC Micro Controller

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

Page 20: Introduction to PIC Micro Controller

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.)

Page 21: Introduction to PIC Micro Controller

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

Page 22: Introduction to PIC Micro Controller

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

Page 23: Introduction to PIC Micro Controller

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

Page 24: Introduction to PIC Micro Controller

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

Page 25: Introduction to PIC Micro Controller

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

Page 26: Introduction to PIC Micro Controller

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

Page 27: Introduction to PIC Micro Controller

bsf STATUS,RP0

bcf STATUS,RP0

Page 28: Introduction to PIC Micro Controller

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

Page 29: Introduction to PIC Micro Controller

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

Page 30: Introduction to PIC Micro Controller

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.

Page 31: Introduction to PIC Micro Controller

:020000040000FA:020000000428D2:0800080083160F30850086010C:100010008312FF3086008E01FE30920001308C008A:100020008D008F008C0B1B280C1485181B280130A9:100030008E06FF308C008D0B26280D1405192628FE:10004000FF308D008F0B262803308F008519122872:100050000E1C12280F1C3128920D121003181214B6:100060003528920C921303189217120886008F1CE1:100070001228482001308C008D0012286430900036:1000800000000000000000000000900B4028080065:0C009000053091003E20910B4A2808002A:02400E00F33F7E:00000001FF

Example of Hex File

leddemo1.hex

Page 32: Introduction to PIC Micro Controller

Writing ProgrammeClick Device -> MICROCHIP ->OK

MPU/MCU -> PIC16F84A ->Run

Page 33: Introduction to PIC Micro Controller

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.)