Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help with commenting on my code

Status
Not open for further replies.

Darkstar3000

New Member
So I have this question

Write an assembly language program for PIC 16F684 to switch ON LEDs to count in binary from 0 to 7. Allow a delay of about 1second between the change.Write the program, verify its operation by executing it

I wrote the program but I'm not sure if it does the job correctly since I'm not really comfortable with assembly language so can anyone take a look at it and tell me if it's doing what the question specifies and if my comments are correct. I can't test it out as I'm currently on vacation so I just need a verification of my work.

I'm using MPLAB V8.84

Thanks in advance.
Code:
; WRITTEN BY            
; DATE                  28/04/12
; FILE SAVED AS         BinaryCounter.asm
; DEVICE                PIC16F684
; FUNCTION				PROGRAM COUNTS FROM 0 TO 7 IN BINARY
; -----------------------   EQUATES    ------------------------------------
PORTA   EQU     05h    ;LABELS THE PORTA REGISTER AS "PORTA"
;DELAY COUNTERS 
COUNT1 EQU 20h;
COUNT2 EQU 21h

;------------------------ CONSTANTS -----------------------------------------
;IDENTIFIES CONSTANTS FOR REQUIRED LEDS
;I/O for LEDS
#define TRIS_D0_D1	B'00001111'	; TRISIO SETTING FOR D0 AND D1
#define TRIS_D2_D3	B'00101011'	; TRISIO SETTING FOR D2 AND D3

; the LEDS
#define D0	B'00010000'		; D0 LED
#define D1	B'00100000'		; D1 LED
#define D2	B'00010000'		; D2 LED
#define D3	B'00000100'		; D3 LED

; ----------------------- MAIN PROGRAM ------------------------------------
START   ORG     0X00    ;'ORG' SPECIFIES THE MEMORY LOCATION OF THE PROGRAM

; LED 0 ON
LOOP0
        MOVLW   TRIS_D0_D1;		;PUT 00001111 INTO W
        TRIS    PORTA   		;
        CLRF    PORTA			;CLEAR THE CONTENTS OF PORTA
        MOVLW   D0      		;PUT 00010000 INTO W
        MOVWF   PORTA   		;MOVE W ONTO PORTA 
		
		;Delay:
		decfsz    COUNT1,1       ;DECREASE THE VALUE OF COUNT1 BY 1
								 ;AND SKIP NEXT LINE IF RESULT IS ZERO
        goto      LOOP0          ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								 ;GO TO LOOP0	
        decfsz    COUNT2,1       ;DECRESE THE VALUE OF COUNT2 BY 1
								 ;AND SKIP NEXT LINE IF RESULT IT ZERO	
        goto      LOOP0          ;IF COUNT2 IS ZERO, CARRY ON OTHERWISE
								 ;GO TO LOOP0


; LED 1 ON
LOOP1
        CLRF    PORTA;			;CLEAR THE CONTENTS OF PORTA
		MOVLW   TRIS_D0_D1;     ;PUT 00001111 INTO W	
        TRIS    PORTA   		; 
        CLRF    PORTA   		;CLEAR THE CONTENTS OF PORTA
        MOVLW   D1   			;PUT 00100000 INTO W
        MOVWF   PORTA  			;MOVE W ONTO PORTA
		
		;Delay:
		decfsz    COUNT1,1      ;DECREASE THE VALUE OF COUNT1 BY 1
								;AND SKIP THE NEXT LINE IF RESULT IS ZERO
        goto      LOOP1         ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP1
        decfsz    COUNT2,1      ;DECREASE THE VALUE OF COUNT2 BY 1 
        goto      LOOP1         ;GO TO LOOP1

; LED 1 & 2 ON
LOOP2
        ;1
        CLRF    PORTA;		    ;CLEAR THE CONTENTS OF PORTA 
		MOVLW   TRIS_D0_D1		;PUT 00001111 INTO W
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR THE CONTENTS OF PORTA
        MOVLW   D1   			;PUT 00100000 INTO W	
        MOVWF   PORTA  			;MOVE W ONTO PORTA 
		
		;2
		CLRF    PORTA		    ;CLEAR THE CONTENTS OF PORTA
		MOVLW   TRIS_D2_D3		;PUT 00101011 INTO W
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR THE CONTENTS OF PORTA
        MOVLW   D2   			;PUT 00010000 INTO W
        MOVWF   PORTA  			;MOVE W ONTO PORTA 
		
		;Delay:
		decfsz    COUNT1,1		;DECREASE THE VALUE OF COUNT1 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO
        goto      LOOP2         ;IF COUNT IS ZERO,CARRY ON OTHERWISE 
								;GO TO LOOP 2 
        decfsz    COUNT2,1      ;DECRESE THE VALUE OF COUNT2 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO 	
        goto      LOOP2         ;IF COUNT2 IS ZERO, CARRY ON OTHERWISE 
								;GO TO LOOP2
