83 lines
2.9 KiB
C
83 lines
2.9 KiB
C
/*------------------------------------------------------------------------------
|
|
* File: L3.c
|
|
* Function: Turns on LEDs at different cycles if switches are pressed
|
|
* Description: This C program takes as input the switches P1.7 and P2.7 and passes uses the switches to
|
|
* turn on the LEDs at P1.1 and P2.0
|
|
* Input: S1 and S2
|
|
* Output:
|
|
* Author(s): Noah Woodlee
|
|
* Lab Section: 09
|
|
* Date: Sept 3, 2021
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
#include <msp430.h>
|
|
#include <stdio.h>
|
|
|
|
#define S1 (P2IN&BIT1)
|
|
#define S2 (P1IN&BIT1)
|
|
#define REDLED BIT0
|
|
#define GREENLED BIT7
|
|
|
|
int main()
|
|
{
|
|
WDTCTL = WDTPW + WDTHOLD; // Watchdog timer
|
|
P1DIR |= REDLED; // Set port 1 output direction
|
|
P4DIR |= GREENLED; // Set port 4 output direction
|
|
P2DIR &= ~BIT1; // Clear direction for P2.1
|
|
P1DIR &= ~BIT1; // Clear direction for P1.1
|
|
|
|
P2REN |= BIT1; // Enable Pull-up resistor at P1
|
|
P2OUT |= BIT1; // Required for proper IO
|
|
P1REN |= BIT1; // Enable Pull-up resistor at P2
|
|
P1OUT |= BIT1; // Required for proper IO
|
|
|
|
P1OUT |= REDLED; // LED 1 is ON
|
|
P4OUT |= GREENLED; // LED 2 is ON
|
|
|
|
unsigned int i = 0;
|
|
while(1)
|
|
{
|
|
if((S1) == 0) // If S1 is pressed
|
|
{
|
|
for(i = 2000; i > 0; i--); //Debounce ~20ms
|
|
if((S1) == 0)
|
|
{
|
|
P4OUT |= GREENLED; // LED 2 is ON
|
|
for (i = 0;i < 20000; i++); //5 Hz
|
|
for (i = 0;i < 10000; i++); // Software delay
|
|
P1OUT ^= REDLED; // Toggle LED 1
|
|
while ((S1) == 0) // As long as S1 is pressed
|
|
{
|
|
P4OUT |= GREENLED; // LED 2 is ON
|
|
for (i = 0;i < 20000; i++); //5 Hz
|
|
for (i = 0;i < 10000; i++); //Software delay
|
|
P1OUT ^= REDLED; // Toggle LED 1
|
|
}
|
|
}
|
|
}
|
|
else if ((S2) == 0) // If S2 is pressed
|
|
{
|
|
for(i = 2000; i > 0; i--); //Debounce ~20ms
|
|
if((S2) == 0)
|
|
{
|
|
P1OUT |= REDLED; // LED 1 is ON
|
|
for (i = 0;i < 50000; i++); //2 Hz
|
|
for (i = 0;i < 25000; i++); // Software delay
|
|
P4OUT ^= GREENLED; // Toggle LED 2
|
|
while ((S2) == 0) // As long as S2 is pressed
|
|
{
|
|
P1OUT |= REDLED; // LED 1 is ON
|
|
for (i = 0;i < 50000; i++); //2 Hz
|
|
for (i = 0;i < 25000; i++); // Software delay
|
|
P4OUT ^= GREENLED; // Toggle LED 2
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
P1OUT |= REDLED;
|
|
P4OUT |= GREENLED;
|
|
}
|
|
}
|
|
}
|