此程序是一个用51单片机来调直流电机转速的程序。当然也可以用来调整led灯的亮度,用定时器2来实现.本文引用地址:https://www.eepw.com.cn/article/201611/323371.htm#include //包括一个52标准内核的头文件#define uchar unsigned char#define uint unsigned int#define ulong unsigned longsbit PWM = P3^3; //要控制的风扇sbit K= P3^7; //按键控制转速uchar scale;//用于保存占空比的输出0的时间份额,总共10份//延时程序void delay10ms(void){unsigned char i,j;for(i=20;i>0;i--)for(j=248;j>0;j--);}// 主程序void main(void){RCAP2H =0xF3; //赋T2的预置值,溢出1次是1/1200秒钟RCAP2L =0x98;scale=10;TR2=1; //启动定时器ET2=1; //打开定时器2中断EA=1; //打开总中断while(1) //程序循环{while(K==0){scale++;while(K==0);if(scale==11)scale=0;}}}//1/1200秒定时器2中断timer2() interrupt 5{static uchar tt=0; //tt用来保存当前时间在一秒中的比例位置TF2=0;tt++;if(tt==10) //每1/120秒整开始输出低电平{tt=0;if(scale!=0) //这里加这一句是为了消除灭灯状态产生的鬼影PWM=0;}if(scale==tt) //按照当前占空比切换输出高电平PWM=1;}
评论