Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is code used to increment and decrement an LED counter using a switch (S1) on the MSP432 P1.1 to increment; and (S2) externally connected

Below is code used to increment and decrement an LED counter using a switch (S1) on the MSP432 P1.1 to increment; and (S2) externally connected to P5.0 and P5.2 to decrement the led.

However, when pressing the increment button, it only works if S2 is also pressed at the same time. What might be the problem here? Need help fixing this code or outlining any issues. THANK YOU!

#include "msp.h"

// Global variable used to hold the current state char currentState;

// Global variable used to hold the number of pushes long int pushes;

void countDown(void); void countUp(void);

/* * Initialization function runs once before the state machine while(1) loop. */ void init(void) { WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // Stop watchdog timer P1DIR |= (BIT0); // Set P1.0 to output direction (RED -- LED 1) P2DIR |= (BIT2 + BIT1 + BIT0); // Set P2, bits 2, 1, 0 to output (RGB -- LED 2)

// P1 is set to input by default in the P1DIR reg P1DIR &= ~(BIT1); // So clearing it here is redundant // Set Buttons to input direction

// Setting the internal resistor to be a pullup // may be redundant if button has an external pullup P1REN |= (BIT1); // enable internal resistor P1OUT |= (BIT1); // make internal resistor pullup

P5DIR |= BIT0; // Set BIT 0 to output P5OUT |= BIT0; // Set BIT 0 as VCC

P5DIR &= ~ (BIT2); // Makes this an input P5REN |= (BIT2); // Turns on internal resistor P5OUT &= ~(BIT2); // Make internal resistor pulldown

currentState = 0; // Global variable init pushes = 0; // Global variable init

P1OUT &= ~(BIT0); // Clear both LED 1 AND Led 2 initially P2OUT &= ~(BIT2 + BIT1 + BIT0); }

// Check S1 Button and count up if it is pressed char readS1Input(void) { char local = 0;

// The P1IN register holds the logical values of the pins that are // configured as inputs

if (!(P1IN & BIT1)) { // Check for S1 input pushes = pushes + 1; // Code that executes when S1 button is pushed local = 1; __delay_cycles(10000); // Wait for debounce to end (bad code) countUp(); while(!(P1IN & BIT1)) { // Wait for Up button to return to "normal" // Must re-read the input port // Not good code because loop could run forever } __delay_cycles(10000); // Again wait for debounce to end (again bad code) } return local; }

// Check S2 Button and count down if it is pressed char readS2Input(void) { char local = 0;

// The P1IN registers hold the logical values of the pins that are // configured as inputs

if (!(P5IN & BIT2)) { // Check for S2 input pushes = pushes + 1; // Code that executes when S2 button is pushed local = 2; __delay_cycles(10000); // Wait for debounce to end (bad code) countDown(); while(!(P5IN & BIT2)) { // Wait for Up button to return to "normal" // Must re-read the input port // Not good code because loop could run forever } __delay_cycles(10000); // Again wait for debounce to end (again bad code) } return local; }

void countUp(void) { P1OUT |= BIT0; // Set LED1 to indicate increment switch (currentState){ case 0 : currentState = 1; P2OUT &= ~(BIT2 + BIT1); P2OUT |= (BIT0); break; case 1 : currentState = 2; P2OUT &= ~(BIT2 + BIT0); P2OUT |= (BIT1); break; case 2 : currentState = 3; P2OUT &= ~(BIT2); P2OUT |= (BIT1 + BIT0); break; case 3 : currentState = 4; P2OUT &= ~(BIT0 + BIT1); P2OUT |= (BIT2); break; case 4 : currentState = 5; P2OUT &= ~(BIT1); P2OUT |= (BIT2 + BIT0); break; case 5 : currentState = 6; P2OUT &= ~(BIT0); P2OUT |= (BIT2 + BIT1); break; case 6 : currentState = 7; P2OUT |= (BIT2 + BIT1 + BIT0); break; case 7 : currentState = 0; P2OUT &= ~(BIT2 + BIT1 + BIT0); break; } }

void countDown(void) { P1OUT &= ~(BIT0); // Turn off LED1 to indicate decrement switch (currentState){ case 2 : currentState = 1; P2OUT &= ~(BIT2 + BIT1); P2OUT |= (BIT0); break; case 3 : currentState = 2; P2OUT &= ~(BIT2 + BIT0); P2OUT |= (BIT1); break; case 4 : currentState = 3; P2OUT &= ~(BIT2); P2OUT |= (BIT1 + BIT0); break; case 5 : currentState = 4; P2OUT &= ~(BIT0 + BIT1); P2OUT |= (BIT2); break; case 6 : currentState = 5; P2OUT &= ~(BIT1); P2OUT |= (BIT2 + BIT0); break; case 7 : currentState = 6; P2OUT &= ~(BIT0); P2OUT |= (BIT2 + BIT1); break; case 0 : currentState = 7; P2OUT |= (BIT2 + BIT1 + BIT0); break; case 1 : currentState = 0; P2OUT &= ~(BIT2 + BIT1 + BIT0); break; } }

/** * main.c */ void main(void) { WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer init(); // One time initialization function

/* * The main while loop of the state machine will constantly scan the inputs * and update the system based on those inputs * This version of the code updates the LED's on the button press */ while(1) { readS1Input(); // Check the S1 button readS2Input(); // Check the S2 button

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

BaSO4 would be product of the reaction of what acid and what base?

Answered: 1 week ago

Question

Define induction and what are its objectives ?

Answered: 1 week ago

Question

Discuss the techniques of job analysis.

Answered: 1 week ago

Question

How do we do subnetting in IPv6?Explain with a suitable example.

Answered: 1 week ago

Question

Explain the guideline for job description.

Answered: 1 week ago

Question

What is job description ? State the uses of job description.

Answered: 1 week ago

Question

c. What groups were least represented? Why do you think this is so?

Answered: 1 week ago