2 Replies Latest reply on Mar 24, 2015 4:02 AM by sglee367

    How to sync timer interrupts to an external signal ?

    sglee367

      Hi, I'm working on a project using a Galileo gen2.

       

      I'm writing codes using Visual Micro, so I'm doing almost in the same way as I've been doing with other Arduino boards.

       

      Now i should make my Galileo to do some job every 10ms (100Hz, therefore) and that Interrupt Service Routine should be synced to an external signal.

      That external signal is a pulse signal and it signals the Galileo every second.

       

      So I think my Galileo should work in this way :

      1) request an interrupt every second (pin-change interrupt)

      2) in pin-change ISR, enable a timer, which overflows every 10ms, thus make an timer-overflow interrupt requested every 10ms

      3) in the timer-overflow, check how many times the ISR has been called. If it does not exceeds 100 times, do some jobs that were originally intended.

       

      Here, I can make my Galileo interrupted every second using attachInterrupt() function

      but the problem is the timer-overflow interrupt.

       

      I wrote like this.

      ------------------------------------------------------------------------------------------------------------------

      void setup(){

           attachInterrupt(PIN_PPS, ISR, FALLING);

      }

      void loop(){
      }

      void ISR(){

           Timer1.initialize(5000);

           Timer1.attachInterrupt(ISR_Timer,5000);
      }

      volatile unsigned int TimerLoopCount = 0;

      void ISR_Timer(){

           if(TimerLoopCount < 100){

                TimerLoopCount++;

                // do some jobs that were originally intended

           else{   

                TimerLoopCount = 0;

           }

      }

      ------------------------------------------------------------------------------------------------------------------


      Here, I found that when I call Timer1.attachInterrupt method, the pin-change interrupt (attachInterrupt(PIN_PPS, ISR, FALLING);) dies out.

      Timer1 does not work, either. (The result is the same when I call Timer1.initialize in the setup(){} block.)

      Since I'm a newbie in stuff like system programming and embedded programming, I'm kinda lost now.


      How can I make this work?

      or Where can I find some solutions or guidelines I can learn something related to this?


      Thank you so much for your help in advance!

        • 1. Re: How to sync timer interrupts to an external signal ?
          Intel_Peter

          Hello sglee367,

           

          Have you updated your board to the latest firmware? Also, can I see your full code?

           

          Peter.

          • 2. Re: How to sync timer interrupts to an external signal ?
            sglee367

            Hi, Peter, and thank you for your reply!

            My board is already on the latest firmware.

            Actually, I think I solved the problem. I've just edited the 'interrupt.c', 'interrupt.h', 'TimerOne.cpp', 'TimerOne.h' so that I can now reset the 'ct' value (of the timer interrupt that I attached, which can be found in the interrupt structure in the 'interrupt.c') to zero when I call a certain function from the loop(){} block or somewhere. Therefore, if I reset the 'ct' value of the ISR that should run every 10ms, it will be on sync with the external signal.

            Since I'm now working on another similar work, I'm not able to test it right now, but I think I can test my code tomorrow or in a few days and share here. Thank you