本科单片机课程设计,附Proteus仿真效果图
要求:
思路很简单,使用定时器0控制单片机IO口输出不同占空比的触发脉冲即可控制转速
代码参考:
#include <reg52.h>
#define uchar unsigned char
sbit key1=P1^0; //?a1?
sbit out1=P1^1; //ê?3??y×a
sbit out2=P1^2; //ê?3?·′×a
sbit key2=P1^3; //?y×a
sbit key3=P1^4; //·′×a
sbit key4=P1^5; //????±è+1
sbit key5=P1^6; //????±è-1
sbit led1=P2^2; //??ê??y×a
sbit led2=P2^1; //??ê?·′×a+
sbit led3=P2^0; //??ê??aê?/?Yí£
void Key_Scan();
void Delayms(int ms);
void Timer0Init(void) ;
uchar start=0;
uchar intr;
uchar zheng=1;
uchar fan=0;
uchar m=3;
unsigned int tt,time;
void main()
{
Timer0Init();
while(1)
{
if(m==3)time=100;
else if(m==7)time=200;
else if(m==11)time=400;
else if(m==15)time=800;
else if(m==19)time=1200;
Key_Scan();
}
}
/************************°′?üé¨?è***************************/
void Key_Scan() //°′?üé¨?è
{
if(key1==0)
{
Delayms(5);
if(key1==0){start=!start;}
while(!key1);
}
if(key2==0)
{
Delayms(5);
if(key2==0){zheng=1;fan=0;}
while(!key2);
}
if(key3==0)
{
Delayms(5);
if(key3==0){zheng=0;fan=1;}
while(!key3);
}
if(key4==0)
{
Delayms(5);
if(key4==0){if(m<=15){m=m+4;}}
while(!key4);
}
if(key5==0)
{
Delayms(5);
if(key5==0){if(m>=7){m=m-4;}}
while(!key5);
}
}
/******************èí?t?óê± 5ms*****************/
void Delayms(int ms) //?óê±oˉêy
{
int x,y;
for(x=0;x<ms;x++)
for(y=0;y<110;y++);
}
/******************?¨ê±?÷02úéú1ms?óê±*******************/
void Timer0Init()
{
TMOD|=0X01;
TH0=0XFC;
TL0=0X18;
ET0=1;
EA=1;
TR0=1;
}
/*******************?¨ê±?÷0?D?? 2úéúPWM?¢????LED*****************************************/
void Timer0() interrupt 1
{
TH0=0XFC;
TL0=0X18;
if(start==1)
{
intr++;
if(zheng==1)
{
if(intr==1) {out1=1;out2=0;}
else if(intr==m){out1=0;out2=0;}
else if(intr==20){intr=0;}
}
else if(fan==1)
{
if(intr==1) {out1=0;out2=1;}
else if(intr==m){out1=0;out2=0;}
else if(intr==20){intr=0;}
}
}
if(start==1)
{
if(zheng==1){led1=0;led2=1;}
if(fan==1){led1=1;led2=0;}
tt++;
if(tt<=time){led3=0;}
else if(tt==time*2){tt=0;led3=1;}
}
else if(start==0)
{led1=1;led2=1;led3=1;}
}
|