Need help with Counter/Timer application


Closed Thread
Results 1 to 22 of 22

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    Omaha, Nebraska USA
    Posts
    263


    Did you find this post helpful? Yes | No

    Default

    Thanks. I wanted to try something like that, but the problem is the processor may have to do other things while the time is ticking away. Let me show it this way:

    time 0.0 seconds -> We have an input, so we start counting.

    time 0.1 seconds -> Is test1, test2, or test3 equal to 1? No? Let's keep counting.

    . . .

    time 12.3 seconds -> Is test1, test2, or test3 equal to 123? YES, test2 is! Let's quickly perform operation A while counting continues in the background. Is there any test value greater than 123? YES, test3 is, so let's just keep counting

    . . .

    time 45.6 seconds -> Is test1, test2, or test3 equal to 456? YES, test3 is! Let's quickly perform operation B while counting continues in the background. Is there any test value greater than 456? NO!--let's reset the counter to 0 and wait for another input.
    Russ
    N0EVC, xWB6ONT, xWN6ONT

    "Easy to use" is easy to say.

  2. #2
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Russ,
    Have a look at Darrel Taylor's Instant Interrupts. http://darreltaylor.com/DT_INTS-14/intro.html


    From this (and Darrel's elapsed timer) you can get an interrupt driven timer to count out your .1sec intervals.

    Then you can put the conditional tests and assosiated code either:
    a) In the interrupt handler itself (OK if the code is short, but not very good for lots of code)
    b) In your main loop, and poll the timer value for changes.

    This should get you started in the right direction. There are otherways to do this, but Darrel Talyor has generously done all the hard work for you getting the interrupts working.

    HTH,
    Steve

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default A true Multitasking Processor

    BTW - Since you are playing with time (which passes always and so quick and causes us to get older physically), how will you handle an incoming operation say B while the previous operation say A is still in progress?

    Is there a possibility like that?

    Here is another wish I should post to Wish list : A true Multitasking Processor – say four separate operations simultaneously.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    A PIC already multi-tasks...

    It runs multiple Timers all independantly from each other or anything else that's happening...

    It runs Multiple Comparators and Op-Amps (if fitted) all independantly from anything else...

    It can Transmit and Receive on it's USART (if fitted) independantly from anything else...

    It can do ADC conversion independantly from anything else...

    It can do PWM Output independantly from anything else...

    That's because it's got the HARDWARE to do it. However, until we get multi-core PICs, it can only execute one software instruction at a time. Even your desktop PC only executes one instruction at a time (ignoring the new dual-core CPU's), but it time-slices, so it appears like you are running multiple tasks. Time-Slicing is a kernel function of an OS which sits below any higher-level application program. PICs can time-slice too, but because until recently their Program Memory has been limited to only a few Kb, by the time you've added a true multi-tasking kernel, there's not been much room for anything else layered on top.

    So until someone markets an embedded multitasking OS for the PICs, you're limited to writing clever programs that perform seemingly 'simultaneous' functions yourself.

  5. #5
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie
    A PIC already multi-tasks...
    That's because it's got the HARDWARE to do it. However, until we get multi-core PICs, it can only execute one software instruction at a time. ....

    That is what I meant.

    For example:

    Code:
    While PORTB.0 = 0 
      do a kind of loop here...
    wend
    
    While PORTA.0 = 1
      do another kind of loop here...
    wend

    These two while statements can not execute et the same time. This is just an example.

    BTW; humans also multi-task albeit most of us do not even realize that we do.


    ---------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  6. #6
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer
    That is what I meant.

    For example:

    Code:
    While PORTB.0 = 0 
      do a kind of loop here...
    wend
    
    While PORTA.0 = 1
      do another kind of loop here...
    wend

    These two while statements can not execute et the same time.

    ---------------
    True... because of the way the program is written it will repeatedly service the PortB loop until such time as PORTB.0 = 1

    If you want BOTH loops to be serviced then there is a way of doing that. Admittedly they wont be serviced simultaneously but each will be repeatedly serviced

    Code:
    ExampleLoop:
    
    If PORTB.0 = 0 then
      do a kind of loop here...
    Endif
    
    If PORTA.0 = 1
      do another kind of loop here...
    Endif
    
    Goto ExampleLoop
    I have a project where a PIC is decoding IR signals and also receiving short commands over a serial port. The main loop is basically

    Code:
    Main:
    Gosub CheckForIR
    Gosub CheckForSerial
    Goto Main
    The IR check takes the most time but that is the main activity that the PIC has to do. Serial data is received by the USART so will buffer upto two characters. Servicing the Serial data normally takes only 1ms so that it returns more or less immediately unless serial data is received at which point it waits in the serial loop a bit longer to receive any extra data as that now becomes the priority.

    Its not true multi tasking but everything does get done OK
    Keith

    www.diyha.co.uk
    www.kat5.tv

  7. #7
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by keithdoxey
    .....

    True... because of the way the program is written it will repeatedly service the PortB loop until such time as PORTB.0 = 1

    If you want BOTH loops to be serviced then there is a way of doing that. Admittedly they wont be serviced simultaneously but each will be repeatedly serviced.....

    I wanted to indicate that when a continious loop is in progress and you need that progress as long as your circuit is in operation, you will not be able to do other things.

    In my example above, "While" statements have loops inside.
    They are not a single line of instructions, they are loops.

    One must be completed so that other can run. But, if you need an infinite loop, it will never exit that loop.

    However, if we had a dual core PIC as Melanie mentioned or quad core, then you can do a simple thing as below. Say CORE1, CORE2, CORE3 etc. are new commands of PBP's 2010 version.


    Code:
    CORE1    'Separate Job Handler.
    
    While PORTB.0 = 0 
       Do a loop here....
    Wend
    
    END CORE1
    
    CORE2     'Separate Job Handler.
    
    While PORTB.A = 1 
       Do a loop here....
    Wend
    
    END CORE2
    
    
    CORE3      'Separate Job Handler.
    ...
    END CORE3
    
    ..
    ...
    ....
    
    etc...
    they all work simultaneously.


    Am I being too much imaginery here?
    Last edited by sayzer; - 1st September 2006 at 10:24.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  8. #8
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer
    true Multitasking Processor – say four separate operations simultaneously.
    Which is fine until you want to do six things simultaneously.

    As Melanie said
    Even your desktop PC only executes one instruction at a time (ignoring the new dual-core CPU's), but it time-slices, so it appears like you are running multiple tasks.
    My laptop is currently running many programs in the background and appears to be a speedy machine but when I first boot it up it takes forever due to the heavy processor demands for each of the applications starting up.

    To achieve the appearance of effective multitasking you must prioritise each task and ensure that a routine to service that task is performed often enough for the program to work.

    eg. Task levels "Critical", "Important", "Menial"

    Code:
    Main:
    
    For MenialTasks = 0 to 5
       For ImportantTasks = 0 to 3
          Gosub CriticalTask
          Select Case ImportantTasks
              Case 0 
                  Gosub MostImportantTask
              Case 1 
                  Gosub LessImportantTask1
              Case 2 
                  Gosub MostImportantTask
              Case 3 
                  Gosub LessImportantTask2
          End Select
       Next ImportantTasks
       Select Case MenialTasks
              Case 0 
                  Gosub MenialTask1
              Case 1 
                  Gosub MenialTask2
              Case 2 
                  Gosub MenialTask3
              Case 3 
                  Gosub MenialTask4
              Case 4 
                  Gosub MenialTask5
              Case 5 
                  Gosub MenialTask6
          End Select
    Next MenialTasks
    
    Goto Main
    To complete both the nested loops takes 24 circuits.
    (6 MenialTasks * 4 ImportantTasks).

    If you look you will see that the subroutines called are...

    CriticalTask - 24 times (once every loop of the ImportantTasks)
    MostImportantTask - 12 times (every other loop of ImportantTasks)
    LessImportantTask 1 & 2 - 6 times each (once per ImportantTasks loop)

    Each MenialTask gets called once for every complete cycle of both loops.

    You just need to figure out the most important things to do and how to keep track of what you are doing
    Keith

    www.diyha.co.uk
    www.kat5.tv

  9. #9
    Join Date
    Aug 2006
    Location
    Omaha, Nebraska USA
    Posts
    263


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer
    BTW - Since you are playing with time (which passes always and so quick and causes us to get older physically), how will you handle an incoming operation say B while the previous operation say A is still in progress?

    Is there a possibility like that?
    Sad to say, yes there is. That's why I'm having one PIC handling the user interface, inputs, and master timing, and other PICs performing the requisite operations.

    Here is another wish I should post to Wish list : A true Multitasking Processor – say four separate operations simultaneously.
    As you can see, I'm kind of approaching that with primitive "distributed (or parallel) processing".

    All of my prior work over the years has involved the Z80, the 8051 family, and the 68HCxx line, always with "glue" in the form of support chips (RAM, PROM, EEPROM, SPIs and/or PPIs--you know), so it's kind of fun to look at "doing it all with PICs large and small".

    PS: A lot of my practical PIC education is coming from reading Melanie's posts in this forum (and in the archives). Melanie, I'd like to nominate you for goddess.
    Last edited by RussMartin; - 1st September 2006 at 05:29.
    Russ
    N0EVC, xWB6ONT, xWN6ONT

    "Easy to use" is easy to say.

Similar Threads

  1. Simple TMR1 application example needed
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 4th February 2010, 09:52
  2. Unusual Interrupts Application Problem
    By Joe Rocci in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 8th May 2009, 11:55
  3. PIC16F877A for application in mobile robot...
    By mcbeasleyjr in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 30th January 2009, 14:47
  4. DS1307 automotive application
    By DynamoBen in forum Schematics
    Replies: 10
    Last Post: - 31st August 2006, 00:33
  5. Timer Application
    By charudatt in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th February 2004, 06:05

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