4 changed files with 300 additions and 0 deletions
-
100stm32_pwm_computer.cpp
-
31stm32_pwm_computer.sln
-
147stm32_pwm_computer.vcxproj
-
22stm32_pwm_computer.vcxproj.filters
@ -0,0 +1,100 @@ |
|||
#define _CRT_SECURE_NO_DEPRECATE |
|||
#include <stdio.h>
|
|||
#include <stdint.h>
|
|||
#include <stdbool.h>
|
|||
|
|||
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; |
|||
} |
@ -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 |
@ -0,0 +1,147 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|||
<ItemGroup Label="ProjectConfigurations"> |
|||
<ProjectConfiguration Include="Debug|Win32"> |
|||
<Configuration>Debug</Configuration> |
|||
<Platform>Win32</Platform> |
|||
</ProjectConfiguration> |
|||
<ProjectConfiguration Include="Release|Win32"> |
|||
<Configuration>Release</Configuration> |
|||
<Platform>Win32</Platform> |
|||
</ProjectConfiguration> |
|||
<ProjectConfiguration Include="Debug|x64"> |
|||
<Configuration>Debug</Configuration> |
|||
<Platform>x64</Platform> |
|||
</ProjectConfiguration> |
|||
<ProjectConfiguration Include="Release|x64"> |
|||
<Configuration>Release</Configuration> |
|||
<Platform>x64</Platform> |
|||
</ProjectConfiguration> |
|||
</ItemGroup> |
|||
<PropertyGroup Label="Globals"> |
|||
<VCProjectVersion>16.0</VCProjectVersion> |
|||
<Keyword>Win32Proj</Keyword> |
|||
<ProjectGuid>{855c915d-29ae-4273-80aa-3374e8b9803f}</ProjectGuid> |
|||
<RootNamespace>stm32pwmcomputer</RootNamespace> |
|||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> |
|||
</PropertyGroup> |
|||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> |
|||
<ConfigurationType>Application</ConfigurationType> |
|||
<UseDebugLibraries>true</UseDebugLibraries> |
|||
<PlatformToolset>v143</PlatformToolset> |
|||
<CharacterSet>Unicode</CharacterSet> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> |
|||
<ConfigurationType>Application</ConfigurationType> |
|||
<UseDebugLibraries>false</UseDebugLibraries> |
|||
<PlatformToolset>v143</PlatformToolset> |
|||
<WholeProgramOptimization>true</WholeProgramOptimization> |
|||
<CharacterSet>Unicode</CharacterSet> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> |
|||
<ConfigurationType>Application</ConfigurationType> |
|||
<UseDebugLibraries>true</UseDebugLibraries> |
|||
<PlatformToolset>v143</PlatformToolset> |
|||
<CharacterSet>Unicode</CharacterSet> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> |
|||
<ConfigurationType>Application</ConfigurationType> |
|||
<UseDebugLibraries>false</UseDebugLibraries> |
|||
<PlatformToolset>v143</PlatformToolset> |
|||
<WholeProgramOptimization>true</WholeProgramOptimization> |
|||
<CharacterSet>Unicode</CharacterSet> |
|||
</PropertyGroup> |
|||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> |
|||
<ImportGroup Label="ExtensionSettings"> |
|||
</ImportGroup> |
|||
<ImportGroup Label="Shared"> |
|||
</ImportGroup> |
|||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> |
|||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |
|||
</ImportGroup> |
|||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> |
|||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |
|||
</ImportGroup> |
|||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> |
|||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |
|||
</ImportGroup> |
|||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> |
|||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |
|||
</ImportGroup> |
|||
<PropertyGroup Label="UserMacros" /> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> |
|||
<LinkIncremental>true</LinkIncremental> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> |
|||
<LinkIncremental>false</LinkIncremental> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> |
|||
<LinkIncremental>true</LinkIncremental> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> |
|||
<LinkIncremental>false</LinkIncremental> |
|||
</PropertyGroup> |
|||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> |
|||
<ClCompile> |
|||
<WarningLevel>Level3</WarningLevel> |
|||
<SDLCheck>true</SDLCheck> |
|||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
|||
<ConformanceMode>true</ConformanceMode> |
|||
</ClCompile> |
|||
<Link> |
|||
<SubSystem>Console</SubSystem> |
|||
<GenerateDebugInformation>true</GenerateDebugInformation> |
|||
</Link> |
|||
</ItemDefinitionGroup> |
|||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> |
|||
<ClCompile> |
|||
<WarningLevel>Level3</WarningLevel> |
|||
<FunctionLevelLinking>true</FunctionLevelLinking> |
|||
<IntrinsicFunctions>true</IntrinsicFunctions> |
|||
<SDLCheck>true</SDLCheck> |
|||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
|||
<ConformanceMode>true</ConformanceMode> |
|||
</ClCompile> |
|||
<Link> |
|||
<SubSystem>Console</SubSystem> |
|||
<EnableCOMDATFolding>true</EnableCOMDATFolding> |
|||
<OptimizeReferences>true</OptimizeReferences> |
|||
<GenerateDebugInformation>true</GenerateDebugInformation> |
|||
</Link> |
|||
</ItemDefinitionGroup> |
|||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> |
|||
<ClCompile> |
|||
<WarningLevel>Level3</WarningLevel> |
|||
<SDLCheck>true</SDLCheck> |
|||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
|||
<ConformanceMode>true</ConformanceMode> |
|||
</ClCompile> |
|||
<Link> |
|||
<SubSystem>Console</SubSystem> |
|||
<GenerateDebugInformation>true</GenerateDebugInformation> |
|||
</Link> |
|||
</ItemDefinitionGroup> |
|||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> |
|||
<ClCompile> |
|||
<WarningLevel>Level3</WarningLevel> |
|||
<FunctionLevelLinking>true</FunctionLevelLinking> |
|||
<IntrinsicFunctions>true</IntrinsicFunctions> |
|||
<SDLCheck>true</SDLCheck> |
|||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
|||
<ConformanceMode>true</ConformanceMode> |
|||
</ClCompile> |
|||
<Link> |
|||
<SubSystem>Console</SubSystem> |
|||
<EnableCOMDATFolding>true</EnableCOMDATFolding> |
|||
<OptimizeReferences>true</OptimizeReferences> |
|||
<GenerateDebugInformation>true</GenerateDebugInformation> |
|||
</Link> |
|||
</ItemDefinitionGroup> |
|||
<ItemGroup> |
|||
<ClCompile Include="stm32_pwm_computer.cpp" /> |
|||
</ItemGroup> |
|||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> |
|||
<ImportGroup Label="ExtensionTargets"> |
|||
</ImportGroup> |
|||
</Project> |
@ -0,0 +1,22 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|||
<ItemGroup> |
|||
<Filter Include="源文件"> |
|||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> |
|||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions> |
|||
</Filter> |
|||
<Filter Include="头文件"> |
|||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> |
|||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions> |
|||
</Filter> |
|||
<Filter Include="资源文件"> |
|||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> |
|||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> |
|||
</Filter> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<ClCompile Include="stm32_pwm_computer.cpp"> |
|||
<Filter>源文件</Filter> |
|||
</ClCompile> |
|||
</ItemGroup> |
|||
</Project> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue