⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adc_key.c

📁 avr adc control source and adc key input source
💻 C
字号:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <avr/wdt.h>
#include <stdlib.h>
#include <string.h>

#include "adc_key.h"
#include "main.h"

extern unsigned char			InputCurKey, InputPreKey;
extern unsigned short	    ChatterCurKey;
extern unsigned char			PushTime;
extern unsigned char			IsRepeat, KeyEnable, ContinueKeyCheckEnable;

//extern unsigned short	Event_BtTimer;
u08 Bufcnt = 0;
unsigned char Filtbuf[4] = {0,0,0,0};

extern KeyStatusDef	KeyStatus;
//extern KeyTypeDef 	KeyAttribute[TOTAL_KEY];
extern KeyTypeDef 	KeyAttribute[TOTAL_NEWKEY];

void adc_init(u08 channel)
{
	ADCSRA = 0x00; //disable adc
	ADMUX = channel; //select adc input 0
}

void Adc_Start( u08 channel )
{
	ADMUX = channel;
	ADCSRA = 0xC7; //AD enable, ADSC:1, free runnung:0,ADIF,ADIE:0
}

void Adc_Stop( u08 channel )
{
	ADMUX = channel;
	ADCSRA = 0x07; // Adc disable
}


unsigned short Adc_Data( unsigned char mux )
{
		unsigned short value = 0x0000;
		ADCSRA &= 0x3f; // Adc_disable, ADSC:0
		ADMUX |= mux;
		ADCSRA |= 0xC7; // Adc_Enable
		_delay_ms(3);

 		value = (ADCL)&0x00ff;
 		value |= (ADCH << 8);
 		value &= 0x03ff;
 		_delay_ms(6);
 		ADCSRA &= 0x3f; // Adc_disable, ADSC:0
 		return value;
}
/*
void KEY_InitKeyAttribute(void)
{
	// Init Key Type
	KeyAttribute[KEY_PDMHZ].Type				  = SHORT_PUSH_TYPE | LONG_PUSH_TYPE;
  KeyAttribute[KEY_PDMHZ].HoldTime		  = HOLD_TIME_FAST;
	KeyAttribute[KEY_PDMHZ].RepeatTime	  = REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_PDKHZ].Type				  = SHORT_PUSH_TYPE | LONG_PUSH_TYPE;
	KeyAttribute[KEY_PDKHZ].HoldTime		  = HOLD_TIME_FAST;
	KeyAttribute[KEY_PDKHZ].RepeatTime	  = REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_PDATT].Type				  = SHORT_PUSH_TYPE | LONG_PUSH_TYPE;
	KeyAttribute[KEY_PDATT].HoldTime		  = HOLD_TIME_FAST;
	KeyAttribute[KEY_PDATT].RepeatTime	  = REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_UP].Type						  = SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_UP].HoldTime				  = HOLD_TIME_FAST;
	KeyAttribute[KEY_UP].RepeatTime			  = REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_THREUP].Type				  = SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_THREUP].HoldTime		  = HOLD_TIME_FAST;
	KeyAttribute[KEY_THREUP].RepeatTime	  = REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_PWDUP].Type				  = SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_PWDUP].HoldTime		  = HOLD_TIME_FAST;
	KeyAttribute[KEY_PWDUP].RepeatTime	  = REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_PDGAINUP].Type				= SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_PDGAINUP].HoldTime		= HOLD_TIME_FAST;
	KeyAttribute[KEY_PDGAINUP].RepeatTime	= REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_NSGAINUP].Type				= SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_NSGAINUP].HoldTime		= HOLD_TIME_FAST;
	KeyAttribute[KEY_NSGAINUP].RepeatTime	= REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_NSMHZ].Type					= SHORT_PUSH_TYPE | LONG_PUSH_TYPE;
	KeyAttribute[KEY_NSMHZ].HoldTime			= HOLD_TIME_FAST;
	KeyAttribute[KEY_NSMHZ].RepeatTime	= REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_NSKHZ].Type					= SHORT_PUSH_TYPE | LONG_PUSH_TYPE;
	KeyAttribute[KEY_NSKHZ].HoldTime			= HOLD_TIME_FAST;
	KeyAttribute[KEY_NSKHZ].RepeatTime	= REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_NSATT].Type					= SHORT_PUSH_TYPE | LONG_PUSH_TYPE;
	KeyAttribute[KEY_NSATT].HoldTime			= HOLD_TIME_FAST;
	KeyAttribute[KEY_NSATT].RepeatTime	= REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_DOWN].Type						= SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_DOWN].HoldTime				= HOLD_TIME_FAST;
	KeyAttribute[KEY_DOWN].RepeatTime	= REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_THREDOWN].Type				= SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_THREDOWN].HoldTime		= HOLD_TIME_FAST;
	KeyAttribute[KEY_THREDOWN].RepeatTime	= REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_PWDDOWN].Type				= SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_PWDDOWN].HoldTime		= HOLD_TIME_FAST;
	KeyAttribute[KEY_PWDDOWN].RepeatTime	= REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_PDGAINDOWN].Type			= SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_PDGAINDOWN].HoldTime	= HOLD_TIME_FAST;
	KeyAttribute[KEY_PDGAINDOWN].RepeatTime	= REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_NSGAINDOWN].Type			= SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_NSGAINDOWN].HoldTime	= HOLD_TIME_FAST;
	KeyAttribute[KEY_NSGAINDOWN].RepeatTime	= REPEAT_TIME_FAST;
	// Init Key Values	
	KeyEnable				= ENABLE;
	
	KeyStatus.KeyNum		= KEY_NO_INPUT;
	KeyStatus.KeyType		= NO_PUSH;
	KeyStatus.Usable		= DISABLE;
	
	InputPreKey 			= KEY_NO_INPUT;
	
	ChatterCurKey			= KEY_NO_INPUT;
	
	IsRepeat				= CLR;
	ContinueKeyCheckEnable	= OFF;
}
*/
void KEY_SetKeyEnable(u08 OnOff)
{
	if ( OnOff == ENABLE )		KeyEnable = ENABLE;
	else						KeyEnable = DISABLE;
}

