two Loops running on the same programme together

Discussion in 'Embedded Systems and Microcontrollers' started by kasun1993, Jun 18, 2016.

  1. kasun1993

    Thread Starter New Member

    Jun 13, 2016
    8
    0
    i am writing two loop programmes to two pins,, I want to start this two programmes together and run in mikroc,, is there any method of running these two programmes parallely???? help me
    I have attached my programme image,,,, MY.jpg
     
  2. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,951
    387
    The short answer is no, not like that, but you can do it like this:
    Set up a timer to generate an interrupt, perhaps every 200mS. Then on every 3 interrupts toggle port d, and every 10 interrupts toggle port a. This also leaves the uC with lots of free time to do something else.
     
    kasun1993 likes this.
  3. Picbuster

    Member

    Dec 2, 2013
    374
    50
    • Please identify what you want to do and why.
    • two loops creating pulses?
    • pulses in phase if so please identify accuracy and max jitter allowed.(nS/uS/mS)
    • processor used clock speed.
    • I like to help you but need more information
    • Picbuster
     
    kasun1993 likes this.
  4. kasun1993

    Thread Starter New Member

    Jun 13, 2016
    8
    0
    yes my task is to generate clock pulse from second loop and generate a random digital signal(10011100111111 like thisl,,,finally send this clock and data through a serial in parallel out shift register and shift that data on seven segment parallel,,, I tried with ne555 cloc it is difficult to keep the clock in steady frequency,,,,
     
  5. kasun1993

    Thread Starter New Member

    Jun 13, 2016
    8
    0

    tnxx,,, I will try with interuppts
     
  6. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,694
    904
    How are you generating the random signal within the PIC? Have you considered an external generator? Zener noise is a common source. Here's and application note from Maxim on that: https://www.maximintegrated.com/en/app-notes/index.mvp/id/3469 There are may more examples, if you search on zener noise generator.

    John
     
  7. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,951
    387
    You can use a pseudo random generator in PIC code which will generate apparently random numbers if that would do. Most C compilers include one so it easy to use.
     
  8. dannyf

    Well-Known Member

    Sep 13, 2015
    1,811
    362
    Sure. Count time from last action and once that exceeds your desired duration, go into action.

    With that, you can do as many 'loops' as you want.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    While using interrupts will get you more processing time it comes at the expense of more complicated programming.

    For your simple case you can use a simple dumb delay: both loops have a common factor of 100 ms, so you can make one loop with that delay and just check how many times it has run. Every sixth time do one thing, every tenth time do the other.
     
  10. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    In nearly any MCU project there are a number of things you want to do more or less concurrently. Therefore, starting your MCU learning from linear programs (such as blinking LEDs with delays) is really a bad idea, because then you need to re-write all your linear programs to make them work simultaneously. There are a number of ways you can use to ensure concurrent executions. It would take too long to discuss them here in details, so I'm only going to list them:

    - Interleaving the code into a single linear program. Usually a bad idea.
    - Using state machines. Very common and generally good way, but requires more programming.
    - Offloading tasks to interrupts and hardware modules. Very efficient if you can do it right.
    - Cooperative multitasking. A scheduler (usually developed by a third party) is called to switch threads.
    - Using RTOS. Lets you run multiple linear programs, but it's a resource hog
     
    nsaspook and jpanhalt like this.
  11. Picbuster

    Member

    Dec 2, 2013
    374
    50
    AlbertHall is correct use the random (rand command ) from std lib.
    Each C compiler should have it.
    If not available in micro C then move to a Microchip compiler it's also free and does the job.

    set by timer interrupt a flag. ( save time stamp)
    in main calculate random number.
    output the lot.
    reset flag.
    remark: make sure that timer interval > time main loop.
    calculate error and caused by main time loop correct time stamp if needed.
    Picbuster.
     
Loading...