Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program for the MSP430 using Energia that will place an interrupt on the

ID: 3768459 • Letter: W

Question

Write a program for the MSP430 using Energia that will place an interrupt on the push2 button to blink either the red or green LED in one of two particular patterns for each color LED. The first time the button is pushed the red LED should turn on for 1 second and then turn off. The second time the button is pushed the red LED should turn on for 1 second, turn off for 1 second, turn back on for 1 second, and off again. The third time the button is pushed the green LED should turn on for 1 second and then turn off. The fourth time the button is pushed the green LED should turn on for 1 second, turn off for 1 second, turn back on for 1 second, and off again. The fifth time the button is pushed the red LED should turn on for 1 second and then turn off. The sixth time the button is pushed the red LED should turn on for 1 second, turn off for 1 second, turn back on for 1 second, and off again…. (Etc. etc. etc.)

Explanation / Answer

A few models are given below .Try them

2nd model

The Launchpad board has two pushbuttons. One is hooked to reset, so we might not be able to access that (I don’t know yet). But the other one is hooked to pin 3, and we can definitely check that. It turns out that pin 3 is high when the button is not pushed and goes low when we press it.

We can use that to change the behavior of our lights. Let’s blink only the red light when the button is down and blink both lights (not necessarily alternately as you’ll see) when the button is up.

When you press switch 2 (which is connected to pin 3) the green LED will quit changing state, and only the red LED will flash. When you release the button, both will flash again (though whether they alternate or not depends on the timing of your button release).

3rd model (using switch case)

  1st model  ;------------------------------------------------------------------------------    ;   Read the status of built in push button - P1.3  ;      (Note that P1.3 is "1" when the push button is open   ;       and "0" when the button is closed)  ;   Red light if the button is not pushed - P1.0  ;   Green light if the button is pushed - P1.6  ;   Build with Code Composer Studio  ;------------------------------------------------------------------------------                .cdecls C,LIST,"msp430g2231.h"  ; cdecls tells assembler to allow                                              ; the c header file    ;------------------------------------------------------------------------------  ;   Main Code  ;------------------------------------------------------------------------------                .text                           ; program start              .global _main                   ; define entry point    _main       mov.w   #0280h,SP               ; initialize stack pointer              mov.w   #WDTPW+WDTHOLD,&WDTCTL  ; stop watchdog timer                bis.b   #01000001b,&P1DIR       ; make P1.0 and P1.6 output                                              ; all others are inputs by default    Mainloop    bit.b   #00001000b,&P1IN        ; read switch at P1.3              jc      Off                     ; if P1.3 open branch to Off label                                                On          bic.b   #00000001b,&P1OUT       ; clear P1.0 (red off)              bis.b   #01000000b,&P1OUT       ; set P1.6 (green on)              jmp     Wait                    ; branch to a delay routine    Off         bis.b   #00000001b,&P1OUT       ; set P1.0 (red on)              bic.b   #01000000b,&P1OUT       ; clear P1.6 (green off)                Wait        mov.w   #1834,R15               ; load R15 with value for delay  L1          dec.w   R15                     ; decrement R15              jnz     L1                      ; if R15 is not zero jump to L1              jmp     Mainloop                ; jump to the Mainloop label                                                ;------------------------------------------------------------------------------  ;   Interrupt Vectors  ;------------------------------------------------------------------------------              .sect   ".reset"                ; MSP430 RESET Vector              .short  _main                                  .end  

2nd model

The Launchpad board has two pushbuttons. One is hooked to reset, so we might not be able to access that (I don’t know yet). But the other one is hooked to pin 3, and we can definitely check that. It turns out that pin 3 is high when the button is not pushed and goes low when we press it.

We can use that to change the behavior of our lights. Let’s blink only the red light when the button is down and blink both lights (not necessarily alternately as you’ll see) when the button is up.

  #include <io.h>    #define pin0mask  (0x01 << 0)  #define pin3mask  (0x01 << 3)  #define pin6mask  (0x01 << 6)    int main(void) {      /* Hold the watchdog timer so it doesn't reset our chip */      WDTCTL = WDTPW + WDTHOLD;        /* Configure pins 0,6 on port 1 as output pins */      P1DIR = pin0mask | pin6mask;        /* Set pin 6 high.  Basically, this command sets any combination       * of the pins on port 1 high.  Pin 0 is 2^0, pin 1 is 2^2, etc.       * Values can be binary or'd together. Other pins are low.       */      P1OUT = pin6mask;        /* infinite loop */      for( ; ; ) {          /* The following two lines implement a very crude delay loop.           * The actual length of the delay can vary significantly.           * This approach may not work with all compilers.           */          volatile int i;          for( i = 0; i < 20000; i++ );            /* Switch 2 is connected to pin 3.  If it is low, then change the           * blinking behavior.  Sometimes we blink both LEDs, sometimes only           * the red LED.           */          if(( P1IN & pin3mask ) == 0 ) {              /* Toggle just pin 0. */              P1OUT ^= pin0mask;          } else {              /* Toggle both pin 0 and pin 6 */              P1OUT ^= pin0mask|pin6mask;          }      }  }  

When you press switch 2 (which is connected to pin 3) the green LED will quit changing state, and only the red LED will flash. When you release the button, both will flash again (though whether they alternate or not depends on the timing of your button release).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote