{"id":412,"date":"2012-10-16T16:42:05","date_gmt":"2012-10-16T16:42:05","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=412"},"modified":"2023-12-06T18:32:56","modified_gmt":"2023-12-06T18:32:56","slug":"clock_gettime-for-acurate-timing","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/timing\/clock_gettime-for-acurate-timing","title":{"rendered":"clock_gettime() For Acurate Timing"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Setting up to use clockgettime()<\/h4>\n\n\n\n<p>Include the following header file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;time.h><\/code><\/pre>\n\n\n\n<p>You also need to add the&nbsp;librt library to the linker<\/p>\n\n\n\n<p>Using a&nbsp;Geany&nbsp;makefile, add this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-lrt<\/code><\/pre>\n\n\n\n<p>Using eclipse:<\/p>\n\n\n\n<p>Right click your project &gt; Properties &gt; C\/C++ Build &gt; Settings &gt; Cygwin C++ Linker &gt; Libraries. &nbsp;Add a new library entry: rt<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example Which Provides Accurate uS Timing<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/*****************************************************\n\/\/*****************************************************\n\/\/********** DELAY FOR # uS WITHOUT SLEEPING **********\n\/\/*****************************************************\n\/\/*****************************************************\n\/\/Using delayMicroseconds lets the linux scheduler decide to jump to another process.  Using this function avoids letting the\n\/\/scheduler know we are pausing and provides much faster operation if you are needing to use lots of delays.\nvoid DelayMicrosecondsNoSleep (int delay_us)\n{\n\tlong int start_time;\n\tlong int time_difference;\n\tstruct timespec gettime_now;\n\n\tclock_gettime(CLOCK_REALTIME, &amp;gettime_now);\n\tstart_time = gettime_now.tv_nsec;\t\t\/\/Get nS value\n\twhile (1)\n\t{\n\t\tclock_gettime(CLOCK_REALTIME, &amp;gettime_now);\n\t\ttime_difference = gettime_now.tv_nsec - start_time;\n\t\tif (time_difference &lt; 0)\n\t\t\ttime_difference += 1000000000;\t\t\t\t\/\/(Rolls over every 1 second)\n\t\tif (time_difference > (delay_us * 1000))\t\t\/\/Delay for # nS\n\t\t\tbreak;\n\t}\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Example which creates a background heartbeat timer for your projects main loop<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>long int last_heartbeat;\nlong int heartbeat_difference;\nstruct timespec gettime_now;\nint io_state = 0;\nBYTE hb_10ms_timer = 0;\nBYTE hb_100ms_timer = 0;\n\n\n\t\/\/SETUP HEARTBEAT TIMER\n\tclock_gettime(CLOCK_REALTIME, &amp;gettime_now);\n\tlast_heartbeat = gettime_now.tv_nsec;\n\n\n\n\t\/\/---------------------------\n\t\/\/---------------------------\n\t\/\/----- HEARTBEAT TIMER -----\n\t\/\/---------------------------\n\t\/\/---------------------------\n\tclock_gettime(CLOCK_REALTIME, &amp;gettime_now);\n\theartbeat_difference = gettime_now.tv_nsec - last_heartbeat;\t\t\/\/Get nS value\n\tif (heartbeat_difference &lt; 0)\n\t\theartbeat_difference += 1000000000;\t\t\t\t\/\/(Rolls over every 1 second)\n\tif (heartbeat_difference > 1000000)\t\t\t\t\t\/\/&lt;&lt;&lt; Heartbeat every 1mS\n\t{\n\t\t\/\/-------------------------\n\t\t\/\/----- 1mS HEARTBEAT -----\n\t\t\/\/-------------------------\n\t\tlast_heartbeat += 1000000;\t\t\t\t\t\t\/\/&lt;&lt;&lt; Heartbeat every 1mS\n\t\tif (last_heartbeat > 1000000000)\t\t\t\t\/\/(Rolls over every 1 second)\n\t\t\tlast_heartbeat -= 1000000000;\n\t\n\t\n\t\n\t\t\/\/Toggle a pin so we can verify the heartbeat is working using an oscilloscope\n\t\tio_state ^= 1;\t\t\t\t\t\t\t\t\t\/\/Toggle the pins state\n\t\tbcm2835_gpio_write(RPI_GPIO_P1_23, io_state);\n\t\n\t\n\t\n\t\n\t\t\/\/--------------------------\n\t\t\/\/----- 10mS HEARTBEAT -----\n\t\t\/\/--------------------------\n\t\thb_10ms_timer++;\n\t\tif (hb_10ms_timer == 10)\n\t\t{\n\t\t\thb_10ms_timer = 0;\n\t\n\t\n\t\n\t\t} \/\/if (hb_10ms_timer == 10)\n\t\n\t\n\t\t\/\/---------------------------\n\t\t\/\/----- 100mS HEARTBEAT -----\n\t\t\/\/---------------------------\n\t\thb_100ms_timer++;\n\t\tif (hb_100ms_timer == 100)\n\t\t{\n\t\t\thb_100ms_timer = 0;\n\t\n\t\n\t\n\t\t} \/\/if (hb_100ms_timer == 100)\n\t\n\t} \/\/if (heartbeat_difference > 1000000)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">System timer based 1mS timer<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tstatic long int OurSystemTimerValueLast;\n\tlong int TimeDifference;\n\tstruct timespec gettime_now;\n\n\t\/\/----- START OUR SYSTEM TIMER -----\n\tclock_gettime(CLOCK_REALTIME, &amp;gettime_now);\n\tOurSystemTimerValueLast = gettime_now.tv_nsec;\t\t\/\/Get nS value\n\t\n\t\n\t\n\t\/\/----- UPDATE OUR MS TIMER -----\n\tclock_gettime(CLOCK_REALTIME, &amp;gettime_now);\n\tTimeDifference = gettime_now.tv_nsec - OurSystemTimerValueLast;\n\tOurSystemTimerValueLast = gettime_now.tv_nsec;\n\tif (TimeDifference &lt; 0)\n\t\tTimeDifference += 1000000000;\t\t\t\t\/\/(Rolls over every 1 second)\n\t\n\twhile (TimeDifference > 1000000)\t\t\/\/TimeDifference is in nS\n\t{\n\t\tTimeDifference -= 1000000;\n\t\t\/\/----- 1mS TIMER -----\n\t\t\n\t}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Setting up to use clockgettime() Include the following header file: You also need to add the&nbsp;librt library to the linker Using a&nbsp;Geany&nbsp;makefile, add this: Using eclipse: Right click your project &gt; Properties &gt; C\/C++ Build &gt; Settings &gt; Cygwin C++ Linker &gt; Libraries. &nbsp;Add a new library entry: rt Example Which Provides Accurate uS Timing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-412","post","type-post","status-publish","format-standard","hentry","category-timing"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=412"}],"version-history":[{"count":14,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/412\/revisions"}],"predecessor-version":[{"id":3700,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/412\/revisions\/3700"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}