Problem using Else


Closed Thread
Results 1 to 40 of 47

Hybrid View

  1. #1


    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

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

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

  4. #4


    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 21:37.

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

  6. #6


    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

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

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

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

Similar Threads

  1. problem using GOSUB under interrupt
    By fobya71 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 5th March 2010, 20: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, 09:12
  3. Microcode Studio 18f2455 problem?????
    By volkan in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 21st May 2007, 22:04
  4. Hardware problem or what ?
    By Steve S. in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 4th March 2007, 22: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, 23: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