; LED 3 ON
LOOP3

        CLRF    PORTA		    ;CLEAR THE CONTENTS OF PORTA
		MOVLW   TRIS_D2_D3;		;PUT 00101011 INTO W
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR THE CONTENTS OF PORTA
        MOVLW   D3   			;PUT 00000100 INTO W 
        MOVWF   PORTA  			;MOVE W ONTO PORTA
		
		;Delay:
		decfsz    COUNT1,1       ;DECREASE THE VALUE OF COUNT1 BY 1
								 ;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO	
        goto      LOOP3          ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								 ;GO TO LOOP3
        decfsz    COUNT2,1       ;DECREASE THE VALUE OF COUNT2 BY 1 
								 ;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO
        goto      LOOP3        	 ;IF COUNT2 IS ZERO, CARRY ON OTHERWISE
								 ;GO TO LOOP3 
		
; LED 3 & 1 ON
LOOP4

        CLRF    PORTA		    ;CLEAR THE CONTENTS OF PORTA
		MOVLW   TRIS_D2_D3;		;PUT 00101011 INTO W		
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR THE CONTENTS OF PORTA
        MOVLW   D3   			;PUT 00000100 INTO W
        MOVWF   PORTA 			;MOVE W TO PORTA 
		
		CLRF    PORTA				;CLEAR THE CONTENTS OF PORTA
		MOVLW   TRIS_D0_D1		;PUT 00001111 INTO W
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR THE CONTENTS OF PORTA
        MOVLW   D1  			;PUT 00010000 INTO W
        MOVWF   PORTA 			;MOVE W ONTO PORTA
		
		;Delay:
		decfsz    COUNT1,1      ;DECREASE THE VALUE OF COUNT1 BY 1 
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO	
        goto      LOOP4         ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP4
        decfsz    COUNT2,1      ;DECREASE THE VALUE OF COUNT2 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO
        goto      LOOP4         ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP4

; LED 3 & 2 ON		
LOOP5
        CLRF    PORTA			;CLEAR THE CONTENTS OF PORTA				
		MOVLW   TRIS_D2_D3		;PUT 00101011 INTO W	
        TRIS    PORTA   		;	
        CLRF    PORTA   		;CLEAR THE CONTENTS OF PORTA
        MOVLW   D3   			;PUT 00000100 INTO W
        MOVWF   PORTA 			;MOVE W TO PORTA
		
		CLRF    PORTA			;CLEAR THE CONTENTS OF PORTA
		MOVLW   TRIS_D2_D3;		;PUT 00101011 INTO W 
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR THE CONTENTS OF PORTA
        MOVLW   D2  			;00010000
        MOVWF   PORTA 			;MOVE W TO PORTA 
		
		;Delay:
		decfsz    COUNT1,1      ;DECREASE THE VALUE OF COUNT1 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO
        goto      LOOP5         ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP5 
        decfsz    COUNT2,1      ;DECREASE THE VALUE OF COUNT2 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO 	
        goto      LOOP5        	;IF COUNT2 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP5

; LED 3 & 2 & 1
LOOP6
     
	    CLRF    PORTA			;CLEAR THE CONTENTS OF PORTA
		MOVLW   TRIS_D2_D3		;PUT 00101011 INTO W
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR THE CONTENTS OF PORTA
        MOVLW   D3   			;PUT 00000100 INTO W
        MOVWF   PORTA 			;MOVE W TO PORTA
		
		CLRF    PORTA			;CLEAR THE CONTENTS OF PORTA
		MOVLW   TRIS_D2_D3		;PUT 00101011 INTO W
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR THE CONTENTS OF PORTA
        MOVLW   D2  			;PUT 00010000 INTO W
        MOVWF   PORTA 			;MOVE W TO PORTA 
		
		CLRF    PORTA			;CLEAR CONTENTS OF PORTA	
		MOVLW   TRIS_D0_D1		;PUT 00001111 INTO W	 
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR CONTENTS OF PORTA
        MOVLW   D1  			;PUT 00010000 INTO W
        MOVWF   PORTA 			;MOVE W TO PORTA 
		
		;Delay:
		decfsz    COUNT1,1      ;DECREASE THE VALUE OF COUNT1 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO 
        goto      LOOP6         ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP6
        decfsz    COUNT2,1      ;DECREASE THE VALUE OF COUNT2 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO 	
        goto      LOOP6         ;IF COUNT2 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP6

		

        END

