- #1
El Moriana
- 33
- 0
Hi, I have just managed to make a PIC light blink. My question has to do with the syntax. In my code, I use a Delay subroutine that is called in between turning the LED on and turning it off.
I tried two codes. The first had my Delay subroutine being placed before my main code. The second had the subroutine placed after my main code (but before END). Only the second code worked. Why? What is the problem with the first method?
Below are the two codes I used:
************************************
1st METHOD
************************************
list p=16F690 ; defines chip that will be used
#include <p16F690.inc> ; imports instructions specific to PIC used
;================
;configuration
__CONFIG _CP_OFF & _CPD_OFF & _BOR_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _FCMEN_OFF & _IESO_OFF
;================
;declarations
org 1FFh ; reset
goto start ;
org 0 ;
count1 equ 20h ; general purpose registers used in the delay
counta equ 21h ;
countb equ 22h ;
;================
;subroutines
Init ; initialisation subroutine
clrf PORTA ; clears the port file registers
clrf PORTB ;
clrf PORTC ;
bsf STATUS,RP0 ; selects bank 1
bcf STATUS,RP1 ;
clrf TRISC ; sets all of portC to output
bcf STATUS,RP0 ; selects bank 0
bcf STATUS,RP1 ;
retlw 0
Delay movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1, f
goto d1
retlw 0
;================
;main code
start call Init ; calls the intitialization subroutine
main bsf PORTC,0 ; LED on
call Delay
bcf PORTC,0 ; LED off
call Delay
goto main ; infinite loop
END ; end of program
;================
************************************
2nd METHOD
************************************
list p=16F690 ; defines chip that will be used
#include <p16F690.inc> ; imports instructions specific to PIC used
;================
;configuration
__CONFIG _CP_OFF & _CPD_OFF & _BOR_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _FCMEN_OFF & _IESO_OFF
;================
;declarations
org 1FFh ; reset
goto start ;
org 0 ;
count1 equ 20h ; general purpose registers used in the delay
counta equ 21h ;
countb equ 22h ;
;================
;subroutines
Init ; initialisation subroutine
clrf PORTA ; clears the port file registers
clrf PORTB ;
clrf PORTC ;
bsf STATUS,RP0 ; selects bank 1
bcf STATUS,RP1 ;
clrf TRISC ; sets all of portC to output
bcf STATUS,RP0 ; selects bank 0
bcf STATUS,RP1 ;
retlw 0
;================
;main code
start call Init ; calls the intitialization subroutine
main bsf PORTC,0 ; LED on
call Delay
bcf PORTC,0 ; LED off
call Delay
goto main ; infinite loop
Delay movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1, f
goto d1
retlw 0
END ; end of program
;================
I tried two codes. The first had my Delay subroutine being placed before my main code. The second had the subroutine placed after my main code (but before END). Only the second code worked. Why? What is the problem with the first method?
Below are the two codes I used:
************************************
1st METHOD
************************************
list p=16F690 ; defines chip that will be used
#include <p16F690.inc> ; imports instructions specific to PIC used
;================
;configuration
__CONFIG _CP_OFF & _CPD_OFF & _BOR_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _FCMEN_OFF & _IESO_OFF
;================
;declarations
org 1FFh ; reset
goto start ;
org 0 ;
count1 equ 20h ; general purpose registers used in the delay
counta equ 21h ;
countb equ 22h ;
;================
;subroutines
Init ; initialisation subroutine
clrf PORTA ; clears the port file registers
clrf PORTB ;
clrf PORTC ;
bsf STATUS,RP0 ; selects bank 1
bcf STATUS,RP1 ;
clrf TRISC ; sets all of portC to output
bcf STATUS,RP0 ; selects bank 0
bcf STATUS,RP1 ;
retlw 0
Delay movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1, f
goto d1
retlw 0
;================
;main code
start call Init ; calls the intitialization subroutine
main bsf PORTC,0 ; LED on
call Delay
bcf PORTC,0 ; LED off
call Delay
goto main ; infinite loop
END ; end of program
;================
************************************
2nd METHOD
************************************
list p=16F690 ; defines chip that will be used
#include <p16F690.inc> ; imports instructions specific to PIC used
;================
;configuration
__CONFIG _CP_OFF & _CPD_OFF & _BOR_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _FCMEN_OFF & _IESO_OFF
;================
;declarations
org 1FFh ; reset
goto start ;
org 0 ;
count1 equ 20h ; general purpose registers used in the delay
counta equ 21h ;
countb equ 22h ;
;================
;subroutines
Init ; initialisation subroutine
clrf PORTA ; clears the port file registers
clrf PORTB ;
clrf PORTC ;
bsf STATUS,RP0 ; selects bank 1
bcf STATUS,RP1 ;
clrf TRISC ; sets all of portC to output
bcf STATUS,RP0 ; selects bank 0
bcf STATUS,RP1 ;
retlw 0
;================
;main code
start call Init ; calls the intitialization subroutine
main bsf PORTC,0 ; LED on
call Delay
bcf PORTC,0 ; LED off
call Delay
goto main ; infinite loop
Delay movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1, f
goto d1
retlw 0
END ; end of program
;================