#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:59:26编辑过]
|