C51代码PWM.docx

上传人:b****2 文档编号:1623952 上传时间:2022-10-23 格式: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

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

当前位置:首页 > IT计算机 > 互联网

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

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