|
#include "reg52.h"
sbit LED=P2^0 ;
unsigned char count; //計(jì)數(shù)
unsigned char PWM_VLAUE; //占空比比對(duì)值
typedef unsigned int uint16;
typedef unsigned char uint8;
sbit k1=P1^0;
void delay10ms(uint8 i)
{
uint16 j=100;
while(i--)
while(j--);
}
void timer0_init()
{
TMOD=0x02;
TH0=0x47; //定時(shí)器溢出值設(shè)置,每隔200us發(fā)起一次中斷。
TL0=0X47;
TR0=1;
ET0=1;
EA=1;
count =0;
}
void time0() interrupt 1
{
count++;
if(!k1)
{
delay10ms(1);
if(!k1)
{
PWM_VLAUE+=1;
}
if(PWM_VLAUE>=10)
{
PWM_VLAUE=1;
}
}
if(count == PWM_VLAUE) //判斷是否到了點(diǎn)亮LED的時(shí)候
LED = 1; //點(diǎn)亮LED
if(count >= 10) //當(dāng)前周期結(jié)束
{
LED = 0; //熄滅LED
count = 0; //重新計(jì)時(shí)
}
}
void main()
{
count = 0;
PWM_VLAUE = 0;
LED = 1; //默認(rèn)LED熄滅
timer0_init(); //定時(shí)器0初始化
while(1);
}
|
|