430 likes | 733 Views
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.
E N D
EE345: Introduction to MicrocontrollersThe Programmable Interface Controller (PIC): PIC 16F84 Prof. Ahmad Abu-El-Haija
Outline • Definition of microcontroller • Architecture • Registers • Instructions • Applications • Programming Examples
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
Architecture – PIC 16F84 • 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.
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.
Pins of 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. EE345 - Introduction to Microcontrollers
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.
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.
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.
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.
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.
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.
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 registers Locations 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.
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
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
Bit test, indirect mode • BTFSS PORTA,3 • GOTO label • BTFSC INTCON,5
Other instructions • CLRW • CLRF • MOVF • … Next we see how typical instructions are explained in the data sheet …
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".
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
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.
Example 1 – Single LED Circuit • This circuit consists of a single LED lamp wired to Port B, line 0, as shown below. EE345 - Introduction to Microcontrollers
Example 1 – Single LED Circuit • 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. EE345 - Introduction to Microcontrollers
Single LED Circuit – program 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 values processor 16f84A include <p16f84A.inc> __config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF EE345 - Introduction to Microcontrollers
Single LED Circuit – program cont’d ; variables in PIC RAM ; None used ; main program org 0 ; start at address 0 goto main ; space for interrupt handler org 0x04 ; main program main ; Initialize all lines in Port-B for output bsf STATUS, RP0 ; Bank 1 movlw B’00000000’ ; w = 00000000 binary movwf 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, RP0 movlw B’00000001’ movwf PORTB ; Line 0 ON, all others OFF ; Endless loop intentionally hangs up program wait goto wait end EE345 - Introduction to Microcontrollers
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 program delay: ; procedure instructions go here return ; End of procedure EE345 - Introduction to Microcontrollers
LED Flasher – Delay Loop delay: movlw .200 ; w = 200 decimal movwf j ; j = w jloop: movwf k ; k = w kloop: decfsz k,f ; k = k-1, skip next if zero goto kloop decfsz j,f ; j = j-1, skip next if zero goto jloop return Code assumes that two variables were created in the processor’s GPR space, as follows: ; Declare variables at 2 memory locations j equ 0x0c k equ 0x0d EE345 - Introduction to Microcontrollers
LED Flasher – Program processor 16f84A include <p16f84A.inc> __config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF ;===================================================== ; variables in PIC RAM ;===================================================== ; Declare variables at 2 memory locations j equ 0x0c k equ 0x0d ;===================================================== ; m a i n p r o g r a m ;===================================================== org 0 ; start at address 0 goto main ;============================= ; space for interrupt handler ;============================= org 0x04 ;============================= EE345 - Introduction to Microcontrollers
LED Flasher – Program cont’d ; main program ;============================= main: ; Initialize all line in Port-B for output movlw B’00000000’ ; w = 00000000 binary tris PORTB ; Set up Port-B for output ; ; Program loop to turn LED on and off LEDonoff: ; Turn on line 0 in Port-B. All others remain off movlw B’00000001’ ; LED ON movwf PORTB call delay ; Local delay routine ; Turn off line 0 in Port-B. movlw B’00000000’ ; LED OFF movwf PORTB call delay goto LEDonoff ;================================ EE345 - Introduction to Microcontrollers
LED Flasher – Program cont’d ; delay subroutine ;================================ delay: movlw .200 ; w = 200 decimal movwf j ; j = w jloop: movwf k ; k = w kloop: decfsz k,f ; k = k-1, skip next if zero goto kloop decfsz j,f ; j = j-1, skip next if zero goto jloop return End EE345 - Introduction to Microcontrollers
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. EE345 - Introduction to Microcontrollers
LED/Pushbutton Circuit EE345 - Introduction to Microcontrollers
LED/Pushbutton – Program ; Configure as before main: ; Initialize all lines in Port-B for output movlw B’00000000’ ; w = 00000000 binary tris PORTB ; Set up Port-B for output ; Initialize Port-A, line 0, for input movlw B’00000001’ ; w = 00000001 binary tris PORTA ; Set up RA0 for input ; Program loop to test state of pushbutton switch ; read PB switch state LEDctrl: ; 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 set goto 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 high goto LEDctrl turnOFF: ; Routine to turn OFF LED bcf PORTB,0 ; RB0 low goto LEDctrl End EE345 - Introduction to Microcontrollers
Wehope material was clear & useful …. PIC is much more complex …. You will spend all of next course studying it. GOOD LUCK ! EE345 - Introduction to Microcontrollers