This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

comprehension question Timer

Hi,

maybe i have a silly question, but i didn't programm timers before. I know how they work. But how is it possible that there are only three Timers in the 51822 and so many parts of the programm part (PWM,BLE,GPIOTE...) which need a timer? How can i organize my timers?

and is it possible to run one timer and write a function like "get_actual_time" which gives me the time in the moment i call the function?

Best reagards, Nils :)

Parents
  • Hi Nils,

    I don't have any sample code of the round robin scheduler that I can share.

    But I have attached my code for setting up RTC1 for a 1ms interrupt, with two 32 bit counters for milliseconds and seconds since the "beginning of time" .

    To build the scheduler, you would add the timer_variables and bool stuff after line 73.

    You can use either of the two 32 bit counters to do some other neat things in your code, like this:

    [b] temp = RTC1_Milliseconds; while(RTC1_Milliseconds < (temp+37)) {} // wait here for 37 ms

    temp = RTC1_Seconds;
    {
        // lots of stuff goes in here that takes a long time to execute
    }
    printf("Execution time in seconds:  %6d\n", RTC1_Seconds - temp);[/b]
    

    The example code supplied refers to one of my header files, which must also be included in routines that use this stuff. the only relevant lines to this example in the header are:

    [b]extern uint32_t RTC1_Milliseconds; extern uint32_t RTC1_Seconds; [/b]

    Enjoy.

    RTC1_ms_timer.c

  • The accuracy of the frequency you get from RTC0 or 1 depends on the accuracy of LFCLK. In my example function RTC1_ms_timer.c , I have selected the RC oscillator on line 36. In the reference manual on page 54, LFCLKSRC has 3 possible choices. If you use either the synthesizer, or the 32768 Hz external crystal, you will have accuracy that depends on the crystal, probably around 60 ppm. On page 34 of the product spec, the spec fTOL,RC32k is given as +/- 2% , which is +/- 20000 ppm .

    There is also a calibration process that can use the 16MHz oscillator to temporarily (4 seconds according to fTOL,CAL,RC32k) improve accuracy of the RC oscillator to +/- 250 ppm. I've read the description in the ref manual at 12.1.3 , page 51, the information provided is insufficient for me to figure out how this should be used. I could not find any examples for this part of the chip.

    If you just want an approximate 1 ms tick, then the RC oscillator may be sufficient. If you need a more accurate tick, then use either the synthesizer (which needs the 16MHz crystal) or use the 32768 Hz crystal.

    Also, although you write "a perfect millisecond" , I suspect that it is just close, rather than perfect. Watch the signal on the oscilloscope, as you apply heat (hot air gun) or cold (can of instant freeze) to the chip, and you will see that it is temperature dependent. Probably also dependent on VCC.

Reply
  • The accuracy of the frequency you get from RTC0 or 1 depends on the accuracy of LFCLK. In my example function RTC1_ms_timer.c , I have selected the RC oscillator on line 36. In the reference manual on page 54, LFCLKSRC has 3 possible choices. If you use either the synthesizer, or the 32768 Hz external crystal, you will have accuracy that depends on the crystal, probably around 60 ppm. On page 34 of the product spec, the spec fTOL,RC32k is given as +/- 2% , which is +/- 20000 ppm .

    There is also a calibration process that can use the 16MHz oscillator to temporarily (4 seconds according to fTOL,CAL,RC32k) improve accuracy of the RC oscillator to +/- 250 ppm. I've read the description in the ref manual at 12.1.3 , page 51, the information provided is insufficient for me to figure out how this should be used. I could not find any examples for this part of the chip.

    If you just want an approximate 1 ms tick, then the RC oscillator may be sufficient. If you need a more accurate tick, then use either the synthesizer (which needs the 16MHz crystal) or use the 32768 Hz crystal.

    Also, although you write "a perfect millisecond" , I suspect that it is just close, rather than perfect. Watch the signal on the oscilloscope, as you apply heat (hot air gun) or cold (can of instant freeze) to the chip, and you will see that it is temperature dependent. Probably also dependent on VCC.

Children
No Data
Related