Problem using Else


Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 40 of 47
  1. #1

    Default Problem using Else

    I am using the latest version of Picbasic Pro with Micro Studio Plus, if I write something like:

    IF Portb.0=0 THEN Portb.1=1
    ELSE
    Portb.1=0
    ENDIF

    Or
    IF i=%00000001 THEN HIGH 1
    ELSE
    LOW 1
    ENDIF

    ERROR Line 49: ELSE without a matching IF..THEN. (TEST(mod1).pbp)

    Just about anything I try using else doesn't work, Any suggestions? Thanks again

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    IF i=%00000001 THEN HIGH 1
    ELSE
    LOW 1
    ENDIF


    should be like

    IF i=%00000001 THEN
    HIGH 1
    ELSE
    LOW 1
    ENDIF
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3


    Did you find this post helpful? Yes | No

    Default

    IT WORKS!!!!!!!!!!!!!

    Thank you very much

  4. #4


    Did you find this post helpful? Yes | No

    Default

    can I put a pause in for debounce?
    IF i=%00000001 THEN
    HIGH 1
    pause 50
    ELSE
    LOW 1
    ENDIF

  5. #5
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer View Post
    IF i=%00000001 THEN HIGH 1
    ELSE
    LOW 1
    ENDIF


    should be like

    IF i=%00000001 THEN
    HIGH 1
    ELSE
    LOW 1
    ENDIF
    If you want to fit the THEN on one line, then the ELSE must be there too. To fit multiple commands on your line, you need a colon ":" between them...

    IF i=%00000001 THEN HIGH 1 : ELSE : LOW 1
    (And I don't remember if the Endif is required in this situation or not.)

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by tenaja View Post
    If you want to fit the THEN on one line, then the ELSE must be there too. To fit multiple commands on your line, you need a colon ":" between them...

    IF i=%00000001 THEN HIGH 1 : ELSE : LOW 1
    (And I don't remember if the Endif is required in this situation or not.)
    I'm not sure that statement is 100% valid, and I'm all about using colons! I'll have to try it out later today.
    Also, GOSUBs don't work well after a single line IF/THEN, and neither do FOR/NEXT loops.

  7. #7


    Did you find this post helpful? Yes | No

    Default Colons & If

    The manual says:

    W2 = W0
    W0 = W1
    W1 = W2
    is the same as:
    W2 = W0 : W0 = W1 : W1 = W2

    So why wont this compile:
    IF mydata=%01010110 THEN: HIGH 7:else: low 7: ENDIF:PAUSE 50:GOTO loop
    ERROR Line 48: Bad expression. (Test(test mod2).pbp)

    I've even tried it as this :
    IF mydata=%01010110 THEN HIGH 7:else: low 7: ENDIF:PAUSE 50:GOTO loop

    Still no go
    I just started to try to use multi-line statements as suggested, sure would look better
    Last edited by tazntex; - 24th July 2008 at 19:23. Reason: Typing error

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by tazntex View Post
    The manual says:
    W2 = W0
    W0 = W1
    W1 = W2
    is the same as:
    W2 = W0 : W0 = W1 : W1 = W2
    Yes, that is completely valid..

    So why wont this compile:
    IF mydata=%01010110 THEN: HIGH 7:else: low 7: ENDIF:PAUSE 50:GOTO loop
    ERROR Line 48: Bad expression. (Test(test mod2).pbp)

    I've even tried it as this :
    IF mydata=%01010110 THEN HIGH 7:else: low 7: ENDIF:PAUSE 50:GOTO loop
    Just like I said in my last post, I'm not 100% sure that an else in the IF/THEN is valid, and apparently it's not. So you still have to split up the If/Then/Else/Endif statements.

    This is valid:
    Code:
    If mydata = x then
        high 7
    else
        low 7
    endif
    pause 50:goto loop
    You're examples above are not valid.
    These don't work either:
    Code:
    IF mydata = x then pause 50 : GOSUB something
    
    If mydata = x then for x = 1 to 10 : pause 1 : next x
    These work:
    Code:
    if mydata = x then
     pause 50 : gosub something
    endif
    
    if mydata = x then
     for x = 1 to 10 : pause 1 : next x
    endif
    Just a few examples of what works and what doesn't when using colons...and I know colons!

  9. #9


    Did you find this post helpful? Yes | No

    Default

    Well that makes me feel better, I always try to learn something each day. Thanks again for the explanation.

  10. #10
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by tazntex View Post
    can I put a pause in for debounce?
    IF i=%00000001 THEN
    HIGH 1
    pause 50
    ELSE
    LOW 1
    ENDIF
    debounce?

    Code:
    IF i=%00000001 THEN
    
     WHILE i=%00000001
      PAUSE 50 
     WEND
    
     HIGH 1
    
    ELSE
    
     LOW 1
    
    ENDIF
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  11. #11


    Did you find this post helpful? Yes | No

    Default

    I have tried the:
    IF i=%00000001 THEN

    WHILE i=%00000001
    PAUSE 50
    WEND

    HIGH 1

    ELSE

    LOW 1

    ENDIF

    using this on four outputs my program does nothing with these four. If I take out the while statement and use the :
    If i=%00000001 THEN
    HIGH 1
    ELSE
    LOW 1
    ENDIF
    then my outputs for these four comes on and stays on, forever.
    I been experimenting with SELECT CASE and what I am trying to accomplish is if I am pressing the button on the transmitter, the output selected comes on and stays on until I release the button instead of cycling on/off. So here is my latest rx version which does nothing, what am I doing wrong?

    INCLUDE "bs2defs.bas"
    @ DEVICE PIC16F628a,XT_OSC
    @ DEVICE pic16F628a, WDT_OFF
    @ DEVICE pic16F628a, PWRT_ON
    @ DEVICE pic16F628a, MCLR_ON
    @ DEVICE pic16F628a, BOD_ON
    @ DEVICE pic16F628a, LVP_OFF
    @ DEVICE pic16F628a, CPD_OFF
    @ DEVICE pic16F628a, PROTECT_OFF
    DEFINE OSC 4
    mydata1 VAR byte
    mydata2 var byte
    address1 var byte
    address2 var byte
    checksum var byte
    chk_sum var byte
    serpin VAR porta.1
    PORTA = 0
    PORTB = 0
    trisa = %00000010
    trisb = %10000000
    CMCON=%00000111
    PAUSE 50

    loop:
    gosub loop1
    CheckSum = (address1 + address2)
    CheckSum = CheckSum + (mydata1 + mydata2)
    IF checksum != chk_sum THEN loop
    IF (mydata1) != (mydata2) THEN loop
    IF (address1) != (address2) THEN loop

    select case mydata1
    case 0
    mydata1=%01010110
    toggle 0
    pause 50

    case 1
    mydata1=%01011001
    high 1
    pause 50
    case else
    low 1

    case 2
    mydata1=%01011010
    high 2
    pause 50
    case else
    low 2

    case 3
    mydata1=%01100101
    high 3
    pause 50
    case else
    low 3

    case 4
    mydata1=%01100110
    high 4
    pause 50
    case else
    low 4

    case 5
    mydata1=%01101001
    toggle 5
    pause 50

    case 6
    mydata1=%01101010
    toggle 6
    pause 50
    end select
    goto loop

    loop1:
    SERIN serpin,N9600,[254],address1,address2,mydata1,mydata2,chk_sum
    Return

    One more thing when I copy and paste my code to this forum everything becomes lower case. I have been reminded as a courtesy of DEFINE OSC 4 just wanted to let ya'll know
    why everything appears in the lower case. Many Thanks

  12. #12
    Join Date
    Oct 2004
    Posts
    440


    Did you find this post helpful? Yes | No

    Default

    FineLine Viewer was created to sort out the if - endif statements by drawing brackets between the if and endif's.

    See IDE FineLine Viewer.
    Scroll down to and download v1.3

    Be sure to have "Hide extensions for known file types"
    unchecked in Any folder>Tools>Folder options>View to see
    the ".TXT" to remove.


    Norm
    Attached Images Attached Images  

  13. #13
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    For one thing, you've got more than one CASE ELSE inside the SELECT CASE block.
    Check your manual...

  14. #14
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Order of operations, I dunno maybe the compiler sorts this out, but . . . logic dictates, cmcon goes before anything portA, then tris registers dictate input or output, then port dictates hi or low status. Unless you are writing this for a basic stamp, why INCLUDE "bs2defs.bas" I would and always do use INCLUDE "MODEDEFS.BAS", as I haven't the foggiest as to what makes a stamp tick. Now having said this , someone will explain to me why I am all wrong
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  15. #15


    Did you find this post helpful? Yes | No

    Default

    Ok, I am seeing some light, hopefully it isn't attached to a train. Thanks for pointing out the CASE ELSE, never thought about that. I started with sample code from RENTRON's site and that why I had INCLUDE "bs2defs.bas" up there, and the CMCON=%00000111 never saw exactly where that should be placed in the manual. One thing is for certain I have learned a lot from this forum. And thanks for the FINE LINE link.

    I still have a problem with:
    'This will do nothing

    IF mydata1=%01011001 THEN
    WHILE mydata1=%01011001
    pause 50 ' pause for debounce
    WEND
    HIGH 1
    ELSE
    LOW !
    ENDIF

    or
    'This works, to turn on but will not turn off

    IF mydata1=%01011001 THEN
    HIGH 1
    ELSE
    LOW !
    ENDIF

    Thanks again
    Last edited by tazntex; - 6th August 2008 at 20:37.

  16. #16
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Is there another "half" to this code, that is sending data? I ask because I want to try it out and am too lazy to write code to test it
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  17. #17


    Did you find this post helpful? Yes | No

    Default

    Here is the entire code with the WHILE WEND example I was trying to use for a debounce.

    Please ignore what should be upper case because when I copy and paste it changes back to lower case.

    INCLUDE "MODEDEFS.BAS"
    @ DEVICE PIC16F628a,XT_OSC
    @ DEVICE pic16F628a, WDT_OFF
    @ DEVICE pic16F628a, PWRT_ON
    @ DEVICE pic16F628a, MCLR_ON
    @ DEVICE pic16F628a, BOD_ON
    @ DEVICE pic16F628a, LVP_OFF
    @ DEVICE pic16F628a, CPD_OFF
    @ DEVICE pic16F628a, PROTECT_OFF
    DEFINE OSC 4
    CMCON=%00000111
    PORTA = 0
    PORTB = 0
    trisa = %00000010
    trisb = %10000000
    mydata1 VAR byte
    mydata2 var byte
    address1 var byte
    address2 var byte
    checksum var byte
    chk_sum var byte
    serpin VAR porta.1
    PAUSE 50

    loop:
    gosub loop1
    CheckSum = (address1 + address2)
    CheckSum = CheckSum + (mydata1 + mydata2)
    IF checksum != chk_sum THEN loop
    IF (mydata1) != (mydata2) THEN loop
    IF (address1) != (address2) THEN loop

    if mydata1=%01010110 then toggle 0
    pause 50

    if mydata1=%01011001 then
    while mydata1=%01011001 ' trying this example for debounce
    pause 50
    wend
    high 1
    else
    low 1
    endif

    if mydata1=%01011010 then
    while mydata1=%01011010
    pause 50
    wend
    high 2
    else
    low 2
    endif

    if mydata1=%01100101 then
    while mydata1=%01100101
    pause 50
    wend
    high 3
    else
    low 3
    endif

    if mydata1=%01100101 then
    while mydata1=%01100101
    pause 50
    wend
    high 4
    else
    low 4
    endif

    if mydata1=%01101001 then toggle 5

    if mydata1=%01101010 then toggle 6

    goto loop

    loop1:
    SERIN serpin,N9600,[254],address1,address2,mydata1,mydata2,chk_sum
    Return

  18. #18
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    I think you're confused on the correct method of debouncing.

    The object is to check for a switch hit, wait for the bouncing to stop, then check again and see if the same switch is still hit.
    The way you are doing it, you'll catch the bouncing and kick out of your check before you get a chance to check it after the bouncing is over...

    Try this:

    Code:
    INCLUDE "MODEDEFS.BAS"
    @ DEVICE PIC16F628a,XT_OSC
    @ DEVICE pic16F628a, WDT_OFF
    @ DEVICE pic16F628a, PWRT_ON
    @ DEVICE pic16F628a, MCLR_ON
    @ DEVICE pic16F628a, BOD_ON
    @ DEVICE pic16F628a, LVP_OFF
    @ DEVICE pic16F628a, CPD_OFF
    @ DEVICE pic16F628a, PROTECT_OFF
    DEFINE OSC 4 
    CMCON=%00000111 : PORTA = 0 : PORTB = 0 : trisa = %00000010
    trisb = %10000000 : mydata1 VAR byte : mydata2 var byte : address1 var byte
     address2 var byte : checksum var byte : chk_sum var byte : serpin VAR porta.1
    PAUSE 50 
    loop:     gosub loop1 : CheckSum = (address1 + address2) + (mydata1 + mydata2)
    IF checksum != chk_sum THEN loop
    IF (mydata1) != (mydata2) THEN loop
    IF (address1) != (address2) THEN loop
    if mydata1=%01010110 then toggle 0
    pause 50
    lp1:
    if mydata1=%01011001 then
    pause 50
    if mydata1=%01011001
    high 1 
    else
    low 1
    endif 
    if mydata1=%01011010 then
    pause 50
    if  mydata1=%01011010
    high 2
    else
    low 2
    endif
    if mydata1=%01100101 then
    pause 50
    if mydata1=%01100101
    high 3
    else
    low 3
    endif
    if mydata1=%01100101 then
    pause 50
    if mydata1=%01100101
    high 4
    else
    low 4
    endif
    if mydata1=%01101001 then toggle 5
    if mydata1=%01101010 then toggle 6
    goto loop
    loop1:
    SERIN serpin,N9600,[254],address1,address2,mydata1,mydata2,chk_sum
    Return

  19. #19
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by tazntex View Post
    Here is the entire code with the WHILE WEND example I was trying to use for a debounce.

    Please ignore what should be upper case because when I copy and paste it changes back to lower case.
    That is because you typed it in lower case and MCS changes it for it's use, when you copy it to the clipboard it reveals the truth.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  20. #20
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Please let us see the code you are sending to this program. Are yoy sending from a PC or a PIC?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  21. #21
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    Also, GOSUBs don't work well after a single line IF/THEN, and neither do FOR/NEXT loops.
    I missed this when it was first posted (on the basis I don't read everything)... this is news to me... do you have examples?

    I've written some pretty complex programs and haven't come across this problem - unless of course you're dropping COLONs all over the place and your GOSUB follows on... not something I've ever done (on one line).

    Example

    If A=0 then Goto Start else A=A+1:Gosub DoThis

    This is equivalent to...

    If A=0 then
    Goto Start
    else
    A=A+1
    Gosub DoThis
    Endif

    And NOT as may be expected...

    If A=0 then
    Goto Start
    else
    A=A+1
    endif
    Gosub DoThis

    There is a distinct difference (and some tricky workings) if you bung all your code on one line separated with colons. It's not really the way it should be written. There are rules for IF/THEN/ELSE, WHILE/WEND, FOR/NEXT etc.

  22. #22


    Did you find this post helpful? Yes | No

    Default

    Joe, here is the code I am sending from pic to pic:

    INCLUDE "MODEDEFS.BAS"
    @ DEVICE PIC16F628a,XT_OSC
    @ DEVICE pic16F628a, WDT_OFF
    ' Watchdog Timer
    @ DEVICE pic16F628a, PWRT_ON
    ' Power-On Timer
    @ DEVICE pic16F628a, MCLR_ON
    ' Master Clear Options (Internal)
    @ DEVICE pic16F628a, BOD_ON
    ' Brown-Out Detect
    @ DEVICE pic16F628a, LVP_OFF
    ' Low-Voltage Programming
    @ DEVICE pic16F628a, CPD_OFF
    ' Data Memory Code Protect
    ' Set to CPD_OFF for Development Copy
    ' Set to CPD_ON for Release Copy
    @ DEVICE pic16F628a, PROTECT_OFF
    define osc 4
    CMCON=%00000111
    trisb = %11111111
    trisa = %00010111
    chk_sum var byte
    dgood var porta.2
    serpin var porta.3
    nokey con %11111111
    synch con 254
    address con %00000001
    keyin var portb
    keydata var byte
    E VAR PORTB.0
    D VAR PORTB.1
    G VAR PORTB.2
    A VAR PORTB.3
    B VAR PORTB.4
    C VAR PORTB.5
    F VAR PORTB.6
    na VAR PORTB.7
    PreAmble CON $A5 ' 10100101
    findkey:
    if A = 0 then sw0
    if B = 0 then sw1
    if C = 0 then sw2
    if D = 0 then sw3
    if E = 0 then sw4
    if F = 0 then sw5
    if G = 0 then sw6

    goto findkey
    sw0:
    keydata = %01010110
    goto dout
    sw1:
    keydata = %01011001
    goto dout
    sw2:
    keydata = %01011010
    goto dout
    sw3:
    keydata = %01100101
    goto dout
    sw4:
    keydata = %01100110
    goto dout
    sw5:
    keydata = %01101001
    goto dout
    sw6:
    keydata = %01101010
    goto dout

    dout:
    low dgood
    pause 100
    chk_sum = (address + address)
    chk_sum = chk_sum + (keydata + keydata)
    goto transmit

    transmit:

    serout serpin,N9600[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    if keyin = nokey then x2
    goto findkey

    x2:
    high dgood
    keydata = %00000000
    serout serpin,N9600,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    goto findkey
    end

    Anyhow, it works, tx side seems to be fine, my big problem is on the rx side each output that is a if/then statement will come on but when I release the button it stays on. The only way to make it turn off is to press another momentary button. The toggle function works fine, although if I hold onto the button to long it will toggle on/off. if/then example I have tried after Skimask post:

    if mydata1=%01011001 then pause 50
    if mydata1=%01011001 then
    high 1
    else
    low 1
    endif

    I am using a direct cable link, and will try again later with the Linx modules.

    "logic dictates, cmcon goes before anything portA, then tris registers dictate input or output, then port dictates hi or low status."
    And I have modified the suggestion to this tx program yet, but I will later this evening.

    Many thanks for the help.
    Last edited by tazntex; - 7th August 2008 at 04:41.

  23. #23
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    I missed this when it was first posted (on the basis I don't read everything)... this is news to me... do you have examples?
    I've written some pretty complex programs and haven't come across this problem - unless of course you're dropping COLONs all over the place and your GOSUB follows on... not something I've ever done (on one line).
    I don't have any specific examples off the top of my head, although I've never actually used an If/Then/Else on one continuous line. Thanks for the idea
    And yes, you are right about the colons. I try to stuff as much into a single line as I can, just so I don't have to scroll up and down so much. Therefore I tend to run into various issues with the aforementioned statements. I just know what works, and when something doesn't, I have a rough idea of where to look My current 'project' is about 4000 lines of my 'colonized' code. I split it up one day and came up with something around 19,000 lines. Sure, it's not easy to read, but I wrote it, I know what's where, and for the most part, others don't have to read it (and probably couldn't anyways )
    And you're right, it's probably not so much the way that code should be written, but there's nothing that says it CAN'T be written in such a fashion...Unless of course it doesn't work right.

  24. #24
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Anyhow, it works, tx side seems to be fine, my big problem is on the rx side each output that is a if/then statement will come on but when I release the button it stays on. The only way to make it turn off is to press another momentary button. The toggle function works fine, although if I hold onto the button to long it will toggle on/off. if/then example I have tried after Skimask post:
    Debouncing isn't going to work very well without a 'switch' to actually debounce. If you're trying to debounce a received signal, I don't think that it's the right way to go about it.
    This shouldn't be all that hard...but if you continue having problems, you might want to thinking about sending MAKE and BREAK codes with the TX similar to a PC keyboard.
    I.E. code1 = button 1 pressed
    code2 = button 1 released
    code3 = button 2 pressed....
    and so on and so on...

  25. #25
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    is the space in the word "address" a cut paste error or a typo in the transmit code?
    Code:
    transmit:
    
    serout serpin,N9600[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,<font color=red>addr ess</font color>,address,keydata,keydata,chk_sum]
    if keyin = nokey then x2
    goto findkey
    
    x2:
    high dgood
    keydata = %00000000
    serout serpin,N9600,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,<font color=red>addr ess</font color>,address,keydata,keydata,chk_sum]
    goto findkey
    end
    Last edited by Archangel; - 7th August 2008 at 06:46.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  26. #26
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    I can turn on any of the ports using realterm and a max232 using the code below.
    Code:
         INCLUDE "MODEDEFS.BAS"
    ;@ DEVICE PIC16F628a,XT_OSC
    ;@ DEVICE pic16F628a, WDT_OFF
    ;@ DEVICE pic16F628a, PWRT_ON
    ;@ DEVICE pic16F628a, MCLR_ON
    ;@ DEVICE pic16F628a, BOD_ON
    ;@ DEVICE pic16F628a, LVP_OFF
    ;@ DEVICE pic16F628a, CPD_OFF
    ;@ DEVICE pic16F628a, PROTECT_OFF
    @MyConfig = _HS_OSC & _WDT_OFF & _PWRTE_ON & _LVP_OFF 
    @MyConfig = MyConfig & _BODEN_OFF 
    @ __config  MyConfig
    DEFINE OSC 20
    CMCON = 7
    
    trisa = %00000010
    trisb = %00000000
    
    PORTA = 0
    PORTB = 0
    
    MYDATA1  VAR byte
    MYDATA2  var byte
    address1 var byte
    address2 var byte
    checksum var byte
    chk_sum  var byte
    ;serpin   VAR PortA.1
    PAUSE 50
    
    loop:
    high portA.2
    pause 500
    low portA.2
    gosub loop1
    serout portA.0,T9600,[254,2,mydata1," ",mydata2,"          "]
    serout portA.0,T9600,[$FE,$C0,address1," ",address2,"          " ]
    'CheckSum = (address1 + address2)
    'CheckSum = CheckSum + (mydata1 + mydata2)
    'IF checksum   != chk_sum THEN loop
    'IF (mydata1)  != (mydata2) THEN loop
    'IF (address1) != (address2) THEN loop
    
    if mydata1= 86 then toggle portB.0
    
    
    if mydata1= 89 then
    'while mydata1 = 89 ' trying this example for debounce
    'pause 50
    'wend
    high PortB.1
    
    else
    low PortB.1
    endif
    
    if mydata1 = 90 then
    'while mydata1 = 90
    'pause 50
    'wend
    high PortB.2
    
    else
    low PortB.2
    endif
    
    if mydata1= 101 then
    'while mydata1 = 101
    'pause 50
    'wend
    high PortB.3
    
    else
    low PortB.3
    endif
    
    if mydata1 = 102 then
    'while mydata1 = 102
    'pause 50
    'wend
    high PortB.4
    
    else
    low PortB.4
    endif
    
    if mydata1=105 then toggle PortB.5
    if mydata1=106 then toggle PortB.6
    
    goto loop
    
    loop1:
    SERIN2 PortA.1,84,[DEC address1,DEC ADDRESS2,DEC MYDATA1,DEC MYDATA2]
    Return
    end
    Much stuff just commented out, serouts added so as to see variable contents, a blinky added for life signs, easy to undo. Thanks for sharing your problem, I got to learn too.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  27. #27
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Tazntex,

    You are forgetting something I think.


    When you press a button, your IF statement works only WHILE you are pressing the button.

    When you do NOT press a button, your ELSE statement works continuously.

    This is why "you think" that you see no change; because it happens so fast when you hit the button down and release it.

    I think you should set an IF statement with the first button press, and keep it that way until you press it second time. Like a flip flop.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  28. #28


    Did you find this post helpful? Yes | No

    Default

    Yes Joe that is a typo in the copy and paste. When I read all the replies with example code how are ya'll inserting the code block whereas I am just copying and pasting my code to this reply. I am assuming, which I don't like to do, that under additional options I could attach my questionable code.
    What does the DEC mean and do? In the manual I understand it to be Decimal as in MYDATA would be recieved as a decimal. After reading many post on Manchester, I would think I would only be using Binary, but it would seem in the end Binary, Decimal and Hex would all be the same.

    loop1:
    SERIN2 PortA.1,84,[DEC address1,DEC ADDRESS2,DEC MYDATA1,DEC MYDATA2]
    Return
    end

    Sayzer, in the flip/flop method you are speaking of, I can see how to turn an output on/off with the toggle command, but I do not understand how I could press the button on the tx and the output would come on the receiver but would have to release the button and press again to make it go off. As I have it using the toggle command as long as I am pressing the button on the tx the rx output just cycles on and off. Well I will keep reading the PBP manual maybe thats in there and I have missed it. By the way I am using the PBP v2.50 with the update installed.

    Many, many thanks to all for your help and patience.
    Last edited by tazntex; - 7th August 2008 at 12:56. Reason: typo

  29. #29
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by tazntex View Post
    Yes Joe that is a typo in the copy and paste. When I read all the replies with example code how are ya'll inserting the code block whereas I am just copying and pasting my code to this reply. I am assuming, which I don't like to do, that under additional options I could attach my questionable code.
    Check out this page for all the neat stuff here on the forums:
    http://www.picbasic.co.uk/forum/misc...bbcode#imgcode

    (take out the extra spaces here)

    [ code ]This will go in a code block[ / code ]

    Code:
    This will go in a code block
    And there's a bunch more formatting type stuff listed on that page linked above.


    What does the DEC mean and do? In the manual I understand it to be Decimal as in MYDATA would be recieved as a decimal.
    It actually means that SERIN (or whatever) will wait until it receives x characters to be interpreted as a decimal.
    For instance:
    SERIN xxx,xxx,xxx, [ DEC1 x ]
    Waits for a single character between "0" and "9", preceeded by a character that's not between "0" and "9" and followed by another character that's not between "0" and "9".
    Another for instance:
    SERIN xxx,xxx,xxx, [ DEC5 x ]
    Waits for 5 consecutive characters that are each between "0" and "9", preceeded by any character that's not between "0" and "9" and followed by another character that's not between "0" and "9".
    So, the DEC (or HEX or BIN) modifier actually does 'parsing' of an input stream for you.
    HEX would wait for ASCII characters between "0" and "9" in addition to "a" (or "A") through "f" (or "F").
    BIN would only wait for "0" and "1".
    For reference..."0" = ASCII code 48 ($30), "9" = ASCII code 57 ($39), and so on. Just so we're on the same page here.

  30. #30
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Mostly it means I have an easier time dealing with decimal numbers, than binary, as Jeremy pointed out you can use hex, binary, or decimal, it's all good.

    Quote Originally Posted by tazntex View Post
    As I have it using the toggle command as long as I am pressing the button on the tx the rx output just cycles on and off.
    Your receiver doesn't just stop because you are still holding the button, and besides your transmitter is sending a signal, not some d/c value, additionally when you go the R/F route, this is where switch debouncing comes in and should be on the transmitter I think. I think the transmitter program should send a packet of data, size determined by you, for each valid button press and not just keep sending continuously, otherwise scratchy switches will corrupt your data.
    Last edited by Archangel; - 7th August 2008 at 17:58.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  31. #31


    Did you find this post helpful? Yes | No

    Default

    ok, how come if i use this on the receiver:

    if mydata1=%01100101 then pause 50
    if mydata1=%01100101 then
    high 4
    else
    low 4
    endif

    And with this clip on the transmitter:

    transmit:

    serout serpin,N9600,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    if keyin = nokey then x2
    goto findkey

    x2:
    high dgood
    keydata = %00000000
    serout serpin,N9600,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    goto findkey

    Why is it that by transmiting to the receiver =%01100101 by pressing the button then once I release the button the transmitter sends keydata = %00000000 which I would think the ELSE would then turn PORTB.4 LOW, but it doesn't. Only when I press another button which activates the other three outputs which are using IF/THEN, ELSE. Then and only then will PORTB.4 go LOW, but the next output comes on and stay on like PORTB.4. Now the outputs I am toggling work just fine. Hopefully I am making some sense, and the folks with the white coats won't come get me
    Thanks again, Ya'll have been a great help

  32. #32
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    On the receiver end change to SERIN2.

    Have it wait for the "synch" and use the time out modifier. The time out modifier will take place of the "else".

    So the reciever is "waiting" for the synch, if it does not get in in the specified time, the program will jump to where ever.

    You do not need to send keydata = %00000000 . Once the receiver does not see the synch it will time out and go on.

    I agree with Joe, debounce on the receiver end. Here is a simple but not so efficient way one of my kids debounced with a radio using SERIN.

    The code waits for the number 9 and writes the next bit to a variable. 9 is the synch in this case. Then if the variable = 3 enough times then another piece of code executes. You will see the the next piece is called "send". He was doing a relay project. If the 9 was not seen the program went back to looping, if the 9 and 3 was seen the program sends something out to the next receiver.

    He debounced the heck out of it I was told there is to much noise in the shop.

    Code:
    LOOP: 
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN D1
    GOTO LOOP
    
    D1:
    PAUSE 50
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN D2
    GOTO LOOP
    
    D2:
    PAUSE 50
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN D3
    GOTO LOOP
    
    D3:
    PAUSE 50
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN D4
    GOTO LOOP
    
    D4:
    PAUSE 50
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN D5
    GOTO LOOP
    
    D5:
    PAUSE 50
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN SEND
    GOTO LOOP
    
    D6:
    PAUSE 50
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN D7
    GOTO LOOP
    
    D7:
    PAUSE 50
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN D8
    GOTO LOOP
    
    D8:
    PAUSE 50
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN D9
    GOTO LOOP
    
    D9:
    PAUSE 50
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN D10
    GOTO LOOP
    
    D10:
    PAUSE 50
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN SEND
    GOTO LOOP
    
    D11:
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN D12
    GOTO LOOP
    
    D12:
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN D13
    GOTO LOOP
    
    D13:
    SERIN PORTC.4,T2400,[9],net
    IF net = 3 THEN SEND
    GOTO LOOP
    Dave
    Always wear safety glasses while programming.

  33. #33


    Did you find this post helpful? Yes | No

    Default

    That is interesting, thanks so much.

  34. #34


    Did you find this post helpful? Yes | No

    Default Still trying

    After many tries I am about to take a hammer to my processor and just put it out of its misery. I have tried several different versions of this an they always do the same thing. I press a button on the transmitter data link works great on the the receiver the bits that I am toggling works good, but no matter what I try the outputs on portb 1-4 just come on but they won't go off. I've added a few lines to make sure the receiver knows keydata=%00000000 when no key is pressed, even add a little delay to hear it on my service monitor.
    transmit:

    serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    if keyin = nokey then pause 10
    if keyin = nokey then x2
    goto findkey

    x2: 'I have taken this out as suggested but then what tells the Rx mydata1,mydata2 to equal %00000000 other than CLEAR or mydata1=%00000000?
    high dgood
    keydata = %00000000
    serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    goto findkey

    On the receiver side:
    loop1:
    SERIN2 serpin,16780,[wait(254),address1,address2,mydata1,mydata2,chk_su m]
    Return
    when I am sending data everything checks out fine using the checksum then continues on, but for whatever reason even though when I release the key on the transmitter, and it sends keydata=%00000000 , I would think mydata1,mydata2 being (%00000000 + %00000000) would match and continue on so the ELSE command would return the outputs back to their low state but it doesn't.

    loop:
    gosub loop1 ' check serial
    CheckSum = (address1 + address2)
    CheckSum = CheckSum + (mydata1 + mydata2)
    IF checksum != chk_sum THEN loop
    IF (mydata1) != (mydata2) THEN loop
    IF (address1) != (address2) THEN loop



    Anyone know what I am doing wrong?
    Last edited by tazntex; - 14th August 2008 at 04:01.

  35. #35
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Read thru your code very very carefully again! And again....and again....and again....
    Use a piece of paper or something to keep track of what your program is doing if you have to.
    Your transmit program is doing EXACTLY what you are writing it to do, like I was trying to explain in the other thread.
    YOU aren't doing anything wrong, your program isn't doing anything wrong. Again, it's doing exactly what you programmed it to do.
    What you have to do is program it to do what you WANT it to do...
    The problem is in your program, your logic flow. Read it...You'll get it...

  36. #36
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    You know, the idea of this forum is to provide asnwers for peoples questions.

    If you see something helpfull that you can provide, by all means, speak up and point it out.

    Telling someone to re-read their own program over and over, as if (ha ha, I know what's wrong and you don't).

    Does not help.
    <br>
    DT

  37. #37
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    I would, and I will, eventually, maybe. The O/P is seeming to have difficulty in realizing that PICs do only what they are programmed to do...nothing more, nothing less, and just because a programmer wants the PIC to do something, doesn't mean it's going to do it, at least not without proper program and logic flow and hardware to back it up. Feeding, fishing, teaching, learning, all that stuff... He'll get it eventually. He's already figured out a few things for himself. It's only a matter of time before a light bulb goes on...

  38. #38


    Did you find this post helpful? Yes | No

    Default

    Ya'll are great, I will go back through this, print it and try to figure it out. Will be back after awhile and let ya'll know what I have or haven't found.



    Thanks again

  39. #39


    Did you find this post helpful? Yes | No

    Default

    INCLUDE "bs2defs.bas"
    @ DEVICE PIC16F628a,XT_OSC
    @ DEVICE pic16F628a, WDT_OFF
    ' Watchdog Timer
    @ DEVICE pic16F628a, PWRT_ON
    ' Power-On Timer
    @ DEVICE pic16F628a, MCLR_ON
    ' Master Clear Options (Internal)
    @ DEVICE pic16F628a, BOD_ON
    ' Brown-Out Detect
    @ DEVICE pic16F628a, LVP_OFF
    ' Low-Voltage Programming
    @ DEVICE pic16F628a, CPD_OFF
    ' Data Memory Code Protect
    ' Set to CPD_OFF for Development Copy
    ' Set to CPD_ON for Release Copy
    @ DEVICE pic16F628a, PROTECT_OFF
    DEFINE OSC 4
    mydata1 VAR byte
    mydata2 var byte
    address1 var byte
    address2 var byte
    checksum var byte
    chk_sum var byte
    serpin VAR porta.1 'serial input pin
    PORTA = 0
    PORTB = 0
    trisa = %00000010
    trisb = %10000000
    CMCON=%00000111
    PAUSE 50 ' settle time

    loop:
    clear 'Do I really need to do this? I tried to make sure everything is back to 0
    gosub loop1 'After button is released on tx mydata1=%00000000
    CheckSum = (address1 + address2) 'address sent %00000001 added together in this case this = 10
    CheckSum = CheckSum + (mydata1 + mydata2) '10 + (%00000000 +%00000000)= 10
    IF checksum != chk_sum THEN loop 'checksum = chk_sum because of above
    IF (mydata1) != (mydata2) THEN loop '(mydata1) = (mydata2)
    IF (address1) != (address2) THEN loop '(address1) != (address2)
    'Since everything should be a match We move on
    if mydata1=%01010110 then rly1 'this works so I will go to the next one

    if mydata1=%01011001 then rly2 '1st case will turn portb.1 on
    if mydata1=%00000000 then rly2 '2nd case appears to do nothing but as I stated above the check
    'should be correct and mydata1=%00000000
    if mydata1=%01011010 then rly3 'I will skip the rest of this area because it
    if mydata1=%00000000 then rly3 'behaves the same as rly2 the rly6 rly7 ok.

    if mydata1=%01100101 then rly4
    if mydata1=%00000000 then rly4

    if mydata1=%01100110 then rly5
    if mydata1=%00000000 then rly5

    if mydata1=%01101001 then rly6
    if mydata1=%01101010 then rly7

    rly1:
    if mydata1=%01010110 then pause 10
    if mydata1=%01010110 then toggle 0
    pause 10
    goto loop
    rly2:
    if mydata1=%01011001 then pause 10 'Down here the fun begins
    if mydata1=%01011001 then high 1 '1st case will turn portb.1 on
    if mydata1=%00000000 then low 1 'Since the checksum is correct why didn't I get here
    'mydata1=%00000000 so this should go low but doesn't
    goto loop 'here we go back to loop so I will goto loop1
    rly3:
    if mydata1=%01011010 then pause 10
    if mydata1=%01011010 then high 2
    if mydata1=%00000000 then low 2
    goto loop
    rly4:
    if mydata1=%01100101 then pause 10
    if mydata1=%01100101 then high 3
    if mydata1=%00000000 then low 3
    goto loop
    rly5:
    if mydata1=%01100101 then pause 10
    if mydata1=%01100110 then high 4
    if mydata1=%00000000 then low 4
    goto loop
    rly6:
    if mydata1=%01101001 then pause 10
    if mydata1=%01101001 then TOGGLE 5
    pause 10
    goto loop
    rly7:
    if mydata1=%01101010 then pause 10
    if mydata1=%01101010 then TOGGLE 6
    pause 10
    GOTO loop


    loop1:
    SERIN2 serpin,16780,[wait(254),address1,address2,mydata1,mydata2,chk_su m]
    Return
    'loop1 receives the following
    'transmit:

    ' serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    ' if keyin = nokey then pause 10
    ' if keyin = nokey then x2
    ' goto findkey

    'x2:
    ' high dgood
    ' keydata = %00000000
    ' serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    ' serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    ' serout serpin,N2400,[$55,$55,$55,$55,$55,$AA,$AA,$AA,$AA,$AA,synch,addr ess,address,keydata,keydata,chk_sum]
    ' goto findkey

    'on the GOSUB loop1 after the tx button is released I should move on to x2
    'x2 sends the preamble, and I am still getting my synch plus address,data an chk_sum
    'so I should pass through the checksum area of my receiver code and on to the rest of
    'my program. When the program arrives at "if mydata1=%00000000 then rly2 " it should
    'ask "if mydata1=%00000000 then low 1" and respond and portb.0 should go low but it
    'never seems to get there. I switch from the If/Then/Else to make it simpler for me
    'to break it down.

    'I've been over the tx side and I feel confident everything there is working correctly

    ' Am I heading in the right direction?

  40. #40
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    So between post #34 and #39, that is your entire code for both TX and RX?

    And is it possible for you to connect an LCD to the RX for troubleshooting? Or at least replace those relays with LEDs.

Similar Threads

  1. problem using GOSUB under interrupt
    By fobya71 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 5th March 2010, 19:52
  2. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  3. Microcode Studio 18f2455 problem?????
    By volkan in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 21st May 2007, 21:04
  4. Hardware problem or what ?
    By Steve S. in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 4th March 2007, 21:39
  5. 1 slave 1 master 1 MAX232 1 problem ?
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 31st July 2005, 22:59

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts