-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpwm.cpp
More file actions
61 lines (53 loc) · 1.5 KB
/
Copy pathpwm.cpp
File metadata and controls
61 lines (53 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct freq {
double f;
double source;
int pwm_size;
int m;
int p;
};
bool operator<(const freq & lhs, const freq & rhs)
{
return lhs.f < rhs.f;
}
int calculate_peroid(double source, vector < freq > &freqs)
{
int available_p[] = { 1, 3, 5, 6 };
int available_pwm_size[] = { 6, 9 };
for (int i = 0;
i < sizeof(available_pwm_size) / sizeof(available_pwm_size[0]);
i++) {
int pwm_size = available_pwm_size[i];
for (int m = 0; m < 8; m++) {
for (int j = 0;
j < sizeof(available_p) / sizeof(available_p[0]);
j++) {
int p = available_p[j];
freq finfo;
finfo.f =
(source * (1 << m)) / ((1 << pwm_size) * p *
1.0);
finfo.source = source;
finfo.pwm_size = pwm_size;
finfo.m = m;
finfo.p = p;
freqs.push_back(finfo);
}
}
}
}
int main()
{
vector < freq > freqs;
calculate_peroid(1.024, freqs);
calculate_peroid(32.768, freqs);
calculate_peroid(19200, freqs);
stable_sort(freqs.begin(), freqs.end());
for (const freq & e:freqs)
cout << "f = " << e.f << ", source = " << e.
source << ", pwm_size = " << e.pwm_size << ", m = " << e.
m << ", p = " << e.p << endl;
}