Browse Source

update

master
zhaohe 4 years ago
parent
commit
f7412f7ada
  1. 0
      README.md
  2. 1
      linux/.gitignore
  3. 38
      linux/README.md
  4. 1
      linux/build.sh
  5. 142
      linux/main.c
  6. BIN
      release/stm32_pwm_computer.exe
  7. 0
      windows/.gitattributes
  8. 0
      windows/.gitignore
  9. 42
      windows/README.md
  10. 0
      windows/stm32_pwm_computer.cpp
  11. 0
      windows/stm32_pwm_computer.sln
  12. 0
      windows/stm32_pwm_computer.vcxproj
  13. 0
      windows/stm32_pwm_computer.vcxproj.filters

0
README.md

1
linux/.gitignore

@ -0,0 +1 @@
*.out

38
linux/README.md

@ -0,0 +1,38 @@
# README
## 编译
```bash
./build.sh
```
## 使用
```bash
# Usage: ././stm32_timer_pwm_computer.out system_clk(mhz) expect_freq(khz)
➜ stm32_timer_pwm_computer ./stm32_timer_pwm_computer.out 168 38
systemclk : 168.000000 mhz
expect freq: 38.000000 khz
******************************************************
* APB1: TIM2 TIM3 TIM4 TIM5 TIM6 TIM7 TIM12 TIM13 TIM14
*
* timer_module_clk: 84.000000 mhz
* freq : 38.009051 khz
* period : 0.026310 ms
* prescaler : 0
* autoreload : 2209
*
**
* APB2: TIM1 TIM8 TIM9 TIM10 TIM11
*
* timer_module_clk: 168.000000 mhz
* freq : 38.000453 khz
* period : 0.026315 ms
* prescaler : 0
* autoreload : 4420
***
```

1
linux/build.sh

@ -0,0 +1 @@
gcc main.c -o stm32_timer_pwm_computer.out

142
linux/main.c

@ -0,0 +1,142 @@
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#if 0
void trigger_pwm(int32_t freq, float duty, int32_t pluse_num) {
//PWM输
HAL_TIM_PWM_Stop_IT(&htim3, TIM_CHANNEL_1);
if (freq == 0) {
LOGE("E:trigger pwm fail,freq == 0\n");
return;
}
// ,
float psc_x_arr = 90 * 1000 /*系统时钟*/ * 1000 / freq;
uint32_t psc = 0;
uint32_t arr = 65534;
for (; arr > 2; arr--) {
psc = psc_x_arr / arr;
if (psc >= 1) {
uint32_t tmparr = psc_x_arr / psc;
if (tmparr >= 65534) continue;
break;
}
}
if (psc == 0) return;
if (arr <= 3) return; //
arr = psc_x_arr / psc;
arr = arr - 1;
psc = psc - 1;
__HAL_TIM_SET_AUTORELOAD(&htim3, arr);
__HAL_TIM_SET_PRESCALER(&htim3, psc);
// PWM通道占空比
uint16_t comparevalue = duty * arr;
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, comparevalue);
if (pluse_num == 0) {
g_continue_output_mode = true;
} else {
g_pulse_num = pluse_num - 1;
g_continue_output_mode = false;
}
__HAL_TIM_SET_COUNTER(&htim3, 0);
LOGD("a[%d] p[%d]c[%d]\r\n", arr, psc, comparevalue);
HAL_TIM_PWM_Start_IT(&htim3, TIM_CHANNEL_1);
}
#endif
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 = timer_in_clk * 1000 /*定时器模块时钟*/ * 1000 / infreq;
uint32_t psc = 0;
uint32_t arr = 65534;
for (; arr > 2; arr--)
{
psc = 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;
}

BIN
release/stm32_pwm_computer.exe

0
.gitattributes → windows/.gitattributes

0
.gitignore → windows/.gitignore

42
windows/README.md

@ -0,0 +1,42 @@
# README
## TODO
支持windows图形界面
## 编译
```bash
./build.sh
```
## 使用
```bash
# Usage: ././stm32_timer_pwm_computer.out system_clk(mhz) expect_freq(khz)
➜ stm32_timer_pwm_computer ./stm32_timer_pwm_computer.out 168 38
systemclk : 168.000000 mhz
expect freq: 38.000000 khz
******************************************************
* APB1: TIM2 TIM3 TIM4 TIM5 TIM6 TIM7 TIM12 TIM13 TIM14
*
* timer_module_clk: 84.000000 mhz
* freq : 38.009051 khz
* period : 0.026310 ms
* prescaler : 0
* autoreload : 2209
*
**
* APB2: TIM1 TIM8 TIM9 TIM10 TIM11
*
* timer_module_clk: 168.000000 mhz
* freq : 38.000453 khz
* period : 0.026315 ms
* prescaler : 0
* autoreload : 4420
***
```

0
stm32_pwm_computer.cpp → windows/stm32_pwm_computer.cpp

0
stm32_pwm_computer.sln → windows/stm32_pwm_computer.sln

0
stm32_pwm_computer.vcxproj → windows/stm32_pwm_computer.vcxproj

0
stm32_pwm_computer.vcxproj.filters → windows/stm32_pwm_computer.vcxproj.filters

Loading…
Cancel
Save