{"id":158,"date":"2004-07-08T19:06:39","date_gmt":"2004-07-09T00:06:39","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/category\/code\/picbasic-pro\/158"},"modified":"2012-03-08T12:03:02","modified_gmt":"2012-03-08T17:03:02","slug":"simple-pwm-in-picbasic-pro","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/picbasic-pro\/158\/","title":{"rendered":"Simple PWM in PicBasic Pro"},"content":{"rendered":"<p><span style=\"font-family: Monaco; font-size: 9px;\">This is an example of pulsewidth modulation for the PIC. The value received from an analog input is used to dim an LED, using the PWM command. The LED is on RD2, and the analog in is on RA0.<\/span><\/p>\n<p style=\"font: 9.0px Monaco;\">The PWM command has three parameters: the pin, the duty cycle (a byte), and the number of times to pulse the pin (a word). The duty cycle is how long the pin is on for each cycle. If the duty cycle is 100% (255), then the pin is on all the time. A duty cycle of 50% turns the pin on for half of each cycle, and so forth.<\/p>\n<p style=\"font: 9.0px Monaco;\">At 4MHz, one on-off cycle is about 5 milliseconds. A higher number of cycles makes for smoother PWMing, but less interactivity, because the PIC does nothing else until it&#8217;s finished all the cycles for each PWM command.<\/p>\n<p><!--more--><\/p>\n<pre>' Simple PWM.\r\n' by Tom Igoe, 2004\r\n\r\n' This example takes an analog input on RA0 and uses it to generate\r\n' a duty cycle for the PWM command. The PWM command is used\r\n' to dim an LED on pin RD0.\r\n\r\n ' Define ADCIN parameters\r\nDEFINE  ADC_BITS        10      ' Set number of bits in result\r\nDEFINE  ADC_CLOCK       3       ' Set clock source (3=rc)\r\nDEFINE  ADC_SAMPLEUS    15      ' Set sampling time in uS\r\n\r\nadcVar  VAR WORD    ' ADC  result\r\ndutyCycle var byte  ' Duty cycle for PWM\r\n\r\n' Set PORTA to all input\r\nTRISA = %11111111\r\n' Set up ADCON1\r\nADCON1 = %10000010 \r\n\r\noutput portc.3\r\n\r\nmain:\r\n    ADCIN 0, adcVar\r\n    ' convert ADC value to a byte value:\r\n    dutyCycle = adcVar \/ 4\r\n    ' PWM the LED. The third parameter is the number of\r\n    ' cycles to repeat the PWM.  at 4MHz, 1 cycle = 5 ms.\r\n    pwm portd.2, dutyCycle, 10\r\ngoto main<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is an example of pulsewidth modulation for the PIC. The value received from an analog input is used to dim an LED, using the PWM command. The LED is on RD2, and the analog in is on RA0. The PWM command has three parameters: the pin, the duty cycle (a byte), and the number &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tigoe.com\/pcomp\/code\/picbasic-pro\/158\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Simple PWM in PicBasic Pro&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100,10],"tags":[],"class_list":["post-158","post","type-post","status-publish","format-standard","hentry","category-code","category-picbasic-pro"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":2,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"predecessor-version":[{"id":1094,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/158\/revisions\/1094"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}