/*
*************************************************************************
*名称: unsigned int GetADCValue(unsigned char uchADCChannel) *
*功能: 获取按键ADC值 *
*参数: 无 *
*返回: 按键ADC值 *
*************************************************************************
*/
unsigned int GetADCValue(unsigned char uchADCChannel)
{
unsigned char i;
unsigned int uiADCValue = 0;
ADMUX = _BV(REFS0)|uchADCChannel; // 参考电压AVCC选择
for (i=0; i<4; i++) // 连续转换4次
{
ADCSRA |= (1< ADCSRA |= (1< loop_until_bit_is_set(ADCSRA, ADIF); // 等待ADC转换结束
ADCSRA |= (1 << ADIF); // 写1清除标志位
uiADCValue =uiADCValue + ADC;
}
uiADCValue /= 4; // 取平均值
return uiADCValue;
}
/*
*************************************************************************
*名称: unsigned char GetKeyValue(void) *
*功能: 获取按键值 *
*参数: 无 *
*返回: 按键值 *
*************************************************************************
*/
unsigned char GetKeyValue(void)
{
// 5.24V下按键与对应ADC值如下
// KEY NO KEY0 KEY1 KEY2 KEY3
// ADC 1023 0012 390 565 669
unsigned char uchKeyValue = NOKEY; // 按键码
unsigned int uiKeyADC = 0; // 按键ADC值
uiKeyADC = GetADCValue(ADC_CHANNEL);
if(uiKeyADC<1000) // 如果有键按下
{
DelayNms(10); // 延时10ms消抖
uiKeyADC = GetADCValue(ADC_CHANNEL);
if(uiKeyADC < 32) // KEY0
{
uchKeyValue = KEY0;
}
else if(uiKeyADC < 410) // KEY1
{
uchKeyValue = KEY1;
}
else if(uiKeyADC < 585) // KEY2
{
uchKeyValue = KEY2;
}
else if(uiKeyADC < 689) // KEY3
{
uchKeyValue = KEY3;
}
}
return uchKeyValue;
}
来源:http://blog.ednchina.com/wormchen/29512/message.aspx
文章评论(0条评论)
登录后参与讨论