• AVR Freaks

AnsweredHot!PIC18F2431 I can't get btfss to work properly

Page: 1234 > Showing page 1 of 4
Author
BertKu
mr
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2015/09/16 10:03:22
  • Location: 0
  • Status: offline
2020/11/03 02:18:44 (permalink)
0

PIC18F2431 I can't get btfss to work properly

I wrote a small program for my gate motor and am baffled that I had it first working beautiful and suddenly btfss instruction does not skip when set anymore. I have gone into circles and must have overlooked something like the correct bank.But I am now worried that for software I wrote for customers, this also may happen suddenly. What did I overlook?
include "P18F2431.INC"
; list P=P18F2431
; date : 26-10-2020
; Author Bert for;
; file name Gate-Motor.asm

#define OPEN          PORTB,5 ; switch checking for open Gate and contact is = set
#define CLOSED      PORTB,3 ; switch checking for closed Gate = contact is set
#define SWITCH      PORTB,1 ; start of process switch from Receiver
;                                          ; receiver wil only make contact for a short period,
;                                          ; only to start the process.
#define LED1           PORTB,0 ; red LED 820 Ohm
#define FORWARD   PORTC,6 ; motor closing the Gate
#define BACKWARD PORTC,4 ; motor openening the Gate
      cnt      equ 40h
      cnt1    equ 41h
      cnt2    equ 42h
; ------------------------------------------------------------------------------
     org      0x000            ; startup vector
Initialize
     clrf       PORTB
    movlw   B'00101010' ; 3 input and the rest outputs
    movwf  TRISB
    clrf       PORTC
    movlw  B'00000000' ; all outputs to motors
    movwf TRISC
    bcf      LED1            ; if LED is set to on,program is running
Loop
    bcf      OPEN            ; ensure that motor cannot start
    bcf      CLOSED        ; ensure that motor cannot start
    bcf      SWITCH        ; ensure that process cannot start
    bcf       FORWARD    ; ensure that motor cannot start
    bcf       BACKWARD  ; ensure that motor cannot start
Loop1
    movlw  0x01
    movwf cnt
next1
    decfsz cnt
    goto  next1
    btfss  SWITCH ; check whether the correct radiosignal is received
    goto  Loop1
    goto  checkgate
checkgate
    btfss  OPEN  ; Check whether the gate is open and contact made
    btfss  CLOSED  ; Check whether the gate is closed and contact made
    goto  opengate
    goto  closegate ; start opening the gate
opengate
    bsf  FORWARD ; motor is opening the gate
    movlw   0x01
    movwf   cnt
    movwf   cnt1
    movwf   cnt2
next2
    decfsz    cnt
    goto      next2
    decfsz   cnt1
    goto      next2
    decfsz   cnt2
    goto     next2
busyopening
    btfss    OPEN ; check untill gate is fully open
    goto    busyopening ; go back until gate is fully open and contact made
    bcf      FORWARD
    goto    Loop

Loop2
    movlw 0x01
    movwf cnt
    movwf cnt1
    movwf cnt2
next3
    decfsz   cnt
    goto  next3
   decfsz  cnt1
   goto   next3
   decfsz  cnt2
   goto   next3
   goto checkgate
closegate
   bsf BACKWARD ; motor is opening the gate
   movlw 0x01
   movwf cnt
   movwf cnt1
   movwf cnt2
next4
    decfsz   cnt
    goto    next4
    decfsz   cnt1
    goto     next4
    decfsz   cnt2
    goto    next4
 busyclosing
    btfss   CLOSED
    goto   busyclosing
    bcf     BACKWARD
    goto   Loop

      END
The program is continuously jumping back, although the contacts are made. Thank you If you could let me know what I do wrong and have been doing wrong on a number of customers small projects and seems to are working well.
 
I have modified the 2 days old program and added the spaces back in which were omitted during copying and also the original program did not have ,3 and ,5 that was added to show RIC is that what he meant. I should never have done that, as it has created confusion. 
No I have most likely another problem, most likely a technical problem which may interfere with my 20 MHz oscillator or whatever problem. I will figure it out and have appreciated your inputs. I am very pleased to realise that I dont have a problem with my overseas products. Thanks all. I need to attend my other urgent problems, I will figure it out what is happening. I have rewritten my program on my XP laptop, with battery power.  no interferences from other sources. Will let you know what the result was. Thanks All Bert
post edited by BertKu - 2020/11/05 02:50:43
#1
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 03:40:18 (permalink)
+1 (1)
Probably not your immediate problem, but don't write to PORT registers to set output pins.
Change
#define LED1 PORTB,0 ; red LED 820 Ohm
#define FORWARD PORTC,6 ; motor closing the Gate
#define BACKWARD PORTC,4 ; motor openening the Gate

