Hello!
I am brand new to this forum so please forgive any ignorance I display.
My instructor has given us a sample of PIC Assembly Code to evaluate and explain. I have never done any coding before and he encouraged us to use whatever resources we can to get the job done.
So, if anyone could provide any insight you would be helping this new student out tremendously.
Thanks in advance!
I am brand new to this forum so please forgive any ignorance I display.
My instructor has given us a sample of PIC Assembly Code to evaluate and explain. I have never done any coding before and he encouraged us to use whatever resources we can to get the job done.
So, if anyone could provide any insight you would be helping this new student out tremendously.
Thanks in advance!
Rich (BB code):
;Start here
;First, set the processor type
LIST P=16F84
;Second, set the configuration bits according to the datasheet
;Code protect off, power-up timer on, watchdog off, RC oscillator
__CONFIG H'3FFB'
;Next, declare some variables (copied)
PORTB EQU 6 ;
TRISB EQU 86H ;
OPTREG EQU 81H ;
STATUS EQU 3 ;
CARRY EQU 0 ;
RP0 EQU 5 ;
MSB EQU 3 ;
;Declare some new variables...according to the datasheet, user RAM starts at address 0x0C
W_TEMP EQU 0x0C ;
STATUS_TEMP EQU 0x0D ;
TEMP1 EQU 0x0E ;
TEMP2 EQU 0x0F ;
SECH EQU 0x10 ;
SECL EQU 0x11 ;
T0OVFLCNT EQU 0x12 ;
TMR0 EQU 0x01 ;
INTCON EQU 0x0B ;
T0IF EQU 2 ;
T0IE EQU 5 ;
GIE EQU 7 ;
;Here, a macro will be declared so that in the code, the same instructions won't need to be typed over
;and over to complete
;this function. Instead, wherever SUB1616 is located in the code, when the code is compiled, all these instructions
;will be inserted in that spot of the code.
;This macro does A1:A2 - B1:B2, only to check whether or not A1:A2 is >= B1:B2
SUB1616 macro A1,A2,B1,B2
MOVF B2,0 ;
SUBWF A2,0 ;
MOVF B1,0 ;
BTFSS STATUS,CARRY ;
INCFSZ B1,0 ;
GOTO $ + 3 ;
BCF STATUS,CARRY ;
GOTO $ + 2 ;
SUBWF A1,0 ;
endm
;An interrupt will be used, so the main code address and the interrupt code address must be declared
;According to the datasheet, main code goes at 0x0000, and interrupt code goes at 0x0004
ORG 0x0000
GOTO MAIN
ORG 0x0004
MOVWF W_TEMP ;
MOVF STATUS,0
MOVWF STATUS_TEMP ;
INT_TMR0 BCF INTCON,T0IF ;
INCF T0OVFLCNT,1 ;
BTFSS T0OVFLCNT,4 ;
GOTO INT_TMR0END ;
CLRF T0OVFLCNT ;
INCFSZ SECL,1 ;
GOTO INT_TMR0END ;
INCF SECH,1 ;
GOTO INT_TMR0END ;
INT_TMR0END MOVF STATUS_TEMP,0 ;
MOVWF STATUS ;
SWAPF W_TEMP,1
SWAPF W_TEMP,0 ;
RETFIE
MAIN CLRF PORTB ;
CLRF STATUS ;
CLRF W_TEMP
CLRF STATUS_TEMP
CLRF TEMP1
CLRF TEMP2
CLRF SECH
CLRF SECL
CLRF T0OVFLCNT
CLRF TMR0
CLRF INTCON
BSF STATUS,RP0 ;
CLRF TRISB ;
MOVLW 0x07
MOVWF OPTREG ;
BCF STATUS,RP0 ;
CLRF TMR0 ;
BCF INTCON,T0IF ;
BSF INTCON,T0IE ;
BSF INTCON,GIE ;
BSF PORTB,0 ;
BCF STATUS,CARRY ;
A MOVLW 0x00 ;
MOVWF TEMP1 ;
MOVLW 0x02
MOVWF TEMP2 ;
BCF INTCON,GIE ;
SUB1616 SECH,SECL,TEMP1,TEMP2 ;
BSF INTCON,GIE ;
BTFSS STATUS,CARRY ;
GOTO A ;
BCF PORTB,0 ;
B MOVLW 0x00 ;
MOVWF TEMP1 ;
MOVLW 0x3E
MOVWF TEMP2 ;6
BCF INTCON,GIE ;
SUB1616 SECH,SECL,TEMP1,TEMP2 ;
BSF INTCON,GIE ;
BTFSS STATUS,CARRY ;
GOTO B ;
BSF PORTB,1 ;
C MOVLW 0x00 ;
MOVWF TEMP1 ;
MOVLW 0x7A
MOVWF TEMP2 ;
BCF INTCON,GIE ;
SUB1616 SECH,SECL,TEMP1,TEMP2 ;
BSF INTCON,GIE ;
BTFSS STATUS,CARRY ;
GOTO C ;
BSF PORTB,2 ;
D MOVLW 0x00 ;
MOVWF TEMP1 ;
MOVLW 0xB6
MOVWF TEMP2 ;
BCF INTCON,GIE ;
SUB1616 SECH,SECL,TEMP1,TEMP2 ;
BSF INTCON,GIE ;
BTFSS STATUS,CARRY ;
GOTO D ;
BSF PORTB,3 ;
E MOVLW 0x01 ;
MOVWF TEMP1 ;
MOVLW 0x2E
MOVWF TEMP2
BCF INTCON,GIE ;
SUB1616 SECH,SECL,TEMP1,TEMP2 ;
BSF INTCON,GIE ;
BTFSS STATUS,CARRY ;
GOTO E ;
BSF PORTB,4 ;
BCF INTCON,GIE ;
BCF INTCON,T0IE ;
CLRF TMR0 ;
BCF INTCON,T0IF ;
GOTO E ;
END