Microprocessors and Programming Dr. Kadir ERKAN Department of Mechatronics Engineering Fall : 2013 10/20/2013 1
Interpretation of Assembly Instructions ADDLW h 10 ; literal (constant) oriented ADDWF TOPLA,d ; byte oriented interpretation BCF SONUC,5 ; bit oriented interpretation CALL ETIKET ; branching 10/20/2013 2
10/20/2013 3
Decision Making by Assembly Instructions BTFSC, BTFSS BTFSC REGISTER,b Komut_1 Komut_2 BTFSS REGISTER,b Komut_1 Komut_2 Test_PORTA BTFSS REGISTER,b GOTO Test_PORTA BSF PORTB,0 10/20/2013 4
Loop Programming by Assembly Instructions TEKRAR DECFSZ SAYAC,F GOTO TEKRAR BSF PORTB,0 At first, a number is assigned to SAYAC register. In each loop assigned number is decreased by one until the number reaches to zero 10/20/2013 5
Loop Programming by Assembly Instructions 10/20/2013 6
Internal Instruction Cycle Instruction Instruction Cycle GOTO 2 CALL 2 RETURN 2 Writing instruction to PC 2 DECFSZ 1 2 RETLW 2 RETFIE 2 INCFSZ 1 2 BTFSC 1 2 BTFSS 1 2 10/20/2013 7
Minimum Time Delay by a Single Loop Arrangement Minimum time delay is obtained by assigning a constant value to the register as h 01. 10/20/2013 8
Maximum Time Delay by a Single Loop Arrangement Maximum time delay is obtained by assigning a constant value to the register as h FF. 10/20/2013 9
Calculation of Instruction Cycle Number The number of cycle is directly related with the constant (N) that is assigned to the SAYAC register, thus, a simple formula can be developed as; Command Cycle Number (CCN) = 1+1+1x(N-1)+2+2x(N-1) CCN = 3N+1 N>>1 then if 1 is neglected CCN = 3N EXAMPLE: a PIC 16F8XX is driven by a 4 MHz oscillator and it is required to write down a subroutine providing 250 μs time delay; N =82 CCN =3x82+1=247 3xNOP =3x1=3 TOTAL =247+3=250 10/20/2013 10
Calculation of Instruction Cycle Number Determination of the constant N for a known latency N=LT.f/12 LT: latency f: external oscillator frequency EXAMPLE: to obtain 300μs time delay what value should be assigned to H for a PIC driven by a 10MHz oscillator frequency. LT=300μs; N=LT.f/12=300.10/12=250 N = d 250 =h FA As a result, h FA is to be assigned to SAYAC register. 10/20/2013 11
Time Delay by Double Loop Programming By single loop arrangement, a maximum 766 cycle time delay is obtained! When more than 766 cycle is required, netsed loop of time delay programming is used 1 μs/cycle 196606 cycle. 1 μs/cycle = 0,196s latency is obtained CNN=3.M.N 10/20/2013 12
Subroutine Programming by Assembly Instructions Asubroutine contains a sequence of instructions, begins with a label (subroutine_name) and ends with command return or retlw. The main difference comparing to macro is that subroutine is not replaced by its code in the program, but program jumps to subroutine to execute it. It happens every time the assembler encounters command call Subroutine_name in the program. On the command return, it leaves a subroutine and continues execution from where it left off the main program. Subroutine may be defined both prior to or upon the call 10/20/2013 13
Subroutine Programming by Assembly Instructions 1. Main program is executed from the beginning. 2. CALL ALTP: jumped to the subroutine ALTP 3. Departing address of main program is stored automatically inside STACK 4. Subroutine is executed starting from the first line 5. once RETURN is encountered, the subroutine is finished and then return back to the main programme 6. Departing address is taken automatically from STACK pointer 7. Main program is executed untill reaching END instruction 10/20/2013 14
10/20/2013 15 Automatic Stairway Lighting Switch RA0/AN0 2 RA1/AN1 3 RA2/AN2/VREF-/CVREF 4 RA4/T0CKI/C1OUT 6 RA5/AN4/SS/C2OUT 7 RE0/AN5/RD 8 RE1/AN6/WR 9 RE2/AN7/CS 10 OSC1/CLKIN 13 OSC2/CLKOUT 14 RC1/T1OSI/CCP2 16 RC2/CCP1 17 RC3/SCK/SCL 18 RD0/PSP0 19 RD1/PSP1 20 RB7/PGD 40 RB6/PGC 39 RB5 38 RB4 37 RB3/PGM 36 RB2 35 RB1 34 RB0/INT 33 RD7/PSP7 30 RD6/PSP6 29 RD5/PSP5 28 RD4/PSP4 27 RD3/PSP3 22 RD2/PSP2 21 RC7/RX/DT 26 RC6/TX/CK 25 RC5/SDO 24 RC4/SDI/SDA 23 RA3/AN3/VREF+ 5 RC0/T1OSO/T1CKI 15 MCLR/Vpp/THV 1 U1 PIC16F877A 5V 1k Q1 BC548 ROLE L1 12V L2 12V L3 12V L4 12V BATARYA 10V
Automatic Stairway Lighting Switch ;****************************************************************** ;Merdiven Otomatiği ;****************************************************************** list p=16f877a ; Derleyici için bilgi. #include <P16F877A.INC> ; 877A için gerekli isim-adres e?le?meleri. config h'3f31' ; PWRT on, di?erleri kapal?, ;Osilatör XT ve 4Mhz. SAYAC1 EQU h'0c' SAYAC2 EQU h'0d' SAYAC3 EQU h'0e' ; Gecikme alt programı değişkeni ; Gecikme alt programı değişkeni ; Gecikme alt programı değişkeni ORG 0x000 ; Başlangıç adresi. goto ana_program ; Ana programa git. ORG 0x004 ; Kesme adresi. kesme ; Hatayla kesme olu?ursa, retfie ; bir?ey yapmadan kesmeden geri dön. ana_program clrf PORTA ; PORTA'Yi SIFIRLA clrf PORTB ; PORTB'Yi SIFIRLA bsf STATUS,RP0 ; BANK1'E GEÇ movlw b'00001111' ; W <-- 11111111 movwf TRISB ; PORTB'Yi Giriş YAP movlw h'06' ; PORTA dijital giriş. movwf ADCON1 ; olacak şekilde ADCON1 ayarlandı. movlw b'00000000' ; W <-- 00000000 movwf TRISA ; PORTA'YI GiRiş YAP bcf STATUS,RP0 ; BANK0'A GEÇ 10/20/2013 16
Automatic Stairway Lighting Switch ;===== Butonları kontrol et. A1 btfsc PORTB,0 goto B1 ; A butonuna basılı mı? call LAMBA B1 btfsc PORTB,1 goto C1 ; B butonuna basılı mı? call LAMBA C1 btfsc PORTB,2 goto D1 ; C butonuna basılı mı? call LAMBA D1 btfsc PORTB,3 goto A1 ; D butonuna basılı mı? DONGU goto ; Sonsuz döngü DONGU ;============LAMBA ALT PROGRAMI============== LAMBA bsf PORTA,1 ; PortA çıkışını aktif yap call GECIKME bcf PORTA,1 ; PortA çıkışını pasif yap return ;=============GECİKME ALT PROGRAMI======= GECIKME ; 60 sn lik gecikme alt programı movlw d'255' ; W<--D'255' movwf SAYAC1 ; SAYAC1 <-- W G1 movlw d'255' ; W<--D'255' movwf SAYAC2 ; SAYAC2 <-- W G2 movlw d'255' ; W<--D'255' movwf SAYAC3 ; SAYAC3 <-- W G3 decfsz SAYAC3,F goto G3 decfsz SAYAC2,F goto G2 decfsz SAYAC1,F goto G1 return 10/20/2013 17 END
Look-up Table Programming A look-up table is a block of data that is held in program memory, which can be accessed by the program and used within it. The table is formed as a subroutine. Every byte of data in the table is accompanied by a special instruction, retlw. This instruction is another return from subroutine, but with a difference it requires an 8-bit literal operand. As it implements the subroutine return, it picks up its operand and puts it into the W register. The table is essentially a list of retlw instructions, each with its byte of data. 10/20/2013 18
Look-up Table Programming EXAMPLE: a 7-segment display is connected to PORTB of PIC mc and the characters represented by 7-segment display is stored inside a look-up table. The programme is to fetch the corresponding chracter from the table and writes on the PORTB 10/20/2013 19
LIST P=16F877A INCLUDE P16F877.INC Look-up Table Programming CLRF PORTB ; PortB ye bağlı ledleri söndür BSF STATUS,5 ; Bank1 e geç CLRF TRISB ; PortB nin uçlarını çıkış yap BCF STATUS,5 ; Bank0 a geç BASLA MOVLW h 05' CALL TABLO MOVWF PORTB DONGU GOTO DONGU TABLO ADDWF PCL,F ; PCL ß W( h 05 ) RETLW h 3F RETLW h 06 RETLW h 5B RETLW h 4F RETLW h 66 RETLW h 6D ; W ß h 6D RETLW h 7D RETLW h 07 RETLW h 7F RETLW h 6F RETLW h 77 RETLW h 7C RETLW h 39 RETLW h 5E RETLW h 79 RETLW h 71 RETLW h 80 END Number 5 is shown by 7- segment display 10/20/2013 20
LIST P=16F877 INCLUDE P16F877.INC SAYAC1 EQU SAYAC2 EQU SAYAC EQU Look-up Table Programming h 20 ;SAYAC1 e adres atandı h 21 ;SAYAC2 e adres atandı h 22' CLRF PORTB ; PortB ye bağlı ledleri söndür BSF STATUS,5 ; Bank1 e geç CLRF TRISB ; PortB nin uçlarını çıkış yap BCF STATUS,5 ;Bank0 a geç BASLA MOVLW h 00' ; b 00000000 sayısını W ya yükle MOVWF SAYAC ; W kayıtçısının içeriğini SAYAC a yükle DONGU MOVF SAYAC,W ; W ß SAYAC ANDLW B 00000001' ; W nin üst bitini sıfırla CALL CEV_TAB ; çevrim tablosunu çağır MOVWF PORTB ; kodu 7 segmentte göster INCF SAYAC,F ; SAYAC ß SAYAC+1 CALL GECIKME ; GOTO DONGU CEV_TAB ADDWF PCL,F ; PCL ß W (SAYAC) RETLW h 3F ; 0 RETLW h 06' ; 1 RETLW h 5B ; 2 RETLW h 4F ; 3 RETLW h 66' ; 4 RETLW h 6D ; 5 RETLW h 7D ; 6 RETLW h 07' ; 7 RETLW h 7F ; 8 RETLW h 6F ; 9 RETLW h 77' ; A RETLW h 7C ; B RETLW h 39' ; C RETLW h 5E ; D RETLW h 79' ; E RETLW h 71' ; F GECİKME MOVLW h FF MOVWF SAYAC1 DONGU1 MOVLW h FF MOVWF SAYAC2 DONGU2 DECFSZ SAYAC2,F GOTO DONGU2 DECFSZ SAYAC1,F GOTO DONGU1 RETURN 10/20/2013 21 END
Interrupt System There are 2 methods for communicating between the microcontroller and the external system: POLLING INTERRUPTS Polling In this method, microcontroller accesses at the exact time interval the external device, and gets the required information. The time periods is determined by user. In fact, you can say that, when using the method - polling, the processor must access the device itself and request the desired information that is needed to be processed. In fact we see that in this method, there is no independence for the external systems themselves. They depend on the microcontroller. The processor may only access the external device and get from it the information needed. The main drawback writing program that uses this method is a waste of time. The micro needs to wait and review whether a new information arrived. 10/20/2013 22
Interrupt System Interrupting Interrupt is the signal sent to the microcontroller to mark the event that requires immediate attention. Interrupt is asking" the processor to stop to perform the current program and to make time to execute a special code. In fact, the method of interrupt defines the option to transfer the information generated by internal or external systems inside the microcontrollers by themself! Once the system has finished the task imposed on it, the processor will be notified that it can access and receive the information and use it. External hardware devices are sending interrupts to microcontroller in order to receive the treatment. The microcontroller can send to itself an interrupt as a result of executing the code to report the failure in the process. In the multi-processor system, processors can send interrupts to each other as communication between them, for example for the division of work between them. There are two types of interrupts: software interrupts and hardware interrupts. 10/20/2013 23
Interrupt System Software interrupts come from a program that runs by the processor and request the processor to stop running the program, go to make a interrupt and then to return to continue to execute the program. Example: Procedure - when there is a procedure call, the processor stops the execution of the program, jumps to the place in memory that reserved for a procedure executes the procedure and only then returns back to the program and continues to execute. The hardware interrupts are sent to the microcontroller by external hardware devices. Some of the interrupts can be blocked = (masking) by Interrupt Enable bit (IE). When the interrupt is blocked the micro does not see the request for an interrupt, therefore won t be available to execute it. The blocked interrupt won t be executed till the block is removed. There are interrupts that can not be blocked. These are used to report on critical hardware issues, such as the drop of voltage. We want an immediate response from the microcontroller to these kind interrupts, without the ability to ignore them. 10/20/2013 24
Interrupt System 10/20/2013 25
Interrupt System 10/20/2013 26
Interrupt System The microcontroller has 14 interrupt sources XXIF is an interrupt flag that shows the result that we are getting from an interrupt. XXIE is an interrupt enable bit that is used to enable or block the interrupt. The interrupts on the left side of the figure (previous slide) are low priority and all of them together can be blocked by enabling bit interrupt PEIE = 0. We can determine whether or not to allow the system to address the interrupts. This is done by using Global Interrupt Enable bit GIE. 10/20/2013 27
Interrupt System 10/20/2013 28
;****************************************************************** ; Kesmelerin genel özellikleri ve RB0 harici kesmesi. ; PORTB Çıkış ==> LED : XT ==> 4Mhz ;****************************************************************** list p=16f877a ; Derleyici için bilgi. #include <P16F877A.INC> EXAMPLE ORG 0x000 ; Başlangıç Adresi. goto ana_program ; Ana programa git. ORG 0x004 ; Kesme vektörü adresi. bcf INTCON, INTF ; Kesme bayrağını indir. bsf PORTB, 7 ; PORTB nin 7 bit ine bağlı LED i yak. retfie ; Kesmeden geri dön. ana_program clrf PORTB ; PORTB yi temizle. movlw b'10010000 ; RB0 kesmesini aç. movwf INTCON bsf STATUS,RP0 ; 1. sayfaya geç. clrf OPTION_REG ; Kesme düşen kenarda. movlw b'00000001' ; PORTB nin 0.bit i giriş diğerleri çıkış movwf TRISB bcf STATUS,RP0 ; 0. sayfaya geç. dongu goto dongu ; Döngüye devam et. end ;**************************************************************** 10/20/2013 29
10/20/2013 30
10/20/2013 31
10/20/2013 32
10/20/2013 33
10/20/2013 34