C52单片机PWM控制电机转速
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit led=P1^0;
sbit key1=P3^4;
sbit key2=P3^5;
uint code table[]={3,4,6,8,10,12, 14,16,18,20,22,24,26,28,30,32,34, 36,38,40};
uchar t1=0,t2=0,cl=1,i;
uint aa,num,t0;
bit c=1;
void delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
TMOD=0x10;
EA=1;
ET1=1;
TR1=1;
TH1=0x00;
TL1=0x00;
}
void keyscan()
{
if(key1==0)
{
delay(10);
if(key1==0)
{
cl++;
num=table[cl];
while(!key1);
}
}
if(key2==0)
{
delay(10);
if(key2==0)
{
cl--;
num=table[cl];
while(!key2);
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
}
void timer_1() interrupt 3
{
TL1 = 0x3c; //200us
TH1 = 0xff;
t1++;
t2++;
if(t1 <= cl)
led = 0; //这三行通过t1与cl比较,控制led亮灭,也就是PWM调光。
if(t1 > cl)
led = 1; //
if(t1 == 50) t1 = 0; //
if(t2 ==250) //250次中断以后,进入以下语句,更改cl值,改变亮度。
改变比较的值(250) { //可以调整闪烁速度。
t2=0;
if(c)
{
if(cl == num)
c=0; //由亮变暗标识
}
if(cl==3)
{
c=1;
}
}
}。