{"id":4033,"date":"2022-02-01T15:13:22","date_gmt":"2022-02-01T13:13:22","guid":{"rendered":"https:\/\/www.dclabs.lt\/dainiaus\/?p=4033"},"modified":"2022-02-01T15:39:21","modified_gmt":"2022-02-01T13:39:21","slug":"stm32-pwm-prescaler-calculator-for-pwm-frequency","status":"publish","type":"post","link":"https:\/\/www.dclabs.lt\/dainiaus\/stm32-pwm-prescaler-calculator-for-pwm-frequency\/","title":{"rendered":"STM32 PWM prescaler calculator for PWM frequency"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Just to have a quick way to calculate the prescaler value for required PWM frequency&#8230; Made this while testing the Nucleo-F446RE devboard with STM32F446RE processor, should be valid for all STM32-s.<\/p>\n\n\n\n<!DOCTYPE html>\n<html>\n   <head>\n      <title>Prescaler calculator<\/title>\n\t  <meta charset=\"utf-8\"\/>\n      <script type=\"text\/javascript\">\n\t  \n\t\t\t\tfunction calculate_prescaler()\n\t\t\t\t{\n\n\t\t\t\t\tvar clock_frequency = document.getElementsByName('clk_frq')[0].value;\n\t\t\t\t\tvar wished_pwm_steps = document.getElementsByName('pwm_stp')[0].value;\n\t\t\t\t\tvar wished_pwm_frequency = document.getElementsByName('pwm_frq')[0].value;\n                    var prescaler_value = (((parseFloat(clock_frequency) * 1000000) \/ parseFloat(wished_pwm_steps) \/ parseFloat(wished_pwm_frequency))-1);\n                    \n\t\t\t\t\tif (prescaler_value >= 0 && prescaler_value <= 65535) \n\t\t\t\t\t{\t\t\t\t\t\t\n\t\t\t\t\t\tdocument.getElementsByName('prescaler')[0].value= prescaler_value;\n\t\t\t\t\t\tdocument.getElementsByName('prescaler_rnd')[0].value= Math.round(prescaler_value);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n                    \tif (prescaler_value < 0)\n                        {\n\t\t\t\t\t\tdocument.getElementsByName('prescaler')[0].value= \"Prescaler value less than 0!\";\n                        document.getElementsByName('prescaler_rnd')[0].value= \"Adjust input values.\";\n\t\t\t\t\t\t}\n                        else \n                        {\n                        if (prescaler_value >= 65536)\n                        \t{\n                            document.getElementsByName('prescaler')[0].value= \"Prescaler value more than 65535!\";\n                            document.getElementsByName('prescaler_rnd')[0].value= \"Adjust input values.\";\n                            }\n                        }\n                    }\n\t\t\t\t}\n         \n         function isNumeric (evt)\n         {\n         \tvar theEvent = evt || window.event;\n         \tvar key = theEvent.keyCode || theEvent.which;\n         \tkey = String.fromCharCode (key);\n         \tvar regex = \/[0-9]|\\.\/;\n         \tif ( !regex.test(key) )\n         \t{\n         \t\ttheEvent.returnValue = false;\n         \t\tif(theEvent.preventDefault) theEvent.preventDefault();\n         \t}\n         }\n\n      <\/script>\n   <\/head>\n   <body>\n      \n      <br>\n      <table>\n         <tr>\n            <td>\n               Clock frequency (<span style=\"color:red;\">MHz<\/span>): \n            <\/td>\n            <td>\n               <input type=\"text\" onfocus=\"this.value=''\" name=\"clk_frq\" onkeypress=\"return isNumeric(event)\">\n            <\/td>\n         <\/tr>\n         <tr>\n            <td>\n               Wished PWM steps: \n            <\/td>\n            <td>\n               <input type=\"text\" onfocus=\"this.value=''\" name=\"pwm_stp\" onkeypress=\"return isNumeric(event)\">\n            <\/td>\n         <\/tr>\n         <tr>\n            <td>\n               Wished PWM frequency (<span style=\"color:red;\">Hz<\/span>): \n            <\/td>\n            <td>\n               <input type=\"text\" onfocus=\"this.value=''\" name=\"pwm_frq\" onkeypress=\"return isNumeric(event)\">\n            <\/td>\n         <\/tr>\n      <\/table>\n      <table>\n         <tr>\n            <td>\n               Calculated prescaler value: \n            <\/td>\n            <td>\t\t\t\t\n               <output name=\"prescaler\"><\/output>\n            <\/td>\n         <\/tr>\n         <tr>\n            <td>\n               Rounded prescaler value: \n            <\/td>\n            <td>\t\t\t\t\n               <output name=\"prescaler_rnd\"><\/output>\n            <\/td>\n         <\/tr>\n\n\n      <\/table>\n      <button onClick=\"calculate_prescaler()\">Calculate !<\/button><br>\n   <\/body>\n<\/html>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just to have a quick way to calculate the prescaler value for required PWM frequency&#8230; Made this while testing the Nucleo-F446RE devboard with STM32F446RE processor, should be valid for all STM32-s. Prescaler calculator Clock frequency (MHz): Wished PWM steps: Wished PWM frequency (Hz): Calculated prescaler value: Rounded prescaler value: Calculate !<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,35],"tags":[],"class_list":["post-4033","post","type-post","status-publish","format-standard","hentry","category-skaiciuotuvai","category-stm32"],"_links":{"self":[{"href":"https:\/\/www.dclabs.lt\/dainiaus\/wp-json\/wp\/v2\/posts\/4033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dclabs.lt\/dainiaus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dclabs.lt\/dainiaus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dclabs.lt\/dainiaus\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dclabs.lt\/dainiaus\/wp-json\/wp\/v2\/comments?post=4033"}],"version-history":[{"count":23,"href":"https:\/\/www.dclabs.lt\/dainiaus\/wp-json\/wp\/v2\/posts\/4033\/revisions"}],"predecessor-version":[{"id":4060,"href":"https:\/\/www.dclabs.lt\/dainiaus\/wp-json\/wp\/v2\/posts\/4033\/revisions\/4060"}],"wp:attachment":[{"href":"https:\/\/www.dclabs.lt\/dainiaus\/wp-json\/wp\/v2\/media?parent=4033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dclabs.lt\/dainiaus\/wp-json\/wp\/v2\/categories?post=4033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dclabs.lt\/dainiaus\/wp-json\/wp\/v2\/tags?post=4033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}