How to use a Timer with Arduino ?

Discussion in 'UDOO NEO' started by cyrilf, Oct 7, 2016.

  1. cyrilf

    cyrilf Active Member

    Joined:
    Feb 19, 2015
    Messages:
    168
    Likes Received:
    47
    Hi,

    I would like to use the analog input from the Arduino on the UDOO Neo. Analog inputs are useless if you cannot read at a certain frequency. So my question is how to use a timer to read analog input on the Arduino?

    I tried the TimerOne library but I get an error at compile time :
    Code:
    WARNING: library TimerOne claims to run on [avr] architecture(s) and may be incompatible with your current board which runs on [solox] architecture(s).
    ~/Documents/src/arduino/sketch_sep15b/sketch_sep15b.ino: In function 'void setup()':
    sketch_sep15b:44: error: 'Timer9' was not declared in this scope
      Timer9.initialize(150000);
      ^
    
    I also tried other libraries such as Metro, TimerDue, MsTimer, ... without success.

    Thanks :)
     
    Last edited: Oct 7, 2016
  2. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
  3. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
  4. cyrilf

    cyrilf Active Member

    Joined:
    Feb 19, 2015
    Messages:
    168
    Likes Received:
    47
    Hi,

    Thanks for your reply :)

    I need to be able to read at a certain frequency (mesure the time beetween each reads) as precise as possible. Adding a lag with millis() is not a solution as reading analog input takes a few microseconds. Usually, the frequency of reading is about 100-200 us, 1ms is far too long :/
     
    Last edited: Oct 10, 2016
  5. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Andrea Rovai likes this.
  6. cyrilf

    cyrilf Active Member

    Joined:
    Feb 19, 2015
    Messages:
    168
    Likes Received:
    47
    Well, I edited the source code of SimpleTimer and I uncommented the line to use micros() instead of millis(). I'll try for my needs.
     
    waltervl likes this.
  7. cyrilf

    cyrilf Active Member

    Joined:
    Feb 19, 2015
    Messages:
    168
    Likes Received:
    47
    I was not able to use micros() but I found the HWtimer library in the board package (udooneo-arduino-solox-1.6.7/variants/udooneo/mqx/release/bsp/hwtimer.h). Here is the documentation on how to use the timer on Freescale MQX.

    An example with a 200us period timer:
    Code:
    #include <imx6sx_sdb_m4.h>
    #include <hwtimer_epit.h>
    
    #define PERIOD 200
    
    #define NB_MESURES 600
    float values[NB_MESURES];
    
    HWTIMER timer;
    
    void readValue(void *param)
    {
      values[n] = analogRead(PIN);
      n = (n + 1) % NB_MESURES;
    }
    
    void setup()
    {
      analogReadResolution(12);
      Serial.begin(115200);
    
      hwtimer_init(&timer, &BSP_HWTIMER1_DEV, BSP_HWTIMER1_ID, 1);
      hwtimer_set_period(&timer, BSP_HWTIMER1_SOURCE_CLK, PERIOD);
      hwtimer_callback_reg(&timer, readValue, 0);
      hwtimer_start(&timer);
    }
    
    void loop()
    {
    }
     
    Last edited: Oct 21, 2016
    modjo and waltervl like this.

Share This Page