Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <msp430.h>
- volatile unsigned int s2State;
- volatile unsigned int s1State;
- volatile unsigned int s2Pressed;
- volatile unsigned int s1Pressed;
- volatile unsigned int areLedsEnabled = 0;
- volatile unsigned int isBlinkingEnabled = 0;
- volatile unsigned int delay = 0;
- volatile unsigned int ledsState = 0;
- void setUpButtons() {
- P1DIR &= !(BIT7);
- P2DIR &= !(BIT2);
- P1OUT |= BIT7;
- P1REN |= BIT7;
- P2OUT |= BIT2;
- P2REN |= BIT2;
- }
- void setUpLeds() {
- P1DIR |=BIT0;
- P8DIR |=BIT1;
- P8DIR |=BIT2;
- }
- void setUp() {
- setUpButtons();
- setUpLeds();
- }
- void enableLeds() {
- P1OUT |= BIT0;
- P8OUT |= BIT1;
- P8OUT |= BIT2;
- }
- void disableLeds() {
- P1OUT &= ~(BIT0);
- P8OUT &= ~(BIT1);
- P8OUT &= ~(BIT2);
- }
- int s1Released() {
- return s1Pressed == 1 && s1State != 0;
- }
- void enableOrDisableLeds() {
- if (s1Released()) {
- if (areLedsEnabled == 0) {
- areLedsEnabled++;
- enableLeds();
- } else if (areLedsEnabled > 0) {
- areLedsEnabled--;
- if(isBlinkingEnabled > 0){
- isBlinkingEnabled--;
- }
- disableLeds();
- }
- s1Pressed = 0;
- } else if (s1State == 0) {
- s1Pressed = 1;
- }
- }
- void enableOrDisableBlinking() {
- if (s2Pressed == 1 && s2State != 0) {
- if (isBlinkingEnabled == 0) {
- isBlinkingEnabled++;
- } else if (isBlinkingEnabled > 0) {
- isBlinkingEnabled--;
- }
- s2Pressed = 0;
- } else if (s2State == 0 && areLedsEnabled > 0) {
- s2Pressed = 1;
- }
- }
- void blinkIfNeccessary() {
- if (isBlinkingEnabled > 0 && areLedsEnabled > 0 && ledsState == 1) {
- disableLeds();
- ledsState = 0;
- } else if (isBlinkingEnabled > 0 && areLedsEnabled > 0) {
- enableLeds();
- ledsState = 1;
- } else if (isBlinkingEnabled == 0 && areLedsEnabled > 0) {
- enableLeds();
- ledsState = 1;
- } else {
- disableLeds();
- ledsState = 0;
- }
- }
- void updateButtonState() {
- s1State = (P1IN & BIT7);
- s2State = (P2IN & BIT2);
- }
- void wait() {
- delay = 10000;
- do delay--;
- while(delay != 0);
- }
- void run() {
- updateButtonState();
- enableOrDisableLeds();
- enableOrDisableBlinking();
- blinkIfNeccessary();
- wait();
- }
- void stopWatchDog() {
- WDTCTL = WDTPW | WDTHOLD;
- }
- int main(void)
- {
- stopWatchDog();
- setUp();
- while(1)
- {
- run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement