1.元件介绍
2.驱动电路
3.电机调速
一般会保证一个周期的时间是一样的
应用:
1.LED呼吸灯
#include <REGX52.H>sbit LED=P2^0;void Delay(unsigned int t)
{while(t--);
}
void main()
{unsigned char Time,i;while(1){for(Time=0;Time<100;Time++){for(i=0;i<20;i++){LED=0;Delay(Time);LED=1;Delay(100-Time);}}for(Time=100;Time>0;Time--){for(i=0;i<20;i++){LED=0;Delay(Time);LED=1;Delay(100-Time);}}}}
应用二:用独立按键控制产生不同的电机转速
#include <REGX52.H>
#include "Delay.h"
#include "Key.h"
#include "Nixie.h"
#include "Timer0.h"sbit Motor=P1^0;unsigned char Counter,Compare; //计数值和比较值,用于输出PWM
unsigned char KeyNum,Speed;void main()
{Timer0_Init();while(1){KeyNum=Key();if(KeyNum==1){Speed++;Speed%=4;if(Speed==0){Compare=0;} //设置比较值,改变PWM占空比if(Speed==1){Compare=50;}if(Speed==2){Compare=75;}if(Speed==3){Compare=100;}}Nixie(1,Speed);}
}void Timer0_Routine() interrupt 1
{TL0 = 0x9C; //设置定时初值TH0 = 0xFF; //设置定时初值Counter++;Counter%=100; //计数值变化范围限制在0~99if(Counter<Compare) //计数值小于比较值{Motor=1; //输出1}else //计数值大于比较值{Motor=0; //输出0}
}
效果展示:
独立按键可以控制电机转速
注意:接在01和5V上
原理图