程序以验证。可以使用。用的是定时中断;P1尾输出端;我只写出了连个按键的试验效果,如果你需要更多的话,你自己往上加按键就OK 了希望随你有所帮助 #include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit s1=P3^2;
sbit s2=P3^3;
sbit s3=P3^4;
uchar t,x;
void delay(uint z)
{
uint x,y;
for (x=0;x
}
void init()
{
x=40;
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
init();
while(1)
{
if(s1==0)
{
delay(5);
if(s1==0)
{
while(!s1);
x=10;
}
}
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
x=20;
}
}
}
}void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t++;
if(t==x)
{
t=0;
P1=~P1;
}
}
ORG 0000HAJMP MAINMAIN:MOV R7,#0FFHMOV R6,#0FFHMOV R5,#0MOV R6,#0SETB P1.0LCALL DELAYCLR P1.0LCALL DELAY1JNB P2.0,ZJJNB P2.1,JXLJMP MAIN ZJ: DEC R6; 增加 INC R4 LJMP MAIN JX: INC R6; 减小 DEC R4 LJMP MAIN DELAY:D1: DJNZ R7,D1 DJNZ R6,DELAY RETDELAY1:D2: DJNZ R5,D2 DJNZ R4,DELAY RET高电平时间为(R7*2+3)*R6;低电平时间为(R5*2+3)*R4;P2.0是低电平时增加占空比p2.1是低点平时减小占空比P1.0口是pwm信号输出 大致写下哈.给点辛苦分吧