1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

code does not play fair??

Discussion in 'Microcontrollers' started by MrDEB, May 21, 2017.

  1. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    ONLINE
    been trying to figure out WHY this code keeps outputting regardless of switch setting.
    What it is supposed to do is run only the desired IF THEN statements but it runs all four IF THEN statements. If I have the switch on pos4 (Pg4 = pressed then all three leds light up but the code cycles through all four IF THEN statements.
    the rotary switch has the common pin grounded and each position pin is held HIGH
    Verified that the correct pic pin is going LOW.
    her is the switch, kept looking at switch diagram to be sure I didn't miss anything http://www.mouser.com/ds/2/96/220-786387.pdf
    this shouldn't be this hard. I used a working example from my Mexican train game that works but the switches are separate momentary on/off but same code format.
     
  2. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,118
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
    Why am I doing this? I never learn.

    First, since the data sheet you linked has switches with a number of different encodings, it's impossible to determine if you bungled the switch choice.

    Next, I don't know what's connected to PortA:1-4, but all of your if/then statements are setting some of the set high, with nothing ever setting them low.


    Nope. Not gonna do it again. As I read your post to figure out what the heck you're trying to do, and how you know the code is going through each section, I'm totally lost. Until you can write a clear articulated question, with diagrams showing what's actually connected at the moment you run the code to what, no one will probably be able to help you.

    Sorry. I have more rewarding things to do.
     
  3. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,015
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    Switches are active low and he probably hasn't turned analogue off.

    Mike.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,160
    Likes:
    909
    Location:
    Rochdale UK

    MrDEB Do yourself a favour..... When posting, please supply ALL of the code and if possible your schematic...

    We cannot see what you are seeing, I don't posses that power!!
     
  6. tumbleweed

    tumbleweed Member

    Joined:
    Jan 25, 2007
    Messages:
    51
    Likes:
    15
    ... or at least show us the definitions for Pg1-Pg4 and 'pressed'
     
    • Agree Agree x 1
  7. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    ONLINE
    I have the switches tied to HIGH via 10K pull-ups
    Have setalldigital.bas--maybe need additional code?
    Need the portA.0 for ADC
    have leds connected to port C.5, D6, D7 using to determine state of switch selection.
     

    Attached Files:

  8. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,118
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
    MrDEB, you write interesting code. Completely non-functional but someone could spend hours studying how your mind works.

    First, although you have included Utils.bas (which includes the command SetAllDigital) and SetDigitalIO.bas (which also has a command to make all inputs digital), nowhere do I see the command to actually set the inputs to digital. But that is the least of your problems.

    You are reading 4 bits of port A to determine the status of the switch (which we still can't understand as you haven't said which part you used) which you have dimensions as Pg1 - Pg4. The 4 bits are inputs.

    Looking at the first if/then command, you test if Pg1/PortA.1 = pressed / 0. If it is, then you set the other port A bits high? What is it you think you're doing here? Those bits are the state of the switch pins. What are you trying to accomplish?!?!???

    Me thinks you are somewhat confused. How much are you charging this guy for your efforts?

    tmp_26483-20170522_0854271496679095.png
     
  9. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    ONLINE
    I tried using just
    but that didn't work either. Then just out of curiosity I tried using %11111101 t indicate portA.1 as LOW
    reason for setting the other port pins HIGH was to make sure they are indeed HIGH
    My plan is to have 4 separate programs then each switch position jumps to a sub route.Using leds to indicate that the switch is taking the port pin LOW.
    I am assuming that by using INPUT portA.1 is enough? for the command to set the port to INPUT. Correct me if I am wrong.
    Note I still need to use the PORTA.0 for ADC input.
    Not sure if I tried using trisA=%11111111
    the IF THEN statements are same as I used for my Mexican Train and it worked but not in this application??
    I got something wrong somewhere. Just trying to discover where. I was rechecking the rotary switch and I verified that the port pin inputs are going LOW.
     
  10. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,118
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
    Read my previous message again.
     
  11. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    ONLINE
    pg1 to pg4 are the switch outputs connected to port pins A1, A2, A3, A4
    I removed the rereading of the port pins as indicated in post #8
    it is reading the switch but it keeps falling through and executing all 4 IF THEN statements.
    It should stop only on the first true if then but it goes through all four regardless of switch setting except the pg4 input
     
  12. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,118
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
     
  13. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    ONLINE
    command to set the input to digital??
    does the setalldigital.bas do that??
    I tried TrisA=%11111111 but no different
    it appears to just be executing all the code and not following the IF then statements regardless of being true or not
     
  14. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,118
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
  15. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    ONLINE
    I found the missing command!!
    setalldigital
    now it looks like it works but will be sure with futher testing
     
  16. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,118
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
  17. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,118
    Likes:
    90
    Location:
    Seattle, WA
    ONLINE
    You will of course have to set A0 to analog to use it as an ADC.
     
  18. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    ONLINE
    that's my next hurdle.
     
  19. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,800
    Likes:
    134
    Location:
    morristown,tn
    DIM pressed AS BYTE Cant work no way
    Dim pressed as bit
    or Dim pressed as boolean

    Code (text):
     pressed = 0
    if pressed true
    //do this
    else
    do nothing
    endif
     
    Code (text):

    pressed = 0
    if pressed = 1
    //do this
    else
    // do nothing
    endif
     
    Way you have it theres no test
    dim pressed as byte
    if you your reading more then one switch you need to do something like the
    Code (text):

    if portA = $f  
    // no switch pressed
    pressed = false
    elseif portA  <> $f
    // switch has been pressed
     
    hell I got to here and said why even used press
    you need to read the port

    Code (text):

    If portA = $F
    // do nothing
    endif
    If portA = $7
    // do what you want here
    endif
    If portA = $3
    // do what you want here
    endif
    If portA = $1
    // do what you want here
    endif
     
     
  20. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    ONLINE
    using Pressed as byte seems to work.
    It worked on my Mexican Train code as well.
    working on the ADC and a rand number generator code.
     
  21. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,800
    Likes:
    134
    Location:
    morristown,tn
    Your using pressed as a test to see if a pin has changed it don't need to be a byte just a bit or boolean
    you reading 4 pins what if you press 2 of them by mistake But if you ok with code that works when it feels like it thats cool.
     

Share This Page