to
#define LED1 LATB,0 ; red LED 820 Ohm
#define FORWARD LATC,6 ; motor closing the Gate
#define BACKWARD LATC,4 ; motor openening the Gate

 
You never mention which BTFSS is misbehaving, or how you know it is, but I can see a big problem with
#define OPEN PORTB,5 ; switch checking for open Gate and contact is = set
#define CLOSED PORTB,3 ; switch checking for closed Gate = contact is set

and
btfss OPEN,3 ; Check whether the gate is open and contact made
btfss CLOSED,5 ; Check whether the gate is closed and contact made

So, which pin is "OPEN" ?
RB5 or RB3?
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
BertKu
mr
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2015/09/16 10:03:22
  • Location: 0
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:00:06 (permalink)
0
thanks you for your reply Vexorg, My apology, it maybe confusing, but I have learned that if one has to look at it a few years later, it is then easier to understand what one has done 5 years back. It was all working beautiful , untill???
By copying the spaces disapeard, pleas read it as follow.
 
Loop
           bcf     OPEN ; ensure that motor cannot start
           bcf     CLOSED ; ensure that motor cannot start
           bcf SWITCH ; ensure that process cannot start
           bcf FORWARD ; ensure that motor cannot start
           bcf BACKWARD ; ensure that motor cannot start
Loop1
           movlw    0x01
           movwf    cnt
next1
           decfsz    cnt
           goto       next1
           btfss      SWITCH         ; check whether the correct radio signal is received
           goto      Loop1
           goto      checkgate
checkgate
           btfss      OPEN,3 ; Check whether the gate is open and contact made
           btfss      CLOSED,5 ; Check whether the gate is closed and contact made
           goto      opengate
           goto      closegate ; start opening the gate
opengate
           bsf        FORWARD ; motor is opening the gate
           movlw   0x01
etc etc. Yes indeed the loops are for giving the switches and motor time to start , as soon it works again I put FF in the delay. It is frustrating during test routine to click 256 times before I find out what is not working. Thus I change it always to 01\, I have now only to click once.Trust that thsi explains it. Bert
#3
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:07:06 (permalink)
0
Did you read my post Bert?
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
BertKu
mr
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2015/09/16 10:03:22
  • Location: 0
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:08:40 (permalink)
+2 (2)
My apology Ric, thank you for responding. It is my way of programming. OPEN means the switch which is made or going to be made when the gate has opened totally.   CLOSE means the gate switch which closes (make contact) when the gate is closed. All 3 btfss are suddenly no longer working. I then thought maybe the IC is gone faulty, thsu I use also a new brand new PIC18F2431,  still the same problem. I tried to select the bank wherein the btfss is kept, but maybe I have made also a mistake in doing the bank select correctly ( I took it out again) as it also did not work. Bert
 
#5
BertKu
mr
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2015/09/16 10:03:22
  • Location: 0
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:10:53 (permalink)
+1 (1)
Dear Ric, I have a long delay in my landline mailbox server, sometimes up to 3 hours, my mobile phone is instantaneous. My apology. Yes I did reply to your thread. I will use in future your proposal, I had it in, but took it out. Bert
 
post edited by BertKu - 2020/11/03 04:14:24
#6
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:13:50 (permalink)
0
gkuijpers@telkomsa.net
My apology Ric, thank you for responding. It is my way of programming. OPEN means the switch which is made or going to be made when the gate has opened totally.   CLOSE means the gate switch which closes (make contact) when the gate is closed. All 3 btfss are suddenly no longer working. I then thought maybe the IC is gone faulty, thsu I use also a new brand new PIC18F2431,  still the same problem. I tried to select the bank wherein the btfss is kept, but maybe I have made also a mistake in doing the bank select correctly ( I took it out again) as it also did not work. Bert

I think you missed my point.
Look at your defines, then look at how you have used them.
I will ask again WHICH PIN is "OPEN" ?
Is it RB3 or is it RB5 ?
 
 Here is one of your defines
#define OPEN PORTB,5 ; switch checking for open Gate and contact is = set

and where you used it
btfss OPEN,3 ; Check whether the gate is open and contact made

