AN INTRODUCTION TO PIC MICROCONTROLLERS- The name PIC was originally an acronym for "ProgrammableIntelligent Computer".- A PIC microcontroller is a processor with built in memory andRAM and you can use it to control your projects.- What this really means for you is that you have a very powerfuldevice that has many useful built in modules e.g.* EEPROM.* Timers.* Analogue comparators.* UART.
INTRODUCTION (CONT.)Why PIC is popular? low cost ,wide availability with high clock speed availability of low cost or free development tools Only 37 instructions to remember serial programming and re-programming with flash memory capability Its code is extremely efficient, allowing the PIC to run with typically less program memory than its larger competitors PIC is very small and easy to implement for non- complex problems and usually accompanies to the microprocessors as an interface
TWO DIFFERENT ARCHITECTURES Harvard Architectures Von-Neumann (newer arch.) Architecture
TWO DIFFERENT ARCHITECTURES Harvard Architectures Von-Neumann Architecture Used mostly in RISC CPUs Used in: 80X86 (CISC PCs) Separate program bus and data bus: can be of different widths Only one bus between CPU and memory For example, PICs use: RAM and program memory Data memory (RAM): a small share the same bus and the number of 8bit registers same memory, and so must have Program memory (ROM): the same bit width. 12bit, 14bit or 16bit wide (in EPROM, FLASH, or ROM)
RISC VS. CISCComplex Instruction Set Computer (CISC)Is a computer where single instructions can execute severallow-level operations (such as a loadfrom memory, an arithmetic operation, and a memory store)and/or are capable of multi-step operations or addressingmodes within single instructions. Used in: 80X86, 8051, 68HC11, etc. Many instructions (usually > 100) Several addressing modes Usually takes more than 1 internal clock cycle (Tcyc) to execute
RISC VS. CISCReduced Instruction Set Computer (RISC)Is a CPU design strategy based on the insight that simplifiedinstructions can provide higher performance if this simplicity enablesmuch faster execution of each instruction. A computer based on thisstrategy is a reduced instruction set computer also called RISC. Used in: SPARC, ALPHA, Atmel AVR, etc. Few instructions (usually < 50) Only a few addressing modes Executes 1 instruction in 1 internal clock cycle (Tcyc)
FAMILY CORE ARCHITECTURE DIFFERENCES The PIC Family: Cores 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
THE PIC FAMILY: SPEED Can use crystals, clock oscillators, or even an RC circuit. Some PICs have a built in 4MHz RC clock, Not very accurate, but requires no external components! Instruction speed = 1/4 clock speed (Tcyc = 4 * Tclk) All PICs can be run from DC to their maximum specified speed: 12C50x 4MHz 12C67x 10MHz 16Cxxx 20MHz 17C4x / 17C7xxx 33MHz 18Cxxx 40MHz
CLOCK AND INSTRUCTION CYCLES Instruction Clock Clock from the oscillator enters a microcontroller via OSC1 pin where internal circuit of a microcontroller divides the clock into four even clocks Q1, Q2, Q3, and Q4 which do not overlap. These four clocks make up one instruction cycle (also called machine cycle) during which one instruction is executed. Execution of instruction starts by calling an instruction that is next in string. Instruction is called from program memory on every Q1 and is written in instruction register on Q4. Decoding and execution of instruction are done between the next Q1 and Q4 cycles. On the following diagram we can see the relationship between instruction cycle and clock of the oscillator (OSC1) as well as that of internal clocks Q1-Q4. Program counter (PC) holds information about the address of the next instruction.
THE PIC FAMILY: PROGRAM MEMORY Technology: EPROM, FLASH, or ROM It varies in size from one chip to another. - examples: 12C508 512 12bit instructions 16C711 1024 (1k) 14bit instructions 16F877 8192 (8k) 14bit instructions 17C766 16384 (16k) 16bit instructions
MICROCHIP PIC 16F84 MICROCONTROLLERHigh Performance RISC CPU:Only 35 single word instructions to learnAll single cycle instructions except for programbranches, which are two-cycleOperating speed: DC - 20 MHz clock input DC - 200ns instruction cycle
MICROCHIP PIC 16F84 MICROCONTROLLERBelow is a diagram showing the pin-outs of the PIC 16F84. We willgo through each pin, explaining what each is used for.
MICROCHIP PIC 16F84 MICROCONTROLLERRA0 To RA4RA is a bidirectional port. That is, it can beconfigured as an input or an output. The numberfollowing RA is the bit number (0 to 4). So, we haveone 5-bit directional port where each bit can beconfigured as Input or Output.RB0 To RB7RB is a second bidirectional port. It behaves inexactly the same way as RA, except there are 8 -bits involved.
MICROCHIP PIC 16F84 MICROCONTROLLERVSS And VDDThese are the power supply pins. VDD is thepositive supply, and VSS is the negativesupply, or 0V. The maximum supply voltage thatyou can use is 6V, and the minimum is 2V.OSC1/CLK IN And OSC2/CLKOUTThese pins is where we connect an externalclock, so that the microcontroller has some kindof timing.
MICROCHIP PIC 16F84 MICROCONTROLLERMCLRThis pin is used to erase the memory locationsinside the PIC (i.e. when we want to re-programit. In normal use it is connected to the positivesupply rail.T0CK1This is another clock input, which operates aninternal timer. It operates in isolation to the mainclock.
MICROCHIP PIC 16F84 MICROCONTROLLERBelow is a diagram showing the pin-outs of the PIC 16F84. We willgo through each pin, explaining what each is used for. PORT APORT B
PIC DATA MEMORYThe mostimportantregistershaveaddressesin all thefourbanks The data memory is devided into 4 memory banks
THE REGISTERSA register is a place inside the PIC that can be written to, read fromor both.you will notice is that it is split into two - Bank 0 and Bank1. Bank 1 is used to control the actual operation of the PIC, forexample to tell the PIC which bits of Port A are input and whichare output. Bank 0 is used to manipulate the data.An example is as follows: Let us say we want to make one bit onPort A high. First we need to go to Bank 1 to set the particularbit, or pin, on Port A as an output. We then come back to Bank 0and send a logic 1 (bit 1) to that pin.
THE REGISTERSThe most common registers in Bank 1 we are going to use areSTATUS, TRISA and TRISB. The first allows us to come back toBank 0, TRISA allows us to select which pins on Port A are outputand which are input, TRISB allows us to select which pins on Port Bare output and which are input. The SELECT register in Bank 0allows us to switch to Bank 1.STATUSTo change from Bank 0 to Bank 1 we tell the STAUS register. We dothis by setting bit 5 of the STATUS register to 1. To switch back toBank 0, we set bit 5 of the STATUS register to 0. The STATUSregister is located at address 03h.
THE REGISTERSTRISA and TRISB.These are located at addresses 85h and 86h respectively. Toprogram a pin to be an output or an input, we simply send a 0 or a 1to the relevant bit in the register.So, on Port A we have 5 pins, and hence 5 bits. If I wanted to setone of the pins to input, I send a ‘1’ to the relevant bit. If I wanted toset one of the pins to an output, I set the relevant bit to ‘0’. The bitsare arranges in exactly the same way as the pins, in other words bit0 is RA0, bit 1 is RA1, bit 2 is RA2 and so on. PORT A Pin RA4 RA3 RA2 RA1 RA0 Bit Number 4 3 2 1 0 Binary 0 0 1 1 0
THE REGISTERSW ( working register )The W register is a general register in which you can put anyvalue that you wish. Once you have assigned a value to W, youcan add it to another value, or move it. If you assign anothervalue to W, its contents are overwritten.
THE REGISTERSNow what he have to do is turn an LED on. We do this bymaking one of the pins (the one with the LED connected to it)high. In other words, we send a ‘1’ to the pin.movlw 02hmovwf 05hSo, now our LED is on, we now need to turn it off:movlw 00hmovwf 05h
REGISTER ADDRESSING MODESDirect Addressing:Uses 7 bits of 14 bit instruction to identify a register file address 8thand 9th bit comes from RP0 and RP1 bits of STATUS register.
PIC PERIPHERALSThe PIC microcontroller has many builtin peripherals and this can make using themquite daunting at first which is why I have madethis introductory page with a summary of eachmajor peripheral block.
PIC PERIPHERALS PIC microcontroller PIC microcontroller Feature feature description Flash memory Re-programmable program storage. RAM Memory storage for variables. EEPROM Long term stable memory : Electrically Erasable Programmable Read Only Memory. I/O ports High current Input/output ports (with pin direction change). USART Built in RS232 protocol (only needs level translator chip).
PIC PERIPHERALSFlash memoryThis is the program storage area and gives you the mostimportant benefit for using a PIC microcontroller - You programthe device many times. PIC microcontroller RAM The PIC microcontroller RAM size is also important as it stores all your variables and intermediate data. Note: You can usually alter the program to use less RAM by choosing the right variable sizes or changing how your program works For example dont use floating point alter it to use a different variable type e.g. you can use long integers with fixed point operation to avoid floating point.
PIC PERIPHERALSPIC microcontroller EEROMElectrically Erasable ROM is used to store data that must besaved between power up and power down.This area is readable and writable and has a much longer lifethan the main program store i.e. it has been designed for morefrequent use.
PIC PERIPHERALSI/O PortsInput / Output ports let you communicate with the outside world soyou can control leds, LCDs or just about anything with the rightinterface. You can also set them as inputs to gather information.Pin directionMost PIC microcontroller pins can be set as an input or and outputand this can be done on the fly e.g. for a dallas 1 wire system a pincan be written to generate data and read at a later stage. The TRISregister controls the I/O direction and setting a bit in this registerto zero sets the pin as output while setting it as one sets the pinas input.