{"id":465,"date":"2014-06-04T10:29:18","date_gmt":"2014-06-04T10:29:18","guid":{"rendered":"https:\/\/ibex.tech\/embedded\/?p=465"},"modified":"2022-02-18T15:37:49","modified_gmt":"2022-02-18T15:37:49","slug":"timers","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/dspic\/xc16-dspic\/timers","title":{"rendered":"Timers"},"content":{"rendered":"<h4>\nSetup Timers<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n#include &lt;timer.h&gt;\r\n\r\n\t\/\/----- SETUP TIMER 4\/5 -----\r\n\t\/\/Used for: Heartbeat\r\n\tOpenTimer45(T4_ON &amp; T4_GATE_OFF &amp; T4_PS_1_1 &amp; T4_SOURCE_INT, (INSTRUCTION_CLOCK_FREQUENCY \/ 1000));\t\t\/\/1mS irq, 1:1 prescaller\r\n\tWriteTimer45(0);\r\n\tConfigIntTimer45(T4_INT_PRIOR_1 &amp; T4_INT_ON);\t\t\t\/\/Interrupt priority level 1\r\n\r\n\t\/\/----- SETUP TIMER 6 -----\r\n\t\/\/Used for: Buzzer PWM\r\n\t\/\/4000Hz = 250uS per complete PWM cycle\r\n\tT6CON = 0x8000;\t\t\t\t\/\/Timer on, 16bit, 1:1 (Fosc \/2)\r\n\tPR6 = (INSTRUCTION_CLOCK_FREQUENCY \/ 8000);\r\n\tTMR6 = 0;\r\n\r\n\t_T6IP = 4;\t\t \t\t\/\/7(highest) to 1(lowest)\r\n\t_T6IF = 0;\r\n\t_T6IE = 0;\r\n\r\n\t\/\/----- SETUP TIMER 7 -----\r\n\t\/\/Used for: \r\n\t\/\/Trigger every 60uS\r\n\tT7CON = 0x8000;\t\t\t\t\/\/Timer on, 16bit, 1:1 (Fosc \/2)\r\n\tPR7 = (60 * 40);\t\t\t\/\/40 cycles per 1uS\r\n\tTMR7 = 0;\r\n\r\n\t_T7IP = 3;\t\t \t\t\/\/7(highest) to 1(lowest)\r\n\t_T7IF = 0;\r\n\t_T7IE = 1;\r\n<\/code><\/pre>\n<h5>\nAlternatively setup 32bit timer without the library<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t\/\/----- SETUP TIMER 4\/5 -----\r\n\t\/\/Used for: Heartbeat\r\n\tT5CONbits.TON = 0;\t\t\t\/\/Stop any 16-bit Timer3 operation\r\n\tT4CONbits.TON = 0;\t\t\t\/\/Stop any 16\/32-bit Timer3 operation\r\n\tT4CONbits.T32 = 1;\t\t\t\/\/Enable 32-bit Timer mode\r\n\tT4CONbits.TCS = 0;\t\t\t\/\/Select internal instruction cycle clock\r\n\tT4CONbits.TGATE = 0;\t\t\/\/Disable Gated Timer mode\r\n\tT4CONbits.TCKPS = 0b00;\t\t\/\/Select 1:1 Prescaler\r\n\tTMR5 = 0x00;\t\t\t\t\/\/Clear 32-bit Timer (msw)\r\n\tTMR4 = 0x00;\t\t\t\t\/\/Clear 32-bit Timer (lsw)\r\n\tPR5 = (INSTRUCTION_CLOCK_FREQUENCY \/ 1000) &gt;&gt; 16;\t\t\/\/Load 32-bit period value (msw)\r\n\tPR4 = (INSTRUCTION_CLOCK_FREQUENCY \/ 1000) &amp; 0xffff;\t\/\/Load 32-bit period value (lsw)\r\n\t_T5IP = 0x01;\t\t\t\t\/\/Set Timer Interrupt Priority Level - 7(highest) to 1(lowest)\r\n\t_T5IF = 0;\t\t\t\t\t\/\/Clear Timer Interrupt Flag\r\n\t_T5IE = 1;\t\t\t\t\t\/\/Enable Timer interrupt\r\n\tT4CONbits.TON = 1;\t\t\t\/\/Start 32-bit Timer\r\n<\/code><\/pre>\n<h4>\nTimer Interrupt Functions<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\/\/Timer 45 Interrupt\r\nvoid __attribute__((interrupt, auto_psv)) _T5Interrupt(void)\r\n{\r\n\t_T5IF = 0;\t\t\t\t\t\t\t\t\t \/\/Clear interrupt flag\r\n\r\n\/\/Timer 6 interrupt\r\nvoid __attribute__((interrupt, auto_psv)) _T6Interrupt(void)\r\n{\r\n\t_T6IF = 0;\t\t\t\t\t\t\t\t\t \/\/Clear interrupt flag\r\n\r\n\/\/Timer 7 interrupt\r\nvoid __attribute__((interrupt, auto_psv)) _T7Interrupt(void)\r\n{\r\n\t_T7IF = 0;\t\t\t\t\t\t\t\t\t \/\/Clear interrupt flag\r\n\t\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setup Timers #include &lt;timer.h&gt; \/\/&#8212;&#8211; SETUP TIMER 4\/5 &#8212;&#8211; \/\/Used for: Heartbeat OpenTimer45(T4_ON &amp; T4_GATE_OFF &amp; T4_PS_1_1 &amp; T4_SOURCE_INT, (INSTRUCTION_CLOCK_FREQUENCY \/ 1000)); \/\/1mS irq, 1:1 prescaller WriteTimer45(0); ConfigIntTimer45(T4_INT_PRIOR_1 &amp; T4_INT_ON); \/\/Interrupt priority level 1 \/\/&#8212;&#8211; SETUP TIMER 6 &#8212;&#8211; \/\/Used for: Buzzer PWM \/\/4000Hz = 250uS per complete PWM cycle T6CON = 0x8000; \/\/Timer on, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-465","post","type-post","status-publish","format-standard","hentry","category-xc16-dspic"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/comments?post=465"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/465\/revisions"}],"predecessor-version":[{"id":473,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/465\/revisions\/473"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}