Trapping a flag/state change (rapidly)


Results 1 to 10 of 10

Threaded View

  1. #8
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default Re: Trapping a flag/state change (rapidly)

    Quote Originally Posted by HankMcSpank View Post
    ....but I need a whole heap of different pauses (& some quite longish...I've always had an epic fail when I've tried to use timers for long pauses!) ....now I realise I could just reset the timer preload value accordingly each time I need a different pause, but ....aaargh ....brainache. I guess that's what it comes down to... a solution that avoids brainache ...
    Here is pseudocode that blinks 2 LEDs at different speeds (1Hz and 2Hz) and does not use any pause (blocking delay) type commands. You can easily expand it to any number of LEDs (or tasks) with no performance degradation. It uses a single hardware timer to provide a system tick (also known as a heartbeat). It can be used as a template to create simple multitasking applications.
    Code:
    sub timer1 interrupt
      inc(cyctick1)
      inc(cyctick2)
    
      if cyctick1 > 500      ' 500mS delay, 1st. counter
        cyctick1 = 0         ' reset counter
        cycflag1 = 1         ' set flag        
      endif
      if cyctick2 > 250      ' 250mS delay, 2nd. counter
        cyctick2 = 0         ' reset counter
        cycflag2 = 1         ' set flag 
      endif
    end sub  
    
    sub usrfunction1
      toggle LED0
    end sub
    
    sub usrfunction2
      toggle LED1
    end sub
    
    main:
      cyctick1=0:cyctick2=0 ' Initialize variables
      ccyflag1=0:cycflag2=0 '         "
      
      timer1 = 1000         ' set up timer1 to interrupt every 1mS
    
      while true
        if cycflag1=1 then  ' check if 500mS has expired
          cycflag1 = 0      ' reset flag 1
          do userfunction1
        endif
        if cycflag2=1 then  ' check if 250mS has expired
          cycflag2 = 0      ' reset flag 2
          do userfunction2
        endif
      wend
    Last edited by rmteo; - 4th May 2011 at 01:20.
    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

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