Here's a link to the asm if the code is messy **broken link removed**
 
Last edited:
You are creating INPUTS:

MOVLW TRIS_D0_D1

PUT 00001111 INTO W


You need much better documentation.
Don't just comment about WHAT the instruction is doing but WHY it is being done
 
Last edited:
You are creating INPUTS:

MOVLW TRIS_D0_D1

PUT 00001111 INTO W


You need much better documentation.
Don't just comment about WHAT the instruction is doing but WHY it is being done

Alright, that makes sense but I pieced this program together from samples given to us throughout the weeks so I'm not really sure what everything is doing. That's why my comments lack detail.
 
Here's a revised version with hopefully better comments

Code:
; WRITTEN BY            
; DATE                  28/04/12
; FILE SAVED AS         BinaryCounter.asm
; DEVICE                PIC16F684
; FUNCTION				PROGRAM COUNTS FROM 0 TO 7 IN A BINARY SEQUENCE USING LEDS 
; -----------------------   EQUATES    ------------------------------------
PORTA   EQU     05h    ;LABELS THE PORTA REGISTER AS "PORTA"
;DELAY COUNTERS TO COUNT DOWN A DELAY
COUNT1 EQU 20h;
COUNT2 EQU 21h

;------------------------ CONSTANTS -----------------------------------------
;IDENTIFIES CONSTANTS FOR REQUIRED LEDS
;I/O for LEDS
#define TRIS_D0_D1	B'00001111'	; TRISIO SETTING FOR D0 AND D1
#define TRIS_D2_D3	B'00101011'	; TRISIO SETTING FOR D2 AND D3

; the LEDS
#define D0	B'00010000'		; D0 LED
#define D1	B'00100000'		; D1 LED
#define D2	B'00010000'		; D2 LED


; ----------------------- START OF PROGRAM ------------------------------------
START   ORG     0X00    ;'ORG' SPECIFIES THE MEMORY LOCATION OF THE PROGRAM

; LED 0 ON
LOOP0
        MOVLW   TRIS_D0_D1;		;MOVE PREDEFINED VALUE TO TRISA TO SWITCH ON LED0
        TRIS    PORTA   		;
        CLRF    PORTA			;CLEAR ALL OUTPUTS 
        MOVLW   D0      		;ASSIGN THE VALUE OF D0 TO W 
        MOVWF   PORTA   		;MOVE PREDEFINED VALUE TO PORTA TO SWITCH ON LED0
		
		;Delay:
		decfsz    COUNT1,1       ;DECREASE THE VALUE OF COUNT1 BY 1
								 ;AND SKIP NEXT LINE IF RESULT IS ZERO
        goto      LOOP0          ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								 ;GO TO LOOP0	
        decfsz    COUNT2,1       ;DECRESE THE VALUE OF COUNT2 BY 1
								 ;AND SKIP NEXT LINE IF RESULT IT ZERO	
        goto      LOOP0          ;IF COUNT2 IS ZERO, CARRY ON OTHERWISE
								 ;GO TO LOOP0


; LED 1 ON
LOOP1
        CLRF    PORTA;			;CLEAR ALL OUTPUTS
		MOVLW   TRIS_D0_D1;     ;MOVE PREDEFINED VALUE TO TRISA TO SWITCH ON LED1	
        TRIS    PORTA   		; 
        CLRF    PORTA   		;CLEAR ALL OUTPUTS 
        MOVLW   D1   			;ASSIGN THE VALUE OF D1 TO W 
        MOVWF   PORTA  			;MOVE PREDEFINE VALUE TO PORTA TO SWITCH ON LED1
		
		;Delay:
		decfsz    COUNT1,1      ;DECREASE THE VALUE OF COUNT1 BY 1
								;AND SKIP THE NEXT LINE IF RESULT IS ZERO
        goto      LOOP1         ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP1
        decfsz    COUNT2,1      ;DECREASE THE VALUE OF COUNT2 BY 1 
        goto      LOOP1         ;GO TO LOOP1

