Question: we are going to build a capacitive sensor using the timer module on MSP430. Reading Assignment: Please read 8.2.6 Pin Oscillator from the MSP430 user-guide.

we are going to build a capacitive sensor using the timer module on MSP430.

Reading Assignment:

Please read 8.2.6 Pin Oscillator from the MSP430 user-guide.

Lab Requirements:

1- Use the starter code and setup the two LEDs

2-. Connect a wire to P1.4 as a capacitive input.

3- Add your code to blink two LEDs whenever you touch the wire.

Code starter : 'at the end complete the code when you see " Write your code here "

======================

/**** LAB7_Starter Code*/************* /****************************************************************************** // MSP430G2xx3 Demo - Capacitive Touch, Pin Oscillator Method, 1 button // // Description: Basic 1-button input using the built-in pin oscillation feature // on GPIO input structure. PinOsc signal feed into TA0CLK. WDT interval is used // to gate the measurements. Difference in measurements indicate button touch. // LEDs flash if input is touched. // // ACLK = VLO = 12kHz, MCLK = SMCLK = 1MHz DCO // // MSP430G2xx3 // ----------------- // /|\| XIN|- // | | | // --|RST XOUT|- // | | // | P1.4|<--Capacitive Touch Input 1 // | | // LED 2 <--|P1.6 | // | | // LED 1 <--|P1.0 | // | | // | | // //****************************************************************************** /* Define User Configuration values */ /*----------------------------------*/ /* Defines WDT SMCLK interval for sensor measurements*/ #include  #define WDT_meas_setting (DIV_SMCLK_512) /* Defines WDT ACLK interval for delay between measurement cycles*/ #define WDT_delay_setting (DIV_ACLK_512) /* Sensor settings*/ #define KEY_LVL 150 // Defines threshold for a key press /*Set to ~ half the max delta expected*/ /* Definitions for use with the WDT settings*/ #define DIV_ACLK_512 (WDT_ADLY_16) // ACLK/512 #define DIV_SMCLK_512 (WDT_MDLY_0_5) // SMCLK/512 // Global variables for sensing unsigned int base_cnt, meas_cnt; int delta_cnt; char key_pressed; int cycles; /* System Routines*/ void measure_count(void); // Measures each capacitive sensor /*declarte your required function*/ // LED gradient routine /* Main Function*/ int main(void) { unsigned int i,j; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer DCOCTL = 0; // Select lowest DCOx and MODx settings BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz DCOCTL = CALDCO_1MHZ; BCSCTL3 |= LFXT1S_2; // LFXT1 = VLO IE1 |= WDTIE; // enable WDT interrupt P2SEL = 0x00; // No XTAL P1OUT = 0x00; P1DIR = 0x41; // P1.0 & P1.6 = LEDs P1OUT = 0x00; __bis_SR_register(GIE); // Enable interrupts measure_count(); // Establish baseline capacitance base_cnt = meas_cnt; for(i=15; i>0; i--) // Repeat and avg base measurement { measure_count(); base_cnt = (meas_cnt+base_cnt)/2; } while (1) { j = KEY_LVL; key_pressed = 0; // Assume no keys are pressed measure_count(); // Measure all sensors delta_cnt = base_cnt - meas_cnt; // Calculate delta: c_change /* Handle baseline measurement for a base C decrease*/ if (delta_cnt < 0) // If negative: result increased { // beyond baseline, i.e. cap dec base_cnt = (base_cnt+meas_cnt) >> 1; // Re-average quickly delta_cnt = 0; // Zero out for pos determination } if (delta_cnt > j) // Determine if each key is pressed { // per a preset threshold j = delta_cnt; key_pressed = 1; // key pressed } else key_pressed = 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; } } WDTCTL = WDT_delay_setting; // WDT, ACLK, interval timer /* Handle baseline measurement for a base C increase*/ if (!key_pressed) // Only adjust baseline down { // if no keys are touched base_cnt = base_cnt - 1; // Adjust baseline down, should be } // slow to accomodate for genuine __bis_SR_register(LPM3_bits); // Call your required function } } // End Main /* Watchdog Timer interrupt service routine*/ #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector=WDT_VECTOR __interrupt void watchdog_timer(void) #elif defined(__GNUC__) void __attribute__ ((interrupt(WDT_VECTOR))) watchdog_timer (void) #else #error Compiler not supported! #endif { TA0CCTL1 ^= CCIS0; // Create SW capture of CCR1 __bic_SR_register_on_exit(LPM3_bits); // Exit LPM3 on reti } /* Measure count result (capacitance) of each sensor*/ /* Routine setup for four sensors, not dependent on NUM_SEN value!*/ void measure_count(void) { TA0CTL = TASSEL_3+MC_2; // TACLK, cont mode TA0CCTL1 = CM_3+CCIS_2+CAP; // Pos&Neg,GND,Cap /*Configure Ports for relaxation oscillator*/ /*The P2SEL2 register allows Timer_A to receive it's clock from a GPIO*/ /*See the Application Information section of the device datasheet for info*/ P1SEL2 |= BIT4; /*Setup Gate Timer*/ WDTCTL = WDT_meas_setting; // WDT, ACLK, interval timer TA0CTL |= TACLR; // Clear Timer_A TAR __bis_SR_register(LPM0_bits+GIE); // Wait for WDT interrupt TA0CCTL1 ^= CCIS0; // Create SW capture of CCR1 meas_cnt = TACCR1; // Save result WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer } void pulse_LED(void) {  /*Write your code here*/ }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!