Hello, I am trying to have an MSP430G2203 in a launchpad perform 2 different operations. S2 should pulse led 2 when pressed. (this part works) The mode of the pulse is supposed to be determined by a switch on P1.4.
However when I press S1 P1IFG is being set on bits 3,4,5 and in ISR runs through both if statements. This changes the "Mode" every time S2 is pressed.
Here is my Code:
/* * main.c */#include "msp430g2203.h"#define Trigger BIT3 // will change to P2.3 in final code#define ModeSwitch BIT4 // will change to P1.0 in final code#define Solenoid BIT6#define LED1 BIT0unsigned int Dwell=100000;unsigned int RateOfFire=100000;unsigned int Mode=0;void SetIO(void);void ConfigClocks(void);void FaultRoutine(void);void ModeSelect(void);void FullAuto(void);void SemiAuto(void);void ThreeBallBurst(void);void SixBallBurst(void);void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer ConfigClocks(); SetIO(); while(1){ _BIS_SR(LPM3_bits + GIE); // Enter LPM3 w/interrupt }}void SetIO(void){ P1DIR = Solenoid; // P1.6 output (solenoid fire) P1REN = 0; // Enable pull-up resistor P1OUT = 0; // outputs off //Set Input Interrupts P1IE |= (Trigger | ModeSwitch); // P1.3 interrupt enabled P1IES |= (Trigger | ModeSwitch); // P1.3 Hi/lo edge P1IFG &=~(Trigger | ModeSwitch); // P1.3 IFG cleared P2DIR = 0X02; // set eyes output P2OUT = 0; // Turn off eyes}void ConfigClocks(void){ if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF) FaultRoutine(); // If calibration data is erased // run FaultRoutine() BCSCTL3 |= LFXT1S_0; // LFXT1 = Normal Operation IFG1 &= ~OFIFG; // Clear OSCFault flag BCSCTL2 |= SELM_3 + DIVM_0; // MCLK = LXFT1/1}void FaultRoutine(void) { P1OUT = BIT0; // P1.0 on (red LED) while(1); // TRAP }// Port 1 interrupt service routine#pragma vector=PORT1_VECTOR__interrupt void Port_1(void){ if(P1IFG&Trigger){ switch(Mode){ case 0: SemiAuto();break; case 1: FullAuto();break; case 2: ThreeBallBurst();break; case 3: SixBallBurst();break; } //SixBallBurst(); } if(P1IFG&ModeSwitch){ ModeSelect(); } P1IFG &= ~(Trigger | ModeSwitch); // P1.3 and P1.4 IFG cleared}void ModeSelect(void){ Mode +=1; if(Mode>3){ Mode=0; }}void SemiAuto(void){ P1OUT = Solenoid; // Turn On Solenoid //_delay_cycles(100000); // SET THE "ON" TIME FOR SOLENOID P1OUT = 0X00; // Turn off Solenoid}void FullAuto(void){ while ((P1IN & Trigger) == 0x00){ P1OUT = Solenoid; // Turn On Solenoid _delay_cycles(100000); // SET THE "ON" TIME FOR SOLENOID P1OUT = 0X00; // Turn off Solenoid _delay_cycles(100000); }}void ThreeBallBurst(void){ int burst = 3; int count = 0; for (count=0;count <burst;count++){ P1OUT = Solenoid; // green LED on _delay_cycles(100000); // SET THE "ON" TIME FOR SOLENOID P1OUT = 0X00; // green LED off _delay_cycles(100000); }}void SixBallBurst(void){ int burst = 6; int count = 0; for (count=0;count <burst;count++){ P1OUT = Solenoid; // green LED on _delay_cycles(100000); // SET THE "ON" TIME FOR SOLENOID P1OUT = 0X00; // green LED off _delay_cycles(100000); }}
When a port pin is (a) an input, (b) not connected to anything, and (c) not internally pullup/down, it might go up or down due to leakage or RF pickup. As a result, he corresponding IFG will be set too.
Thanks, I had set the P1REN=1 before but then I never got an interrupt when I pressed the button, so I thought I miss read the setting to be 0=enabled. I missed the part about P1OUT corresponding bits needing to be set to 1 also.
I appreciate the Help as this was my first time posting a question.