0
votes
    #include p16f88.inc


;----------memory data--------

program org 0x00
    goto    preparation

Preparation
    bsf status,RP0
    bsf trisb,trisb0    ; load port rb0 as input
    bcf trisb,TRISB4    ; load port rb4 as output   
    bcf status,RP0
    goto    begin
;---------------begin---------------
begin
    btfss   portb,rb0
    goto    begin   
    goto    on

on
    bsf portb,rb4
    goto    begin
    end

I mean I can do led on but i Can't do it off. What Must I add to do led off when I left to press the button ?

led

Best regards.

Edit : Now I add this to the code, but Led off When I push switch 3 or 4 times ?

program org 0x00
    goto    preparation

Preparation
    bsf status,RP0
    bsf trisb,trisb0    ; load port rb0 as input
    bcf trisb,TRISB4    ; load port rb4 as output   
    bcf status,RP0
    goto    begin
;---------------begin---------------
begin
    btfss   portb,rb0
    goto    begin   
    goto    on

on
    bsf portb,rb4
    btfsc   portb,rb0   /* <----------  I add this part*/
    goto    begin       /* <----------  for off the led*/
    bcf portb, rb4  /* <----------  again*/
    goto    begin
    end

Is it normal in proteus Or My code is ok ?

1
Why would it go off? What instruction in your code do you think is supposed to turn it off? - Ross Ridge
I ross. I add another part , but just off the led sometimes when I push 3 or 4 times the button. Is it proteus ? - NIN
I think you need to draw a flow chart, and also consider a way of debouncing your switch closure. - Weather Vane
Your closer, but your tests for a button press is wrong. A button press is actually the transition from button up to button down. You don't want the LED to change when the button is down because that means it will keep changing while the button is down, so fast you can't see it changing, like what is happening in your code. You want the LED to change when the button goes from up to down so it only happens once per push. - Ross Ridge
ty ross. I got it. But I was trying the whole day (since yesterday). Now more complex code: Press button=lighting on, Press button=lighting off - NIN

1 Answers

0
votes

Add a pull-down resistor to the switch input and try this:

program org 0x00
goto    preparation

Preparation
bsf status,RP0
bsf trisb,trisb0    ; load port rb0 as input
bcf trisb,TRISB4    ; load port rb4 as output   
bcf status,RP0
goto    begin
;---------------begin---------------
begin
btfss   portb,rb0
goto    begin   
goto    on

on
bsf     portb,rb4
btfsc   portb,rb0   
goto    on          ;don't go to begin but keep testing for the switch to be released
bcf     portb, rb4  
goto    begin
end