📄 adc.c
字号:
/*
;********int The adc***********
;configure channel CH5,CH4(p1.5,p1.4) for adc <------adcf=30h=00110000b
;The adc Clock is 500K=Fcpu/(4*(adclk+1))=40M/80 <------adclk
;setb ADEN(enable adc) <------adcon=20h
;adcon=25h+8 means "configure the p1.5 and start adc"
;adcon=24h+8 means "configure the p1.4 and start adc"
;The result of 8 bits is ADDH
;The result of 10 bits is ADDH<<2+ADDL
*/
void int_adc()
{ ADCF=0x30;
ADCLK=19;
ADCON=0x20;
}
void adc_process()
{
Uint value;
Uchar i;
switch (state_adc)
{
case 0: P1_2=0;P1_0=1;P1_3=0;P1_1=0;state_adc++;break;
case 1: ADCON=0x65+8;state_adc++;break;
case 2: ADCON=0x20;value=(ADDH<<2)|ADDL;
if (value>=1015)
{state_adc=0;mov_adc=0;open_adc=1;break;}
else if (close_adc==1) {state_adc=0;open_adc=0;break;}
else state_adc++;break;
case 3: P1_2=0;P1_0=1;P1_3=1;P1_1=0;state_adc++;break;
case 4: ADCON=0x65+8;state_adc++;break;
case 5: ADCON=0x20;value=(ADDH<<2)|ADDL;
i=adc_cishu-1;
do { adc_x[i]=adc_x[i-1];i--;}
while (i>0);
adc_x[0]=value;
P1_2=1;P1_0=0;P1_3=0;P1_1=1;state_adc++;break;
case 6: ADCON=0x64+8;state_adc++;break;
case 7: ADCON=0x20;value=(ADDH<<2)|ADDL;
i=adc_cishu-1;
do { adc_y[i]=adc_y[i-1];i--;}
while (i>0);
adc_y[0]=value;
mov_adc++;
if (mov_adc==adc_cishu)
{close_adc=1;open_adc=0;}
P1_2=0;P1_0=1;P1_3=0;P1_1=0;state_adc=1;break;
default: break;
};
}
//X轴:39y=950*39-865x<=====>y=(950*39-39*x)/865
//Y轴:835y=925*835-239x<=====>y=(925*835-835*x)/239
Uint average_adc(bit f)
{
Uchar i;
Uint x,y;
x=y=0;
if (f){
for (i=0;i<adc_cishu;i++)
{y=y+adc_y[i];}
y=y/adc_cishu;
return((920-y)*49/170);
}
else
{for (i=0;i<adc_cishu;i++)
{x=x+adc_x[i];}
x=x/adc_cishu;
return((950-x)*39/855);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -