天天看點

單片機控燈(pwm脈寬調制信号)

#include"reg52.h"

#include"ls138.h"

#include"delay.h"

sbit L1=P0^0;

sbit S7=P1^0;

unsigned char count=0;

unsigned char pwm_duty=0;

void InitTimer0()

{

TMOD=0x01;

TH0=(65535-100)/256;

TL0=(65535-100)%256;

ET0=1;

EA=1;

}

void ServicerTimer0() interrupt 1

{

TH0=(65535-100)/256;

TL0=(65535-100)%256;

count++;

if(count==pwm_duty)

{

L1=1;

}

else if(count==100)
{
  L1=0;
  count=0;
}
           

}

unsigned char stat=0;

void ScanKeys()

{

if(S70)

{

delayUs2x(100);

if(S70)

{

switch(stat)

{

case 0:

L1=0;

TR0=1;

pwm_duty=10;

stat=1;

break;

case 1:
		pwm_duty=50;
		stat=2;
		break;

		case 2:
		pwm_duty=90;
		stat=3;
		break;


		case 3:
		L1=1;
		TR0=0;
		//pwm_duty=100;
		stat=0;
		break;
	 }
	 while(S7==0);
	 
  }
           

}

}

void main()

{

SelectHC573(4);

L1=1;

InitTimer0();

while(1)

{

ScanKeys();

}

}

繼續閱讀