{"id":478,"date":"2014-07-25T12:51:14","date_gmt":"2014-07-25T12:51:14","guid":{"rendered":"https:\/\/ibex.tech\/embedded\/?p=478"},"modified":"2024-06-24T11:51:02","modified_gmt":"2024-06-24T10:51:02","slug":"pwm-2","status":"publish","type":"post","link":"https:\/\/ibex.tech\/embedded\/microchip\/pic18\/c18-compiler\/pwm-2","title":{"rendered":"PWM"},"content":{"rendered":"<p>\n&nbsp;\n<\/p>\n<h4>\nPWM Setup<br \/>\n<\/h4>\n<p>\nPWM Period = [(PR2) + 1] x&nbsp;4 x&nbsp;TOSC x&nbsp;(TMR2 Prescale Value)\n<\/p>\n<p>\nPWM Duty Cycle = (CCPR4L:CCP4CON&lt;5:4&gt;) x TOSC x&nbsp;(TMR2 Prescale Value)\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\nSo your PWM period is adjustable using an 8bit PR# value and a 2 bit timer prescaller value (x1, x4, x16) and is based on the instruction clock speed (4 x TOSC)\n<\/p>\n<p>\nThe duty cycle is 2 bits more than your PR# value as it is based on the osciallator speed (TOSC, not 4 x TOSC).\n<\/p>\n<p>\nThe output pin is high at the start of a new period. &nbsp;When the timer value matches the CCPR#H&nbsp;duty cycle value, and the oscillator or timer prescaller if active matches the low 2 bits CCPR#L the pin is set low.\n<\/p>\n<h4>\nExample<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/-------------------------\r\n\t\/\/----- SETUP TIMER 2 -----\r\n\t\/\/-------------------------\r\n\t\/\/Used for: PWM\r\n\tT2CON = 0b00000110;\t\t\t\t\t\/\/TMR2 on, 1:16 prescale, 1:1 postscale\r\n\tPR2 = 0xff;\t\t\t\t\t\t\t\/\/Sets the PWM period\r\n\r\n\t\/\/-----------------------------\r\n\t\/\/----- SETUP CCP1 AS PWM -----\r\n\t\/\/-----------------------------\r\n\tCCPTMRS0 = 0b00000000;\t\t\/\/CCP1 uses Timer2\r\n\tCCP1CON = 0b00001100;\t\t\/\/PWM mode, bits 5:4 are low 2 bits of 10bit PWM duty cycle\r\n\r\n\t\/\/Set PWM duty cycle\r\n\tCCP1CONbits.DC1B = 0;\t\t\/\/Bits 1:0\r\n\tCCPR1L = 64;\t\t\t\t\/\/Bits 9:2\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; PWM Setup PWM Period = [(PR2) + 1] x&nbsp;4 x&nbsp;TOSC x&nbsp;(TMR2 Prescale Value) PWM Duty Cycle = (CCPR4L:CCP4CON&lt;5:4&gt;) x TOSC x&nbsp;(TMR2 Prescale Value) &nbsp; So your PWM period is adjustable using an 8bit PR# value and a 2 bit timer prescaller value (x1, x4, x16) and is based on the instruction clock speed (4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127,21],"tags":[],"class_list":["post-478","post","type-post","status-publish","format-standard","hentry","category-pwm","category-c18-compiler"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/478","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=478"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":784,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/posts\/478\/revisions\/784"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/embedded\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}