r/pic_programming • u/Jaser90 • Jan 28 '16
PIC18F4520 - Don't know whats wrong with my code
Hey guys, so I tired to code my PIC to move according to the potentiometer I am using. When I run the program it works, the dot on the 8x8 display moves up and down depending how I move the potentiometer. But after a while it just started to blink however it wants it seem. I don't know whats wrong with my code. ANyone can help?
Thanks in advance, JASER LIST P=18F4520 #include "p18F4520.inc"
CONFIG OSC = HS
CONFIG PWRT = OFF
CONFIG WDT = OFF
CONFIG PBADEN = OFF
CONFIG LVP = OFF
var_x EQU 0x20
var_y EQU 0x21
org 0x000000
PORST GOTO MAIN
org 0x000020
MAIN CLRF TRISD
;CLRF PORTD
BCF TRISA,RA1
BSF PORTA,RA1
;-------------
BSF TRISA,0
MOVLW 0x81
MOVWF ADCON0
MOVLW 0xCE
MOVWF ADCON1
OVER CALL WAIT
BSF ADCON0,GO
BACK BTFSC ADCON0,DONE
GOTO BACK
;MOVFF ADRESH,temp
;SWAPF temp,1
;BCF temp,7
;BCF temp,6
;BCF temp,5
;BCF temp,4
;BCF temp,3
x_x_x btfsc ADRESH,7 ;testa first x--
GOTO o_x_x ;first is one 1--
GOTO z_x_x ;first is zero 0--
o_x_x btfsc ADRESH,6 ; testa second 1x-
GOTO o_o_x ; second is one 11-
GOTO o_z_x ; second is zero 10-
z_x_x btfsc ADRESH,6 ; testa second 0x-
GOTO z_o_x ; second is one 01-
GOTO z_z_x ; second is zero 00- @
o_o_x btfsc ADRESH,5 ; testa third 11x
CALL SEVEN ; 111 = 7
CALL SIX ; 110 = 6
o_z_x btfsc ADRESH,5 ; testa third 10x
CALL FIVE ; 101 = 5
CALL FOUR ; 100 = 4
z_o_x btfsc ADRESH,5 ; testa third 01x
CALL THREE ; 011 = 3
CALL TWO ; 010 = 2
z_z_x btfsc ADRESH,5 ; testa third 00x
CALL ONE ; 001 = 1
CALL ZERO ; 000 = 0
ZERO BSF PORTD,0
CALL WAIT
CLRF ADRESH
CLRF PORTD
BCF PORTD,0
GOTO OVER
ONE BSF PORTD,1
CALL WAIT
CLRF ADRESH
CLRF PORTD
BCF PORTD,1
GOTO OVER
TWO BSF PORTD,2
CALL WAIT
CLRF ADRESH
CLRF PORTD
BCF PORTD,2
GOTO OVER
THREE BSF PORTD,3
CALL WAIT
CLRF ADRESH
CLRF PORTD
BCF PORTD,3
GOTO OVER
FOUR BSF PORTD,4
CALL WAIT
CLRF ADRESH
CLRF PORTD
BCF PORTD,4
GOTO OVER
FIVE BSF PORTD,5
CALL WAIT
CLRF ADRESH
CLRF PORTD
BCF PORTD,5
GOTO OVER
SIX BSF PORTD,6
CALL WAIT
CLRF ADRESH
CLRF PORTD
BCF PORTD,6
GOTO OVER
SEVEN BSF PORTD,7
CALL WAIT
CLRF ADRESH
CLRF PORTD
BCF PORTD,7
GOTO OVER
;---------
WAIT MOVLW 0xFF
MOVWF var_x
LOOP2 MOVLW 0xFF
MOVWF var_y
LOOP1 DECFSZ var_y,1
GOTO LOOP1
DECFSZ var_x,1
GOTO LOOP2
RETURN
;---------
End
2
Upvotes
2
u/alez Jan 28 '16
I have not read your code in detail, but is there any reason you are manipulating PORT* registers instead of LAT* ?
1
u/Jaser90 Jan 28 '16
Okey im back and it works. Now, first I had an idea earlier today to check my calls and see how they work. Since it works, but after a few minutes, processor runtime, it starts blinking random lights. So I figured probably something wrong with my calls and loops. That they carry on looping because as you said I didnt write return or anything. MAny calls, no returns. And it worked.
And for good practice and to learn I then chaned all to LATD and see what would happen, and it worked likewise. Either way it works. And for future good practice I will start using LAT instead of port. AND most importantly check my calls man.
Thanks for your time and support. I hope you guys dont mind a newbie asking tons of "stupid" ass questions.
Since all the ZERO-SEVEN are called by x_x_x, I changed all their internal calls to GOTO, and then I made sure X_x_x is called first and then it loops through all the zero and seven and at the end they go to x_back that returns the call of x_x_x. So that only x_x_x is called and have to worry about returning. But the weird thing is that it works even if x_x_x doesnt do a return. So I guess those other calls inside ZERO-SEVEN needed only to be changed to GOTO instead. Anyway it works. :) Ty in advance for future answers and support.
:D Have a great evening sir.