PWM 调LED亮度 在实验板上通过
仿真电路图如下:(下载本帖最后附件可看清晰图)
51单片机源程序如下:
- #include <AT89X51.H>
- #define uchar unsigned char
- #define uint unsigned int
- uint sec,miaoshu,tcnt,count,a;
- sbit p30=P3^0;
- sbit p31=P3^1;
- sbit p20=P2^0;
- sbit p27=P2^7;
-
- void Delay(uchar t)
- {
- uchar i;
- while(t--)
- {
- for ( i = 0; i < 200 ;i ++);
- }
- }
- void t0(void) interrupt 1 using 0 //定时T0中断服务函数
- {
-
- TH0=(65536-10)/256;
- TL0=(65536-10)%256;
- tcnt++;
- if(tcnt<=miaoshu){p20=1;}
- if(tcnt>miaoshu) {p20=0;}
- if(tcnt==20) //调频率
- {
- tcnt=0;
- if(p30==1&&p31==1){a=0;p27=1;}
-
- if(a==0)
- {
- if(p30==0){if(miaoshu>0 ){miaoshu--;a=1;p27=0;}} //调占空比
- if(p31==0){if(miaoshu<20){miaoshu++;a=1;p27=0;}} //调占空比
- }
-
- }
-
- }
- void main(void)//主程序
- {
-
- TMOD=0x01;
- TH0=(65536-10)/256;
- TL0=(65536-10)%256;
- TR0=1; //开始定时
- ET0=1; //允许T0 产生中断
- EA=1;
-
- miaoshu=10;tcnt=0;
- while(1)//调用各个函数模块,死循环
- {
- ;
- }
- }
复制代码
仿真文件和程序下载:
led调光.rar
(43.44 KB, 下载次数: 39)
|