The LED and switch are connected to Port 1, pin 0 and pin 3 respectively. Clicking the switch generates an interrupt. Initially, the LED is in OFF state, if the switch is pressed, the LED starts blinking till next interrupt occurrence/switch press.
If pull mode is enabled by setting PxREN register then either the pull up or pull down option can be selected by setting or clearing the corresponding bit in PxOUT register. One can either select high to low or low to high transition as the trigger for interrupt by setting/clearing the corresponding bit in PxIES register. Detailed information regarding the port settings can be found in the datasheet.
The state of the switch is tracked by a variable named "blink".
Do check the demonstration video: MSP430 with a switch and an LED
#include<msp430.h> unsigned int blink = 0; void main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer P1DIR |= BIT0; // Set P1.0 to output direction P1OUT &= ~BIT0; //clear pin P1REN |= BIT3; //enable pull mode P1OUT |= BIT3; //pull up P1IES |= BIT3; //triggers when button pressed, high to low P1IE |= BIT3; //enable interrupt __enable_interrupt(); //global/general interrupts enabled for(;;) { if (blink > 0) { P1OUT ^= BIT0; //toggle LED __delay_cycles(100000); //delay of 10000 cycles at 1MHz } } } //Port 1 Interrupt Service Routine #pragma vector = PORT1_VECTOR __interrupt void Port_1(void) { blink ^= 0x01; P1IFG &= ~BIT3; // P1.3 interrupt flag cleared P1OUT &= ~BIT0; // clear LED to start in off state }
No comments:
Post a Comment