Assembly Code Help Needed

Thread Starter

mobiusmorte

Joined Aug 22, 2014
2
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! :D

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
 

jpanhalt

Joined Jan 18, 2008
11,087
Suggestions for solving the homework:

1) Add meaningful comments. You may need to start with each line and say what it does. For something like "CLRF PORTB" , that alone may not be very meaningful in the context of the whole program; however, after the instruction set becomes second nature to you, you will be able to look at several lines of code (e.g., think of it as a paragraph) and write a comment to describe what those lines do.

Once you get the comments, the flow of the program should be more apparent.

2) I have found flow charting a complex program to be helpful and use Lucid Chart (free version): https://www.lucidchart.com
Some people don't find flow charting helpful -- it's a personal choice. In my case, I had been using flow charting for many years before I even knew what a "PIC" was, so using it for writing code just came naturally.

John
 

MaxHeadRoom

Joined Jul 18, 2013
28,698
The first thing to do is go to the Picmicro site and download the 16F84 manual and look up details of the particular functions and also refer to the instruction set.
Also for laying out the code I prefer to place a Colon at routine positions it makes it easier when searching.
As so.

Rich (BB code):
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
You also may learn something from this http://www.winpicprog.co.uk/pic_tutorial.htm
Max.
 

joeyd999

Joined Jun 6, 2011
5,287
My condolences, mobiusmorte.

This is an embarrassingly awful snippet of code. Now I know where bad programmers come from...

In any case, you'll need the data sheet for the 16F84. Go line by line, and work out what each line does. It will be helpful to use the simulator in MPLAB -- use the watch window to see how each instruction operates.

Hopefully, you'll get a feel for what's going on in the '84. But, IMHO, you are not learning good coding practices...
 

Thread Starter

mobiusmorte

Joined Aug 22, 2014
2
Thank you to all the suggestions, I have a plan of action now. Hopefully I will be able to help others in the future in return.
 
Top