I'm having a bit of trouble getting this little circuit working, and I think it's because of switch bouncing (couple that with that the switches them selves are of very poor quality)
I choose only to focus on "Take/Accept Command" (SW1, left half) for bridge, and "Take/Accept Command" (SW2, right half) for engine room. The mission is to send the engine command between bridge and engine control room.
When ECR pushes on the switch, and bridge is in control, the engine room LED is supposed to flash indicating request to take the engine, and the bridge then has to receipt. Like pong.
The problem: The lamps never turn off, and the flashing just shifts from bridge to ECR. If I deactivate (is there a way to debug without having to // the delays?) the delays and run the program step by step, things seem to work fine.
I have a suspicion, that even though I tried to save the bounce with an interrupt, things still bounce. I tried to deactivate the interrupt while inside the ISR, and then activate it before exit ISR. That seems to get the program more stable, but still not fixed.
Some hits would be lovely.
I choose only to focus on "Take/Accept Command" (SW1, left half) for bridge, and "Take/Accept Command" (SW2, right half) for engine room. The mission is to send the engine command between bridge and engine control room.
When ECR pushes on the switch, and bridge is in control, the engine room LED is supposed to flash indicating request to take the engine, and the bridge then has to receipt. Like pong.
The problem: The lamps never turn off, and the flashing just shifts from bridge to ECR. If I deactivate (is there a way to debug without having to // the delays?) the delays and run the program step by step, things seem to work fine.
I have a suspicion, that even though I tried to save the bounce with an interrupt, things still bounce. I tried to deactivate the interrupt while inside the ISR, and then activate it before exit ISR. That seems to get the program more stable, but still not fixed.
Some hits would be lovely.
Code:
#include <msp430.h>
#define ECR_CMD_LED BIT1 //Port 1.1
#define ECR_M BIT4 //Port 1.4
#define ENGINE_ON BIT5 //Port 1.5
#define ECR_TAKE BIT0 //Port 1.0
#define BRIDGE_TAKE BIT0 //Port 2.0
#define BRIDGE_M BIT1 //Port 2.1
#define BRIDGE_CMD_LED BIT2 //Port 2.2
#define SPEAKER_ON BIT3 //Port 2.3
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
/*
P1DIR = 0x00;
P2DIR = 0x00;
P1REN = 0x00;
P1OUT = 0x00;
P2OUT = 0x00;
*/
P1OUT = ECR_M | ECR_TAKE;
P1DIR = ECR_CMD_LED | ENGINE_ON ; //Port 1 outputs (rest are inputs)
P1REN = ECR_M | ECR_TAKE; //Enable internal pull-up resistor (this only enables it, it does not dictate direction)
P2OUT = BRIDGE_M | BRIDGE_TAKE;
P2DIR = BRIDGE_CMD_LED | SPEAKER_ON; //Port 1 outputs (rest are outputs)
P2REN = BRIDGE_M | BRIDGE_TAKE; //Enable internal pull-up resistor (this only enables it, it does not dictate direction)
//| ECR_M_DOWN;
P1IES |= ECR_TAKE; // | ECR_M_DOWN;
P1IFG |= ECR_TAKE; // & ~ECR_M_DOWN;
// | BRIDGE_M_DOWN; //Enable interrupt
P2IES |= BRIDGE_TAKE; // | BRIDGE_M_DOWN; //Set interrupt HIGH TO LOW
P2IFG |= BRIDGE_TAKE; // & ~BRIDGE_M_DOWN; //Clear interrupt flag due to being set by PxOUT line
P1OUT |= ECR_CMD_LED; //Start command station. Make the command LED's random someday
_BIS_SR(GIE); //Enable ISR (interrupt service routine)
while(1)
{
if((P2IN & BRIDGE_TAKE) == 0)
{P2IE |= BRIDGE_TAKE;} //Go to interrupt
if((P1IN & ECR_TAKE) == 0)
{P1IE |= ECR_TAKE;} //Go to Port_1 interrupt
}
return 0;
}
#pragma vector = PORT1_VECTOR;
__interrupt void Port_1(void) //Engine - for engine to gain control, bridge must accept!
{
P2IE &= ~BRIDGE_TAKE;
P1IE &= ~ECR_TAKE; //Disable interrupt while bounce
__delay_cycles(22000);
while((P2IN & BRIDGE_TAKE) == 1) //Loop While NOT pressed is it supposed to be
{P1OUT ^= ECR_CMD_LED; //Toggle engine command while waiting for bridge to accept
__delay_cycles(220000);} //Blink, blink
P1OUT |= ECR_CMD_LED;
P2OUT &= ~BRIDGE_CMD_LED;
P1IFG &= ~ECR_TAKE;
P1IE |= ECR_TAKE;
P2IE |= BRIDGE_TAKE; //Enable interrupt before exit
}
#pragma vector = PORT2_VECTOR;
__interrupt void Port_2(void) //Bridge - for bridge to gain control, engine must accept!
{
P1IE &= ~ECR_TAKE;
P2IE &= ~BRIDGE_TAKE;
__delay_cycles(2300); //End bounce
while((P1IN & ECR_TAKE) == 1) //Loop while not pressed
{P2OUT ^= BRIDGE_CMD_LED; //Wait for ECR to accept
__delay_cycles(220000);}
P2OUT |= BRIDGE_CMD_LED;
P1OUT &= ~ECR_CMD_LED;
P2IFG &= ~BRIDGE_TAKE;
P1IE |= ECR_TAKE;
P2IE |= BRIDGE_TAKE;
}
Attachments
-
51.8 KB Views: 8