C51代码PWM.docx

上传人:b****5 文档编号:3911897 上传时间:2022-11-26 格式:DOCX 页数:21 大小:17.93KB
下载 相关 举报
C51代码PWM.docx_第1页
第1页 / 共21页
C51代码PWM.docx_第2页
第2页 / 共21页
C51代码PWM.docx_第3页
第3页 / 共21页
C51代码PWM.docx_第4页
第4页 / 共21页
C51代码PWM.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

C51代码PWM.docx

《C51代码PWM.docx》由会员分享,可在线阅读,更多相关《C51代码PWM.docx(21页珍藏版)》请在冰豆网上搜索。

C51代码PWM.docx

C51代码PWM

1.#include

2.#include

3.

4.sbit  K1=P1^4;              //增加键

5.sbit  K2=P1^5;              //减少键

6.sbit  P00=P0^1;

7.sbit  BEEP=P3^7;          //蜂鸣器

8.unsignedcharPWM=0xe7;  //赋初值

9.

10.voidBeep();

11.voiddelayms(unsignedcharms);

12.voiddelay(unsignedchart);

13.

14./*********************************************************/

15.voidmain()

16.{ 

17.

18.    P1=0xff;

19.  TMOD=0x21;

20.  TH0=0xff;        //50us延时常数

21.  TL0=0xce;        //频率调节

22.

23.  TH1=PWM;        //脉宽调节

24.  TL1=0;

25.

26.    EA=1;

27.    ET0=1;

28.    ET1=1;

29.  

30.    TR0=1;

31.

32.  while

(1)

33.  {

34.    do{

35.        if(PWM!

=0xff)

36.        {PWM++;delayms(10);}

37.        elseBeep();

38.      }

39.    while(K1==0);

40.

41.    do{

42.        if(PWM!

=0xce)

43.      {PWM--;delayms(10);}

44.        elseBeep();

45.      }

46.    while(K2==0);

47.  }

48.}

49.

50.voidtimer0()interrupt1

51.{ 

52.  TR1=0;

53.  TH0=0xff;

54.  TL0=0xce;

55.  TH1=PWM;

56.  TR1=1;

57.  P00=0;    //启动输出

58.}

59.

60.

61.voidtimer1()interrupt3

62.{

63.  TR1=0;

64.  P00=1;    //结束输出

65.}

66.

67./*********************************************************/

68.//蜂鸣器子程序

69./*********************************************************/

70.

71.voidBeep()   

72.  {

73.    unsignedchari  ;

74.    for(i=0  ;i<100  ;i++)

75.      {

76.      delay(100)  ;

77.      BEEP=!

BEEP  ;          //Beep取反

78.      }

79.    BEEP=1  ;                  //关闭蜂鸣器

80.    delayms(100);

81.  }

82.

83./*********************************************************/

84.//延时子程序

85./*********************************************************/ 

86.voiddelay(unsignedchart)

87.{

88.  while(t--)  ;

89.}

90.

91./*********************************************************/

92.//延时子程序

93./*********************************************************/

94.voiddelayms(unsignedcharms)

95.

96.{

97.  unsignedchari;

98.  while(ms--)

99.    {

100.      for(i=0;i<120;i++);

101.    }

102.}

103.

104./*********************************************************/

105.#include

106.#include

107.

108.sbit  K1=P1^4;              //增加键

109.sbit  K2=P1^5;              //减少键

110.sbit  P00=P0^1;

111.sbit  BEEP=P3^7;          //蜂鸣器

112.unsignedcharPWM=0xe7;  //赋初值

113.

114.voidBeep();

115.voiddelayms(unsignedcharms);

116.voiddelay(unsignedchart);

117.

118./*********************************************************/

119.voidmain()

120.{ 

121.

122.    P1=0xff;

123.  TMOD=0x21;

124.  TH0=0xff;        //50us延时常数

125.  TL0=0xce;        //频率调节

126.

127.  TH1=PWM;        //脉宽调节

128.  TL1=0;

129.

130.    EA=1;

131.    ET0=1;

132.    ET1=1;

133.  

134.    TR0=1;

135.

136.  while

(1)

137.  {

138.    do{

139.        if(PWM!

=0xff)

140.        {PWM++;delayms(10);}

141.        elseBeep();

142.      }

143.    while(K1==0);

144.

145.    do{

146.        if(PWM!

=0xce)

147.      {PWM--;delayms(10);}

148.        elseBeep();

149.      }

150.    while(K2==0);

151.  }

152.}

153.

154.voidtimer0()interrupt1

155.{ 

156.  TR1=0;

157.  TH0=0xff;

158.  TL0=0xce;

159.  TH1=PWM;

160.  TR1=1;

161.  P00=0;    //启动输出

162.}

163.

164.

165.voidtimer1()interrupt3

166.{

167.  TR1=0;

168.  P00=1;    //结束输出

169.}

170.

171./*********************************************************/

172.//蜂鸣器子程序

173./*********************************************************/

174.

175.voidBeep()   

176.  {

177.    unsignedchari  ;

178.    for(i=0  ;i<100  ;i++)

179.      {

180.      delay(100)  ;

181.      BEEP=!

BEEP  ;          //Beep取反

182.      }

183.    BEEP=1  ;                  //关闭蜂鸣器

184.    delayms(100);

185.  }

186.

187./*********************************************************/

188.//延时子程序

189./*********************************************************/ 

190.voiddelay(unsignedchart)

