#include <pic.h> unsigned int i,max,X11=0,X12=0,X13=0,X14=0,X15=0,t=2; unsigned long AD_vasule[2],ADOUT,ADOUT_2; unsigned long timer[5]; //11-15腳輸出計數器 unsigned long delay11=0 ; //模擬輸入延時 unsigned long TIMEALL ; unsigned long DELAY; #define TIME 2 //設置時間1秒
void up_on(void) //I/O配置函數 { INTCON=0x00; //關閉所有中斷 ADCON1=0X04; //A/D模塊設置AN0 AN1 AN3為模擬輸入;其他引腳為數字輸出 TRISC=0XE0; //設置11-15腳為輸出 RBPU=0; //RB設置弱上拉 } /////////////////// void AD(void) //A/D配置函數 { ADCS1=1; //FOSC/32 A/D轉換時鐘 ADCS0=0; CHS2=0; //設置AN1為模擬輸入 CHS1=0; CHS0=1; ADIE=1; //A/D轉換中斷允許 PEIE=1; //外圍中斷允許 TRISA1=1; //設置RA1 為輸入方式 ADON=1; //A/D模塊開始工作 } //////////// void tmint0(void) //timer0初始化函數 { T0CS=0; //TMR0工作于定時器方式 PSA=0; //TMR0用分頻 PS2=0; //TMR0輸入16分頻 PS1=1; PS0=1; T0IF=0; //清除TMR0 的中斷標志 T0IE=1; //TMR0中斷允許
}
/////////
//////////
void interrupt AD_timer(void) //A/D和timer中斷函數 { if(T0IF) //判斷timer0是否發生中斷 { T0IF=0; //清除中斷標志
if(!RB4&&RB5&&RB6) {max=0; timer[1]=TIMEALL;timer[2]=TIMEALL;timer[3]=TIMEALL; timer[4]=TIMEALL; t=4;} //控制方式1 else if(RB4&&!RB5&&RB6){ max=1; timer[2]=TIMEALL;timer[3]=TIMEALL; timer[4]=TIMEALL; if(X12!=1){X11=0;X12=1;X13=0;X14=0;X15=0; i=0; timer[0]=0;timer[1]=TIMEALL; } t=2; }//控制方式2 else if(!RB4&&!RB5&&RB6){ max=2; timer[3]=TIMEALL; timer[4]=TIMEALL; if(X13!=1){X11=0;X12=0;X13=1;X14=0;X15=0; i=0; timer[0]=0;timer[1]=TIMEALL;timer[2]=TIMEALL; } t=3; } //控制方式3 else if(RB4&&RB5&&!RB6) {max=3; timer[4]=TIMEALL; if(X14!=1){X11=0;X12=0;X13=0;X14=1;X15=0; i=0; timer[0]=0;timer[1]=TIMEALL;timer[2]=TIMEALL;timer[3]=TIMEALL; } t=4; } //控制方式4 else if(!RB4&&RB5&&!RB6) {max=4 ; //控制方式5 if(X15!=1){X11=0;X12=0;X13=0;X14=0;X15=1; i=0; timer[0]=0;timer[1]=TIMEALL;timer[2]=TIMEALL;timer[3]=TIMEALL;timer[4]=TIMEALL; } t=5; } else max=5; if(timer[0]<TIMEALL)timer[0]++; if (timer[1]<TIMEALL)timer[1]++; if (timer[2]<TIMEALL)timer[2]++; if (timer[3]<TIMEALL)timer[3]++; if (timer[4]<TIMEALL)timer[4]++; if(delay11<DELAY) delay11++; else { delay11=0; if(max==0) {i=0;timer[0]=0;} else { if (i<max) i++; else i=0; if(max<5) timer=0; } } } if(ADIF) //判斷AD是否發生中斷 { AD_vasule[0]=ADRESL; //保存AD轉換結果低8位 AD_vasule[1]=ADRESH; //保存AD轉換結果高2位 AD_vasule[1]=AD_vasule[1]<<2; //A/D結果高2位左移2位 AD_vasule[0]=AD_vasule[0]>>6; ADOUT=AD_vasule[1]+AD_vasule[0]; //轉換成10位結果保存 ADIF=0; //清除中斷標志 ADGO=1; //開始下次轉換 } }
/////////////////////// ////////////////////// void OUT_DELAY(void)////計算延時 { ADOUT_2=ADOUT; if(ADOUT_2<10) ADOUT_2=10; if(ADOUT_2>=818) ADOUT_2=1024; if(max==0) DELAY=TIMEALL*1024*4/(5*ADOUT_2); //控制方式1延時 else DELAY= TIMEALL*1024/(t*ADOUT_2); }
//////////////////// void key5(void) //控制方式1-5
{ OUT_DELAY();//計算延時 if((i==0)&&(timer[0]<TIMEALL)) { RC0=1; } if(((i!=0)&&(timer[0]>=TIMEALL)&&(DELAY<=TIMEALL))||((DELAY>TIMEALL)&&(timer[0]>=TIMEALL)))
{ RC0=0; } /////////////////////////////////// if((i==1)&&(timer[1]<TIMEALL)) { RC1=1; } if(((i!=1)&&(timer[1]>=TIMEALL)&&(DELAY<=TIMEALL))||((DELAY>TIMEALL)&&(timer[1]>=TIMEALL))) { RC1=0; } ////////////////////////////////////// /////////////////////////////////// if((i==2)&&(timer[2]<TIMEALL)) { RC2=1; } if(((i!=2)&&(timer[2]>=TIMEALL)&&(DELAY<=TIMEALL))||((DELAY>TIMEALL)&&(timer[2]>=TIMEALL))) { RC2=0; } ////////////////////////////////////// /////////////////////////////////// if((i==3)&&(timer[3]<TIMEALL)) { RC3=1; } if(((i!=3)&&(timer[3]>=TIMEALL)&&(DELAY<=TIMEALL))||((DELAY>TIMEALL)&&(timer[3]>=TIMEALL))) { RC3=0; } ////////////////////////////////////// /////////////////////////////////// if((i==4)&&(timer[4]<TIMEALL)) { RC4=1; } if(((i!=4)&&(timer[4]>=TIMEALL)&&(DELAY<=TIMEALL))||((DELAY>TIMEALL)&&(timer[4]>=TIMEALL))) { RC4=0; }
}
////////////////// ////////////////////
void main(void) { TIMEALL= 244*TIME; //計算延時 up_on(); //I/O配置函數 AD() ; //A/D配置函數 tmint0(); //timer0初始化函數 ei(); //打開所有中斷 ADGO=1; //設置這位為1開始下次轉換 for(i=1000;i>10;i--); //延時等待穩定 i=0; while(1) { key5() ; //控制方式1-5 }
}
兼職PIC全系列開發: QQ 122533653
16f873.rar
(142.2 KB, 下載次數: 26)
2012-7-10 13:58 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
[此貼子已經被作者于2012-7-10 13:59:26編輯過]
|