其实很简单就是通过亮灭的时间占比来实现的,我们肉眼分辨不出它瞬间的亮灭,只会合成一个亮度
所以通过此方法能实现功能。这边就直接贴代码了。
#include <STC15F2K60S2.H>
unsigned int count=0;
unsigned int num=10;
#define k1 P33
#define k2 P32
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void Timer0Init(void) /16 1T
{
AUXR |= 0x80; //?????1T??
TMOD &= 0xF0; //???????
TMOD |= 0x01; //???????
TL0 = 0xD7; //??????
TH0 = 0xFD; //??????
TF0 = 0; //??TF0??
TR0 = 1;
EA=1;
ET0=1; //???0????
}
void main()
{
Timer0Init();
P2=P2&0x1f|0x80;
P0=0xff;
while(1)
{
if(k1==0){
Delay5ms(); if(k1==0)
num=10;
}
if(k2==0){
Delay5ms(); if(k2==0)
num=20;
}
if(P31==0){
Delay5ms(); if(P31==0)
num=30;
}
if(P30==0){
Delay5ms();
if(P30==0)
num=40;
}
}
void timer0() interrupt 1
{
TL0 = 0xD7; //??????
TH0 = 0xFD; //50us
count++;
if(count>num)count=0;
if(count>=10)
{
P2=P2&0x1f|0x80;
P02=1;
}
else
{
P2=P2&0x1f|0x80;
P02=0;
}
}