Problem using Else


Closed Thread
Results 1 to 40 of 47

Hybrid View

  1. #1
    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.

  2. #2


    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.

  3. #3
    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...

  4. #4
    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.

  5. #5
    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.

  6. #6
    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

  7. #7


    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

  8. #8
    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.

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