; LED 0 & 1 ON
LOOP2
        ;0
        CLRF    PORTA;		    ;CLEAR ALL OUTPUTS 
		MOVLW   TRIS_D0_D1		;MOVE PREDEFINED VALUE TO TRISA TO SWITCH ON LED0
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR ALL OUTPUTS
        MOVLW   D0   			;ASSSIGN THE VALUE OF D0 TO W 	
        MOVWF   PORTA  			;MOVE PREDEFINE VALUE TO PORTA TO SWITCH ON LED0
		
		;1
		CLRF    PORTA		    ;CLEAR ALL OUTPUTS
		MOVLW   TRIS_D0_D1		;MOVE PREDEFINED VALUE TO TRISA TO SWITCH ON LED1
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR ALL OUTPUTS
        MOVLW   D1   			;ASSIGN THE VALUE OF D0 TO W
        MOVWF   PORTA  			;MOVE PREDEFINED VALUE TO PORTA TO SWITCH ON LED1
		
		;Delay:
		decfsz    COUNT1,1		;DECREASE THE VALUE OF COUNT1 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO
        goto      LOOP2         ;IF COUNT IS ZERO,CARRY ON OTHERWISE 
								;GO TO LOOP 2 
        decfsz    COUNT2,1      ;DECRESE THE VALUE OF COUNT2 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO 	
        goto      LOOP2         ;IF COUNT2 IS ZERO, CARRY ON OTHERWISE 
								;GO TO LOOP2
; LED 2 ON
LOOP3

        CLRF    PORTA		    ;CLEAR ALL OUTPUTS
		MOVLW   TRIS_D2_D3;		;MOVE PREDEFINED VALUE TO TRISA TO SWITCH ON LED2
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR ALL OUTPUTS
        MOVLW   D2   			;ASSIGN THE VALUE OF D2 TO W 
        MOVWF   PORTA  			;MOVE PREDEFINED VALUE TO PORTA TO SWITH ON LED2
		
		;Delay:
		decfsz    COUNT1,1       ;DECREASE THE VALUE OF COUNT1 BY 1
								 ;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO	
        goto      LOOP3          ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								 ;GO TO LOOP3
        decfsz    COUNT2,1       ;DECREASE THE VALUE OF COUNT2 BY 1 
								 ;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO
        goto      LOOP3        	 ;IF COUNT2 IS ZERO, CARRY ON OTHERWISE
								 ;GO TO LOOP3 
		
; LED 0 & 2 ON
LOOP4
		CLRF    PORTA			;CLEAR ALL OUTPUTS
		MOVLW   TRIS_D0_D1		;MOVE PREDEFINED VALUE TO TRISA TO SWITCH ON LED0
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR ALL OUTPUTS
        MOVLW   D0  			;ASSSIGN THE VALUE OF D0 TO W 
        MOVWF   PORTA 			;MOVE PREDEFINED VALUE TO PORTA TO SWITCH ON LED0		



        CLRF    PORTA		    ;CLEAR ALL OUTPUTS
		MOVLW   TRIS_D2_D3;		;MOVE PREDEFINED VALUE TO TRISA TO SWITCH ON LED2		
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR ALL OUTPUTS
        MOVLW   D2   			;ASSIGN THE VALUE OF D2 TO W 
        MOVWF   PORTA 			;MOVE PREDEFINED VALUE TO PORTA TO SWITCH ON LED2 
		
		
		
		;Delay:
		decfsz    COUNT1,1      ;DECREASE THE VALUE OF COUNT1 BY 1 
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO	
        goto      LOOP4         ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP4
        decfsz    COUNT2,1      ;DECREASE THE VALUE OF COUNT2 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO
        goto      LOOP4         ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP4

; LED 1 & 2 ON		
LOOP5
        CLRF    PORTA			;CLEAR ALL OUTPUTS				
		MOVLW   TRIS_D0_D1		;MOVE PREDEFINED VALUE TO TRISA TO SWITCH ON LED1	
        TRIS    PORTA   		;	
        CLRF    PORTA   		;CLEAR ALL OUTPUTS
        MOVLW   D1  			;ASSIGN THE VALUE OF D1 TO W 
        MOVWF   PORTA 			;MOVE PREDEFINED VALUE TO PORTA TO SWITCH ON LED1
		
		CLRF    PORTA			;CLEAR ALL OUTPUTS
		MOVLW   TRIS_D2_D3;		;MOVE PREDEFINED VALUE TO TRISA TO SWITCH ON LED2
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR ALL OUTPUTS
        MOVLW   D2  			;ASSIGN THE VALUE OF D2 TO W 
        MOVWF   PORTA 			;MOVE PREDEFINED VALUE TO PORTA TO SWITCH ON LED2
		
		;Delay:
		decfsz    COUNT1,1      ;DECREASE THE VALUE OF COUNT1 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO
        goto      LOOP5         ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP5 
        decfsz    COUNT2,1      ;DECREASE THE VALUE OF COUNT2 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO 	
        goto      LOOP5        	;IF COUNT2 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP5

