Register
Log in
Create new paste
Archive
Trending Pastes
main.c
BY:
a guest
|
SYNTAX:
C#
|
POSTED:
2012-03-21 11:26:20
|
EXPIRES:
Never
|
EXPOSURE:
Public
HITS:
38
Short URL:
|
DOWNLOAD
|
REPORT ABUSE
|
/* * main.C * * Created on: Feb 8, 2012 * Author: meltwater * * Provided "as is" free for use and modification, provided credit is given. */ #include <msp430g2553.h> #define SWITCH BIT3 /* Port 1.3 */ #define SWITCH_IN P1IN /* Port 1 Input */ #define SWITCH_OUT P1OUT /* Port 1 Output */ #define SWITCH_DIR P1DIR /* Port 1 Direction */ #define SWITCH_IFG P1IFG /* Port 1 Interrupt Flag */ #define SWITCH_IES P1IES /* Port 1 Interrupt Edge Select */ #define SWITCH_IE P1IE /* Port 1 Interrupt Enable */ #define SWITCH_REN P1REN /* Port 1 Resistor Enable */ #define LED0 BIT0 /* Port 1.0 */ #define LED0_OUT P1OUT /* Port 1 Output */ #define LED0_DIR P1DIR /* Port 1 Direction */ //Set either EN_POLLING or EN_INTERRUPT //To choose polling input check or interrupt driven check //#define EN_POLLING #define EN_INTERRUPT #ifdef EN_POLLING #undef EN_INTERRUPT #else #define EN_INTERRUPT #endif void toggleLED() { //Toggle LED on/off state LED0_OUT ^= LED0; // LED0 = toggle } //######################################## #ifdef EN_POLLING void HwInitPolling(void) { //LED Init LED0_DIR |= LED0; //Set LED to output (DIR=output) //Switch Init SWITCH_DIR &= ~SWITCH; //Clear switch pin (DIR=input) SWITCH_REN |= SWITCH; //Set pull resistor enable } int inputTest(void) { int value = 0; //Test the input port switch pin if((SWITCH & SWITCH_IN)) //active low switch { value = 1; } else { value = 0; } return value; } void switchTest( void ) { int switch_state = 0; int switch_state_old = 0; //setup the GPIO port HwInitPolling(); while (1) { switch_state_old = switch_state; //read switch state switch_state = inputTest(); //check for input change if (switch_state_old != switch_state) { toggleLED(); } } } #endif //######################################## //######################################## #ifdef EN_INTERRUPT void HwInitInterrupt(void) { //LED Init LED0_DIR |= LED0; //Set LED to output (DIR=output) //Switch Init SWITCH_DIR &= ~SWITCH; //Clear switch pin (DIR=input) SWITCH_REN |= SWITCH; //Set resistor enable SWITCH_IE |= SWITCH; //Enable interrupt for switch pin SWITCH_IES |= SWITCH; //Enable edge select as Hi/lo edge SWITCH_IFG &= ~SWITCH; //Clear the interrupt flag ready } void switchTest( void ) { //setup the GPIO port HwInitInterrupt(); _BIS_SR(LPM4_bits + GIE); // Enter LPM4 w/interrupt } // Port 1 interrupt service routine #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { // LED0 = toggle toggleLED(); SWITCH_IES ^= SWITCH; //Toggle edge select between Hi/Lo to Lo/Hi etc SWITCH_IFG &= ~SWITCH; //Clear the interrupt flag } #endif //######################################## void main( void ) { // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; switchTest(); }
create a
new version
of this paste