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

programming a byte on one pin

Discussion in 'Microcontrollers' started by MrDEB, Nov 4, 2012.

  1. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Check in next week Mike I might have some further software developments using the blindfold method--lol
    It works for Washington D.C.---lol
     
  2. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    Thank you for having a good sense of humor, and I agree about Washington D.C. (wink).

    As bewildering as it might seem to a few of us who watch you take the long meandering route, as opposed to a more direct route, I don't think anyone can dispute that you always arrive at your destination with the results you were looking for. If you enjoyed the journey and accomplished your goals, what else matters? Right?

    Happy Holidays and cheerful regards, Mike
     
    Last edited: Nov 28, 2012
  3. ghostman11

    ghostman11 Well-Known Member

    Joined:
    Nov 22, 2009
    Messages:
    1,192
    Likes:
    99
    Location:
    Scotland
    i am also a avid MR DEB reader!! has to be said it is always a riveting read ;)- and to be honest i can see why some get frustrated but you have to admire MR DEB'S single minded determination to do the thing come hell or high water!! full marks for not giving up :D and Burt i am gonna get you canonized by the pope! saint Burt of ETO lol well done to both.
     
  4. dave

    Dave New Member

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


     
  5. tumbleweed

    tumbleweed Member

    Joined:
    Jan 25, 2007
    Messages:
    51
    Likes:
    15

    Gotta admit I'd have thrown in the towel a lot sooner than I could have a 15 page thread on the subject.

    But, back to the fun...
    And it would. In the latest code I see it could be almost a second before you leave RIDE and get back to the main button polling loop. In the original code it could have been almost twice that.

    Did you try my suggestion? Polling the button press pins inside RIDE should get you back quite a bit faster.
     
  6. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Well I just got back from doing some minor repairs to our church.
    When I have the monster running, durring the RIDE routine, then press a button, BAM the led flashes. then it goes into the turn routiine.
    Can't tell if its one flash or two short flashes but it does flash upon pressing a button.
    I have a video of it in action. Just need to upload. You can see the green LED flash for amusement purposes only. Just don't get to excited. I know saint Burt will ---lol
     
  7. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Video of LED

    The button is really small but when I press it the RIDE sequence stops and it jumps to a turn routine. I don't think its two flashes?
    [video]http://s992.beta.photobucket.com/user/MrDEB/media/Video7.mp4.html[/video]
    Going to investigate if I can slow it down then see just where the routine stops in the middle or ?
     
  8. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,794
    Likes:
    134
    Location:
    morristown,tn
    ONLINE
    In the for next loop of ride just test the buttons and exit. IF pressed and if the loop is 750mS your button test cold happen less then 100 mS which would be fine and no debounce needed.

    If you really think about it ride should be the main loop, Then if you call the subs left right run that loop and test the button press each run of the for next loop which can be a fast test
     
    Last edited: Nov 28, 2012
  9. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,118
    Likes:
    90
    Location:
    Seattle, WA
    Burt is entirely right. "RIDE" is the default state when the program is doing nothing else. "LEFT" and "RIGHT" are exceptions to the default state, initiated by a bottom press or head tip for whatever...who can keep track. Since it's always doing RIDE, put it in the main program loop.
     
  10. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    I did this before reading

    But yes RIDE should be in the main loop. Here I inserted a CHECK SWITCH in the RIDE SUB (going to make RIDE in main loop instead of sub?
    I slowed down the RIDE sub and now it stops and exits to what ever turn is wanted.
    Now to mess up Tumbleweeds code. Don't worry I saved my Bowels of hell code under a different name.
    Code (text):
    Sub RIDE()
        Dim index As Byte
        Dim X As Byte
     
        For index = 0 To Bound (Ride_Data)
            DelayMS(500)//10 500us for constant on
            X = Ride_Data(index)
     
            LE = 0
            ResetPin_R = 1   // disable OE RED
            ResetPin_G = 1  // disable OE Green
            //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            // CHECK SWITCHES
            If s1 = 0 Then          // sw 1 closed
            count = 0
           exit
        End If
     
        If s2 = 0 Then          // sw 2 closed
            count = 0
        exit
     
        End If
        //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  
     
            Shift.Out(MSB_FIRST, X ,8)  
            LE = 1         //Latch data
            PORTB = R_Rows(index)  
            DelayUS(200) //20 us
            ResetPin_G = 0  // output LOW = ON HIGH = OFF
            ResetPin_R = 1
            LE = 0
       
        Next
     
    End Sub
     
     
  11. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    Just sit right back and hear a tale

    A tale of a faithful code, that set sail for a 12 page tour, a 12 page tour.


    Well I changed things around, put the RIDE in the main loop then inserted a check switch routoines in between.
    Slowed down the RIDE sequence and upon pressing a switch, BAM it immedieatly or there abouts, goes to a turn sequence.
    This seems S M OO T H ER
    THANKS to ST. Burts suggestion
    Code (text):
    {
    *****************************************************************************
    *  Name    : UNTITLED.BAS                                                   *
    *  Author  : [select VIEW...EDITOR OPTIONS]                                 *
    *  Notice  : Copyright (c) 2012 [select VIEW...EDITOR OPTIONS]              *
    *          : All Rights Reserved                                            *
    *  Date    : 11/28/2012                                                     *
    *  Version : 1.0                                                            *
    *  Notes   :                                                                *
    *          :                                                                *
    *****************************************************************************
    }
    {
    *****************************************************************************
    *  Name    : UNTITLED.BAS                                                   *
    *  Author  : [select VIEW...EDITOR OPTIONS]                                 *
    *  Notice  : Copyright (c) 2012 [select VIEW...EDITOR OPTIONS]              *
    *          : All Rights Reserved                                            *
    *  Date    : 11/27/2012                                                     *
    *  Version : 1.0                                                            *
    *  Notes   :  tumbleweed code rev 2                                                              *
    *          :                                                                *
    *****************************************************************************
    }
    Device = 18F2420
    Clock = 20
     
    // external xtal
    Config OSC = HS
     
    // uncomment next line to use internal osc
    //Include "InternalOscillator.bas"          // module sets Config OSC = INTIO7 for you
     
    Include "shift.bas"
    Include "utils.bas"
    Include "convert.bas"
     
    Dim SDI As PORTC.4
    Dim CLK As PORTC.5
    Dim LE As PORTC.3
    Dim RED_OE As PORTC.0
    Dim GREEN_OE As PORTC.1
     
    Dim DataPin As SDI  
    Dim ClockPin As CLK
    Dim ResetPin_R As RED_OE
    Dim ResetPin_G As GREEN_OE
     
    // pushbutton switches inputs S1 and S2
    Dim s1 As PORTA.2
    Dim s2 As PORTA.3
    Dim count As Byte
    dim switch as byte
    dim led as porta.0
    Dim index As Byte
    Dim X As Byte  
    Const Data(15)As Byte = (%00010001, %00001010 ,%00000100, %00010001, %00001010, %00000100,
    %00010001, %000001010 ,%00000100, %000010001,%00001010, %00000100, %000010001,%00001010, %00000100)
     
    Const Rows(15) As Byte =(%00000001, %000000010 ,%00000100, %000000010, %00000100, %00001000,
     %00000100 ,%000001000 ,%00010000 ,%00001000,%00010000 ,%00100000, %00010000 ,%00100000, %01000000)
     
    Const L_TRows(15) As Byte =( %01000000,%00100000,%00010000,%00100000,%000010000, %00001000,
    %00010000, %00001000, %00000100 ,%00001000, %000000100,%00000010,%00000100, %000000010,%00000001)
     
    Const Ride_Data(15)As Byte = (%00011111, %00000100 ,%00011111, %00000100, %00011111, %00000100,
    %00011111, %00000100 ,%00011111, %00000100,%00011111, %00000100, %00011111,%00000100, %00011111)
     
    Const R_Rows(15) As Byte =(%00001000, %011111111 ,%00001000, %011111111, %00001000, %01111111,
     %00001000 ,%011111111 ,%00001000 ,%01111111,%00001000 ,%01111111, %00001000 ,%01111111, %00001000)
     

     
    Sub RIGHT()
        Dim index As Byte
        Dim X As Byte
     
        For index = 0 To Bound (Data)
            DelayMS(10)//10 500us for constant on
            X = Data(index)
     
            LE = 0
            ResetPin_R = 1   // disable OE RED
            ResetPin_G = 1  // disable OE Green
            Shift.Out(MSB_FIRST, X ,8)  
            LE = 1         //Latch data
            PORTB = Rows(index)  
            DelayUS(20) //20 us
            ResetPin_R = 0  // output LOW = ON HIGH = OFF
            ResetPin_G = 1
            LE = 0
        Next
     
    End Sub
     
    Sub LEFT()
        Dim index As Byte
        Dim X As Byte
     
        For index = 0 To Bound (Data)
            DelayMS(10)//10 500us for constant on
            X = Data(index)
     
            LE = 0  // move to top
            ResetPin_R = 1   // disable OE RED
            ResetPin_G = 1  // disable OE Green
            Shift.Out(MSB_FIRST, X ,8)  
            LE = 1         //Latch data
            PORTB = L_TRows(index)   // display byte on matrix
            DelayUS(20) //10  1 us for constant on
            ResetPin_R = 0  // output LOW = ON HIGH = OFF
            ResetPin_G = 1
        Next
     
    End Sub
     
    // start of main
    SetAllDigital
     
    Shift.SetOutput(DataPin)
    Shift.SetClock(ClockPin)
     
    High(ResetPin_R)        // make output and set high
    High(ResetPin_G)        // make output and set high
    Low(LE)                 // make output and set low
     
    PORTB = 0
    TRISB = %00000000       // make PORTB outputs
     
    // make S1 and S2 switch inputs (assumes there are external pullups on the pins)
    Input(s1)
    Input(s2)
       
     
    While true                   // durring RIDE the code jumps to here if switch = 1
       
      //  Dim index As Byte
      //  Dim X As Byte
     
        For index = 0 To Bound (Ride_Data)
            DelayMS(500)//10 500us for constant on
            X = Ride_Data(index)
     
            LE = 0
            ResetPin_R = 1   // disable OE RED
            ResetPin_G = 1  // disable OE Green
            //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            // CHECK SWITCHES
           
            If s1 = 0 Then          // sw 1 closed
            count = 0
            repeat
            LEFT
            delayms(200)
            inc(count)
            until count=10
           End If  
           
           If s2 = 0 Then          // sw 1 closed
            count = 0
            repeat
            RIGHT
            delayms(200)
            inc(count)
            until count=10
           End If  
           
        //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  
     
            Shift.Out(MSB_FIRST, X ,8)  
            LE = 1         //Latch data
            PORTB = R_Rows(index)  
            DelayUS(200) //20 us
            ResetPin_G = 0  // output LOW = ON HIGH = OFF
            ResetPin_R = 1
            LE = 0
       
        Next
     
    wend
    End
     
     
  12. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    You know, I was just kind of thinking the same thing. Now that (I hope) I understand what he's trying to do I suspect a minor restructuring would probably solve a couple problems, including switch response, by simply building the main program loop around the switch code and using the switch "debounce" interval as the main loop timing interval.

    What do you guys think of this loop model in pseudo C code (below)? I've taken a number of short-cuts, including connecting the driver IC <latch> and </oe> lines to a single I/O for both the green and red driver ICs.

    If you don't understand the parallel switch state logic, just think of it as non-blocking multi-switch "black box" code that filters out all but a "new press" switch state change and uses it to emulate a "toggle" switch. That is, any "new press" will toggle a flag from either on-to-off or from off-to-on. In operation this means when you press the <left> turn signal switch, the left turn signal sequence continues indefinitely, until you press the <left> turn signal switch again. Of course using momentary contact push button switches to emulate toggle switches allows using an LED on the same I/O pin so you can have a visual "turn signal on" indicator right at each switch at practically no cost.

    Could a structure change be beneficial?

    Regards, Mike

    Code (text):
        while(1)                           //
        { delay_ms(25);                    // 25-ms 'debounce' interval
         /*                                                                 *
          *  K8LH parallel switch state logic (with "new press" filter)     *
          *                                                                 *
          *  swnew  ___---___---___---_____   invert active lo switches     *
          *  swold  ____---___---___---____   switch state latch            *
          *  swnew  ___-__-__-__-__-__-____   changes, press or release     *
          *  swnew  ___-_____-_____-_______   filter out 'release' bits     *
          *  flags  ___------______--------   toggle flag bits for main     *
          *                                                                 */
          swnew = ~porta;                  // sample active lo switches
          swnew &= 0b00011000;             // on the RA4..RA3 pins only
          swnew ^= swold;                  // changes, press or release
          swold ^= swnew;                  // update switch state latch
          swnew &= swold;                  // filter out 'release' bits
          flags ^= swnew;                  // toggle flag bits for main

          if(swnew)                        // if any new press (toggle)
          { index = 0;                     // reset array index
          }                                //

         /*                                                                 *
          *  refresh matrix display for "ride", "left", or "right" mode     *
          *                                                                 */
          if(!flags)                       // if no switches (ride mode)
          { if(index > bound(ride_data))   // if index overflow
              index = 0;                   // reset index
            shiftout(ride_data[index]);    // send "ride" column SR data
            redlat = 1;                    // red disp off & latch data
            grnlat = 1;                    // grn disp off & latch data
            portb = ride_rows[index++];    // portb = "ride" row data
            grnlat = 0;                    // grn display on
          }                                //
          else                             // sw1 (right) or sw2 (left)
          { if(index > bound(turn_data))   // if index overflow
              index = 0;                   // reset index
            shiftout(turn_data[index]);    // send "turn" column SR data
            redlat = 1;                    // red disp off & latch data
            grnlat = 1;                    // grn disp off & latch data
            if(flags.3)                    // if sw1 (right) 'on'
              portb = rt_rows[index++];    // portb = right row data
            else                           // otherwise, sw2 (left) 'on'
              portb = lt_rows[index++];    // portb = left row data
            redlat = 0;                    // red display on
          }                                //
        }                                  // end while(1){}
      }                                    // end main{}
     
     
    Last edited: Nov 29, 2012
  13. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,794
    Likes:
    134
    Location:
    morristown,tn
    ONLINE
    That's to simple but its the right way to handle this after all you want turn signals on the whole time you turn and now all we need is a way to flip the handle back when where going strait. :D:D:D
     
  14. Pigskin Tony

    Pigskin Tony New Member

    Joined:
    Aug 20, 2012
    Messages:
    18
    Likes:
    0
    Where is the daily installment of "As the Shipwreck Turns"?????
     
  15. MrDEB

    MrDEB Active Member

    Joined:
    Apr 16, 2007
    Messages:
    4,364
    Likes:
    22
    It went down with the Titanic --lol
    Am connecting up the encoder/dcecoder chips with their respective RX/TX modules.Need to insert a voltage reg as well. In between sessions(get frustrated) I tinker with a project that Brads projectshttp://www.bradsprojects.com/electronics/BradsProjects-Welcome.html has (I received boards but can't get the PICKIT2 to recognize the PIC.)
    Having to recheck all my connections as the PICKIT2 keeps saying ALL PROTECT or CODE PROTECT?The PIC is a 16F677
     

Share This Page