Red de conocimiento informático - Aprendizaje de código fuente - ¿Cómo escribir un programa en C para atenuar la luz con PWM mediante el botón 1 y el botón 2?

¿Cómo escribir un programa en C para atenuar la luz con PWM mediante el botón 1 y el botón 2?

#includelt;reg52.hgt;

#includelt;intrins.hgt;

#define uchar unsigned char

#define uint unsigned int

#define LED P1 //8 diodos emisores de luz

sbit key1=P3^5;

sbit key2=P3^6;

#define LED_ON( ) LED=0x00 //Todas las luces están encendidas

#define LED_OFF() LED=0xff /

#define LED_PWM_MAX 99

#define LED_PWM_MIN 0

uchar LED_Direction=0; //Control de dirección del LED 0:1: iluminar

uchar LED_PWM_Counter=0.//Ciclo de trabajo del LED

uchar count=0 ; //Recuento de interrupciones

bit flag=0; //bit de indicador de escaneo de claves

void main()

{

P3 = 0xff; //Escribe 1 en el puerto de entrada por primera vez

TMOD=0x01

TH0=0xff;

TL0=0x47;

EA=1;

ET0=1;

TR0=1;

mientras(1)

{

si(bandera)

{

Si(clave1==0)

{

si( (LED_PWM_Countlt ;=LED_PWM_MAX)amp;amp;(LED_Direction==0))

{

LED_PWM_Count ;

if(LED_PWM_ Countgt;LED_PWM_MAX)

{

LED_PWM_Count=LED_PWM_MAX;

LED_Direction=1;

}

}

}

if(key2==0)

{

if((LED_PWM_Countgt;=LED_PWM_MIN)amp;

{

if((LED_PWM_Countgt;=LED_PWM_MIN)amp;(LED_Direction==1))

{

LED_PWM_Count--;

si (LED_PWM_count lt;LED_PWM_MIN)

{

LED_PWM_Count=LED_PWM_MIN;

LED_Direction=0;

}

}

}

if(LED_PWM_Countgt.0)

{

LED_ON();

LED_PWM_Count -- ;

}

else

{

LED_OFF();

}

}

}

}<

/p>

}

}

}

}

.p>void timer0() interrupción 1

{

TH0=0xff;

TL0=0x47;

if(countgt;=10)//Escanea una pulsación de tecla en 2 ms (si no funciona, puedes cambiar el tiempo a un valor menor)

{

count=0

flag=1

;

}

}

Eso es probablemente lo que significa. Si no funciona, ¡puedes modificarlo tú mismo!