C代码PWM.docx
《C代码PWM.docx》由会员分享,可在线阅读,更多相关《C代码PWM.docx(18页珍藏版)》请在冰豆网上搜索。
C代码PWM
1.#include
2.#include
3.sbit K1=P1^4; //增加键
4.sbit K2=P1^5; //减少键
5.sbit P00=P0^1;
6.sbit BEEP=P3^7; //蜂鸣器
7.unsignedcharPWM=0xe7; //赋初值
8.voidBeep();
9.voiddelayms(unsignedcharms);
10.voiddelay(unsignedchart);
11./*********************************************************/
12.voidmain()
13.{
14. P1=0xff;
15. TMOD=0x21;
16. TH0=0xff; //50us延时常数
17. TL0=0xce; //频率调节
18. TH1=PWM; //脉宽调节
19. TL1=0;
20. EA=1;
21. ET0=1;
22. ET1=1;
23.
24. TR0=1;
25. while
(1)
26. {
27. do{
28. if(PWM!
=0xff)
29. {PWM++;delayms(10);}
30. elseBeep();
31. }
32. while(K1==0);
33. do{
34. if(PWM!
=0xce)
35. {PWM--;delayms(10);}
36. elseBeep();
37. }
38. while(K2==0);
39. }
40.}
41.voidtimer0()interrupt1
42.{
43. TR1=0;
44. TH0=0xff;
45. TL0=0xce;
46. TH1=PWM;
47. TR1=1;
48. P00=0; //启动输出
49.}
50.voidtimer1()interrupt3
51.{
52. TR1=0;
53. P00=1; //结束输出
54.}
55./*********************************************************/
56.//蜂鸣器子程序
57./*********************************************************/
58.voidBeep()
59. {
60. unsignedchari ;
61. for(i=0 ;i<100 ;i++)
62. {
63. delay(100) ;
64. BEEP=!
BEEP ; //Beep取反
65. }
66. BEEP=1 ; //关闭蜂鸣器
67. delayms(100);
68. }
69./*********************************************************/
70.//延时子程序
71./*********************************************************/
72.voiddelay(unsignedchart)
73.{
74. while(t--) ;
75.}
76./*********************************************************/
77.//延时子程序
78./*********************************************************/
79.voiddelayms(unsignedcharms)
80.{
81. unsignedchari;
82. while(ms--)
83. {
84. for(i=0;i<120;i++);
85. }
86.}
87./*********************************************************/
88.#include
89.#include
90.sbit K1=P1^4; //增加键
91.sbit K2=P1^5; //减少键
92.sbit P00=P0^1;
93.sbit BEEP=P3^7; //蜂鸣器
94.unsignedcharPWM=0xe7; //赋初值
95.voidBeep();
96.voiddelayms(unsignedcharms);
97.voiddelay(unsignedchart);
98./*********************************************************/
99.voidmain()
100.{
101. P1=0xff;
102. TMOD=0x21;
103. TH0=0xff; //50us延时常数
104. TL0=0xce; //频率调节
105. TH1=PWM; //脉宽调节
106. TL1=0;
107. EA=1;
108. ET0=1;
109. ET1=1;
110.
111. TR0=1;
112. while
(1)
113. {
114. do{
115. if(PWM!
=0xff)
116. {PWM++;delayms(10);}
117. elseBeep();
118. }
119. while(K1==0);
120. do{
121. if(PWM!
=0xce)
122. {PWM--;delayms(10);}
123. elseBeep();
124. }
125. while(K2==0);
126. }
127.}
128.voidtimer0()interrupt1
129.{
130. TR1=0;
131. TH0=0xff;
132. TL0=0xce;
133. TH1=PWM;
134. TR1=1;
135. P00=0; //启动输出
136.}
137.voidtimer1()interrupt3
138.{
139. TR1=0;
140. P00=1; //结束输出
141.}
142./*********************************************************/
143.//蜂鸣器子程序
144./*********************************************************/
145.voidBeep()
146. {
147. unsignedchari ;
148. for(i=0 ;i<100 ;i++)
149. {
150. delay(100) ;
151. BEEP=!
BEEP ; //Beep取反
152. }
153. BEEP=1 ; //关闭蜂鸣器
154. delayms(100);
155. }
156./*********************************************************/
157.//延时子程序
158./*********************************************************/
159.voiddelay(unsignedchart)
160.{
161. while(t--) ;
162.}
163./*********************************************************/
164.//延时子程序
165./*********************************************************/
166.voiddelayms(unsignedcharms)
167.{
168. unsignedchari;
169. while(ms--)
170. {
171. for(i=0;i<120;i++);
172. }
173.}
174./*********************************************************/
175.#include
176.#include
177.sbit K1=P1^4; //增加键
178.sbit K2=P1^5; //减少键
179.sbit P00=P0^1;
180.sbit BEEP=P3^7; //蜂鸣器
181.unsignedcharPWM=0xe7; //赋初值
182.voidBeep();
183.voiddelayms(unsignedcharms);
184.voiddelay(unsignedchart);
185./*********************************************************/
186.voidmain()
187.{
188. P1=0xff;
189. TMOD=0x21;
190. TH0=0xff; //50us延时常数
191. TL0=0xce; //频率调节
192. TH1=PWM; //脉宽调节
193. TL1=0;
194. EA=1;
195. ET0=1;
196. ET1=1;
197.
198. TR0=1;
199. while
(1)
200. {
201. do{
202. if(PWM!
=0xff)
203. {PWM++;delayms(10);}
204. elseBeep();
205. }
206. while(K1==0);
207. do{
208. if(PWM!
=0xce)
209. {PWM--;delayms(10);}
210. elseBeep();
211. }
212. while(K2==0);
213. }
214.}
215.voidtimer0()interrupt1
216.{
217. TR1=0;
218. TH0=0xff;
219. TL0=0xce;
220. TH1=PWM;
221. TR1=1;
222. P00=0; //启动输出
223.}
224.voidtimer1()interrupt3
225.{
226. TR1=0;
227. P00=1; //结束输出
228.}
229./*********************************************************/
230.//蜂鸣器子程序
231./*********************************************************/
232.voidBeep()
233. {
234. unsignedchari ;
235. for(i=0 ;i<100 ;i++)
236. {
237. delay(100) ;
238. BEEP=!
BEEP ; //Beep取反
239. }
240. BEEP=1 ; //关闭蜂鸣器
241. delayms(100);
242. }
243./*********************************************************/
244.//延时子程序
245./*********************************************************/
246.voiddelay(unsignedchart)
247.{
248. while(t--) ;
249.}
250./*********************************************************/
251.//延时子程序
252./*********************************************************/
253.voiddelayms(unsignedcharms)
254.{
255. unsignedchari;
256. while(ms--)
257. {
258. for(i=0;i<120;i++);
259. }
260.}
261./*********************************************************/
262.#include
263.#include
264.sbit K1=P1^4; //增加键
265.sbit K2=P1^5; //减少键
266.sbit P00=P0^1;
267.sbit BEEP=P3^7; //蜂鸣器
268.unsignedcharPWM=0xe7; //赋初值
269.voidBeep();
270.voiddelayms(unsignedcharms);
271.voiddelay(unsignedchart);
272./*********************************************************/
273.voidmain()
274.{
275. P1=0xff;
276. TMOD=0x21;
277. TH0=0xff; //50us延时常数
278. TL0=0xce; //频率调节
279. TH1=PWM; //脉宽调节
280. TL1=0;
281. EA=1;
282. ET0=1;
283. ET1=1;
284.
285. TR0=1;
286. while
(1)
287. {
288. do{
289. if(PWM!
=0xff)
290. {PWM++;delayms(10);}
291. elseBeep();
292. }
293. while(K1==0);
294. do{
295. if(PWM!
=0xce)
296. {PWM--;delayms(10);}
297. elseBeep();
298. }
299. while(K2==0);
300. }
301.}
302.voidtimer0()interrupt1
303.{
304. TR1=0;
305. TH0=0xff;
306. TL0=0xce;
307. TH1=PWM;
308. TR1=1;
309. P00=0; //启动输出
310.}
311.voidtimer1()interrupt3
312.{
313. TR1=0;
314. P00=1; //结束输出
315.}
316./*********************************************************/
317.//蜂鸣器子程序
318./*********************************************************/
319.voidBeep()
320. {
321. unsignedchari ;
322. for(i=0 ;i<100 ;i++)
323. {
324. delay(100) ;
325. BEEP=!
BEEP ; //Beep取反
326. }
327. BEEP=1 ; //关闭蜂鸣器
328. delayms(100);
329. }
330./*********************************************************/
331.//延时子程序
332./*********************************************************/
333.voiddelay(unsignedchart)
334.{
335. while(t--) ;
336.}
337./*********************************************************/
338.//延时子程序
339./*********************************************************/
340.voiddelayms(unsignedcharms)
341.{
342. unsignedchari;
343. while(ms--)
344. {
345. for(i=0;i<120;i++);
346. }
347.}
348./*********************************************************/
349.#include
350.#include
351.sbit K1=P1^4; //增加键
352.sbit K2=P1^5; //减少键
353.sbit P00=P0^1;
354.sbit BEEP=P3^7; //蜂鸣器
355.unsignedcharPWM=0xe7; //赋初值
356.voidBeep();
357.voiddelayms(unsignedcharms);
358.voiddelay(unsignedchart);
359./*********************************************************/
360.voidmain()
361.{
362. P1=0xff;
363. TMOD=0x21;
364. TH0=0xff; //50us延时常数
365. TL0=0xce; //频率调节
366. TH1=PWM; //脉宽调节
367. TL1=0;
368. EA=1;
369. ET0=1;
370. ET1=1;
371.
372. TR0=1;
373. while
(1)
374. {
375. do{
376. if(PWM!
=0xff)
377. {PWM++;delayms(10);}
378. elseBeep();
379.