{"id":2235,"date":"2015-05-10T19:48:21","date_gmt":"2015-05-11T03:48:21","guid":{"rendered":"http:\/\/pididu.com\/wordpress\/?p=2235"},"modified":"2016-01-10T15:15:29","modified_gmt":"2016-01-10T23:15:29","slug":"use-hpwm-on-a-picaxe-processor","status":"publish","type":"post","link":"http:\/\/pididu.com\/wordpress\/blog\/use-hpwm-on-a-picaxe-processor\/","title":{"rendered":"Use hpwm on a PICAXE processor"},"content":{"rendered":"<p>The PICAXE processor, based on the PIC, has a lovely hardware pulse width modulator (hpwm) on board, which is programmed via the <strong>hpwm<\/strong> command. There are 3 modes available: single, half-bridge, and full-bridge. Only half-bridge will be discussed in this article.<\/p>\n<p>A few things about the feature were confusing to me initially, so I&#8217;m documenting some of my learnings here.<\/p>\n<p>Not all PICAXE parts support hpwm. \u00a0Notably, the ultra-cheap 08M2+ does not &#8211; probably because there are not enough I\/O pins to support full-bridge mode. \u00a0In this example, a 14M2+ (firmware version 6.A) is used.<\/p>\n<p>The outputs in half-bridge mode are on the &#8220;hpwm A&#8221; and &#8220;hpwm B&#8221; pins of the chip. \u00a0(Some early versions of the PICAXE documentation had a typo that put one of the outputs on an incorrect pin.) \u00a0The active time of the &#8220;A&#8221; phase is specified by the <strong>duty<\/strong> parameter, and the &#8220;B&#8221; phase is roughly complementary to &#8220;A&#8221;. The pins &#8220;hpwm C&#8221; and &#8220;hpwm D&#8221; are not used by the pulse width modulator in half-bridge mode, and may be used for other purposes if desired.<\/p>\n<p>In the code below, the fundamental clock frequency is set to 32 MHz, or 31.25 nS per cycle. This is the unit of measurement for the <strong>duty<\/strong> parameter. So a <strong>duty<\/strong> of 768 means that the pulse will be active for 768 * 31.25 nS = 24000 nS = 24\u00a0\u03bcS. \u00a0The unit of measurement for the <strong>period<\/strong>\u00a0parameter is 4 clock cycles, and the formula includes an offset of 1. \u00a0So the <strong>period<\/strong> of 255 corresponds to (255+1) * 4 * 31.25 nS = 32000 nS = 32\u00a0\u03bcS. \u00a0In sum, the waveform in the example will have a 24\/32 = 75% duty cycle. The <strong>pwmHHHH<\/strong> sets the polarity of the output pulses to active high. The <strong>deadtime<\/strong> parameter will be discussed later.<\/p>\n<pre>; This program will show the half-bridge mode of the hardware\r\n; pulse width modulator on the PICAXE.\r\n;\r\n; 08-MAY-2015; Roderick.\r\n;\r\n#picaxe 14m2\r\n#no_data\r\n\r\n    symbol deadtime = b0\r\n    symbol period = b1\r\n    symbol duty = w3\r\n\r\n    setfreq m32 ; 32 MHz oscillator frequency\r\n\r\n    deadtime = 0\r\n    period = 255\r\n    duty = 768\r\n\r\n    hpwm pwmhalf, pwmHHHH, deadtime, period, duty\r\n\r\nwaitforever:\r\n    goto waitforever<\/pre>\n<figure id=\"attachment_2236\" aria-describedby=\"caption-attachment-2236\" style=\"width: 731px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/pididu.com\/wordpress\/wp-content\/uploads\/2015\/05\/hpwm-0-deadtime.gif\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-2236\" alt=\"hpwm no deadtime\" src=\"http:\/\/pididu.com\/wordpress\/wp-content\/uploads\/2015\/05\/hpwm-0-deadtime.gif\" width=\"731\" height=\"494\" \/><\/a><figcaption id=\"caption-attachment-2236\" class=\"wp-caption-text\">The hpwm command with approximately a 75% duty cycle. The upper trace (yellow) is the &#8220;A&#8221; output, while the lower trace (green) is the opposite-phase &#8220;B&#8221; output. In this case, the two signals are exact complements of each other.<\/figcaption><\/figure>\n<p>To generate the next trace, this change was made to the program:<\/p>\n<pre>    deadtime = 20<\/pre>\n<p>The dead band delay (deadtime) causes a delay between one phase going inactive and the next phase going active. \u00a0The unit of deadtime consists of 4 fundamental clock cycles. In the case below, a deadtime of 20 translates to 20 * 4 * 31.25 nS = 2500 nS = 2.5 \u03bcS. \u00a0Note that in the trace below <em>each<\/em> of the &#8220;A&#8221; and &#8220;B&#8221; active pulses is 2.5\u00a0\u03bcS narrower than before. \u00a0Incidentally, specifying a deadtime that is too large can completely annihilate the active pulse of either or both phases.<\/p>\n<figure id=\"attachment_2237\" aria-describedby=\"caption-attachment-2237\" style=\"width: 732px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/pididu.com\/wordpress\/wp-content\/uploads\/2015\/05\/hpwm-20-deadtime.gif\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-2237\" alt=\"deadtime value 20\" src=\"http:\/\/pididu.com\/wordpress\/wp-content\/uploads\/2015\/05\/hpwm-20-deadtime.gif\" width=\"732\" height=\"494\" \/><\/a><figcaption id=\"caption-attachment-2237\" class=\"wp-caption-text\">This trace was generated by the same program as the first one, except that the deadtime has been set to 20.<\/figcaption><\/figure>\n<p>As previously mentioned, the period of the waveform above is 32 \u03bcS, and this is, in fact, the maximum possible for a 32 MHz clock frequency. To get a slower waveform, a prescaling divisor of 4, 16, or 64 can be added. The keywords for these are <strong>pwmDIV4<\/strong>, <strong>pwmDIV16<\/strong>, and <strong>pwmDIV64<\/strong>, respectively. (Early documentation had incorrect keywords.) This line of the program was changed as follows to generate the next traces:<\/p>\n<pre>    hpwm pwmDIV4, pwmhalf, pwmHHHH, deadtime, period, duty<\/pre>\n<figure id=\"attachment_2238\" aria-describedby=\"caption-attachment-2238\" style=\"width: 733px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/pididu.com\/wordpress\/wp-content\/uploads\/2015\/05\/hpwm-div4.gif\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-2238\" alt=\"with pwmDIV4 prescale\" src=\"http:\/\/pididu.com\/wordpress\/wp-content\/uploads\/2015\/05\/hpwm-div4.gif\" width=\"733\" height=\"494\" \/><\/a><figcaption id=\"caption-attachment-2238\" class=\"wp-caption-text\">Output waveform using prescale value pwmDIV4. \u00a0Note that the deadtime between active phases is still 2.5 \u03bcS &#8211; unaffected by the pwmDIV4 setting.<\/figcaption><\/figure>\n<p>Below, the same trace zoomed out by a factor of 4, showing the similarity to previous traces in this article. \u00a0 The deadtime of 20 is <em>still based on the fundamental clock frequency,<\/em> and is unaffected by any prescaler such as <strong>pwmDIV4<\/strong>. If more deadtime is needed than can be set according to (127+1) fundamental clocks, then the fundamental clock itself must be slowed with the <strong>setfreq<\/strong> command.<\/p>\n<figure id=\"attachment_2239\" aria-describedby=\"caption-attachment-2239\" style=\"width: 734px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/pididu.com\/wordpress\/wp-content\/uploads\/2015\/05\/hpwm-zoomout-div4.gif\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-2239\" alt=\"20 uS per division\" src=\"http:\/\/pididu.com\/wordpress\/wp-content\/uploads\/2015\/05\/hpwm-zoomout-div4.gif\" width=\"734\" height=\"494\" \/><\/a><figcaption id=\"caption-attachment-2239\" class=\"wp-caption-text\">This is the same as the previous trace, zoomed out by a factor of 4. The deadtime of 2.5 \u03bcS is now proportionately smaller relative to the active pulses.<\/figcaption><\/figure>\n<p>Additional note:   The PICAXE documentation (as of 10\/2015) says that hpwm single mode is not supported on 14M2 or 20M2.  However, as far as I could tell, it worked just fine on a 14M2+.  Possibly their documentation needs updating.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The PICAXE processor, based on the PIC, has a lovely hardware pulse width modulator (hpwm) on board, which is programmed via the hpwm command. There are 3 modes available: single, half-bridge, and full-bridge. Only half-bridge will be discussed in this article. A few things about the feature were confusing to me initially, so I&#8217;m documenting &hellip; <a href=\"http:\/\/pididu.com\/wordpress\/blog\/use-hpwm-on-a-picaxe-processor\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Use hpwm on a PICAXE processor<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[166],"tags":[168,169,167,56,170,171],"_links":{"self":[{"href":"http:\/\/pididu.com\/wordpress\/wp-json\/wp\/v2\/posts\/2235"}],"collection":[{"href":"http:\/\/pididu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pididu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pididu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pididu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2235"}],"version-history":[{"count":0,"href":"http:\/\/pididu.com\/wordpress\/wp-json\/wp\/v2\/posts\/2235\/revisions"}],"wp:attachment":[{"href":"http:\/\/pididu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pididu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2235"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pididu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}