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

📄 mtouch16.c

📁 用pic16f883做的16个键的触摸按键(MTOUCH)实现电容式触摸按键的代码。有mtouch16.c构成
💻 C
字号:
/****************************************************************************
*	Title	:	mTouch Slider Module										*
*	UpDate	:	2008.12.25													*																		*
*	Device	:	PIC16F883I/P												*																									*
*	Compiler:	Hi-tech	V9.60 std											*
*	IDE		:	MPLAB IDE V8.00	                                            *	
*   AUTHER	:	WOOLLEY										            	*
****************************************************************************/
#include "pic.h"
#include "mtouch16.h"
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BORDIS & LVPDIS & IESODIS & FCMDIS);
unsigned char	AvgIndex;
BButtons Buttons;
bank2 unsigned int	RAW[NUM_BUTS];
bank2 unsigned int	AVERAGE[NUM_BUTS];
bank3 unsigned int	TRIP[NUM_BUTS];
bank3 unsigned int	GUARD[NUM_BUTS];
unsigned char	FIRST;
unsigned char	INDEX;//选择的按钮
unsigned int	VALUE;//当前按钮的值	
unsigned int	BIGVAL;	
unsigned int	SMALLVAL;
const unsigned char	COMP1[2]= {0x94, 0x95};
const unsigned char	COMP2[2]= {0xA0, 0xA1};



void Abit_init(void);
void Abit_capinit(void);
void Abit_restart_timer(void);
void interrupt isr(void);
void Abit_next_channel(void);
void main(void)
{
	Abit_init();
	LED1=OFF;
	LED2=OFF;
	LED3=OFF;
	LED4=OFF;
	SPK=OFF;
	while(1)
	{
		LED2=(Buttons.BTN0==1) ? ON :OFF;
		SPK=(Buttons.BTN0==1) ? ON :OFF;
		LED2=(Buttons.BTN1==1) ? ON :OFF;
SPK=(Buttons.BTN1==1) ? ON :OFF;
	//	LED3=(Buttons.BTN2==1) ? ON :OFF;
//SPK=(Buttons.BTN2==1) ? ON :OFF;
	//	LED4=(Buttons.BTN3==1) ? ON :OFF;
//SPK=(Buttons.BTN3==1) ? ON :OFF;
		//////////
		//LED1=(Buttons.BTN4==1) ? ON :OFF;
//SPK=(Buttons.BTN4==1) ? ON :OFF;
	//	LED2=(Buttons.BTN5==1) ? ON :OFF;
//SPK=(Buttons.BTN5==1) ? ON :OFF;
	//	LED3=(Buttons.BTN6==1) ? ON :OFF;
//SPK=(Buttons.BTN6==1) ? ON :OFF;
	//	LED4=(Buttons.BTN7==1) ? ON :OFF;
//SPK=(Buttons.BTN7==1) ? ON :OFF;
		////////
	//	LED1=(Buttons.BTN8==1) ? ON :OFF;
//SPK=(Buttons.BTN8==1) ? ON :OFF;
	//	LED2=(Buttons.BTN9==1) ? ON :OFF;
//SPK=(Buttons.BTN9==1) ? ON :OFF;
	//	LED3=(Buttons.BTN10==1) ? ON :OFF;
//SPK=(Buttons.BTN10==1) ? ON :OFF;
	//	LED4=(Buttons.BTN11==1) ? ON :OFF;
//SPK=(Buttons.BTN11==1) ? ON :OFF;
		////////
	//	LED1=(Buttons.BTN12==1) ? ON :OFF;
//SPK=(Buttons.BTN12==1) ? ON :OFF;
	//	LED2=(Buttons.BTN13==1) ? ON :OFF;
//SPK=(Buttons.BTN13==1) ? ON :OFF;
	//	LED3=(Buttons.BTN14==1) ? ON :OFF;
//SPK=(Buttons.BTN14==1) ? ON :OFF;
	//	LED4=(Buttons.BTN15==1) ? ON :OFF;
//SPK=(Buttons.BTN15==1) ? ON :OFF;
	}
}
void Abit_init()
{

	TRISC2OUT=0;
	TRISC12IN0=1;
	TRISC12IN1=1;
	TRISC12IN2=1;
	TRISC12IN3=1;
	TRISCOM=1;
	TRISSPK=0;
	TRISLED1=0;
	TRISLED2=0;
	TRISLED3=0;
	TRISLED4=0;
	TRISS0=0;
	TRISS1=0;
	TRISS2=0;
	TRISS3=0;
//	TRISE3=0;
	Abit_capinit();
	GIE=1;

}
void Abit_restart_timer(void) 
{

	TMR1L	= 0;							
	TMR1H	= 0;								
	TMR1ON	= 1;								
	TMR0	= 0;							
	T0IF	= 0;
}
void Abit_capinit(void)
{
	for(INDEX=0;INDEX< NUM_BUTS;INDEX++)
	{
		RAW[INDEX]=0;
		AVERAGE[INDEX]=0;
		TRIP[INDEX]=145;
	}
	FIRST=160;
	OSCCON=0X7F;
	INDEX=0;
	CM1CON0=COMP1[0];
	CM2CON0=COMP2[0];
	VRCON=0x87;	
	CM2CON1=0X32;
	ANSEL=0X07;
	ANSELH=0X00;
	SRCON   = 0xF0;
	T1CON	= 0x06;				
	OPTION	= 0x84;
	Abit_restart_timer();
	INTCON=0;
	T0IE=1;
}
void Abit_next_channel(void)
{
	
	if(INDEX<NUM_BUTS-1)
	{
		INDEX++;
		PORTB &= 0XF0;
		PORTB |=((INDEX%16) & 0X0F) ;

	}
	else
	{
		INDEX=0;
		PORTB &= 0XF0;
	}	

	
	//else
	//{
		//INDEX=0;
		//PORTB &=0XF0;
	//}
//	INDEX = (++INDEX) & 0x03;
	if (INDEX == 0)
	{
		CM1CON0 = COMP1[0];							
		CM2CON0 = COMP2[0];
	} 
	else 
	{
		CM1CON0 = COMP1[1];							
		CM2CON0 = COMP2[1];
	}
	



	
}
void interrupt isr(void)
{
	if (T0IF==1 && T0IE==1)
	{
		TMR1ON=0;
		VALUE = TMR1L + (unsigned int)(TMR1H << 8);
		RAW[INDEX]=VALUE;
		BIGVAL=VALUE*16;//当前值左移4位
		SMALLVAL=AVERAGE[INDEX]/16;//当前平均值右移4位
		RAW[INDEX]=BIGVAL;//保存当前最大值
		/////
		if(FIRST>0)
		{
			FIRST--;
			AVERAGE[INDEX] = BIGVAL;
			//Abit_next_channel();
			Abit_restart_timer();
			
			return;
			
		}

       //////////////////
		if (BIGVAL < (AVERAGE[INDEX] - TRIP[INDEX]))
		{
			switch(INDEX)
			{
				case B0:   Buttons.BTN0 =1; break;
						   
				case B1:   Buttons.BTN1 =1; break;
				case B2:   Buttons.BTN2 =1; break;
				case B3:   Buttons.BTN3 =1; break;
				case B4:   Buttons.BTN4 =1; break;
				case B5:   Buttons.BTN5 =1; break;
				case B6:   Buttons.BTN6 =1; break;
				case B7:   Buttons.BTN7 =1; break;
				case B8:   Buttons.BTN8 =1; break;
				case B9:   Buttons.BTN9 =1; break;
				case B10:   Buttons.BTN10 =1; break;
				case B11:   Buttons.BTN11 =1; break;
				case B12:   Buttons.BTN12=1; break;
				case B13:   Buttons.BTN13=1; break;
				case B14:   Buttons.BTN14=1; break;
				case B15:   Buttons.BTN15=1; break;




				default: break;
			}
		}
		if (BIGVAL > (AVERAGE[INDEX] - TRIP[INDEX] + 32)) 
		{
			switch(INDEX)
			{
				case B0:   Buttons.BTN0 =0; break;
						   
				case B1:   Buttons.BTN1 =0; break;
				case B2:   Buttons.BTN2 =0; break;
				case B3:   Buttons.BTN3 =0; break;
				case B4:   Buttons.BTN4 =0; break;
				case B5:   Buttons.BTN5 =0; break;
				case B6:   Buttons.BTN6 =0; break;
				case B7:   Buttons.BTN7 =0; break;
				case B8:   Buttons.BTN8 =0; break;
				case B9:   Buttons.BTN9 =0; break;
				case B10:   Buttons.BTN10 =0; break;
				case B11:   Buttons.BTN11 =0; break;
				case B12:   Buttons.BTN12=0; break;
				case B13:   Buttons.BTN13=0; break;
				case B14:   Buttons.BTN14=0; break;
				case B15:   Buttons.BTN15=0; break;
				default: break;
			}
		}

		if (BIGVAL  > AVERAGE[INDEX])										
		AVERAGE[INDEX] = BIGVAL;	
		if (AvgIndex < AVG_DELAY)		AvgIndex++;	
		else							AvgIndex = 0;
		if (AvgIndex == AVG_DELAY) 
		{
			AVERAGE[INDEX] = AVERAGE[INDEX] + (VALUE - SMALLVAL);
		}
		Abit_next_channel();
	    Abit_restart_timer();

	}
}

⌨️ 快捷键说明

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