So after expansion, you have:
btfss PORTB,5,3 ; Check whether the gate is open and contact made

post edited by ric - 2020/11/03 04:21:27

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#7
BertKu
mr
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2015/09/16 10:03:22
  • Location: 0
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:21:33 (permalink)
0
Thanks Ric,
 
#define    OPEN        PORTB,5 ; switch checking for open Gate and contact is = set
#define    CLOSED    PORTB,3 ; switch checking for closed Gate = contact is set
 
OPEN is pin 26 and Close is pin 24 from the PIC18F2431  Is that what you like to know?
Bert 
 
I see what you mean. That is a finger slip up. as I was trying to solve my problem and have added and changed and did all kind of different ways to solve my problem. You are totally right RIC and I will  correct it.  Bert
post edited by BertKu - 2020/11/03 04:25:52
#8
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:28:31 (permalink) ☼ Best Answerby BertKu 2020/11/03 04:46:56
+2 (2)
gkuijpers@telkomsa.net
I see what you mean. That is a finger slip up. as I was trying to solve my problem and have added and changed and did all kind of different ways to solve my problem. You are totally right RIC and I will  correct it.  Bert

Remember the full form of the BTFSS instruction is:
BTFSS PORT,BIT,ACCESS
Normally you can omit the ACCESS flag, and the assembler inserts the correct value. Your mistake was forcing it to something else.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#9
BertKu
mr
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2015/09/16 10:03:22
  • Location: 0
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:39:19 (permalink)
0 (2)
Thanks Ric, I am a foreigner and your interpretation is sadly not what I meant. Language mistake from me. But the sad problem is non of my btfss seems to work. If I use btfso, it works !! But I cannot figure out what has happen in the background which changed btfso to btfss. Therefore I think that somewhere in the background the btfss function is reversed. But I am going in circles as I cannot figure it out where it comes from. I assum at a given moment it will reverse back to btfss is working then correctly and it will blow my fuse or burn the motor out. 
 
#10
1and0
Access is Denied
  • Total Posts : 12269
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:47:15 (permalink)
+3 (3)
gkuijpers@telkomsa.net
Thanks Ric, I am a foreigner and your interpretation is sadly not what I meant. Language mistake from me. But the sad problem is non of my btfss seems to work. If I use btfso, it works !! But I cannot figure out what has happen in the background which changed btfso to btfss. Therefore I think that somewhere in the background the btfss function is reversed. But I am going in circles as I cannot figure it out where it comes from. I assum at a given moment it will reverse back to btfss is working then correctly and it will blow my fuse or burn the motor out.

You're totally missing Ric's points.
 
Since these

#define OPEN PORTB,5   ; switch checking for open Gate and contact is = set
#define CLOSED PORTB,3 ; switch checking for closed Gate = contact is set

are defined as such, these two instructions

    btfss OPEN,3   ; Check whether the gate is open and contact made
    btfss CLOSED,5 ; Check whether the gate is closed and contact made

will expand to

    btfss PORTB,5,3 ; Check whether the gate is open and contact made
    btfss PORTB,3,5 ; Check whether the gate is closed and contact made

and assemble as

    btfss PORTB,5,1 ; Check whether the gate is open and contact made
    btfss PORTB,3,1 ; Check whether the gate is closed and contact made

or

    btfss PORTB,5,BANKED ; Check whether the gate is open and contact made
    btfss PORTB,3,BANKED ; Check whether the gate is closed and contact made

and we all know PORTB is NOT in Bank 0 on an 18F2431. So, there is your banking error. ;)
post edited by 1and0 - 2020/11/03 04:51:37
#11
BertKu
mr
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2015/09/16 10:03:22
  • Location: 0
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:49:36 (permalink)
0
Thanks Ric, I am going to try that one as per your post 21 minutes ago. btfss,5   and btfss,3 and see whether it is solving my problem. Thanks Bert
 
 
#12
oliverb
Super Member
  • Total Posts : 441
  • Reward points : 0
  • Joined: 2009/02/16 13:12:38
  • Location: 0
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:54:07 (permalink)
+1 (1)
Very short TLDR: be very careful what you put in a #define declaration and where you use it, as if you get it wrong the macro and surrounding code can interract in unexpected ways.
#13
1and0
Access is Denied
  • Total Posts : 12269
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:55:04 (permalink)
0
gkuijpers@telkomsa.net
Thanks Ric, I am going to try that one as per your post 21 minutes ago. btfss,5   and btfss,3 and see whether it is solving my problem. Thanks Bert

