Switching between functions

Status
Not open for further replies.
Hey there,

I finally made a small program that I could switch between routines by a touch of a button.

However it only works for 'ON' and 'OFF' only (or two routines). Other than this (more than 2 routines involved where I need to put the counter), when I put the increment counter, for the first routine, it runs upon the touch of button, but the second one locks up.

Here's the code. Switch is active low btw.

LED Ports are RB0-RB7 and switch port are RA0.

Code:
#include <p18F1320.inc>
    
    CONFIG WDT=OFF; disable watchdog timer
    CONFIG MCLRE = OFF; MCLEAR Pin off
    CONFIG DEBUG = OFF; Enable Debug Mode
    CONFIG LVP = OFF; Low-Voltage programming disabled (necessary for debugging)
    CONFIG OSC = INTIO2;Internal oscillator, port function on RA6 
    CONFIG PWRT = ON
    
    org 0; start code at 0

cblock 0x20        
Delay1 
Delay2
count
var1
endc    

Start:
    
    clrf    TRISA
    clrf    LATA

    clrf    LATB
    clrf    TRISB
    
    movlw    b'01100000'
    movwf    OSCCON
    
    movlw    b'01111111'
    movwf    ADCON1
    
    movlw    0xFF
    movwf    TRISA
    
    clrf    count
    clrf    var1
    
Main:

WaitKeyPress:
    btfsc    PORTA,RA0
    goto    WaitKeyPress
    call    Delay
    incf    var1
    
    movlw    d'1'
    cpfseq    var1
    goto    WaitKeyPress
    goto    TurnLeftLED
    
    movlw    d'2'
    cpfseq    var1
    goto    WaitKeyPress
    goto    TurnRightLED
    
    movlw    d'3'
    cpfseq    var1
    goto    WaitKeyPress
    goto    TurnOnAllLED
    
TurnLeftLED:
    movlw    b'00000001'
    movwf    LATB
    
    Loop1:
    rrncf    LATB
    call    Delay
    
    btfsc    PORTA,RA0
    goto    Loop1

    goto    WaitKeyPress
    goto    TurnLeftLED
    
TurnRightLED:
    movlw    b'10000000'
    movwf    LATB
    
    Loop2:
    rlncf    LATB
    call    Delay
    
    btfsc    PORTA,RA0
    goto    Loop2
    goto    WaitKeyPress
        
    goto    TurnRightLED

TurnOnAllLED:
    clrf    LATB
    
    Loop3:
    movlw    b'111111111'
    
    btfsc    PORTA,RA0
    goto    Loop3
    goto    WaitKeyPress
    
    goto    TurnOnAllLED
    
Delay:
;    movwf    Delay2    
DelayLoop:
    DECFSZ    Delay1,f ;Decrement Delay1 by 1, skip next instruction if Delay1 is 0 
    GOTO Delay
    DECFSZ      Delay2,f
    GOTO Delay
    return

end
 
Last edited:
Hi LT

Just looking at your code there are a couple of things that stand out -
your Turn Left Led - you turn on bit 0 then on the next insruction turn bit 0 off and bit 8 on ..?
Without doing the code for you, think you might need another longer delay, say 1 second, so it gives you chance to see the leds operating etc

Suggest you try running your code though the debugger Sim, use a breakpoint to get though the delay/s and then single step( F7) so you can watch your files and see whats happening.

If thats a bit too much for you at the moment - then print out your code and at the side of it write out a flow chart for each step, if you do that properly, then any error in you code usually hit you straights away.

hth

Richard
 

Oh, that's an LED sequencing program, it's like that: one button press -> row of LEDs turning left (using shift register) -> one button press -> row of LED turning right (same thing) -> one button press -> all LED's turned on and so on...
 
Hi LT,

Sorted that code yet ..?

Had chance to look at it a bit more - a couple of things may help.

I use this little routine to test a switch , it works well, with only a pull up resistor on the switch.

btfsc PORTA,4 ; key pressed? port normally high - low on push button
goto keyout ; if not on, go out

call delay50ms ; delay for 50ms the test again to see if still on
btfsc PORTA,4
goto keyout ; if not still on, go out

incf any_code ; key still on - proceed with key on routine


You say nothing happens after the second keystroke, clue - it does carry on counting..

You also seem to have 3 outcomes to your btfsc or cpfseq - only 2 outcomes possible.

cpfseq here ; if equal skip the next instruction
goto not_equal
incf is_equal


Still suggest you do a flow chart of your code or try the SIMulator - it really will help you see where you are going wrong.

Richard
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…