////////////////////////////////////////////////////////////////////////
//Control de 3 canales PWM por software //
//se utiliza para mesclar el brillo de tres leds;rojo,verde,azul //
//Funcionamiento; //
// PIN_B0=0 Aumenta o disminuye ciclo util canal0 PIN_A0 //
// PIN_B1=0 Aumenta o disminuye ciclo util canal0 PIN_A1 //
// PIN_B2=0 Aumenta o disminuye ciclo util canal0 PIN_A2 //
// PIN_B3=0 disminuye ciclo util, caso contrario aumenta //
////////////////////////////////////////////////////////////////////////
#include <16f877a.h>
//#fuses XT,NOWDT,NOPROTECT,PUT,NOLVP,BROWNOUT
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 20000000) //libreria para generar retardos con un cristal de 20 mhz
#use fast_io(a)
#use fast_io(b)
#Define LED1 PIN_B5 //ROJO
#Define LED2 PIN_B6 //VERDE
#Define LED3 PIN_B7 //AZUL
#include <PWM.h> //libreria para generar PWM por software
void main (void)
{
unsigned byte i=0,j=0,k=0;
BYTE CONST SINE_WAVE[200] = {
128,132,136,139,143,147,150,154,158,161,165,169,172,176,179,
182,186,189,192,195,199,202,204,207,210,213,215,218,220,223,
225,227,229,231,233,235,237,238,240,241,242,243,244,245,246,
247,247,247,248,248,248,248,248,247,247,247,246,245,244,243,
242,241,240,238,237,235,233,231,229,227,225,223,220,218,215,
213,210,207,204,202,199,195,192,189,186,182,179,176,172,169,
165,161,158,154,150,147,143,139,136,132,128,124,120,117,113,
109,106,102,98,95,91,87,84,80,77,74,70,67,64,61,57,54,52,49,
46,43,41,38,36,33,31,29,27,25,23,21,19,18,16,15,14,13,12,11,
10,9,9,9,8,8,8,8,8,9,9,9,10,11,12,13,14,15,16,18,19,21,23,
25,27,29,31,33,36,38,41,43,46,49,52,54,57,61,64,67,70,74,77,
80,84,87,91,95,98,102,106,109,113,117,120,124};
BYTE sine_index=0;
BYTE sine_index1=66;
BYTE sine_index2=132;
Init_Pwm(); //Inicia MACRO para generar PWM por software
set_tris_a(0);
set_tris_b(0x0F); //B0,B1,B2,B3 Entradas botones, B4,B5,B6 Salidas LED RGB
port_b_pullups(true); //Habilita resistencias de pullup
while(true)
{
while(!input(Pin_B4))
{
Write_Duty(0,SINE_WAVE[sine_index]);
Write_Duty(1,SINE_WAVE[sine_index1]);
Write_Duty(2,SINE_WAVE[sine_index2]);
if(++sine_index>=200)sine_index=0;
if(++sine_index1>=200)sine_index1=0;
if(++sine_index2>=200)sine_index2=0;
delay_ms(500);
}
if (!input(PIN_B3))
{
if (!input(PIN_B0)){i++;if(i==0)i=255;Write_Duty(0,i);}
if (!input(PIN_B1)){j++;if(j==0)j=255;Write_Duty(1,j);}
if (!input(PIN_B2)){k++;if(k==0)k=255;Write_Duty(2,k);}
}
else
{
if (!input(PIN_B0)){i--;if(i==255)i=0;Write_Duty(0,i);}
if (!input(PIN_B1)){j--;if(j==255)j=0;Write_Duty(1,j);}
if (!input(PIN_B2)){k--;if(k==255)k=0;Write_Duty(2,k);}
}
delay_ms(10);
}
}