; LED 0 & 1 & 2 ON
LOOP6
     
	    CLRF    PORTA			;CLEAR ALL OUTPUTS
		MOVLW   TRIS_D0_D1		;MOVE PREDEFINE VALUE TO TRISA TO SWITCH ON LED0
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR ALL OUTPUTS
        MOVLW   D0   			;ASSING THE VALUE OF D0 TO W
        MOVWF   PORTA 			;MOVE PREDEFINED VALUE TO PORTA TO SWITCH ON LED0
		
		CLRF    PORTA			;CLEAR ALL OUTPUTS 
		MOVLW   TRIS_D0_D1		;MOVE PREDEFINED VALUE TO TRISA TO SWITCH ON LED1
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR ALL OUTPUTS
        MOVLW   D1  			;ASSIGN THE VALUE OF D1 TO W 
        MOVWF   PORTA 			;MOVE PREDEFINED VALUE TO PORTA TO SWITCH ON LED1 
		
		CLRF    PORTA			;CLEAR ALL OUTPUTS
		MOVLW   TRIS_D2_D3		;MOVE PREDEFINED VALUE TO TRISA TO SWITCH ON LED2	 
        TRIS    PORTA   		;
        CLRF    PORTA   		;CLEAR ALL OUTPUTS
        MOVLW   D2  			;ASSING THE VALUE OF D2 TO W
        MOVWF   PORTA 			;MOVE PREDEFINED VALUE TO PORTA TO SWITCH ON LED2
		
		;Delay:
		decfsz    COUNT1,1      ;DECREASE THE VALUE OF COUNT1 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO 
        goto      LOOP6         ;IF COUNT1 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP6
        decfsz    COUNT2,1      ;DECREASE THE VALUE OF COUNT2 BY 1
								;AND SKIP THE NEXT LINE IF THE RESULT IS ZERO 	
        goto      LOOP6         ;IF COUNT2 IS ZERO, CARRY ON OTHERWISE
								;GO TO LOOP6

		

        END


and the link to the asm **broken link removed**
 
Homework?

I hope this helps you. It is commented and somewhat simpler than your approach.;)

Code:
;**********************************************************************
;                                                                     *
;    Files Required: P16F84.INC                                      *
;                                                                     *

	list		p=16f84		; list directive to define processor
	#include	"P16F84.inc"		; processor specific variable definitions

	
	__CONFIG    _PWRTE_ON  & _WDT_OFF &_HS_OSC  & _CP_OFF ;HS oscillator active.

	

	;****************GPR declarations****************		; note Bank0 last 16bytes,0x70 to 0x7f, visible in all banks as local last 16.
	COUNT0		EQU  	0x20 	; 
	COUNT1		EQU	0x21
	Number		EQU	0x22 ; temp use.
	
	
	Org	0x000				; processor reset vector
 	goto	Main			; go to beginning of program
 	
	
Main;
	;@ an osc clock of 4Mhz, the instruction clock = 1Mhz.
	; 1 sec delay loop requires 1 million instructions
	; A 15 instruction loop run 256 * 256 times will yield approx 1 sec delay.
	;**********************;
	;**Initialize variables**;
	clrf COUNT0
	clrf COUNT1
	clrf  Number
	
	;Setup PORT I/O********
	banksel TRISA; select PORTs i/o setup (bank1)
	clrf TRISA; make PORTA all outputs
	banksel PORTA; select PORTA (bank0)again
	
	; Start binary count
Bcount; increment 3 LED display driven by PORTA,0,1,2 to count in binary to 7 continously
	movf Number,w; get binary number from GPR into W register
	andlw 7; mask number to limit to 7 maximum, 7 = binary 00000111, result in w register
	movwf PORTA; move binary number into PORTA: PORTA,0,1,2 pins will drive the 3 LEDS counting binary to 7.
	incf Number,f; increment binary Number variable GPR by 1
	call Delay0; call a 1 second delay subroutine
	goto Bcount; continuous loop.
	
Delay0; this section consumes 3 inst. * 256 = 768 inst. = .000768 sec , negligible amt.
	incf COUNT0,f
	skpnz
	return
Delay1; 15 cycles *256*256 =  .98304 sec. approx 1 sec.
	incf COUNT1,f; 1 inst cycle
	goto $+1; goto next program line= 2 inst cycles
	goto $+1;"
	goto $+1;"
	goto $+1;"
	goto $+1;"
	nop; 1 inst cycle
	skpnz; 1 inst cycle
	goto Delay0; 2 inst cycles
	goto Delay1 ; 2 inst cycles
	
	END; eof
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top