Delayed output 10 secs


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118

    Default Delayed output 10 secs

    Hi,

    I am relatively new at this and I need a little help.
    I am using a 16F883
    Portb with pull ups as input, portc as instantaneous output and porta as delayed output.
    When I press switch 1 (low), it will toggle portc.1 (on)
    if I maintain switch 1 (low) for 3 seconds it will branch to another section.
    if I press switch 1 again it will toggle portc.1 (off)
    So far so good, my program is working fine but I want to add porta.1 which will trigger approx 10 seconds after portc.1

    Keeping in mind that the explaination above is for 1 switch / 1 output portc / 1 output porta but it will endup having 6 of each.

    Here is my code:

    Code:
    start:
        if sw1 = 0 then Bdelay1
    goto start
    
    Bdelay1:
        pause 25                                  ; debounce
        if sw1 = 0 then
            if ld1 = 0 or swstat = 1 then
                high ld1
                pause 500
                swstat = 1
                l=l+1
                if l => 5 then Bprog1
                goto bdelay1
            else
                 low ld1
                 pause 500
    wait1:     if sw1 = 0 then wait1
            endif
        endif
        swstat = 0   
        goto start
    Thank you

    Mike
    Last edited by lilimike; - 28th May 2009 at 03:24.

  2. #2
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    Well I guess this must be a tough one as no one commented or maybe I didn't explain clearly my issue.
    I have been doing lots of browsing and searching and I figured what I might need is to use a timer with interup but I am confused. If I have 6 switches on (portb) 6 Leds on (portc) each switch trigering its respective led and then again a respective led on porta will be triggered 10 seconds later.

    Sinse there is a chance that a user press switch1, switch2, switch3 and switch4 and even possibly all 6 of them (all within 10 seconds)

    If I could have 6 timers and 6 interupt routines I would manage easily but how can I control my 6 events with just one timer?

    If anyone can help me out, I don't bite!

    Thanks

    Mike

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lilimike View Post
    Well I guess this must be a tough one as no one commented or maybe I didn't explain clearly my issue.
    I think it's just one of those, "you can't get there from here" kind of things.
    Trying to time things with pauses makes it so you can't do anything else at the same time, so 6 buttons would be difficult with the example given.

    However, if you do it a little differently, it's a piece of cake.
    Using a timer to measure even 1ms time periods, and running the main loop freely (without pauses), you can do it like this...
    Code:
    <font color="#008000"><b>CLEAR
    DEFINE </b></font><b>OSC </b><font color="#800000"><b>20
    </b></font><font color="#008000"><b>INCLUDE </b></font><font color="#FF0000">&quot;AllDigital.pbp&quot;         </font><font color="#0000FF"><b><i>; Disable Analog functions
    
    </i></b></font><b>SW1   </b><font color="#008000"><b>VAR </b></font><b>PORTB</b>.<font color="#800000"><b>0
    </b></font><b>SW2   </b><font color="#008000"><b>VAR </b></font><b>PORTB</b>.<font color="#800000"><b>1
    </b></font><b>SW3   </b><font color="#008000"><b>VAR </b></font><b>PORTB</b>.<font color="#800000"><b>2
    </b></font><b>SW4   </b><font color="#008000"><b>VAR </b></font><b>PORTB</b>.<font color="#800000"><b>3
    </b></font><b>SW5   </b><font color="#008000"><b>VAR </b></font><b>PORTB</b>.<font color="#800000"><b>4
    </b></font><b>SW6   </b><font color="#008000"><b>VAR </b></font><b>PORTB</b>.<font color="#800000"><b>5
    
    </b></font><b>LED1  </b><font color="#008000"><b>VAR </b></font><b>PORTC</b>.<font color="#800000"><b>0
    </b></font><b>LED2  </b><font color="#008000"><b>VAR </b></font><b>PORTC</b>.<font color="#800000"><b>1
    </b></font><b>LED3  </b><font color="#008000"><b>VAR </b></font><b>PORTC</b>.<font color="#800000"><b>2
    </b></font><b>LED4  </b><font color="#008000"><b>VAR </b></font><b>PORTC</b>.<font color="#800000"><b>3
    </b></font><b>LED5  </b><font color="#008000"><b>VAR </b></font><b>PORTC</b>.<font color="#800000"><b>4
    </b></font><b>LED6  </b><font color="#008000"><b>VAR </b></font><b>PORTC</b>.<font color="#800000"><b>5
    
    </b></font><b>Counters   </b><font color="#008000"><b>VAR WORD</b></font>[<font color="#800000"><b>6</b></font>]           <font color="#0000FF"><b><i>; 6 words, for 6 buttons
    </i></b></font><b>DelayTime  </b><font color="#008000"><b>CON </b></font><font color="#800000"><b>10000             </b></font><font color="#0000FF"><b><i>; 10 seconds
    </i></b></font><b>T0IF       </b><font color="#008000"><b>VAR </b></font><b>INTCON</b>.<font color="#800000"><b>2          </b></font><font color="#0000FF"><b><i>; TMR0 overflow flag   
    </i></b></font><b>X          </b><font color="#008000"><b>VAR BYTE
    
    </b></font><b>OPTION_REG </b>= <font color="#800000"><b>%01010100           </b></font><font color="#0000FF"><b><i>; WPU on, TMR0 1:32 prescaler, internal clk
    </i></b></font><font color="#008000"><b>PAUSE </b></font><font color="#800000"><b>50                         </b></font><font color="#0000FF"><b><i>; let everything settle
    
    </i></b></font><b>Main</b>:
        <font color="#008000"><b>WHILE </b></font>!<b>T0IF </b>: <font color="#008000"><b>WEND           </b></font><font color="#0000FF"><b><i>; wait for timer to overflow
        </i></b></font><b>TMR0 </b>= <font color="#800000"><b>100                   </b></font><font color="#0000FF"><b><i>; load timer for 1ms
        </i></b></font><b>T0IF </b>= <font color="#800000"><b>0                     </b></font><font color="#0000FF"><b><i>; reset the overflow flag
    
        </i></b></font><font color="#008000"><b>IF </b></font><b>SW1 </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>then </b></font><b>Counters</b>[<font color="#800000"><b>0</b></font>] = <b>DelayTime </b>: <font color="#008000"><b>HIGH </b></font><b>LED1
        </b><font color="#008000"><b>IF </b></font><b>SW2 </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>then </b></font><b>Counters</b>[<font color="#800000"><b>1</b></font>] = <b>DelayTime </b>: <font color="#008000"><b>HIGH </b></font><b>LED2
        </b><font color="#008000"><b>IF </b></font><b>SW3 </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>then </b></font><b>Counters</b>[<font color="#800000"><b>2</b></font>] = <b>DelayTime </b>: <font color="#008000"><b>HIGH </b></font><b>LED3
        </b><font color="#008000"><b>IF </b></font><b>SW4 </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>then </b></font><b>Counters</b>[<font color="#800000"><b>3</b></font>] = <b>DelayTime </b>: <font color="#008000"><b>HIGH </b></font><b>LED4
        </b><font color="#008000"><b>IF </b></font><b>SW5 </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>then </b></font><b>Counters</b>[<font color="#800000"><b>4</b></font>] = <b>DelayTime </b>: <font color="#008000"><b>HIGH </b></font><b>LED5
        </b><font color="#008000"><b>IF </b></font><b>SW6 </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>then </b></font><b>Counters</b>[<font color="#800000"><b>5</b></font>] = <b>DelayTime </b>: <font color="#008000"><b>HIGH </b></font><b>LED6
    
        </b><font color="#008000"><b>FOR </b></font><b>X </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>to </b></font><font color="#800000"><b>5                      </b></font><font color="#0000FF"><b><i>; cycle thru counters
          </i></b></font><font color="#008000"><b>IF </b></font><b>Counters</b>(<b>X</b>) &gt; <font color="#800000"><b>0 </b></font><font color="#008000"><b>THEN           </b></font><font color="#0000FF"><b><i>;  if counter is counting
             </i></b></font><b>Counters</b>(<b>X</b>) = <b>Counters</b>(<b>X</b>) - <font color="#800000"><b>1  </b></font><font color="#0000FF"><b><i>;    decrement counter
             </i></b></font><font color="#008000"><b>IF </b></font><b>Counters</b>(<b>X</b>) = <font color="#800000"><b>0 </b></font><font color="#008000"><b>THEN        </b></font><font color="#0000FF"><b><i>; if counter timed out
               </i></b></font><font color="#008000"><b>SELECT CASE </b></font><b>X                </b><font color="#0000FF"><b><i>;  turn off the appropriate LED
                 </i></b></font><font color="#008000"><b>CASE </b></font><font color="#800000"><b>0 </b></font>: <font color="#008000"><b>LOW </b></font><b>LED1
                 </b><font color="#008000"><b>CASE </b></font><font color="#800000"><b>1 </b></font>: <font color="#008000"><b>LOW </b></font><b>LED2
                 </b><font color="#008000"><b>CASE </b></font><font color="#800000"><b>2 </b></font>: <font color="#008000"><b>LOW </b></font><b>LED3
                 </b><font color="#008000"><b>CASE </b></font><font color="#800000"><b>3 </b></font>: <font color="#008000"><b>LOW </b></font><b>LED4
                 </b><font color="#008000"><b>CASE </b></font><font color="#800000"><b>4 </b></font>: <font color="#008000"><b>LOW </b></font><b>LED5
                 </b><font color="#008000"><b>CASE </b></font><font color="#800000"><b>5 </b></font>: <font color="#008000"><b>LOW </b></font><b>LED6
               </b><font color="#008000"><b>END SELECT
             ENDIF
          ENDIF
        NEXT </b></font><b>X
    </b><font color="#008000"><b>GOTO </b></font><b>Main
    </b>
    No Interrupts required.

    HTH,
    DT

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I should add ... the counters in the previous example are retriggerable. So the delay would be from the Last button Release.

    If the delay should be from the first press, wait 10 seconds, non-retriggerable,
    then replace the switch test lines with these ...
    Code:
    <font color="#000000">    <font color="#008000"><b>IF </b></font>(<b>Counters</b>[<font color="#800000"><b>0</b></font>]=<font color="#800000"><b>0</b></font>) <font color="#008000"><b>AND </b></font>(<b>SW1</b>=<font color="#800000"><b>0</b></font>) <font color="#008000"><b>THEN </b></font><b>Counters</b>[<font color="#800000"><b>0</b></font>] = <b>DelayTime </b>: <font color="#008000"><b>HIGH </b></font><b>LED1
        </b><font color="#008000"><b>IF </b></font>(<b>Counters</b>[<font color="#800000"><b>1</b></font>]=<font color="#800000"><b>0</b></font>) <font color="#008000"><b>AND </b></font>(<b>SW2</b>=<font color="#800000"><b>0</b></font>) <font color="#008000"><b>THEN </b></font><b>Counters</b>[<font color="#800000"><b>1</b></font>] = <b>DelayTime </b>: <font color="#008000"><b>HIGH </b></font><b>LED2
        </b><font color="#008000"><b>IF </b></font>(<b>Counters</b>[<font color="#800000"><b>2</b></font>]=<font color="#800000"><b>0</b></font>) <font color="#008000"><b>AND </b></font>(<b>SW3</b>=<font color="#800000"><b>0</b></font>) <font color="#008000"><b>THEN </b></font><b>Counters</b>[<font color="#800000"><b>2</b></font>] = <b>DelayTime </b>: <font color="#008000"><b>HIGH </b></font><b>LED3
        </b><font color="#008000"><b>IF </b></font>(<b>Counters</b>[<font color="#800000"><b>3</b></font>]=<font color="#800000"><b>0</b></font>) <font color="#008000"><b>AND </b></font>(<b>SW4</b>=<font color="#800000"><b>0</b></font>) <font color="#008000"><b>THEN </b></font><b>Counters</b>[<font color="#800000"><b>3</b></font>] = <b>DelayTime </b>: <font color="#008000"><b>HIGH </b></font><b>LED4
        </b><font color="#008000"><b>IF </b></font>(<b>Counters</b>[<font color="#800000"><b>4</b></font>]=<font color="#800000"><b>0</b></font>) <font color="#008000"><b>AND </b></font>(<b>SW5</b>=<font color="#800000"><b>0</b></font>) <font color="#008000"><b>THEN </b></font><b>Counters</b>[<font color="#800000"><b>4</b></font>] = <b>DelayTime </b>: <font color="#008000"><b>HIGH </b></font><b>LED5
        </b><font color="#008000"><b>IF </b></font>(<b>Counters</b>[<font color="#800000"><b>5</b></font>]=<font color="#800000"><b>0</b></font>) <font color="#008000"><b>AND </b></font>(<b>SW6</b>=<font color="#800000"><b>0</b></font>) <font color="#008000"><b>THEN </b></font><b>Counters</b>[<font color="#800000"><b>5</b></font>] = <b>DelayTime </b>: <font color="#008000"><b>HIGH </b></font><b>LED6</b>
    DT

  5. #5
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    Thanks for the quick response Darrel,

    I tried your code and I feel like I am getting closer but...
    first the delay was over 1 minute so by making DelayTime CON 1500 it is close to 10 seconds. Not sure if it is because I am using the internal oscilator and it is not as accurate as it should be but this is no isue.

    Instead of turning off LEDs 1-5 after 10 seconds I need to turn on another set of Leds so I added TR1 VAR PORTA.0 up to TR6 VAR PORTA.5 and I used those in the select case and so far its all good.

    I now face the following issues:
    if I hold down any of the switches (SW1 to SW6) for 3 seconds, it should cancel the Delaytime that was going to turn on TRx and branch to a different label to do something else.

    If I press and release a switch, 10 seconds later TRx turns on, I need to be able to press and release the same switch to turn off LEDx and TRx at once.

    Another aspect I didn't mention because I will deal with it later is that when TRx turns on, if I don't manually press and release the switch to turn them off, after a set timed period (written in EEPROM) it will eventually turn off both LEDx and TRx

    I have a feeling I am going to spend a few months on this...

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lilimike View Post
    I tried your code and I feel like I am getting closer but...
    first the delay was over 1 minute ... Not sure if it is because I am using the internal oscilator ...
    OK, not a problem. You didn't specify an OSC earlier so I chose 20mhz.

    Let's go with 8Mhz internal.
    Change the OSC define and add this line ...
    Code:
    DEFINE OSC 8
    OSCCON = %01110001               ; 8Mhz Internal
    And change TMR0 = 100 to ...
    Code:
    <font color="#000000">    <b>TMR0 </b>= <font color="#800000"><b>193                   </b></font><font color="#0000FF"><b><i>; load timer for 1ms</i></b></font>
    And put DelayTime back to 10000.

    The values for different OSC and Timer reload periods can be found easily with ...

    PicMultiCalc - (Timer Helper)
    http://www.picbasic.co.uk/forum/atta...7&d=1225550328

    My example was trying to show how to deal with 6 different timers without needing 6 Timers.

    I'm affraid the "Instead of"'s will be your job.
    <br>
    DT

  7. #7
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    Fair enough,
    This has brought me to a level beyong what I had expected and its given me tons of ideas for the rest.

    Thank you for your help.

    Mike

Similar Threads

  1. How to drive the Vinculum VDIP1 in UART mode
    By BrianT in forum Code Examples
    Replies: 41
    Last Post: - 23rd May 2013, 12:34
  2. USB-FTDI[UM232R] with PIC16f877a
    By bjox in forum USB
    Replies: 1
    Last Post: - 23rd February 2008, 22:40
  3. Serious Serial Situation Setbacks...
    By Dansdog in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 7th February 2007, 03:46
  4. Serial Output 10 Bits
    By GEEZER in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th May 2005, 02:14
  5. Using 4 input to control 10 output
    By cibotsan in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 5th November 2004, 12:08

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