主要是想按键检测,我这程序直接读取ADC10MEM能得到准确的电压值,但是转换4次求平均值,值就不正确
#include "studio.h"
unsigned char key_value();
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
while(1)
{
P1DIR |=0X80;
key_value();
}
}
unsigned char key_value()
{ unsigned char i,ad_value=0;
ADC10CTL0 = SREF_1+REFON+REF2_5V+ADC10SHT_2 + ADC10ON;
ADC10CTL1 = INCH_7+CONSEQ_2;
ADC10AE0 |= 0x80;
P1DIR |= 0x01;
ADC10CTL0 |= ENC+ADC10SC;
if (ADC10MEM*2.5/1023<2)
{
P1OUT |=0X80;
for(i=0;i<4;i++)
{
ADC10CTL0 |= ENC+ADC10SC;
while (ADC10CTL1 & ADC10BUSY);
ad_value=ad_value+ADC10MEM;
}
ad_value=ad_value/4;
if(ad_value*2.5/1023>0.2)
P1OUT |=0X01;
else
P1OUT &=~0X01;
}
return ad_value;
} |