unsigned char KEY_CheckKeyStatus(void)
{
	
	if ( KeyStatus.Usable == ENABLE )
	{
		KeyStatus.Usable = DISABLE;
		
		return	ENABLE;
	}
	else
	{
		return	DISABLE;
	}	
	
}

unsigned char KEY_GetKeyNum(void)
{
	return KeyStatus.KeyNum;
}

unsigned char KEY_GetKeyType(void)
{
	return KeyStatus.KeyType;
}


void KEY_CheckADInput(void)
{
	//ADC input
	ChatterCurKey =	Adc_Data(ADC_NUM3);
	
	if(ChatterCurKey <= PD_MHZ_KEYMAX){	
		InputCurKey = KEY_PDMHZ; 
	}
	else if(ChatterCurKey <= PD_KHZ_KEYMAX){ 	
		InputCurKey = KEY_PDKHZ;
	}
	else if(ChatterCurKey <= PD_ATT_KEYMAX){
		InputCurKey = KEY_PDATT;	
	}
	else if(ChatterCurKey <= UP_KEYMAX){
		InputCurKey = KEY_UP;		
	}
	else if(ChatterCurKey <= THRE_UP_KEYMAX){
		InputCurKey = KEY_THREUP;		
	}
	else if(ChatterCurKey <= PWD_UP_KEYMAX){		
		InputCurKey = KEY_PWDUP;		
	}
	else if(ChatterCurKey <= PD_GAIN_UP_KEYMAX){		
		InputCurKey = KEY_PDGAINUP;		
	}
	else if(ChatterCurKey <= NS_GAIN_UP_KEYMAX){		
		InputCurKey = KEY_NSGAINUP;
	}
	else if(ChatterCurKey <= NS_MHZ_KEYMAX){		
		InputCurKey = KEY_NSMHZ;	
	}
	else if(ChatterCurKey <= NS_KHZ_KEYMAX){		
		InputCurKey = KEY_NSKHZ;		
	}
	else if(ChatterCurKey <= NS_ATT_KEYMAX){		
		InputCurKey = KEY_NSATT;		
	}
	else if(ChatterCurKey <= DOWN_KEYMAX){		
		InputCurKey = KEY_DOWN;		
	}
	else if(ChatterCurKey <= THRE_DOWN_KEYMAX){		
		InputCurKey = KEY_THREDOWN;		
	}
	else if(ChatterCurKey <= PWD_DOWN_KEYMAX){		
		InputCurKey = KEY_PWDDOWN;		
	}
	else if(ChatterCurKey <= PD_GAIN_DOWN_KEYMAX){		
		InputCurKey = KEY_PDGAINDOWN;		
	}
	else if(ChatterCurKey <= NS_GAIN_DOWN_KEYMAX){		
		InputCurKey = KEY_NSGAINDOWN;		
	}
	else InputCurKey = KEY_NO_INPUT;

	
	//UART_Printfu08(InputCurKey);
	Filtbuf[Bufcnt] = InputCurKey;

	if(Bufcnt > 2){
		
		Bufcnt = 0;

		if( (Filtbuf[0] ==  Filtbuf[1])  && (Filtbuf[0] ==  Filtbuf[2]) ){
			InputCurKey = Filtbuf[0];
			
		}
		else{
			InputCurKey = KEY_NO_INPUT;
			Filtbuf[0] = Filtbuf[1] = Filtbuf[2] = 0xFF;
		}
			
		if(KeyEnable == ENABLE){
			if(KeyStatus.Usable == DISABLE) KEY_CheckValidKey();
		}
		

	}
	else {
		Bufcnt++;
	}

	//Adc_Stop(ADC_NUM3);
	
}
/****************************************************/
void KEY_InitNewKeyAttribute(void)
{
	// Init Key Type
	KeyAttribute[KEY_ATT_UP].Type				 	 = SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
  KeyAttribute[KEY_ATT_UP].HoldTime		 	 = HOLD_TIME_FAST;
	KeyAttribute[KEY_ATT_UP].RepeatTime	 	 = REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_ATT_DOWN].Type				  = SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_ATT_DOWN].HoldTime		  = HOLD_TIME_FAST;
	KeyAttribute[KEY_ATT_DOWN].RepeatTime	  = REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_KHZ_UP].Type				  	= SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_KHZ_UP].HoldTime		  	= HOLD_TIME_FAST;
	KeyAttribute[KEY_KHZ_UP].RepeatTime	  	= REPEAT_TIME_FAST;
	
	KeyAttribute[KEY_KHZ_DOWN].Type					 = SHORT_PUSH_TYPE | REPEAT_PUSH_TYPE;
	KeyAttribute[KEY_KHZ_DOWN].HoldTime			 = HOLD_TIME_FAST;
	KeyAttribute[KEY_KHZ_DOWN].RepeatTime		 = REPEAT_TIME_FAST;

	// Init Key Values	
	KeyEnable				= ENABLE;
	
	KeyStatus.KeyNum		= KEY_NO_INPUT;
	KeyStatus.KeyType		= NO_PUSH;
	KeyStatus.Usable		= DISABLE;
	
	InputPreKey 			= KEY_NO_INPUT;
	
	ChatterCurKey			= KEY_NO_INPUT;
	
	IsRepeat				= CLR;
	ContinueKeyCheckEnable	= OFF;
}
void KEY_CheckNewADInput(void)
{
	//ADC input
	ChatterCurKey =	Adc_Data(ADC_NUM3);
		if(ChatterCurKey <= ATT_UP_KEYMAX){	
		InputCurKey = KEY_ATT_UP; 
	}
	else if(ChatterCurKey <= ATT_DOWN_KEYMAX){ 	
		InputCurKey = KEY_ATT_DOWN;
	}
	else if(ChatterCurKey <= KHZ_UP_KEYMAX){
		InputCurKey = KEY_KHZ_UP;	
	}
	else if(ChatterCurKey <= KHZ_DOWN_KEYMAX){
		InputCurKey = KEY_KHZ_DOWN;		
	}
	else InputCurKey = KEY_NO_INPUT;

	//UART_Printfu08(InputCurKey);
	Filtbuf[Bufcnt] = InputCurKey;

	if(Bufcnt > 2){
		
		Bufcnt = 0;

		if( (Filtbuf[0] ==  Filtbuf[1])  && (Filtbuf[0] ==  Filtbuf[2]) ){
			InputCurKey = Filtbuf[0];
			
		}
		else{
			InputCurKey = KEY_NO_INPUT;
			Filtbuf[0] = Filtbuf[1] = Filtbuf[2] = 0xFF;
		}
			
		if(KeyEnable == ENABLE){
			if(KeyStatus.Usable == DISABLE) KEY_CheckValidKey();
		}
		

	}
	else {
		Bufcnt++;
	}

	//Adc_Stop(ADC_NUM3);
	
}