You're still missing our points!  I guess you miss my Post #12 since they are only 2 minutes apart. ;)
 
#14
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 04:57:35 (permalink)
+2 (2)
The mistake is
btfss OPEN,3
should be just
btfss OPEN
It is the extra ",3" on the end causing all your problems!
(As 1and0 has explained in great detail)
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#15
BertKu
mr
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2015/09/16 10:03:22
  • Location: 0
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 09:37:57 (permalink)
0
Thank you 1and0 ,  I have done what you wrote, still no luck. I am getting worried. For years I have been exporting equipment with the PIC18F2431, never had any issue and now with this simple program for myself, it has flabbergasted me. 
Tomorrow I will go back to my old laptop with XP and write the program there and see what then happens. But you are right 1and0,  I was under the impression that when I define something that it would automatically shifted to the correct bank. Great mistake from me. Thanks all of you, tomorrow is another day.  
#16
1and0
Access is Denied
  • Total Posts : 12269
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/03 10:42:32 (permalink)
+1 (1)
gkuijpers@telkomsa.net
Thank you 1and0 ,  I have done what you wrote, still no luck. I am getting worried. For years I have been exporting equipment with the PIC18F2431, never had any issue and now with this simple program for myself, it has flabbergasted me. 
Tomorrow I will go back to my old laptop with XP and write the program there and see what then happens. But you are right 1and0,  I was under the impression that when I define something that it would automatically shifted to the correct bank. Great mistake from me. Thanks all of you, tomorrow is another day.  

As Ric said in Post #10, the syntax of the BTFSS instruction is

    BTFSS REGISTER,BIT,ACCESS

which accepts three operands. It is recommended to NOT explicitly code the Ram Access bit, and let MPASM inserts the correct bit for you. That is, you should use this instead

    btfss OPEN     ; Check whether the gate is open and contact made
    btfss CLOSED   ; Check whether the gate is closed and contact made

that will expand to

    btfss PORTB,5  ; Check whether the gate is open and contact made
    btfss PORTB,3  ; Check whether the gate is closed and contact made

and assemble as

    btfss PORTB,5,ACCESS ; Check whether the gate is open and contact made
    btfss PORTB,3,ACCESS ; Check whether the gate is closed and contact made

which should access the two PORTB bits correctly.
#17
BertKu
mr
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2015/09/16 10:03:22
  • Location: 0
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/04 01:58:56 (permalink)
0
Thanks all, 1an0 you made my day. I did not sleep very well, as I had a vision of having to repatriate my goods from Europe and fly it back at great cost, probably more then 100K.
but her is my history. 
1) I wrote the program and programmed it in the chip not write protected
2) it worked flawlessly: clue no1. 
3) I then decided to add the time loops in the program and decided to try my new reborn ICD3 out.
4) but the ICD debugger demanded change of configuration settings
5) I made changes left right and center after the different configuration settings
6) The project did not work properly anymore.
7) I tested with btfsc and it skipped, I tested it with btfss it did not skipped:. Clue no 2
8) Thus I have now concluded that the problem is in my configuration settings so that my ports are not set as it should. i.e. maybe the output is not going to 5 Volt but to 0 volt and thus my switch does not do anything. The same maybe also apply for the other C ports.  My original program was properly working. 
9) I became 80 last week and don't be surprised that I cannot remember anymore what my correct configuration settings was. You guys are all young and bright,  me no longer. Nothing wrong with that.
Thus any help for the correct settings is greatly appreciated. 
 
#18
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/04 02:04:58 (permalink)
+1 (1)
You still seem to be skipping over the big problem we are telling you about.
You should NOT be adding the ",3" or ",5" suffix when you use the "OPEN" and "CLOSED" macros.
Do you understand what the "ACCESS" parameter is? (The third argument to the BTFSS instruction).

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#19
BertKu
mr
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2015/09/16 10:03:22
  • Location: 0
  • Status: offline
Re: PIC18F2431 I can't get btfss to work properly 2020/11/04 02:21:23 (permalink)
0
Sorry Ric, My original program was working. my previous reply before yours 6 minutes earlier explains it all. No hard feelings, as seen on the first thread I did it correct, as it was,  as you requested. Yes I understood it all along. My apology for the time delays. Bert Just a pity that I am not allowed to download the configuration file. (Docx file)
#20
Page: 1234 > Showing page 1 of 4
Jump to:
© 2021 APG vNext Commercial Version 4.5