本帖最后由 junyu99 于 2018-5-2 01:38 编辑
/*----------------------------
初始化ADC
---------------------------- */
void InitADC()
{
P1ASF=0x0f; //设置P1.0 /P1.1/P1.2/P1.3端口作为模拟输入
ADC_RES = 0; //清除结果寄存器
ADC_CONTR = ADC_POWER | ADC_SPEEDHH | ADC_START | 0;
_delay_ms(1); //ADC上电并延时
}
/*******************定时器0*******************/
void Inittimer0()
{
TMOD = 0x01;
TH0 = 0x0FC;
TL0 = 0x18;
EA = 1;
ET0 = 1;
TR0 = 1;
}
uchar PianLi(uchar a) //求|a-127|+1,离中值
{
if(a>127)return (a-127); //1到128
else return (128-a);
}
void chuli(uchar x,uchar y,uchar *c,uchar *d,uchar *e) //
{
uchar pianlix,pianliy;
pianlix=PianLi(x);
pianliy=PianLi(y);
if(pianlix>20||pianliy>20)
{
if(100*pianliy<=41*pianlix)*e=1; // 0到22.5度
else if(100*pianliy>=241*pianlix)*e=3;// 67.5度到90度
else *e=2;// 22.5度到67.5度
if(x>127)*c=1;
else *c=2;
if(y>127)*d=1;
else *d=2;
}
else *c=*d=0;
}
uchar dx1,dx2,dy1,dy2; //1正2负0中
uchar xy1,xy2; //1:0到22.5度 2:22.5度到67.5度 3:67.5度到90度
void main (void)
{
//uchar zhix1,zhix2,zhiy1,zhiy2;
P1M0=0x00;
P1M1=0xff;//P2M0=255;P2M1=0;P3M0=255;P3M1=0;
InitADC(); //初始化ADC
InitTimer0();
while (1) //主循环
{
if(dx1&&dy1) //左摇杆 while(dx1==1&&dy1==1&&xy1=1);
{
if(dx1==1&&dy1==1&&xy1==1){P31=P30=0;_delay_ms(500);P31=P30=1;}
if(dx1==1&&dy1==1&&xy1==2){P31=P30=0;P32=P33=0;_delay_ms(500);P31=P30=1;P32=P33=1;}//
if(dx1==1&&dy1==1&&xy1==3){P32=P33=0;_delay_ms(500);P32=P33=1;}
if(dx1==1&&dy1==2&&xy1==1){P31=P30=0;_delay_ms(500);P31=P30=1;}
if(dx1==1&&dy1==2&&xy1==2){P34=P35=0;P31=P30=0;_delay_ms(500);P34=P35=1;P31=P30=0;}//
if(dx1==1&&dy1==2&&xy1==3){P34=P35=0;_delay_ms(500);P34=P35=1;}
if(dx1==2&&dy1==1&&xy1==1){P36=P37=0;_delay_ms(500);P36=P37=1;}
if(dx1==2&&dy1==1&&xy1==2){P32=P33=0;P36=P37=0;_delay_ms(500);P32=P33=1;P36=P37=1;}//
if(dx1==2&&dy1==1&&xy1==3){P32=P33=0;_delay_ms(500);P32=P33=1;}
if(dx1==2&&dy1==2&&xy1==1){P36=P37=0;_delay_ms(500);P36=P37=1;}
if(dx1==2&&dy1==2&&xy1==2){P34=P35=0;P36=P37=0;_delay_ms(500);P34=P35=1;P36=P37=1;}//
if(dx1==2&&dy1==2&&xy1==3){P34=P35=0;_delay_ms(500);P34=P35=1;}
}
if(dx2&&dy2) //摇杆 while(dx1==1&&dy1==1&&xy1=1);
{
if(dx2==1&&dy2==1&&xy2==1){P21=P20=0;_delay_ms(500);P21=P20=1;}
if(dx2==1&&dy2==1&&xy2==2){P21=P20=0;P22=P23=0;_delay_ms(500);P21=P20=1;P22=P23=1;}//
if(dx2==1&&dy2==1&&xy2==3){P22=P23=0;_delay_ms(500);P22=P23=1;}
if(dx2==1&&dy2==2&&xy2==1){P21=P20=0;_delay_ms(500);P21=P20=1;}
if(dx2==1&&dy2==2&&xy2==2){P24=P25=0;P21=P20=0;_delay_ms(500);P24=P25=1;P21=P20=0;}//
if(dx2==1&&dy2==2&&xy2==3){P24=P25=0;_delay_ms(500);P24=P25=1;}
if(dx2==2&&dy2==1&&xy2==1){P26=P27=0;_delay_ms(500);P26=P27=1;}
if(dx2==2&&dy2==1&&xy2==2){P22=P23=0;P26=P27=0;_delay_ms(500);P22=P23=1;P26=P27=1;}//
if(dx2==2&&dy2==1&&xy2==3){P22=P23=0;_delay_ms(500);P22=P23=1;}
if(dx2==2&&dy2==2&&xy2==1){P26=P27=0;_delay_ms(500);P26=P27=1;}
if(dx2==2&&dy2==2&&xy2==2){P24=P25=0;P26=P27=0;_delay_ms(500);P24=P25=1;P26=P27=1;}//
if(dx2==2&&dy2==2&&xy2==3){P24=P25=0;_delay_ms(500);P24=P25=1;}
}
}
}
void Timer0Interrupt() interrupt 1
{
// TH0 =(65535-1000)/256;
// TL0 =(65535-1000)%256;
chuli(255-zhADC(2),zhADC(3),&dx1,&dy1,&xy1);
chuli(255-zhADC(1),zhADC(0),&dx2,&dy2,&xy2);
}
0