-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpwmtest.c
89 lines (86 loc) · 2.42 KB
/
pwmtest.c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "pwmconfig.h"
int main(int argc, char **argv)
{
int PwmChannel;
int MyPeriod = 1000000000; //period 设置为1s
float rate = 0.5;
int MyDuty = MyPeriod * rate;
char *array[3] = {"B", "C", "D"};
/* handle (optional) flags first */
if(argc != 2) {
fprintf(stderr, "Usage: %s <PWM Channel Name(B or C or D)>\n", argv[0]);
exit(1);
}
if(!strcmp(argv[1],array[0])){
/*export corresponding PWM Channel*/
PwmChannel = 1;
if(pwm_export(PwmChannel) < 0){
printf("PWM export error!\n");
return(-1);
}
if(pwm_disable(PwmChannel) < 0){
printf("PWM disable error!\n");
return(-1);
}
/* set period and duty cycle time in ns */
if(pwm_config(PwmChannel, MyPeriod, MyDuty) < 0){
printf("PWM configure error!\n");
return(-1);
}
/* enable corresponding PWM Channel */
if(pwm_enable(PwmChannel) < 0){
printf("PWM enable error!\n");
return(-1);
}
printf("PWM_B successfully enabled with period - %dms, duty cycle - %2.1f%%\n", MyPeriod/1000000, rate*100);
}
else if(!strcmp(argv[1],array[1])){
PwmChannel = 4;
if(pwm_export(PwmChannel) < 0){
printf("PWM export error!\n");
return(-1);
}
if(pwm_disable(PwmChannel) < 0){
printf("PWM disable error!\n");
return(-1);
}
/* set period and duty cycle time in ns */
if(pwm_config(PwmChannel, MyPeriod, MyDuty) < 0){
printf("PWM configure error!\n");
return(-1);
}
/* enable corresponding PWM Channel */
if(pwm_enable(PwmChannel) < 0){
printf("PWM enable error!\n");
return(-1);
}
printf("PWM_C successfully enabled with period - %dms, duty cycle - %2.1f%%\n", MyPeriod/1000000, rate*100);
}
else if(!strcmp(argv[1],array[2])){
PwmChannel = 2;
if(pwm_export(PwmChannel) < 0){
printf("PWM export error!\n");
return(-1);
}
if(pwm_disable(PwmChannel) < 0){
printf("PWM disable error!\n");
return(-1);
}
/* set period and duty cycle time in ns */
if(pwm_config(PwmChannel, MyPeriod, MyDuty) < 0){
printf("PWM configure error!\n");
return(-1);
}
/* enable corresponding PWM Channel */
if(pwm_enable(PwmChannel) < 0){
printf("PWM enable error!\n");
return(-1);
}
printf("PWM_D successfully enabled with period - %dms, duty cycle - %2.1f%%\n", MyPeriod/1000000, rate*100);
}
else{
printf("wrong PWM Channel input\n");
return(-1);
}
return 0;
}