#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腳輸出計(jì)數(shù)器
unsigned long delay11=0 ; //模擬輸入延時(shí)
unsigned long TIMEALL ;
unsigned long DELAY;
#define TIME 2 //設(shè)置時(shí)間1秒
void up_on(void) //I/O配置函數(shù)
{
INTCON=0x00; //關(guān)閉所有中斷
ADCON1=0X04; //A/D模塊設(shè)置AN0 AN1 AN3為模擬輸入;其他引腳為數(shù)字輸出
TRISC=0XE0; //設(shè)置11-15腳為輸出
RBPU=0; //RB設(shè)置弱上拉
}
///////////////////
void AD(void) //A/D配置函數(shù)
{
ADCS1=1; //FOSC/32 A/D轉(zhuǎn)換時(shí)鐘
ADCS0=0;
CHS2=0; //設(shè)置AN1為模擬輸入
CHS1=0;
CHS0=1;
ADIE=1; //A/D轉(zhuǎn)換中斷允許
PEIE=1; //外圍中斷允許
TRISA1=1; //設(shè)置RA1 為輸入方式
ADON=1; //A/D模塊開始工作
}
////////////
void tmint0(void) //timer0初始化函數(shù)
{
T0CS=0; //TMR0工作于定時(shí)器方式
PSA=0; //TMR0用分頻
PS2=0; //TMR0輸入16分頻
PS1=1;
PS0=1;
T0IF=0; //清除TMR0 的中斷標(biāo)志
T0IE=1; //TMR0中斷允許
}
/////////
//////////
void interrupt AD_timer(void) //A/D和timer中斷函數(shù)
{
if(T0IF) //判斷timer0是否發(fā)生中斷
{
T0IF=0; //清除中斷標(biāo)志
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是否發(fā)生中斷
{
AD_vasule[0]=ADRESL; //保存AD轉(zhuǎn)換結(jié)果低8位
AD_vasule[1]=ADRESH; //保存AD轉(zhuǎn)換結(jié)果高2位
AD_vasule[1]=AD_vasule[1]<<2; //A/D結(jié)果高2位左移2位
AD_vasule[0]=AD_vasule[0]>>6;
ADOUT=AD_vasule[1]+AD_vasule[0]; //轉(zhuǎn)換成10位結(jié)果保存
ADIF=0; //清除中斷標(biāo)志
ADGO=1; //開始下次轉(zhuǎn)換
}
}
///////////////////////
//////////////////////
void OUT_DELAY(void)////計(jì)算延時(shí)
{
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延時(shí)
else DELAY= TIMEALL*1024/(t*ADOUT_2);
}
////////////////////
void key5(void) //控制方式1-5
{
OUT_DELAY();//計(jì)算延時(shí)
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; //計(jì)算延時(shí)
up_on(); //I/O配置函數(shù)
AD() ; //A/D配置函數(shù)
tmint0(); //timer0初始化函數(shù)
ei(); //打開所有中斷
ADGO=1; //設(shè)置這位為1開始下次轉(zhuǎn)換
for(i=1000;i>10;i--); //延時(shí)等待穩(wěn)定
i=0;
while(1)
{
key5() ; //控制方式1-5
}
}
兼職PIC全系列開發(fā): QQ 122533653
歡迎光臨 (http://m.zg4o1577.cn/bbs/) | Powered by Discuz! X3.1 |