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<ccp1temp)
{
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<ccp2temp)
{
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_buffer)
{
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_buffer)
{
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_buffer)
{
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<ccp1temp||CCPR3L<ccp2temp)
{
if(CCPR2L<ccp1temp) CCPR2L++;
if(CCPR3L<ccp2temp) 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<ccp1temp||CCPR3L<ccp2temp)
{
if(CCPR2L<ccp1temp) CCPR2L++;
if(CCPR3L<ccp2temp) 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;
&nbs
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
未说明使用的是什么单片机,开发环境是什么(怀疑是PICC),大概实现的是什么功能,最好是硬件的外围功能等,才能最快的实现注释功能
这程序写得...
一周热门 更多>