191.{

192.  while(t--)  ;

193.}

194.

195./*********************************************************/

196.//延时子程序

197./*********************************************************/

198.voiddelayms(unsignedcharms)

199.

200.{

201.  unsignedchari;

202.  while(ms--)

203.    {

204.      for(i=0;i<120;i++);

205.    }

206.}

207.

208./*********************************************************/

209.#include

210.#include

211.

212.sbit  K1=P1^4;              //增加键

213.sbit  K2=P1^5;              //减少键

214.sbit  P00=P0^1;

215.sbit  BEEP=P3^7;          //蜂鸣器

216.unsignedcharPWM=0xe7;  //赋初值

217.

218.voidBeep();

219.voiddelayms(unsignedcharms);

220.voiddelay(unsignedchart);

221.

222./*********************************************************/

223.voidmain()

224.{ 

225.

226.    P1=0xff;

227.  TMOD=0x21;

228.  TH0=0xff;        //50us延时常数

229.  TL0=0xce;        //频率调节

230.

231.  TH1=PWM;        //脉宽调节

232.  TL1=0;

233.

234.    EA=1;

235.    ET0=1;

236.    ET1=1;

237.  

238.    TR0=1;

239.

240.  while

(1)

241.  {

242.    do{

243.        if(PWM!

=0xff)

244.        {PWM++;delayms(10);}

245.        elseBeep();

246.      }

247.    while(K1==0);

248.

249.    do{

250.        if(PWM!

=0xce)

251.      {PWM--;delayms(10);}

252.        elseBeep();

253.      }

254.    while(K2==0);

255.  }

256.}

257.

258.voidtimer0()interrupt1

259.{ 

260.  TR1=0;

261.  TH0=0xff;

262.  TL0=0xce;

263.  TH1=PWM;

264.  TR1=1;

265.  P00=0;    //启动输出

266.}

267.

268.

269.voidtimer1()interrupt3

270.{

271.  TR1=0;

272.  P00=1;    //结束输出

273.}

274.

275./*********************************************************/

276.//蜂鸣器子程序

277./*********************************************************/

278.

279.voidBeep()   

280.  {

281.    unsignedchari  ;

282.    for(i=0  ;i<100  ;i++)

283.      {

284.      delay(100)  ;

285.      BEEP=!

BEEP  ;          //Beep取反

286.      }

287.    BEEP=1  ;                  //关闭蜂鸣器

288.    delayms(100);

289.  }

290.

291./*********************************************************/

292.//延时子程序

293./*********************************************************/ 

294.voiddelay(unsignedchart)

295.{

296.  while(t--)  ;

297.}

298.

299./*********************************************************/

300.//延时子程序

301./*********************************************************/

302.voiddelayms(unsignedcharms)

303.

304.{

305.  unsignedchari;

306.  while(ms--)

307.    {

308.      for(i=0;i<120;i++);

309.    }

310.}

311.

312./*********************************************************/

313.#include

314.#include

315.

316.sbit  K1=P1^4;              //增加键

317.sbit  K2=P1^5;              //减少键

318.sbit  P00=P0^1;

319.sbit  BEEP=P3^7;          //蜂鸣器

320.unsignedcharPWM=0xe7;  //赋初值

321.

322.voidBeep();

323.voiddelayms(unsignedcharms);

324.voiddelay(unsignedchart);

325.

326./*********************************************************/

327.voidmain()

328.{ 

329.

330.    P1=0xff;

331.  TMOD=0x21;

332.  TH0=0xff;        //50us延时常数

333.  TL0=0xce;        //频率调节

334.

335.  TH1=PWM;        //脉宽调节

336.  TL1=0;

337.

338.    EA=1;

339.    ET0=1;

340.    ET1=1;

341.  

342.    TR0=1;

343.

344.  while

(1)

345.  {

346.    do{

347.        if(PWM!

=0xff)

348.        {PWM++;delayms(10);}

349.        elseBeep();

350.      }

351.    while(K1==0);

352.

353.    do{

354.        if(PWM!

=0xce)

355.      {PWM--;delayms(10);}

356.        elseBeep();

357.      }

358.    while(K2==0);

359.  }

360.}

361.

362.voidtimer0()interrupt1

363.{ 

364.  TR1=0;

365.  TH0=0xff;

366.  TL0=0xce;

367.  TH1=PWM;

368.  TR1=1;

369.  P00=0;    //启动输出

370.}

371.

372.

373.voidtimer1()interrupt3

374.{

375.  TR1=0;

376.  P00=1;    //结束输出

377.}

378.

379./*********************************************************/

380.//蜂鸣器子程序

381./*********************************************************/

382.

383.voidBeep()   

384.  {

385.    unsignedchari  ;

386.    for(i=0  ;i<100  ;i++)

387.      {

388.      delay(100)  ;

389.      BEEP=!

BEEP  ;          //Beep取反

390.      }

391.    BEEP=1  ;                  //关闭蜂鸣器

392.    delayms(100);

393.  }

394.

395./*********************************************************/

396.//延时子程序

397./*********************************************************/ 

398.voiddelay(unsignedchart)

399.{

400.  while(t--)  ;

401.}

402.

403./*********************************************************/

404.//延时子程序

405./*********************************************************/

406.voiddelayms(unsignedcharms)

407.

408.{

409.  unsignedchari;

410.  while(ms--)

411.    {

412.      for(i=0;i<120;i++);

413.    }

414.}

415.

416./*********************************************************/

41

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1