//****************************************************************************** // MSP430G2xx2 Demo - 4-button Capacitive Touch, Pin Oscillator Method, ACLK CCR // // Description: Basic 4-button input using the built-in pin oscillation feature // on GPIO input structure PinOsc signal feed into TA0CLK ACLK [32kHz] is fed // into the CCR1 register, triggering the CCR flag in timer capture mode // Difference in measurements indicate button touch LEDs flash according to the // input touched: // // Input 1: LED1 (LED2 off) // Input 2: LED2 (LED1 off) // Input 3: Both LEDs on // Input 4: Both LEDs flash on/off // // ACLK = LFXT1 = 32768Hz, MCLK = SMCLK = 1MHz DCO // // MSP430G2xx2 // // /|\| XIN|// | | | 32kHz xtal // |RST XOUT|// | | // | P1.1|< Capacitive Touch Input // | | // LED < |P1.6 P1.2|< Capacitive Touch Input // | | // LED < |P1.0 P1.4|< Capacitive Touch Input // | | // | P1.5|< Capacitive Touch Input // // Brandon Elliott/D Dang // Texas Instruments Inc // November 2010 // Built with IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2452.h" /* Sensor settings*/ #define NUM_SEN #define KEY_LVL 1000 press /*Set to ~ half the max delta expected*/ #define LED_1 #define LED_2 (0x01) (0x40) // Defines number of sensors // Defines threshold for a key // P1.0 LED output // P1.6 LED output // Global variables for sensing unsigned int base_cnt[NUM_SEN]; unsigned int meas_cnt[NUM_SEN]; int delta_cnt[NUM_SEN]; unsigned char key_press[NUM_SEN]; char key_pressed; int cycles; const unsigned char electrode_bit[NUM_SEN]={BIT1, BIT2, BIT4, BIT5}; /* System Routines*/ void measure_count(void); // Measures each capacitive sensor void pulse_LED(void); // LED gradient routine /* Main Function*/ void main(void) { unsigned int i,j; WDTCTL = WDTPW + WDTHOLD; BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; BCSCTL2 |= DIVS_2; BCSCTL1 |= DIVA_1; BCSCTL3 |= XCAP_1; IE1 |= WDTIE; P1DIR = LED_1 + LED_2; P1OUT = 0x00; { IFG1 &= ~OFIFG; for (i = 0xFF; i > 0; i ); } while (IFG1 & OFIFG); bis_SR_register(GIE); measure_count(); for (i = 0; i0; i ) // Repeat and avg base measurement { measure_count(); for (j = 0; j 1; // Re-average quickly delta_cnt[i] = 0; // Zero out for pos determination } if (delta_cnt[i] > j) // Determine if each key is pressed { // per a preset threshold key_press[i] = 1; // Specific key pressed j = delta_cnt[i]; key_pressed = i+1; // key pressed } else key_press[i] = 0; /* Delay to next sample, sample more slowly if no keys are pressed*/ if (key_pressed) { BCSCTL1 = (BCSCTL1 & 0x0CF) + DIVA_0; // ACLK/(0:1,1:2,2:4,3:8) cycles = 20; } else { cycles ; if (cycles > 0) BCSCTL1 = (BCSCTL1 & 0x0CF) + DIVA_0; // ACLK/(0:1,1:2,2:4,3:8) else { BCSCTL1 = (BCSCTL1 & 0x0CF) + DIVA_3; // ACLK/(0:1,1:2,2:4,3:8) cycles = 0; } } /* Handle baseline measurment for a base C increase*/ if (!key_pressed) // Only adjust baseline down { // if no keys are touched for (i = 0; i