-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPWM.c
More file actions
51 lines (51 loc) · 1.37 KB
/
Copy pathPWM.c
File metadata and controls
51 lines (51 loc) · 1.37 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
#include "msp430x54x.h"
#include "PWM.h"
//初始化PWM
//P1.2|--> PWM_1
//P1.3|--> PWM_2
//P1.4|--> PWM_3
//P1.5|--> PWM_4
//默认占空比5%
//SMCLK=XT2/4=4MHz,4分频到1MHz,频率200Hz
void PWM_init(void)
{
P1DIR |= BIT2+BIT3+BIT4+BIT5; // P2.2 and P2.3 output
P1DS |= BIT2+BIT3+BIT4+BIT5;
P1SEL |= BIT2+BIT3+BIT4+BIT5; // P1.2 and P2.3 options select
TA0CCR0 = 5000-1; // PWM Period
TA0CCTL1 = OUTMOD_7; // CCR1 reset/set
TA0CCTL2 = OUTMOD_7; // CCR2 reset/set
TA0CCTL3 = OUTMOD_7; // CCR3 reset/set
TA0CCTL4 = OUTMOD_7; // CCR4 reset/set
TA0CTL = TASSEL_2 + ID_2 + MC_1 + TACLR; // SMCLK, up mode, clear TAR
PWM_1(0);
PWM_2(0);
PWM_3(0);
PWM_4(0);
}
//调节占空比
//参数 n 为0~1000的整数,占空比为 n/10 %
void PWM_1(unsigned int n)
{
if(n>1000) n=1000;
// else if(n<0) n=0;
TA0CCR1 = n+1000; // CCR1 PWM duty cycle
}
void PWM_2(unsigned int n)
{
if(n>1000) n=1000;
// else if(n<0) n=0;
TA0CCR2 = n+1000; // CCR1 PWM duty cycle
}
void PWM_3(unsigned int n)
{
if(n>1000) n=1000;
// else if(n<0) n=0;
TA0CCR3 = n+1000; // CCR1 PWM duty cycle
}
void PWM_4(unsigned int n)
{
if(n>1000) n=1000;
// else if(n<0) n=0;
TA0CCR4 = n+1000; // CCR1 PWM duty cycle
}