|
void dim_ccp1()//ECCP2
{
if(CCPR2L>0xfd)
{
while(CCPR2L>0)//如果占空比不等于0
{
CCPR2L--;//占空比逐渐减小
delay2();//延时
if(CCPR2L>=5) ccp1temp=CCPR2L;//!如果占空比
if(RA2!=0 && RC2!=0) break;
}
if(CCPR2L==0) eccp1_initial(0,0);
ccp1_inc_dec=1;
}
else
{
if(CCPR2L==0) ccp1_inc_dec=1;
if(ccp1_inc_dec==0)
{
while(CCPR2L>0)
{
CCPR2L--;
delay2();
if(CCPR2L>=5) ccp1temp=CCPR2L;//!
if(RA2!=0 && RC2!=0) break;
}
if(CCPR2L==0) eccp1_initial(0,0);
ccp1_inc_dec=1;
}
else
{
while(CCPR2L<0xff)
{
CCPR2L++;
delay2();
if(CCPR2L>=5) ccp1temp=CCPR2L;//!
if(RA2!=0 && RC2!=0) break;
}
if(CCPR2L==0xff) eccp1_initial(0xff,0xc0);
ccp1_inc_dec=0;
}
}
}
void dim_ccp1_on()
{
if(ccp1temp == 0) ccp1temp = 5;
while(CCPR2L
{
CCPR2L++;
delay1();
}
if(CCPR2L==0xff) eccp1_initial(0xff,0xc0);
}
void dim_ccp1_off()
{
while(CCPR2L>0)
{
CCPR2L--;
delay1();
}
if(CCPR2L==0) eccp1_initial(0,0);
}
void dim_ccp2()
{
if(CCPR3L>0xfd)
{
while(CCPR3L>0)
{
CCPR3L--;
delay2();
if(CCPR3L>=5) ccp2temp=CCPR3L;//!
if(RA3!=0 && RC4!=0) break;
}
if(CCPR3L==0) eccp2_initial(0,0);
ccp2_inc_dec=1;
}
else
{
if(CCPR3L==0) ccp2_inc_dec=1;
if(ccp2_inc_dec==0)
{
while(CCPR3L>0)
{
CCPR3L--;
delay2();
if(CCPR3L>=5) ccp2temp=CCPR3L;//!
if(RA3!=0 && RC4!=0) break;
}
if(CCPR3L==0) eccp2_initial(0,0);
ccp2_inc_dec=1;
}
else
{
while(CCPR3L<0xff)
{
CCPR3L++;
delay2();
if(CCPR3L>=5) ccp2temp=CCPR3L;//!
if(RA3!=0 && RC4!=0) break;
}
if(CCPR3L==0xff) eccp2_initial(0xff,0xc0);
ccp2_inc_dec=0;
}
}
}
void dim_ccp2_on()
{
if(ccp2temp == 0) ccp2temp = 5;
while(CCPR3L
{
CCPR3L++;
delay1();
}
if(CCPR3L==0xff) eccp2_initial(0xff,0xc0);
}
void dim_ccp2_off()
{
while(CCPR3L>0)
{
CCPR3L--;
delay1();
}
if(CCPR3L==0) eccp2_initial(0,0);
}
void dim_master()
{
float dimx,dimy;
uchar x,y;
uchar num=0;
if(CCPR2L==0 && CCPR3L==0 && Channel==0)
{
CCPR2L=ccp1temp;
CCPR3L=ccp2temp;
if(ccp1temp>ccp2temp){dim_ccp1_on();dim_ccp2_on();}
else {dim_ccp2_on();dim_ccp1_on();}
CCPR2L = 5;
CCPR3L = 5;
master_inc_dec==1;
}
x=CCPR2L;
y=CCPR3L;
if(Channel==1) x=1;
else if(Channel==2) y=1;
if(x!=0 && y!=0)
{
dimx=(float)x;
dimy=(float)y;
if(dim==0)
{
if(dimx>=dimy) dim=dimx/dimy;
else dim=dimy/dimx;
}
if(master_inc_dec==0)
{
while(RA1==0 || RC5==0)
{
if(CCPR2L>=5) ccp1temp=CCPR2L;//!
if(CCPR3L>=5) ccp2temp=CCPR3L;//!
if(CCPR2L>5 && CCPR3L>5) //2%~100%
{
if(x>=y)
{
CCPR2L=x-num;
CCPR3L=(int)(CCPR2L/dim);
}
else
{
CCPR3L=y-num;
CCPR2L=(int)(CCPR3L/dim);
}
delay2();
num++;
}
}
master_inc_dec=1;
}
else
{
while(RA1==0 || RC5==0)
{
if(CCPR2L<0xff && CCPR3L<0xff )
{
if(CCPR2L>=5) ccp1temp=CCPR2L;//!
if(CCPR3L>=5) ccp2temp=CCPR3L;//!
if(x>=y)
{
CCPR2L=x+num;
CCPR3L=(int)(CCPR2L/dim);
}
else
{
CCPR3L=y+num;
CCPR2L=(int)(CCPR3L/dim);
}
delay2();
num++;
}
}
master_inc_dec=0;
}
}
else//====================================================
{
if(master_inc_dec==0)
{
if(y==0 && x!=0)
{
while(CCPR2L>5) //Min 2%
{
CCPR2L--;
delay2();
if(CCPR2L>=5) ccp1temp=CCPR2L;//!
Channel=1;
if(RA1!=0 && RC5!=0) break;
}
ccp2temp = 0;
}
else
{
while(CCPR3L>5) //MIN 2%
{
CCPR3L--;
delay2();
if(CCPR3L>=5) ccp2temp=CCPR3L;//!
Channel=2;
if(RA1!=0 && RC5!=0) break;
}
ccp1temp = 0;
}
master_inc_dec=1;
}
else
{
if(y==0 && x!=0)
{
while(CCPR2L<0xff)
{
CCPR2L++;
delay2();
if(CCPR2L>=5) ccp1temp=CCPR2L;//!
Channel=1;
if(RA1!=0 && RC5!=0) break;
}
if(CCPR2L==0xff) eccp1_initial(0xff,0xc0);
ccp2temp = 0;
}
else
{
while(CCPR3L<0xff)
{
CCPR3L++;
delay2();
if(CCPR3L>=5) ccp2temp=CCPR3L;//!
Channel=2;
if(RA1!=0 && RC5!=0) break;
}
if(CCPR3L==0xff) eccp2_initial(0xff,0xc0);
ccp1temp = 0;
}
master_inc_dec=0;
}
}
}
void save_data()
{
EEIF=0;w_e2_data=CCPR2L;
e2prom_write(8); //save CH1
while(EEIF==0);
EEIF=0;w_e2_data=CCPR3L;
e2prom_write(9); //save CH2
while(EEIF==0);
EEIF=0;
}
void ad_timer(void) interrupt 0
{
uchar i;
ADIF=0;
if(ADCON0!=0x11) //handle dimming channels' value
{
if(ex_pot==0 && (ADCON0==0x29 || ADCON0==0x31))//(0.25~9.5V)
{
if(ADRESH<6) ADRESH=0;
else if(ADRESH>5 && ADRESH<242) ADRESH=(ADRESH*108/100-6);
else if(ADRESH>241) ADRESH=0xff;
}
if(ex_pot==1){ if(ADRESH<0x7f) ADRESH=ADRESH<<1; else ADRESH=0xff;} //ex.pot*2
if(pwm_in==1)
{
if(ADCON0==0x25)
{
if(ten_percent1==2) channel1[0]=channel1[1]=channel1[2]=channel1[3]=channel1[4]=ADRESH;
else channel1[4]=ADRESH;
ADRESH=(channel1[0]+channel1[1]+channel1[2]+channel1[3]+channel1[4]+2)/5;
channel1[4]=ADRESH;
for(i=0;i<4;i++) channel1[i]=channel1[i+1];
}
else if(ADCON0==0x21)
{
if(ten_percent2==2) channel2[0]=channel2[1]=channel2[2]=channel2[3]=channel2[4]=ADRESH;
else channel2[4]=ADRESH;
ADRESH=(channel2[0]+channel2[1]+channel2[2]+channel2[3]+channel2[4]+2)/5;
channel2[4]=ADRESH;
for(i=0;i<4;i++) channel2[i]=channel2[i+1];
}
if(ADRESH>0xf9) ADRESH=0;
else ADRESH=0xff-ADRESH;
}
if(ADCON0==0x29||ADCON0==0x25||ADCON0==0x2d) ccp1_buffer=ADRESH;
if(ADCON0==0x31||ADCON0==0x21||ADCON0==0x35) ccp2_buffer=ADRESH;
switch(ADCON0)
{
case 0x31:
ADCON0=0x29;break;
case 0x21:
ADCON0=0x25;break;
case 0x2d:
ADCON0=0x35;break;
case 0x29:
ADCON0=0x11;break;
case 0x25:
ADCON0=0x11;break;
case 0x35:
ADCON0=0x11;break;
}
}
else //base on the main channel's value choose fuction
{
if(ADRESH<0x33){if(ADCON0==0x2d) ADCON0=0x35; else ADCON0=0x2d;pwm_in=0;ex_pot=0;TRISC=0x3c;} //internal pot.
else if(ADRESH>0xe6) {pwm_in=1;if(ADCON0==0x21) ADCON0=0x25; else ADCON0=0x21;ex_pot=0;TRISC=0xbd;} //PWM IN
else {ex_pot=0;if(RA4==1) ex_pot=1; if(ADCON0==0x31) ADCON0=0x29; else ADCON0=0x31;pwm_in=0;TRISC=0x3c;} //ex_pot. and 0v-10v
if(percentage==2)percentage=2;
}
if(ten_percent1==2) //变化10%
{
if(ccp1_temp>ccp1_buffer) percentage1=ccp1_temp-ccp1_buffer;
else percentage1=ccp1_buffer-ccp1_temp;
if(percentage1>0x19) {percentage=0; ten_percent1=0;}else percentage=1;
}
if(ten_percent2==2) //变化10%
{
if(ccp2_temp>ccp2_buffer) percentage2=ccp2_temp-ccp2_buffer;
else percentage2=ccp2_buffer-ccp2_temp;
if(percentage2<0x19) {if(percentage==0) percentage=3;else if(percentage==1) percentage=2;}
else {ten_percent2=0; if(percentage==3) percentage=0;else if(percentage==2) percentage=1;}
}
switch(percentage) //DC&&PWM-IN Dimming
{
case 0:
if(ccp1_buffer>0x04)
{
while(ccp1_temp>ccp1_buffer)
{
ccp1_temp--;
eccp1_initial(ccp1_temp,ADRESL);
}
while(ccp1_temp
{
ccp1_temp++;
eccp1_initial(ccp1_temp,0xc0);
}
ccp1temp=ccp1_temp;
}
else
{
if(ccp1_buffer>0x03)
{
if(CCPR2L!=0x04) eccp1_initial(0x04,0xc0);
}
else if(ccp1_buffer<0x01)
{
if(CCPR2L!=0) eccp1_initial(0,0);
}
ccp1_temp=CCPR2L;
}
case 1:
if(ccp2_buffer>0x04)//0x04
{
while(ccp2_temp>ccp2_buffer)
{
ccp2_temp--;
eccp2_initial(ccp2_temp,ADRESL);
}
while(ccp2_temp
{
ccp2_temp++;
eccp2_initial(ccp2_temp,0xc0);
}
ccp2temp=ccp2_temp;
}
else
{
if(ccp2_buffer>0x03)//0x03
{
if(CCPR3L!=0x04) eccp2_initial(0x04,0xc0);//0
}
else if(ccp2_buffer<0x01)
{
if(CCPR3L!=0) eccp2_initial(0,0);
}
ccp2_temp=CCPR3L;
}
if(save_en>=3)
{
if(ADCON0==0x11)
{
if(save_channel1==CCPR2L && save_channel2==CCPR3L)//浪涌
save_data();
save_channel1=CCPR2L;save_channel2=CCPR3L;
}
}
dim = 0;
break;
case 3:
if(ccp1_buffer>0x04)
{
while(ccp1_temp>ccp1_buffer)
{
ccp1_temp--;
eccp1_initial(ccp1_temp,ADRESL);
}
while(ccp1_temp
{
ccp1_temp++;
eccp1_initial(ccp1_temp,0xc0);
}
ccp1temp=ccp1_temp;
}
else
{
if(ccp1_buffer>0x03)
{
if(CCPR2L!=0x04) eccp1_initial(0x04,0xc0);
}
else if(ccp1_buffer<0x01)
{
if(CCPR2L!=0) eccp1_initial(0,0);
}
ccp1_temp=CCPR2L;
}
if(save_en>=3)
{
if(ADCON0==0x11)
{
if(save_channel1==CCPR2L && save_channel2==CCPR3L)//浪涌
save_data();
save_channel1=CCPR2L;save_channel2=CCPR3L;
}
}
dim = 0;
break;
}
if(save_en<3)
{
save_en++;
ccp1_temp=ccp1_buffer;
ccp2_temp=ccp2_buffer;
}
delay(0,210);
while(TMR0IF==0);
TMR0IF=0;
ADGO=1;
}
void set_program()
{
uchar num;
GIE=0;
e2prom_read(10);
num=r_e2_data;
if(num != 0x01)
{
//program1
EEIF=0;w_e2_data=0xff;
e2prom_write(0); //save CH1
while(EEIF==0);
EEIF=0;w_e2_data=0xff;
e2prom_write(1); //save CH2
while(EEIF==0);
//program2
EEIF=0;w_e2_data=0xff;
e2prom_write(2); //save CH1
while(EEIF==0);
EEIF=0;w_e2_data=0x38;
e2prom_write(3); //save CH2
while(EEIF==0);
//program3
EEIF=0;w_e2_data=0x38;
e2prom_write(4); //save CH1
while(EEIF==0);
EEIF=0;w_e2_data=0xff;
e2prom_write(5); //save CH2
while(EEIF==0);
//program4
EEIF=0;w_e2_data=0x19;
e2prom_write(6); //save CH1
while(EEIF==0);
EEIF=0;w_e2_data=0xff;
e2prom_write(7); //save CH2
while(EEIF==0);
EEIF=0;w_e2_data=0x00;
e2prom_write(8); //save CH1
while(EEIF==0);
EEIF=0;w_e2_data=0x00;
e2prom_write(9); //save CH2
while(EEIF==0);
EEIF=0;w_e2_data=0x01;
e2prom_write(10);
while(EEIF==0);
EEIF=0;
}
else
{
EEIF=0;w_e2_data=0x01;
e2prom_write(10);
while(EEIF==0);
EEIF=0;
}
GIE=1;
}
void dim_all(void)
{
//====================================================================================
if(RC2==0) //PB3-ccp1RC2
{
GIE=0;
count=0;
delay(0x07,0); //256分频,8ms一个周期
while(TMR0IF==0);
if(RC2==0) count=1;
while(RC2==0)
{
delay(0x07,0);
while(TMR0IF==0);
count++;
if(count>=0x3e) //按键达到0.5s
{
dim_ccp1();
ten_percent1=2;percentage=2;
ten_percent2=2;//!
while(RC2==0);
}
}
if(count<0x3e&&count>=1) //按键小于0.5s为on_off
{
if(CCPR2L==0) dim_ccp1_on(); else dim_ccp1_off();
ten_percent1=2;percentage=2;
ten_percent2=2;//!
count=0;
}
dim=0;
Channel=0;
}
//====================================================================================
if(RC4==0) //PB4-ccp2 Rc4
{
GIE=0;
count=0;
delay(0x07,0); //256分频,8ms一个周期
while(TMR0IF==0);
if(RC4==0) count=1;
while(RC4==0)
{
delay(0x07,0);
while(TMR0IF==0);
count++;
if(count>=0x3e) //按键达到0.5s
{
dim_ccp2();
ten_percent2=2;percentage=2;
ten_percent1=2;//!
while(RC4==0);
}
}
if(count<0x3e&&count>=1) //按键小于0.5s为on_off
{
if(CCPR3L==0) dim_ccp2_on(); else dim_ccp2_off();
ten_percent2=2;percentage=2;
ten_percent1=2;//!
count=0;
}
dim=0;
Channel=0;
}
//====================================================================================
if(RC5==0) //PB2-mster RC5
{
GIE=0;
count=0;
delay(0x07,0);
while(TMR0IF==0);
if(RC5==0) count=1;
while(RC5==0)
{
delay(0x07,0);
while(TMR0IF==0);
count++;
if(count>=0x31)
{
while(RC5==0)
{
dim_master();
ten_percent1=2;
ten_percent2=2;percentage=2;
while(RC5==0);
}
}
}
if(count<0x3e&&count>=1)
{
count=0;
if(Channel == 0)
{
if(CCPR2L == 0 && CCPR3L == 0)
{
while(CCPR2L
{
if(CCPR2L
if(CCPR3L
delay1();
}
if(pwm_in==1) {CCP2CON=0x3c;CCP3CON=0x3c;}
else {CCP2CON=0xbc;CCP3CON=0xbc;}
}
else if(CCPR2L != 0 && CCPR3L != 0)
{
while(CCPR2L>0||CCPR3L>0)
{
if(CCPR2L>0) CCPR2L--;
if(CCPR3L>0) CCPR3L--;
delay1();
}
if(pwm_in==1) {CCP2CON=0x0c;CCP3CON=0x0c;}
else {CCP2CON=0x8c;CCP3CON=0x8c;}
}
else if(CCPR2L != 0 && CCPR3L == 0)
{
dim_ccp1_off();
Channel = 1;
}
else if(CCPR2L == 0 && CCPR3L != 0)
{
dim_ccp2_off();
Channel = 2;
}
}
else if(Channel == 1)
if(CCPR2L==0) dim_ccp1_on(); else dim_ccp1_off();
else if(Channel == 2)
if(CCPR3L==0) dim_ccp2_on(); else dim_ccp2_off();
/*
if(CCPR2L==0||CCPR3L==0)
{
while(CCPR2L
{
if(CCPR2L
if(CCPR3L
delay1();
}
if(pwm_in==1) {CCP2CON=0x3c;CCP3CON=0x3c;}
else {CCP2CON=0xbc;CCP3CON=0xbc;}
}
else
{
while(CCPR2L>0||CCPR3L>0)
{
if(CCPR2L>0) CCPR2L--;
if(CCPR3L>0) CCPR3L--;
delay1();
}
if(pwm_in==1) {CCP2CON=0x0c;CCP3CON=0x0c;}
else {CCP2CON=0x8c;CCP3CON=0x8c;}
}
*/
ten_percent1=2;ten_percent2=2;percentage=2;
}
}
}
void PB1_Program(void)
{
if(RA0==0) //PB1-programming
{
GIE=0;
count=0;
delay(0x07,0);
while(TMR0IF==0);
if(RA0) count=0xff; else count=1;
while(RA0==0) //5s save program
{
delay(0x07,0);
while(TMR0IF==0);
delay(0x07,0);
while(TMR0IF==0);
delay(0x07,0);
while(TMR0IF==0);
count++;
if(count==0xcc) //5s
{
while(count==0xcc && !RA0) RB7=0;
while(count==0xcc) //5s save program
{
if(RA0==0) //save P1
{
delay(0x07,0);
while(TMR0IF==0);
if(RA0==0)
{
EEIF=0;w_e2_data=CCPR2L;
e2prom_write(0); //save CH1
while(EEIF==0);
EEIF=0;w_e2_data=CCPR3L;
e2prom_write(1); //save CH2
while(EEIF==0);
EEIF=0;
while(RA0==0);
break;
}
}
if(RA1==0) //save P2
{
delay(0x07,0);
while(TMR0IF==0);
if(RA1==0)
{
EEIF=0;w_e2_data=CCPR2L;
e2prom_write(2); //save CH1_1
while(EEIF==0);
EEIF=0;w_e2_data=CCPR3L;
e2prom_write(3); //save CH2_1
while(EEIF==0);
EEIF=0;
while(RA1==0);
break;
}
}
if(RA2==0) //save P3
{
delay(0x07,0);
while(TMR0IF==0);
if(RA2==0)
{
EEIF=0;w_e2_data=CCPR2L;
e2prom_write(4); //save CH1_2
while(EEIF==0);
EEIF=0;w_e2_data=CCPR3L;
e2prom_write(5); //save CH2_2
while(EEIF==0);
EEIF=0;
while(RA2==0);
break;
}
}
if(RA3==0) //save P4
{
delay(0x07,0);
while(TMR0IF==0);
if(RA3==0)
{
EEIF=0;w_e2_data=CCPR2L;
e2prom_write(6); //save CH1_2
while(EEIF==0);
EEIF=0;w_e2_data=CCPR3L;
e2prom_write(7); //save CH2_2
while(EEIF==0);
EEIF=0;
while(RA3==0);
break;
}
}
}
if(count==0xcc) RB7=1;
}
}
//if(count<0x31&&count>=1) //按键小于0.4s为select program
//---------------------------------------------------------------------------------------------------------
if(count<0x3e&&count>=1) //P1,P2,P3,P4 cycle
{
e_count++;
while(1)
{
if(e_count==1) //取p1
{
e2prom_read(0);
ccp1temp=r_e2_data;
e2prom_read(1);
ccp2temp=r_e2_data;
while((CCPR2L!=ccp1temp) || (CCPR3L!=ccp2temp))
{
if(CCPR2L
else if(CCPR2L>ccp1temp) CCPR2L--;
//delay1();
if(CCPR3L
else if(CCPR3L>ccp2temp) CCPR3L--;
delay1();
}
ten_percent1=2;ten_percent2=2;
break;
}
else if(e_count==2) //取p2
{
e2prom_read(2);
ccp1temp=r_e2_data;
e2prom_read(3);
ccp2temp=r_e2_data;
while((CCPR2L!=ccp1temp) || (CCPR3L!=ccp2temp))
{
if(CCPR2L
else if(CCPR2L>ccp1temp) CCPR2L--;
//delay1();
if(CCPR3L
else if(CCPR3L>ccp2temp) CCPR3L--;
delay1();
}
ten_percent1=2;ten_percent2=2;
break;
}
else if(e_count==3) //取p3
{
e2prom_read(4);
ccp1temp=r_e2_data;
e2prom_read(5);
ccp2temp=r_e2_data;
while((CCPR2L!=ccp1temp) || (CCPR3L!=ccp2temp))
{
if(CCPR2L
else if(CCPR2L>ccp1temp) CCPR2L--;
//delay1();
if(CCPR3L
else if(CCPR3L>ccp2temp) CCPR3L--;
delay1();
}
ten_percent1=2;ten_percent2=2;
break;
}
else if(e_count==4) //取p4
{
e2prom_read(6);
ccp1temp=r_e2_data;
e2prom_read(7);
ccp2temp=r_e2_data;
while((CCPR2L!=ccp1temp) || (CCPR3L!=ccp2temp))
{
if(CCPR2L
else if(CCPR2L>ccp1temp) CCPR2L--;
//delay1();
if(CCPR3L
else if(CCPR3L>ccp2temp) CCPR3L--;
delay1();
}
ten_percent1=2;ten_percent2=2;e_count=0;
break;
}
else {e_count=0; break;}
}
if(ccp1temp<0x05) ccp1temp=0x05;
if(ccp2temp<0x05) ccp2temp=0x05;
}
ten_percent1=2;ten_percent2=2;percentage=2;
save_data();
GIE=1;
dim=0;
Channel=0;
}
}
void main()
{
uchar i;
/*--------------------系统时钟配置--------------------*/
OSCCON=0xf0; //fosc/4:8M internal clock
/*--------------------I/O端口配置 --------------------*/
OPTION_REG=0xff;
TRISA=0xff; //input
TRISB=0x7f; //input
TRISC=0x3c; //output
ANSELA=0x20; //RA5为模拟功能
ANSELB=0x3f; //RB5为模拟功能
WPUB=0x00;
/*------------------CCP1,2初始化程序------------------*/
CCP2AS=0x05; //禁止自动关断
PWM2CON=0x80; //无死区时间
CCP3AS=0x05;
PWM3CON=0x80;
CCPTMRS0=0xc7; //CCP2用TIMER4,CCP3用TIMER2
CCPTMRS1=0x03;
PR4=0xfe; //PWM周期为128us-7.81KHz
PR2=0xfe; //100%!
TMR2IE=0; //禁CCP定时器中断
TMR4IE=0;
TMR2IF=0;
TMR4IF=0;
percentage=2;
for(i=0;i<28;i++)//1S
{
delay(0x07,0);
while(TMR0IF==0);
delay(0x07,0);
while(TMR0IF==0);
delay(0x07,0);
while(TMR0IF==0);
}
/*--------------------AD初始化程序--------------------*/
ad_initial();
delay(0,210); //delay 5us作为AD采集时间
while(TMR0IF==0);
TMR0IF=0;
ADIF=0;
ADIE=1;
ADGO=1;
/*-------------------开总中断和外设中断---------------*/
INTCON=0xc0; //admittance of GIE,PEIE interrupt,forbid tmr0 interrupt
//TMR0IE=0; //禁TMR0中断
EEIE=0; //禁E2中断
/*------------------------------------------------------*/
while(save_en<3);
ADIE=0;
eccp1_initial(0,0);
eccp2_initial(0,0);
set_program();
dim=0;
e2prom_read(8);
ccp1temp=r_e2_data;
e2prom_read(9);
ccp2temp=r_e2_data;
while((CCPR2L!=ccp1temp)||(CCPR3L!=ccp2temp))
{
if(CCPR2L
else if(CCPR2L>ccp1temp) CCPR2L--;
delay1();
if(CCPR3L
else if(CCPR3L>ccp2temp) CCPR3L--;
delay1();
}
ten_percent1=2;ten_percent2=2;percentage=2;
if(ccp1temp<0x05) ccp1temp=0x05;
if(ccp2temp<0x05) ccp2temp=0x05;
ADIE=1;
RB7=1;
/*--------------------主程判PB循环 -------------------*/
while(1)
{
if(RC3==0)
{
//====================================================================================
if(RA2==0 || RC2==0) //PB3-ccp1RC2
{
GIE=0;
count=0;
delay(0x07,0); //256分频,8ms一个周期
while(TMR0IF==0);
if(RA2==0 || RC2==0) count=1;
while(RA2==0 || RC2==0)
{
delay(0x07,0);
while(TMR0IF==0);
count++;
if(count>=0x3e) //按键达到0.5s
{
dim_ccp1();
ten_percent1=2;percentage=2;
ten_percent2=2;//!
while(RA2==0 || RC2==0);
}
}
if(count<0x3e&&count>=1) //按键小于0.5s为on_off
{
if(CCPR2L==0) dim_ccp1_on(); else dim_ccp1_off();
ten_percent1=2;percentage=2;
ten_percent2=2;//!
count=0;
}
save_data();
GIE=1;
dim=0;
Channel=0;
}
//====================================================================================
PB1_Program();
//====================================================================================
if(RA3==0 || RC4==0) //PB4-ccp2 Rc4
{
GIE=0;
count=0;
delay(0x07,0); //256分频,8ms一个周期
while(TMR0IF==0);
if(RA3==0 || RC4==0) count=1;
while(RA3==0 || RC4==0)
{
delay(0x07,0);
while(TMR0IF==0);
count++;
if(count>=0x3e) //按键达到0.5s
{
dim_ccp2();
ten_percent2=2;percentage=2;
ten_percent1=2;//!
while(RA3==0 || RC4==0);
}
}
if(count<0x3e&&count>=1) //按键小于0.5s为on_off
{
if(CCPR3L==0) dim_ccp2_on(); else dim_ccp2_off();
ten_percent2=2;percentage=2;
ten_percent1=2;//!
count=0;
}
save_data();
GIE=1;
dim=0;
Channel=0;
}
//====================================================================================
if(RA1==0 || RC5==0) //PB2-mster RC5
{
GIE=0;
count=0;
delay(0x07,0);
while(TMR0IF==0);
if(RA1==0 || RC5==0) count=1;
while(RA1==0 || RC5==0)
{
delay(0x07,0);
while(TMR0IF==0);
count++;
if(count>=0x3e)
{
while(RA1==0 || RC5==0)
{
dim_master();
ten_percent1=2;
ten_percent2=2;percentage=2;
while(RA1==0 || RC5==0);
}
}
}
if(count<0x3e&&count>=1)
{
count=0;
if(Channel == 0)
{
if(CCPR2L == 0 && CCPR3L == 0)
{
while(CCPR2L
{
if(CCPR2L
if(CCPR3L
delay1();
}
if(pwm_in==1) {CCP2CON=0x3c;CCP3CON=0x3c;}
else {CCP2CON=0xbc;CCP3CON=0xbc;}
}
else if(CCPR2L != 0 && CCPR3L != 0)
{
while(CCPR2L>0||CCPR3L>0)
{
if(CCPR2L>0) CCPR2L--;
if(CCPR3L>0) CCPR3L--;
delay1();
}
if(pwm_in==1) {CCP2CON=0x0c;CCP3CON=0x0c;}
else {CCP2CON=0x8c;CCP3CON=0x8c;}
}
else if(CCPR2L != 0 && CCPR3L == 0)
{
dim_ccp1_off();
Channel = 1;
}
else if(CCPR2L == 0 && CCPR3L != 0)
{
dim_ccp2_off();
Channel = 2;
}
}
else if(Channel == 1)
if(CCPR2L==0) dim_ccp1_on(); else dim_ccp1_off();
else if(Channel == 2)
if(CCPR3L==0) dim_ccp2_on(); else dim_ccp2_off();
/*
if(CCPR2L==0||CCPR3L==0)
{
while(CCPR2L
{
if(CCPR2L
if(CCPR3L
delay1();
}
if(pwm_in==1) {CCP2CON=0x3c;CCP3CON=0x3c;}
else {CCP2CON=0xbc;CCP3CON=0xbc;}
}
else
{
while(CCPR2L>0||CCPR3L>0)
{
if(CCPR2L>0) CCPR2L--;
if(CCPR3L>0) CCPR3L--;
delay1();
}
if(pwm_in==1) {CCP2CON=0x0c;CCP3CON=0x0c;}
else {CCP2CON=0x8c;CCP3CON=0x8c;}
}
*/
ten_percent1=2;ten_percent2=2;percentage=2;
}
save_data();
GIE=1;
}
}
else
{
GIE=0;
while(1) //select program
{
if(RA0==0) //choose program P1
{
delay(0x07,0);
while(TMR0IF==0);
if(RA0==0)
{
e2prom_read(0);
ccp1temp=r_e2_data;
e2prom_read(1);
ccp2temp=r_e2_data;
while((CCPR2L!=ccp1temp) || (CCPR3L!=ccp2temp))
{
if(CCPR2L
else if(CCPR2L>ccp1temp) CCPR2L--;
//delay1();
if(CCPR3L
else if(CCPR3L>ccp2temp) CCPR3L--;
delay1();
}
ten_percent1=2;ten_percent2=2;percentage=2;
dim=0;
Channel=0;
while(RA0==0);
break;
}
}
if(RA1==0) //choose program P2
{
delay(0x07,0);
while(TMR0IF==0);
if(RA1==0)
{
e2prom_read(2);
ccp1temp=r_e2_data;
e2prom_read(3);
ccp2temp=r_e2_data;
while((CCPR2L!=ccp1temp) || (CCPR3L!=ccp2temp))
{
if(CCPR2L
else if(CCPR2L>ccp1temp) CCPR2L--;
//delay1();
if(CCPR3L
else if(CCPR3L>ccp2temp) CCPR3L--;
delay1();
}
ten_percent1=2;ten_percent2=2;percentage=2;
dim=0;
Channel=0;
while(RA1==0);
break;
}
}
if(RA2==0) //choose program P3
{
delay(0x07,0);
while(TMR0IF==0);
if(RA2==0)
{
e2prom_read(4);
ccp1temp=r_e2_data;
e2prom_read(5);
ccp2temp=r_e2_data;
while((CCPR2L!=ccp1temp) || (CCPR3L!=ccp2temp))
{
if(CCPR2L
else if(CCPR2L>ccp1temp) CCPR2L--;
//delay1();
if(CCPR3L
else if(CCPR3L>ccp2temp) CCPR3L--;
delay1();
}
ten_percent1=2;ten_percent2=2;percentage=2;
dim=0;
Channel=0;
while(RA2==0);
break;
}
}
if(RA3==0) //choose program P4
{
delay(0x07,0);
while(TMR0IF==0);
if(RA3==0)
{
e2prom_read(6);
ccp1temp=r_e2_data;
e2prom_read(7);
ccp2temp=r_e2_data;
while((CCPR2L!=ccp1temp) || (CCPR3L!=ccp2temp))
{
if(CCPR2L
else if(CCPR2L>ccp1temp) CCPR2L--;
//delay1();
if(CCPR3L
else if(CCPR3L>ccp2temp) CCPR3L--;
delay1();
}
ten_percent1=2;ten_percent2=2;percentage=2;
dim=0;
Channel=0;
while(RA3==0);
break;
}
}
break;
}
//============================================================================
dim_all();
save_data();
GIE=1;
}
//======================================================================================================
}
}
|
|