From b75770ccc73fad1daea12e63c9f6a90e19854dcf Mon Sep 17 00:00:00 2001 From: zhaohe <1013909206@qq.com> Date: Mon, 27 Dec 2021 11:31:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stm32_pwm_computer.cpp | 100 +++++++++++++++++++++++++ stm32_pwm_computer.sln | 31 ++++++++ stm32_pwm_computer.vcxproj | 147 +++++++++++++++++++++++++++++++++++++ stm32_pwm_computer.vcxproj.filters | 22 ++++++ 4 files changed, 300 insertions(+) create mode 100644 stm32_pwm_computer.cpp create mode 100644 stm32_pwm_computer.sln create mode 100644 stm32_pwm_computer.vcxproj create mode 100644 stm32_pwm_computer.vcxproj.filters diff --git a/stm32_pwm_computer.cpp b/stm32_pwm_computer.cpp new file mode 100644 index 0000000..804258d --- /dev/null +++ b/stm32_pwm_computer.cpp @@ -0,0 +1,100 @@ +#define _CRT_SECURE_NO_DEPRECATE +#include +#include +#include + +typedef struct +{ + int autoreload; + int prescaler; + float realfreq; +} timer_config_t; + +bool compute_timer_parameter(timer_config_t* config, int timer_in_clk /*mhz*/, int infreq /*hz*/) +{ + float psc_x_arr =(float) timer_in_clk * 1000 /*定时器模块时钟*/ * 1000 / infreq; + uint32_t psc = 0; + uint32_t arr = 65534; + for (; arr > 2; arr--) + { + psc =(uint32_t)( psc_x_arr / arr); + if (psc >= 1) + { + uint32_t tmparr = psc_x_arr / psc; + if (tmparr >= 65534) + continue; + break; + } + } + if (psc == 0) + return false; + if (arr <= 3) + return false; //定时器一周期的分辨率太小了 + arr = psc_x_arr / psc; + + int psc_x_arr_real = arr * psc; + float realfreq = timer_in_clk * 1000.0 /*定时器模块时钟*/ * 1000 / psc_x_arr_real; + + arr = arr - 1; + psc = psc - 1; + + config->autoreload = arr; + config->prescaler = psc; + config->realfreq = realfreq; + // config->compare = arr / 2; + return false; +} + +void dumpconfig_info(timer_config_t* config) +{ + printf("* freq : %f \n", config->realfreq); + printf("* prescaler : %d \n", config->prescaler); + printf("* autoreload: %d \n", config->autoreload); +} + +int main(int argc, char const* argv[]) +{ + if (argc != 3) + { + printf("%s system_clk mhz expect_freq khz\n", argv[0]); + return -1; + } + + float expect_freq = 0; + float systemclk = 0; + + sscanf(argv[1], "%f", &systemclk); + sscanf(argv[2], "%f", &expect_freq); + + printf("systemclk : %f mhz\n", systemclk); + printf("expect freq: %f khz\n", expect_freq); + + int32_t infreq = (int32_t)(expect_freq * 1000); + timer_config_t timerconfig_apb1; + timer_config_t timerconfig_apb2; + + //apb1 + compute_timer_parameter(&timerconfig_apb1, systemclk / 2, infreq); + //apb2 + compute_timer_parameter(&timerconfig_apb2, systemclk, infreq); + + printf("******************************************************\n"); + printf("* APB1: TIM2 TIM3 TIM4 TIM5 TIM6 TIM7 TIM12 TIM13 TIM14\n"); + printf("*\n"); + printf("* timer_module_clk: %f mhz\n", systemclk / 2); + printf("* freq : %f khz\n", timerconfig_apb1.realfreq / 1000.0); + printf("* period : %f ms\n", (1.0 / timerconfig_apb1.realfreq * 1000)); + printf("* prescaler : %d \n", timerconfig_apb1.prescaler); + printf("* autoreload : %d \n", timerconfig_apb1.autoreload); + printf("*\n"); + printf("**\n"); + printf("* APB2: TIM1 TIM8 TIM9 TIM10 TIM11\n"); + printf("*\n"); + printf("* timer_module_clk: %f mhz\n", systemclk); + printf("* freq : %f khz\n", timerconfig_apb2.realfreq / 1000.0); + printf("* period : %f ms\n", (1.0 / timerconfig_apb2.realfreq * 1000)); + printf("* prescaler : %d \n", timerconfig_apb2.prescaler); + printf("* autoreload : %d \n", timerconfig_apb2.autoreload); + printf("***\n"); + return 0; +} \ No newline at end of file diff --git a/stm32_pwm_computer.sln b/stm32_pwm_computer.sln new file mode 100644 index 0000000..31d04d5 --- /dev/null +++ b/stm32_pwm_computer.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.32014.148 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stm32_pwm_computer", "stm32_pwm_computer.vcxproj", "{855C915D-29AE-4273-80AA-3374E8B9803F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {855C915D-29AE-4273-80AA-3374E8B9803F}.Debug|x64.ActiveCfg = Debug|x64 + {855C915D-29AE-4273-80AA-3374E8B9803F}.Debug|x64.Build.0 = Debug|x64 + {855C915D-29AE-4273-80AA-3374E8B9803F}.Debug|x86.ActiveCfg = Debug|Win32 + {855C915D-29AE-4273-80AA-3374E8B9803F}.Debug|x86.Build.0 = Debug|Win32 + {855C915D-29AE-4273-80AA-3374E8B9803F}.Release|x64.ActiveCfg = Release|x64 + {855C915D-29AE-4273-80AA-3374E8B9803F}.Release|x64.Build.0 = Release|x64 + {855C915D-29AE-4273-80AA-3374E8B9803F}.Release|x86.ActiveCfg = Release|Win32 + {855C915D-29AE-4273-80AA-3374E8B9803F}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F94697A8-537D-4F75-9A90-2916196874F0} + EndGlobalSection +EndGlobal diff --git a/stm32_pwm_computer.vcxproj b/stm32_pwm_computer.vcxproj new file mode 100644 index 0000000..88604b0 --- /dev/null +++ b/stm32_pwm_computer.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {855c915d-29ae-4273-80aa-3374e8b9803f} + stm32pwmcomputer + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/stm32_pwm_computer.vcxproj.filters b/stm32_pwm_computer.vcxproj.filters new file mode 100644 index 0000000..70724f6 --- /dev/null +++ b/stm32_pwm_computer.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + \ No newline at end of file