提交 | 用户 | age
|
5dd1b7
|
1 |
#include "STC15F2K60S2.H" |
Q |
2 |
|
|
3 |
void CCP_PWMInit(void) //初始化PCA(Programmable Counter Array)模块作为PWM输出 |
|
4 |
{ |
|
5 |
CCON = 0; //PCA控制寄存器初始化(xx-- -xxx,0000 0000:都是清零就完事了) |
|
6 |
CMOD = 0x02; //PCA工作模式寄存器(x--- xxxx,0000 0010:系统时钟,SYSclk/2) |
|
7 |
CL = 0; //PCA计数器低8位清零 |
|
8 |
CH = 0; //PCA计数器高8位清零 |
|
9 |
|
|
10 |
AUXR1 = 0x00; //辅助寄存器1清零,初始化 |
|
11 |
PCA_PWM0 = 0x00; //PCA的PWM寄存器(xx-- --xx,0000 0000:工作于8 bit PWM) |
|
12 |
CCAP0H = 0x80; //比较/捕获寄存器0高8位设置为 0x80,表示PWM1的重装载值的高8位 |
|
13 |
CCAP0L = 0x80; //比较/捕获寄存器0低8位设置为 0x80,表示PWM1的重装载值的低8位 |
|
14 |
CCAPM0 = 0x42; //比较/捕获工作模式寄存器0(-xxx xxxx,0100 0010:比较器打开,CCP0脚用作PWM输出) |
|
15 |
|
|
16 |
AUXR1 = 0x00; |
|
17 |
PCA_PWM1 = 0x00; |
|
18 |
CCAP1H = 0x80; //256-x/256 = pulse width |
|
19 |
CCAP1L = 0x80; |
|
20 |
CCAPM1 = 0x42; //比较器打开,CCP1脚用作PWM输出 |
|
21 |
CR = 1; //启动PCA计数器 |
|
22 |
} |
|
23 |
|
|
24 |
void PWMInit(void) |
|
25 |
{ |
|
26 |
//该芯片增强型 PWM 输出端口:1.6 1.7 2.1 2.2 2.3 3.7 |
|
27 |
P1M0 &= ~0xc0; //将 0011 1111 与 P1M0 进行位与运算,清零 P1M0 的高2位,结合P1M1,P1.6 P1.7引脚配置更改为准双向口,其他脚开漏 |
|
28 |
//P1M0 &= 0x3f |
|
29 |
P1M1 &= ~0xc0; //P1M1_P1M0状态(同一位置两个寄存器的数值组合):00:准双向;01:推挽;10:高阻;11:开漏 |
|
30 |
P1 &= ~0xc0; //resetP1.6/.P1.7 |
|
31 |
P2M0 &= ~0x0e; //1111 0001 |
|
32 |
P2M1 &= ~0x0e; |
|
33 |
P2 &= ~0x0e; |
|
34 |
P3M0 &= ~0x80; //0111 1111 |
|
35 |
P3M1 &= ~0x80; |
|
36 |
P3 &= ~0x80; |
|
37 |
|
|
38 |
P_SW2 |= 0x80; //使能访问XSFR |
|
39 |
PWMCFG = 0x3f; //配置PWM的初始电平为 high |
|
40 |
PWMCKS = 0x00; //选择PWM的时钟Fosc/(0+1) 22118400/1024=21.6k |
|
41 |
//PWMCKS = 0x07; //选择PWM的时钟Fosc/(8) 22118400/1000/8=2.76k |
|
42 |
PWMC = CYCLE; //设置PWM的周期 |
|
43 |
|
|
44 |
//-----PWM2-------------------------------- |
|
45 |
PWM2T1 = 0x0001; //设置PWM第1次翻转的PWM计数 |
|
46 |
PWM2T2 = CYCLE * 2L / 100; //设置PWM第2次翻转的PWM计数 |
|
47 |
//占空比(PWM2T2-PWM2T1)/PWMC |
|
48 |
PWM2CR = 0x00; //选择PWM2输出到P3.7,不使能PWM2中断 |
|
49 |
//-------PWM3----------------------------- |
|
50 |
PWM3T1 = 0x0001; //设置PWM第1次翻转的PWM计数 |
|
51 |
PWM3T2 = CYCLE * 2L / 100; //设置PWM第2次翻转的PWM计数 |
|
52 |
//占空比(PWM2T2-PWM2T1)/PWMC |
|
53 |
PWM3CR = 0x00; //选择PWM2输出到P2.1,不使能PWM2中断 |
|
54 |
//--------pwm4---------------------------------- |
|
55 |
PWM4T1 = 0x0001; //设置PWM第1次翻转的PWM计数 |
|
56 |
PWM4T2 = CYCLE * 2L / 100; //设置PWM第2次翻转的PWM计数 |
|
57 |
//占空比(PWM2T2-PWM2T1)/PWMC |
|
58 |
PWM4CR = 0x00; //选择PWM2输出到P2.2,不使能PWM2中断 |
|
59 |
//--------pwm5---------------------------------- |
|
60 |
PWM5T1 = 0x0001; //设置PWM第1次翻转的PWM计数 |
|
61 |
PWM5T2 = CYCLE * 2L / 100; //设置PWM第2次翻转的PWM计数 |
|
62 |
//占空比(PWM2T2-PWM2T1)/PWMC |
|
63 |
PWM5CR = 0x00; //选择PWM2输出到P2.3,不使能PWM2中断 |
|
64 |
//--------pwm6---------------------------------- |
|
65 |
PWM6T1 = 0x0001; //设置PWM第1次翻转的PWM计数 |
|
66 |
PWM6T2 = CYCLE * 2L / 100; //设置PWM第2次翻转的PWM计数 |
|
67 |
//占空比(PWM2T2-PWM2T1)/PWMC |
|
68 |
PWM6CR = 0x00; //选择PWM2输出到P1.6,不使能PWM2中断 |
|
69 |
|
|
70 |
//--------pwm7---------------------------------- |
|
71 |
PWM7T1 = 0x0001; //设置PWM第1次翻转的PWM计数 |
|
72 |
PWM7T2 = CYCLE * 2L / 100; //设置PWM第2次翻转的PWM计数 |
|
73 |
//占空比(PWM2T2-PWM2T1)/PWMC |
|
74 |
PWM7CR = 0x00; //选择PWM2输出到P1.7,不使能PWM2中断 |
|
75 |
|
|
76 |
PWMCR = 0x3f; //使能PWM2.3.4.5.6.7,6路信号输出 |
|
77 |
PWMCR |= 0x80; //使能PWM模块 |
|
78 |
//PWMCR |= 0x40; //使能PWM归零中断 |
|
79 |
P_SW2 &= ~0x80; |
|
80 |
} |
|
81 |
//----------pwm2/3/4/5------------------------------------------------------------------- |
|
82 |
void pwm2( unsigned short Wide) //PWM2 |
|
83 |
{ |
|
84 |
if((Wide==0)||(Wide==1)) |
|
85 |
{ |
|
86 |
P_SW2 |= 0x80; //设置辅助寄存器2的位7为1,将PWM2引脚切换到P1.3 |
|
87 |
PWMCR &=~0x01; //PWM控制寄存器的位0清零,即禁用PWM2 |
|
88 |
PWM2=1; //PWM控制寄存器的位0清零,即禁用PWM2 |
|
89 |
P_SW2 &=~0x80; //清除辅助寄存器2的位7,将PWM2引脚切换回原来的位置 |
|
90 |
} |
|
91 |
else if (Wide==CYCLE) |
|
92 |
{ |
|
93 |
PWMCR &=~0x01; //PWM控制寄存器的位0清零,即禁用PWM2 |
|
94 |
PWM2=0; //PWM2引脚置为低电平 |
|
95 |
} |
|
96 |
else |
|
97 |
{ |
|
98 |
P_SW2 |= 0x80; //设置辅助寄存器2的位7为1,将PWM2引脚切换到P1.3 |
|
99 |
PWM2T1 = 0x01; //PWM2定时器1寄存器的值设置为0x01 |
|
100 |
PWM2T2 = Wide; //PWM2定时器2寄存器的值设置为 Wide |
|
101 |
PWMCR |= 0x01; //PWM控制寄存器的位0置为1,即启用PWM2 |
|
102 |
P_SW2 &=~0x80; //清除辅助寄存器2的位7,将PWM2引脚切换回原来的位置 |
|
103 |
|
|
104 |
} |
|
105 |
|
|
106 |
} |
|
107 |
void pwm3(unsigned short Wide) //PWM3 |
|
108 |
{ |
|
109 |
if((Wide==0)||(Wide==1)) |
|
110 |
{ |
|
111 |
P_SW2 |= 0x80; |
|
112 |
PWMCR &=~0x02; |
|
113 |
PWM3=1; |
|
114 |
P_SW2 &=~0x80; |
|
115 |
|
|
116 |
} |
|
117 |
else if(Wide==CYCLE) |
|
118 |
{ |
|
119 |
PWMCR &=~0x02; |
|
120 |
PWM3=0; |
|
121 |
} |
|
122 |
else |
|
123 |
{ |
|
124 |
P_SW2 |= 0x80; |
|
125 |
PWMCFG = 0x3f; //配置PWM的初始电平为 high |
|
126 |
PWM3T1 =0x01 ; |
|
127 |
PWM3T2 =Wide; |
|
128 |
P_SW2 &=~0x80; |
|
129 |
PWMCR |= 0x02; |
|
130 |
|
|
131 |
} |
|
132 |
} |
|
133 |
void pwm4(unsigned short Wide) //PWM4 |
|
134 |
{ |
|
135 |
if((Wide==0)||(Wide==1)) |
|
136 |
{ |
|
137 |
P_SW2 |= 0x80; |
|
138 |
PWMCR &=~0x04; |
|
139 |
PWM4=1; |
|
140 |
P_SW2 &=~0x80; |
|
141 |
} |
|
142 |
else if (Wide==CYCLE) |
|
143 |
{ |
|
144 |
PWMCR &=~0x04; |
|
145 |
PWM4=0; |
|
146 |
} |
|
147 |
else |
|
148 |
{ |
|
149 |
P_SW2 |= 0x80; |
|
150 |
PWM4T1 = 0x01; |
|
151 |
PWM4T2 = Wide; |
|
152 |
P_SW2 &=~0x80; |
|
153 |
PWMCR |= 0x04; |
|
154 |
|
|
155 |
} |
|
156 |
} |
|
157 |
void pwm5(unsigned short Wide) //PWM5 |
|
158 |
{ |
|
159 |
if((Wide==0)||(Wide==1)) |
|
160 |
{ |
|
161 |
P_SW2 |= 0x80; |
|
162 |
PWMCR &=~0x08; |
|
163 |
PWM5=1; |
|
164 |
P_SW2 &=~0x80; |
|
165 |
|
|
166 |
} |
|
167 |
else if (Wide==CYCLE) |
|
168 |
{ |
|
169 |
PWMCR &=~0x08; |
|
170 |
PWM5=0; |
|
171 |
} |
|
172 |
else |
|
173 |
{ |
|
174 |
P_SW2 |= 0x80; |
|
175 |
PWM5T1 = 0x01; |
|
176 |
PWM5T2 = Wide; |
|
177 |
P_SW2 &=~0x80; |
|
178 |
PWMCR |= 0x08; |
|
179 |
|
|
180 |
} |
|
181 |
} |