#include <xc.h>
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTRC oscillator; port I/O function on both RA6/OSC2/CLKO pin and RA7/OSC1/CLKI pin)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB3/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off)
#pragma config CCPMX = RB2 // CCP1 Pin Selection bit (CCP1 function on RB2)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 8000000
int time=0;int pw0=0; int pw1=0; int pw2=0;
int pw0n=0; int pw1n=0; int pw2n=0;
int c=0; int rgb=0;
// El tiempo base en este proyecto es de 10msegundos
// Interrupcion del Timer1 como temporizador
void interrupt Timer1(){
if(PIR1bits.TMR1IF==1){
// Precarga 66353 = 1 milisegundo ( cada 1 milisegundo cambia de estado la bandera)
TMR1H=0xF8;
TMR1L=0x30;
PORTBbits.RB7=!PORTBbits.RB7;
time=time+1;
if(pw0==time){PORTBbits.RB0=0;}
if(pw1==time){PORTBbits.RB1=0;}
if(pw2==time){PORTBbits.RB2=0;}
if(time>=10){
time=0;
if(pw0==0){PORTBbits.RB0=0;}
else{ PORTBbits.RB0=1;}
if(pw1==0){PORTBbits.RB1=0;}
else{ PORTBbits.RB1=1;}
if(pw2==0){PORTBbits.RB2=0;}
else{ PORTBbits.RB2=1;}
}
PIR1bits.TMR1IF=0;// Bit de Bandera del Timer1
}
}
void main(void){
OSCCONbits.IRCF2=1;// 8 MHZ CRISTAL INTERNO
OSCCONbits.IRCF1=1;
OSCCONbits.IRCF0=1;
OPTION_REGbits.nRBPU=0;// PULL UP
TRISB=0b01111000;
PORTBbits.RB0=0;
PORTBbits.RB1=0;
PORTBbits.RB2=0;
/////Bits de configuracion del Timer1 //////////////////////////////////////
INTCONbits.GIE=1;//Habilito las interrupciones Globales
INTCONbits.PEIE=1;//Habilito las interrupciones perifericas
T1CONbits.TMR1ON=1;//Habilito el timer1 (ON=1 - OFF=0) (Timer1 es una interrupcion de modulo periferico)
PIE1bits.TMR1IE=1;//Habilito el timer1 por desvordamiento de timer( configurado como tempirizador)
T1CONbits.TMR1CS=0;// fuente de reloj del timer (reloj interno)
T1CONbits.T1CKPS1=0;//bit de prescaler
T1CONbits.T1CKPS0=0;//bit de prescaler
//prescaler ajustado a 1:1
///////////////////////////////////////////////////////////////////////////
while(1){
////// BOTON R //////////////////
if(PORTBbits.RB3==0 && rgb==0){
while(PORTBbits.RB3==0){}
pw0n=pw0n+1;
if(pw0n<10){pw0=pw0+1; }
if(pw0n>9){pw0=pw0-1;}
if(pw0n==18){pw0n=0;pw0=0;}
}
///// BOTON G /////////////////
if(PORTBbits.RB4==0){
while(PORTBbits.RB4==0){}
pw1n=pw1n+1;
if(pw1n<10){pw1=pw1+1; }
if(pw1n>9){pw1=pw1-1;}
if(pw1n==18){pw1n=0;pw1=0;}
}
////// BOTON B //////////////
if(PORTBbits.RB5==0 && rgb==0){
while(PORTBbits.RB5==0){}
pw2n=pw2n+1;
if(pw2n<10){pw2=pw2+1; }
if(pw2n>9){pw2=pw2-1;}
if(pw2n==18){pw2n=0;pw2=0;}
}
////// BOTON W /////////////
if(PORTBbits.RB6==0 && rgb==0){
while(PORTBbits.RB6==0){}
c=c+1;
if(c==1){pw0=0,pw1=0,pw2=0;}
if(c>1 && c<=10){pw0=pw0+1,pw1=pw1+1,pw2=pw2+1; }
if(c>=11){pw0=pw0-1,pw1=pw1-1,pw2=pw2-1;}
if(c==19){PORTBbits.RB0=0;PORTBbits.RB1=0;PORTBbits.RB2=0; c=0;}
}
if(PORTBbits.RB4==0 && PORTBbits.RB5==0){rgb=1;}
else{rgb=0;}
if(PORTBbits.RB4==0 && PORTBbits.RB6==0){rgb=1;}
else{rgb=0;}
if(PORTBbits.RB5==0 && PORTBbits.RB6==0){rgb=1;}
else(rgb=0);
if(PORTBbits.RB4==0 && PORTBbits.RB5==0 && PORTBbits.RB6==0){rgb=1;}
else{rgb=0;}
}
}
if(pw0==time){PORTBbits.RB0=0;}
if(pw1==time){PORTBbits.RB1=0;}
if(pw2==time){PORTBbits.RB2=0;}
if(time>=10){
time=0;
if(pw0==0){PORTBbits.RB0=0;}
else{ PORTBbits.RB0=1;}
if(pw1==0){PORTBbits.RB1=0;}
else{ PORTBbits.RB1=1;}
if(pw2==0){PORTBbits.RB2=0;}
else{ PORTBbits.RB2=1;}
esta parte del codigo la utilizo para generar el pwm de cualquiera de las tres variables (pw0, pw1,pw2) y funciona de la siguiente manera, cuando hago la pregunta de que si time es igual a pwx si esto es correcto apago la salida x pero para volver a encender la salida x hago la pregunta de que si pwx=0 si esto ocurre me mantedra la salida apagada pero si esto no pasa la volvera a encender y todo esto me hara la señal que yo quiera dependiendo de el valor que tenga pwx, asi que este pedazo de codigo no lo puedo ni tocar porque no me generaria la señal, pero si tienes una manera diferente de hacerlo seria de mucha ayuda para evitar el conficto que puede tener el programa.if(c==19){PORTBbits.RB0=0;PORTBbits.RB1=0;PORTBbits.RB2=0; c=0;} si me di cuenta de ese error esta mañana y lo corregi y utilice las variables como lo tenias pensado y me funcion y tambien agrege c=1 para evitar el pulso de mas que tenia.if(PORTBbits.RB3==0 && r==0){
while(PORTBbits.RB3==0){__delay_ms(100);r=1;}
//
if(PORTBbits.RB3==1 && r==1){
r=0;
pw0n=pw0n+1;
if(pw0n<10){pw0=pw0+1; }
if(pw0n>9){pw0=pw0-1;}
if(pw0n==18){pw0n=0;pw0=0;}
} creo que puede funcionar en fisico, pero me gustaria tu opinion : lo utilizo asi, mientras esta pulsado se mantendra en el ciclo infinito esperando y manteniendo la variable r=1, pero cuando sale del ciclo infinito pregunto de que si la entrada esta en alto y la r=1 si esto pasa hara la secuencia dentro del if pero primero llevando la variable r=0 para que no vuelva a entrar en esta pregunta.
pero si tienes una manera diferente de hacerlo seria de mucha ayuda para evitar el conficto que puede tener el programa