Shop OBEX P1 Docs P2 Docs Learn Events
Newbie here and i seriously need some help with input's on the SX28 — Parallax Forums

Newbie here and i seriously need some help with input's on the SX28

eagletalontimeagletalontim Posts: 1,399
edited 2008-01-15 15:50 in General Discussion
I just got my SX programmer today and have been messing with it, but I cannot get the inputs figured out!

I am attempting to make an up and down counter with a specific output, but for some reason, I just cannot get the inputs to read anything. I need to be able to send a positive signal to an input and it add "1" to variable my_counter, then gosub to process the new value.

I set the chip to delay before each new setting to test if the outputs even worked. They do, but if 2 outputs are on at the same time, one is LED is brighter than the other. Any way to fix that?

Any help would be greatly appreciated!

Comments

  • BeanBean Posts: 8,129
    edited 2008-01-15 01:39
    You need to provide the code and schematic. We have no idea what you are up to without them.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.iElectronicDesigns.com

    ·
  • eagletalontimeagletalontim Posts: 1,399
    edited 2008-01-15 01:52
    Here is the code.

    I am using RB.0 and RB.1 as the positive output going to an LED then to ground for testing purposes for now.
    RA.0 and RA.1 are the inputs that are supposed to add(RA.0) or subtract (RA.1)

    DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
    FREQ            4_000_000
    ID              "FOR-NEXT"
    
    level_up        PIN    RA.0 INPUT
    level_down    PIN    RA.1 INPUT
    sol_a           VAR     RB.0
    sol_b           VAR     RB.1
    TRIS_sols       VAR     TRIS_B
    TRIS_levelselect VAR     TRIS_A
    level        VAR    Byte
    newlevel        VAR    Byte
    PROGRAM Start
    
    Set_level    SUB    1, 1
    
    Start:
      TRIS_sols = %11111100
      level = 1
    
    Main:
      Set_level 1
      IF level_up = 1 THEN
        level = level + 1
        IF level >= 4 THEN 
           level = 4 
        ENDIF
        Set_level level
      ENDIF
      IF level_down = 1 THEN
        level = level - 1
        IF level <= 1 THEN 
           level = 1
        ENDIF
        Set_level level
      ENDIF
      GOTO Main
    
    Set_level:
      newlevel = __PARAM1
      IF newlevel = 1 THEN
        sol_b = 1
        sol_a = 1
      ENDIF
      IF newlevel = 2 THEN
        sol_b = 0
        sol_a = 1
      ENDIF
      IF newlevel = 3 THEN
        sol_b = 0
        sol_a = 0
      ENDIF
      IF newlevel = 4 THEN
        sol_b = 1
        sol_a = 0
      ENDIF
      RETURN
    
  • JonnyMacJonnyMac Posts: 9,218
    edited 2008-01-15 02:10
    The problem is that you're scanning the inputs so quickly -- even at 4 MHz -- that you can't see the change. And right after Main you set the level to 1, and then your program loops back to this point; move the first Set_Level call to the Start section, then insert at 100 ms PAUSE after main to slow down the scan rate.

    Note, too, that you should have pull-downs on your input pins.

    Post Edited (JonnyMac) : 1/15/2008 2:18:24 AM GMT
  • eagletalontimeagletalontim Posts: 1,399
    edited 2008-01-15 02:17
    is there any way to detect if one of the switches going to an input is pressed and not do anything till it is released?
  • BenoitBenoit Posts: 47
    edited 2008-01-15 02:18
    You could try this too:
    
    Main:
        level = level + level_up    ;; wont increase if not level_up
        level = level - level_down ;; wont decrease if not level_down
        IF level >= 4 THEN 
           level = 4 
        ENDIF
        IF level <= 1 THEN 
           level = 1
        ENDIF
    
      Set_level level
    ;;  <set an appropriate pause here>
      GOTO Main
    
    



    -Benoit
  • eagletalontimeagletalontim Posts: 1,399
    edited 2008-01-15 02:30
    well, i am having the problem that if i hold the switch down too long, it will count all the way up or all the way down. I need to be able to hold the switch down for as long as i want and when it is released, it will change the output. can I put an if statement in the sub that will keep it in a loop until it is released?
  • BenoitBenoit Posts: 47
    edited 2008-01-15 02:33
    Main:
    
        IF level_up THEN
            level=level+1
           DO
                pause 100  ;;  adjust until the input is properly debouced
           LOOP WHILE level_up=1
        ENDIF
    
        IF level_down THEN
            level=level - 1
           DO
                pause 100  ;;  adjust until the input is properly debouced
           LOOP WHILE level_down=1
        ENDIF
      
    IF level >= 4 THEN 
           level = 4 
        ENDIF
        IF level <= 1 THEN 
           level = 1
        ENDIF
    
      Set_level level
      PAUSE 100
    GOTO Main
      
    
    



    This is almost straight out of Jon's book, Practical SX/B (page 67)

    -Benoit
  • eagletalontimeagletalontim Posts: 1,399
    edited 2008-01-15 02:53
    hmmm. it is kind of working, but i am not sure what is happening. If I press the up switch, it will not stay on the next level up. it is like the inputs are picking up static or something. is there any way to stop this from happening?
  • JonnyMacJonnyMac Posts: 9,218
    edited 2008-01-15 03:03
    As they say here in Hollywood... for your consideration. The program compiles and looks right, but I didn't bother hooking it up -- give this a try.

    ' =========================================================================
    '
    '   File......
    '   Purpose...
    '   Author....
    '   E-mail....
    '   Started...
    '   Updated...
    '
    ' =========================================================================
    
    
    ' -------------------------------------------------------------------------
    ' Program Description
    ' -------------------------------------------------------------------------
    
    
    ' -------------------------------------------------------------------------
    ' Conditional Compilation Symbols
    ' -------------------------------------------------------------------------
    
    
    ' -------------------------------------------------------------------------
    ' Device Settings
    ' -------------------------------------------------------------------------
    
    DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX, BOR42
    FREQ            4_000_000
    ID              "Name"
    
    
    ' -------------------------------------------------------------------------
    ' I/O Pins
    ' -------------------------------------------------------------------------
    
    BtnUp           PIN     RA.0 INPUT
    BtnDn           PIN     RA.1 INPUT
    
    UnusedRA2       PIN     RA.2 INPUT PULLUP
    UnusedRA3       PIN     RA.3 INPUT PULLUP
    
    SolA            PIN     RB.0 OUTPUT
    SolB            PIN     RB.1 OUTPUT
    
    UnusedRB2       PIN     RB.2 INPUT PULLUP
    UnusedRB3       PIN     RB.3 INPUT PULLUP
    UnusedRB4       PIN     RB.4 INPUT PULLUP
    UnusedRB5       PIN     RB.5 INPUT PULLUP
    UnusedRB6       PIN     RB.6 INPUT PULLUP
    UnusedRB7       PIN     RB.7 INPUT PULLUP
    
    UnusedRC        PIN     RC   INPUT PULLUP
    
    
    ' -------------------------------------------------------------------------
    ' Constants
    ' -------------------------------------------------------------------------
    
    IsOn            CON     1
    IsOff           CON     0
    
    
    ' -------------------------------------------------------------------------
    ' Variables
    ' -------------------------------------------------------------------------
    
    level           VAR     Byte
    
    scan            VAR     Byte
     incLevel       VAR     scan.0
     decLevel       VAR     scan.1
    
    tmpB1           VAR     Byte                    ' work vars
    tmpB2           VAR     Byte
    tmpW1           VAR     Word
    
    
    ' =========================================================================
      PROGRAM Start
    ' =========================================================================
    
    
    ' -------------------------------------------------------------------------
    ' Subroutine / Function Declarations
    ' -------------------------------------------------------------------------
    
    DELAY_MS        SUB     1, 2
    
    SET_LEVEL       SUB     1, 1
    SCAN_BUTTONS    FUNC    1, 0
    
    
    ' -------------------------------------------------------------------------
    ' Program Code
    ' -------------------------------------------------------------------------
    
    Start:
      level = 1
    
    Main:
      SET_LEVEL level
      scan = SCAN_BUTTONS
    
      IF level < 4 THEN
        level = level + incLevel
      ENDIF
    
      IF level > 1 THEN
        level = level - decLevel
      ENDIF
    
      DELAY_MS 100                                  ' minimum loop delay
      DO
        scan = SCAN_BUTTONS
      LOOP UNTIL scan = %00                         ' force button release
    
      GOTO Main
    
    
    ' -------------------------------------------------------------------------
    ' Subroutine / Function Code
    ' -------------------------------------------------------------------------
    
    SUB DELAY_MS
      IF __PARAMCNT = 1 THEN
        tmpW1 = __PARAM1
      ELSE
        tmpW1 = __WPARAM12
      ENDIF
      PAUSE tmpW1
      ENDSUB
    
    ' -------------------------------------------------------------------------
    
    FUNC SCAN_BUTTONS
      tmpB1 = %11
      FOR tmpB2 = 1 TO 5
        tmpB1 = tmpB1 & RA
        DELAY_MS 5
      NEXT
      RETURN tmpB1
      ENDFUNC
    
    ' -------------------------------------------------------------------------
    
    SUB SET_LEVEL
      tmpB1 = __PARAM1
    
      IF tmpB1 = 1 THEN
        SolB = IsOn
        SolA = IsOn
      ELSEIF tmpB1 = 2 THEN
        SolB = IsOff
        SolA = IsOn
      ELSEIF tmpB1 = 3 THEN
        SolB = IsOff
        SolA = IsOff
      ELSEIF tmpB1 = 4 THEN
        SolB = IsOn
        SolA = IsOff
      ELSE                                          ' fix level error
        SolB = IsOn
        SolA = IsOn
        level = 1
      ENDIF
      ENDSUB
    
    ' -------------------------------------------------------------------------
    ' User Data
    ' -------------------------------------------------------------------------
    

    Post Edited (JonnyMac) : 1/15/2008 3:16:33 AM GMT
  • eagletalontimeagletalontim Posts: 1,399
    edited 2008-01-15 03:35
    i was able to get it by putting a while loop in the set_level function, but I still think i am getting problems with static signals. Can i put a resistor from the input to the Vss and on the input side have the switch? What would be a good way to prevent this from happening. I need to ensure that every time the button is pushed, it goes to the next step and never missed a beat.
  • JonnyMacJonnyMac Posts: 9,218
    edited 2008-01-15 06:27
    Do not let your inputs float; this is microcontroller interfacing 101. Since your program wants an active-high input, you should pull-down RA.0 and RA.1 with 10K resistors. You could use the internal pull-ups, but that will mean a change in your code.
  • steve_bsteve_b Posts: 1,563
    edited 2008-01-15 13:11
    Almost sounds like you've got a debounce problem....before a switch fully closes, there are a number of little "stutters" or "arcs" (if you like) that will connect/disconnect in rapid succession....

    You could fix this in hardware or software. Both are handy ways to know....Got down the software path first....at some point you may run out of timing to do the waits/whiles to get rid of the 'bounciness' and you would have to revert to hardware.

    For interests sake, you could connect a pull-up resistor to your switch and then put a capacitor across the switch. What you are doing is making what's called an RC circuit (R for resistor; C for Capacitor), which is basically a delay type of circuit or filter. There are some calculations you could make to determine what either should be....personally, I'd just use an appropriate pullup resistor, like Jon suggest, then pick a capacitor that works for you (this is easily done if you have an oscilloscope!).

    The other way to debounce a circuit (and really the better way IMHO - unless you DO have noise; then the RC could act as a filter as well) is to use a schmitt trigger.
    You set up your switch the same way (pull-up resistor, cap in parallel with switch - ~10nF) then where the resistor and the switch/cap join, you connect the input of a schmitt

    Actually, here's a webpage for future reference...

    www.all-electric.com/schematic/debounce.htm

    Cheers

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <FONT>Steve



    What's the best thing to do in a lightning storm? "take a one iron out the bag and hold it straight up above your head, even God cant hit a one iron!"
    Lee Travino after the second time being hit by lightning!
  • eagletalontimeagletalontim Posts: 1,399
    edited 2008-01-15 15:13
    thanks guys [noparse]:)[/noparse] you are really some good help! I was able to fix it with the code but adjusting the pause times.

    One more thing..... I need to be able to drive a 15vdc 10 amp motor with the sx-28 chip. What would be the best way to drop the power down to not burn up the chip, but still send as much voltage to the motor that i can from one power supply. I was thinking of putting a resistor before the chip and using an NPN transistor rated at 60v 15 amps to control the power. Is that a good way to do it, or should I do something else?
  • steve_bsteve_b Posts: 1,563
    edited 2008-01-15 15:33
    Someone might contest....but I'd try to keep my sx on a different power supply than the motor.
    Control the motor with FETs and SSR's maybe to isolate the sx.

    Not very helpful, but haven't tried it....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <FONT>Steve



    What's the best thing to do in a lightning storm? "take a one iron out the bag and hold it straight up above your head, even God cant hit a one iron!"
    Lee Travino after the second time being hit by lightning!
  • eagletalontimeagletalontim Posts: 1,399
    edited 2008-01-15 15:50
    hmmm. in my case, I have to run off of the same power supply coming in which is 15v 10amps [noparse]:([/noparse]
Sign In or Register to comment.