help me with using pic16f873

Thread Starter

sammymtech

Joined Sep 19, 2010
2
hi i am mtech student. i tried to burn the the following program into pic using ezpic software during the burning process the led in the burner goes red and when i use the pic in the board it didnt work properly. i have tried to use pic16f873 simply to use as a 4bit remote control by initialising 4bits of the portb as input and other 4 bits as output. is there any problem in program??
here is the program

list p=16f873
include <p16f873.inc>
status equ 03h
portb equ 06h
trisb equ 86h
org 0x00
start
bcf status,6
bsf status,5
movlw 0fh
movwf trisb
bcf status,5
clrf portb
fir
btfss portb,0
goto sec
goto sw1
sec
btfss portb,1
goto thir
goto sw2
thir
btfss portb,2
goto fore
goto sw3
fore
btfss portb,3
goto fir
goto sw4
sw1
btfss portb,4
goto on_1
bcf portb,4
goto fir
on_1
bsf portb,4
goto fir
sw2
btfss portb,5
goto on_2
bcf portb,5
goto fir
on_2
bsf portb,5
goto fir
sw3
btfss portb,6
goto on_3
bcf portb,6
goto fir
on_3
bsf portb,6
goto fir
sw4
btfss portb,7
goto on_4
bcf portb,7
goto fir
on_4
bsf portb,7
goto fir
goto start
end
 

SgtWookie

Joined Jul 17, 2007
22,230
When posting source code, you should either attach it as a .txt file, or use the [ code] [ /code] blocks (no spaces) to preserve your formatting; otherwise it all gets left-justified.

I've re-formatted your code for you.

Rich (BB code):
	list p=16f873 
	include <p16f873.inc> 
	status equ 03h 
	portb equ  06h 
	trisb equ  86h 
	org 0x00 
start 
	bcf status,6 
	bsf status,5 
	movlw 0fh 
	movwf trisb 
	bcf status,5 
	clrf portb 
fir 
	btfss portb,0 
	goto sec 
	goto sw1 
sec 
	btfss portb,1 
	goto thir 
	goto sw2 
thir 
	btfss portb,2 
	goto fore 
	goto sw3 
fore 
	btfss portb,3 
	goto fir 
	goto sw4 
sw1 
	btfss portb,4 
	goto on_1 
	bcf portb,4 
	goto fir 
on_1 
	bsf portb,4 
	goto fir 
sw2 
	btfss portb,5 
	goto on_2 
	bcf portb,5 
	goto fir 
on_2 
	bsf portb,5 
	goto fir 
sw3 
	btfss portb,6 
	goto on_3 
	bcf portb,6 
	goto fir 
on_3 
	bsf portb,6 
	goto fir 
sw4 
	btfss portb,7 
	goto on_4 
	bcf portb,7 
	goto fir 
on_4 
	bsf portb,7 
	goto fir 
	goto start 
	end
 

JDT

Joined Feb 12, 2009
657
I have seen plenty of PIC code like this and it is very hard to know if it works or what's wrong with it if it doesn't.

Well written code has plenty of comments - possibly one on every line. Another good idea is to draw a detailed flowchart. In fact I usually start with a flowchart and then turn it into well commented code!
 

SgtWookie

Joined Jul 17, 2007
22,230
JDT echoes my sentiments exactly. Assembler is one of the most difficult languages to understand. Documenting what each line is supposed to do, will be a great help in determining what the problem is.
 

eblc1388

Joined Nov 28, 2008
1,542
Assembly mnemonic by itself is not difficult to understand.

However, the reason/logic why the writer choose to use one particular mnemonic would often take a long time for another person to figure out. This is assuming the software worked correctly and comments are there.

In the case when there are logical errors in codes without comments, e.g. setting bit6 instead of bit5, shifting left instead of shifting right etc... it is next to impossible to find as someone else often don't know what the writer is thinking at that particular moment.
 

Markd77

Joined Sep 7, 2009
2,806
Rich (BB code):
sw1
    btfss portb,4
    goto on_1
    bcf portb,4
    goto fir
on_1
    bsf portb,4
    goto fir
I think you should replace both "goto fir" with "goto sec" because otherwise it might not get to "sec". Make similar changes to the rest.
Unless that was the intention.
 
Top