void KEY_CheckValidKey(void)
{
	if(InputCurKey == KEY_NO_INPUT)			// key leave
	{
		if(InputPreKey != KEY_NO_INPUT)
		{
			if(KeyStatus.KeyType == FIRST_PUSH)
			{
				KeyStatus.KeyNum	= InputPreKey;
				KeyStatus.KeyType	= SHORT_PUSH;
				KeyStatus.Usable	= ENABLE;
			}
			else
			{
				KeyStatus.Usable	= DISABLE;
				
			}
		}		
		
		InputPreKey = KEY_NO_INPUT;
		PushTime	= CLR;
		IsRepeat	= CLR;
	}
	else if(InputCurKey != InputPreKey)		// key first push
	{

		if(InputPreKey == KEY_NO_INPUT)
		{
			KeyStatus.KeyType	= FIRST_PUSH;
			InputPreKey = InputCurKey;
			PushTime	= CLR;
			
			ContinueKeyCheckEnable = ON;
		}
	}
	else									// key pushing
	{
		if(ContinueKeyCheckEnable)// return;
		{
			if(KeyAttribute[InputCurKey].Type&SLIDE_FIXED_TYPE)
			{
				KeyStatus.KeyNum	= InputPreKey;
				KeyStatus.KeyType	= ALWAYS_PUSH;
				KeyStatus.Usable	= ENABLE;
			}
			else
				++PushTime;
			
			if(KeyAttribute[InputCurKey].Type&LONG_PUSH_TYPE)
			{
				if(PushTime == KeyAttribute[InputCurKey].HoldTime)
				{
					KeyStatus.KeyNum	= InputPreKey;
					KeyStatus.KeyType	= LONG_PUSH;
					KeyStatus.Usable	= ENABLE;
					
					ContinueKeyCheckEnable	= OFF;
				}
			}
			else if(KeyAttribute[InputCurKey].Type&REPEAT_PUSH_TYPE)
			{
				if(!IsRepeat)
				{
					if(PushTime == KeyAttribute[InputCurKey].HoldTime)
					{
						KeyStatus.KeyNum	= InputPreKey;
						KeyStatus.KeyType	= PUSH_AND_HOLD;
						KeyStatus.Usable	= ENABLE;
						IsRepeat			= ON;
						PushTime			= CLR;
						

					}
				}
				else
				{
					if(PushTime == KeyAttribute[InputCurKey].RepeatTime)
					{
						KeyStatus.KeyNum	= InputPreKey;
						KeyStatus.KeyType	= PUSH_AND_HOLD;
						KeyStatus.Usable	= ENABLE;
						PushTime			= CLR;
					}
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -