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

📄 mtouch727.c

📁 电容式触摸按键的有效方法
💻 C
📖 第 1 页 / 共 2 页
字号:
//******************************************************************************
// Software License Agreement
//
// The software supplied herewith by Microchip Technology
// Incorporated (the "Company") is intended and supplied to you, the
// Company抯 customer, for use solely and exclusively on Microchip
// products. The software is owned by the Company and/or its supplier,
// and is protected under applicable copyright laws. All rights are
// reserved. Any use in violation of the foregoing restrictions may
// subject the user to criminal sanctions under applicable laws, as
// well as to civil liability for the breach of the terms and
// conditions of this license.
//
// THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
// WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
// TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
// IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
//******************************************************************************

// *****************************************************************************
// Filename:  Main727.c
// Date: Dec. 13, 2007
// Purpose: Illustrate the use of the Capacitive Sensing Module
//	This program scans 16 pads on a PCB. Each pad is sensed by the Capacitive 
//  Sensing Module at a fixed interval.  The frequency of each pad at rest is averaged.l
//  When a pad is touched, the frequency on the Cap. sensing module changes due to the 
//  extra capacitance from the finger.  The change in frequency is noted and the LEDs
//  light up to indicate which button was pressed.
// *****************************************************************************
//
//  Compiler:   Hi-Tech PIC-C v.9.60
// *****************************************************************************

#include	<pic.h>
#include 	"Cap_Sense.h"




#define METHOD			4		// 1= Timer0 Base; 2= Timer2 Base; 3= WDT base; 4=WDT Sleep Base

#if METHOD == 4
__CONFIG(INTIO & WDTEN & PWRTEN & MCLRDIS & UNPROTECT);
#else
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT);
#endif

/** Variables **/

bank1 unsigned int 	reading[NUM_BTTNS];		// current reading for each button
bank1 unsigned int 	average[NUM_BTTNS];		// running average for each button
bank1 unsigned int  threshold;				// threshold value is req'd # counts decrease from avg
unsigned char		index;					// index value relates ea. button and scanning sequence

bank2 unsigned int threshold128;			// Threshold of 1/128th = 1/128th of the average value
bank2 unsigned int threshold64;				// ... used in computation of threshold variable
bank2 unsigned int threshold32;				// ... threshold is a selection of the sum of these
bank2 unsigned int threshold16;				//         "
bank2 unsigned int threshold8;				//         "
bank2 unsigned int threshold4;				//         "
bank2 unsigned int threshold2;				// Threshold value for 1/2

FFlags Flags;								// Flags struct variable contains application status
BButtons Buttons;							// Buttons struct variable indicates button presses

unsigned int	bigval;						// current button bigval   - for averaging technique
unsigned int	smallavg;					// current button smallavg - for averaging technique



/** Constants **/



/** Prototypes **/
void Init(void);
void RestartTimers(void);
void RestartTimer1(void);
void SetNextChannel(void);
void CapInit(void);
void DisplayLEDs(char i);
void interrupt isr(void);
void SLEEP_NOP(void);

/*====================================================================
========================  PROGRAM CODE  ==============================
====================================================================*/

//*********************************************************************
// 	main() ************************************************************
//	Main loop.  Will turn on the LEDs based on button detected during**
//       ISR execution. ***********************************************
// ********************************************************************
void main(void) {

	Init();
	CLRWDT();
	index = 0;										// Begin program scanning channel 0

	while (1) {										// Loop forever

		// React to button presses in application main-loop
		if (Buttons.BTN0)
			DisplayLEDs(0);							// Structuring code based off each button takes up
		if (Buttons.BTN1)							// more application code, but reduces isr code.
			DisplayLEDs(1);							// It also allows reaction to different buttons at 
		if (Buttons.BTN2)							// the same time.
			DisplayLEDs(2);
		if (Buttons.BTN3)							// ALTERNATIVE:
			DisplayLEDs(3);							// For ea. case in the isr, create and set a variable
		if (Buttons.BTN4)							// called 'button_pressed' s.t. button_pressed=index.
			DisplayLEDs(4);							// Then, main loop code would simply require:
		if (Buttons.BTN5)							// DisplayLEDs(button_pressed); 
			DisplayLEDs(5);
		if (Buttons.BTN6)
			DisplayLEDs(6);
		if (Buttons.BTN7)
			DisplayLEDs(7);
		if (Buttons.BTN8)
			DisplayLEDs(8);
		if (Buttons.BTN9)
			DisplayLEDs(9);
		if (Buttons.BTN10)
			DisplayLEDs(10);
		if (Buttons.BTN11)
			DisplayLEDs(11);
		if (Buttons.BTN12)
			DisplayLEDs(12);
		if (Buttons.BTN13)
			DisplayLEDs(13);
		if (Buttons.BTN14)
			DisplayLEDs(14);
		if (Buttons.BTN15)
			DisplayLEDs(15);

#if METHOD == 4	
		SLEEP_NOP();
#endif
	}
}



// ************************************************************************************
// Function: Init() *******************************************************************
// Purpose: Initialize PORTS, Capacitive Sensing Module and Selected Time Base
// ************************************************************************************
void Init(void) 
 {

	// *********************************************************************************
	// PORT and LED Setup **************************************************************
	//**********************************************************************************
	// Pin usage for this sample application
	//--------------------------------------------------------------------------
	//			7		6		5		4		3		2		1		0      |
	// PORTA	.       .       CPS7    CPS6    .       .       LED     LED    |    
	// PORTB    .       .       CPS5    CPS4    CPS3    CPS2    CPS1    CPS0   |
	// PORTC	.       .       .       .       .       .       .       .      | 
	// PORTD	CPS15   CPS14   CPS13   CPS12   CPS11   CPS10   CPS9    CPS8   |
	// PORTE	X		X		X		X		.		LED		.		LED    |
	//--------------------------------------------------------------------------

	TRISA0 = ANSA0 = 0;				// Set LED outs as digital outputs
	TRISE2 = ANSE2 = 0;				// All others remain as inputs (default)
	TRISE0 = ANSE0 = 0;
	TRISA1 = ANSA1 = 0;	
	LED0 = OFF;							// Init to off state
	LED1 = OFF;							// ..
	LED2 = OFF;							// ..
	LED3 = OFF;							// ..

	TRISA  |= 0b00110000;				// Set as analog inputs
	ANSELA |= 0b00110000;				// (should be default)
	TRISB  |= 0b00111111;				//   "
	ANSELB |= 0b00111111;				//   "	
	TRISD  |= 0b11111111;				//   "
	ANSELD |= 0b11111111;				//   "

	OSCTUNE = 0b00100000;

	// *********************************************************************************
	// Initialize Cap Module
	// *********************************************************************************

	CapInit();


	GIE = 1;
 }


//// *************************************************************************************
//// Function: SLEEP_NOP()
//// Purpose: Sleep function with NOP
//// *************************************************************************************
void SLEEP_NOP(void)
{
		SLEEP();
	#asm
		NOP
	#endasm
		SLEEP();
	#asm
		NOP
	#endasm

}


// *************************************************************************************
// Function: CapInit()
// Purpose: Initialize the Capacitive Sense Module and Time Base Modules
// *************************************************************************************

void CapInit(void)
 {

	// Set up variables
	for (index=0; index<16; index++) {
		average[index] = 0;
		reading[index] = 0;
        }
	// ********************************************************************************
	// Initialize for Timer0 time base ************************************************
	// ********************************************************************************
#if METHOD==1							// Timer0 set as time base
	OPTION = 0b11000100;				// Timer0 init
	TMR0IF = 0;							// enable tmr0 intpt
	TMR0IE = 1;	
	T1CON  = 0b01000101;				// Timer1 enable, system clock, 1:1 prescale, 
	T1GCON = 0b11100001;				// Timer1 gate init
#endif	

⌨️ 快捷键说明

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