代码如下:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar time;
double count=26; //?????,???????26%
sbit PWM=P0^2; //P2^0???PWM
sbit key_add=P3^2; //??????? ?I/O?
sbit key_dec=P3^3; //??????? ?I/O?
/************????**************/
void delayxms(uint z); //??
void Motor_add(); //????,??????
void Motor_dec(); //????,??????
void timer0_init(); //???0???
/*********???********************/
void main()
{
timer0_init();
delayxms(5);
while(1)
{
Motor_add();
Motor_dec();
}
}
/****************????**********************/
void delayxms(uint z) //??xms??
{
uint x,y;
for(y=z;x>0;x--)
for(y=110;y>0;y--);
}
void Motor_add() //????
{
if(key_add==0)
{
delayxms(2); //????????,??????????
if(key_add==0)
{
count+=0.5; //????????????0.5
if(count>=32) //???????
{
count=32;
}
}
while(!key_add);
}
}
void Motor_dec()//????
{
if(key_dec==0)
{
delayxms(2);//????????,??????????
if(key_dec==0)
{
count-=0.5; //????????????0.5
if(count<=25) //???????
{
count=25;
}
}
while(!key_dec);
}
}
/***********???0???***********/
void timer0_init()
{
TMOD=0x01; //???0?????1
TH0=(65536-10)/256;
TL0=(65536-10)%256;
TR0=1;
ET0=1;
EA=1;
}
/**************??0????******************/
void timer0_int() interrupt 1
{
TR0=0; //?????????,?????
TH0=(65536-10)/256;
TL0=(65536-10)%256;
TR0=1;
time++;
if(time<count) //count????
{
PWM=1;
}
else
PWM=0;
if(time>=100)
{
time=0;
}
}