Застрял в логике, которая должна быть написана для нажатия кнопки - MSP432
Я застрял в той части своей лаборатории, где нам нужно, чтобы светодиод переключался между тремя разными цветами (красным, зеленым и синим), пока кнопка нажата. Когда кнопка отпущена, индикатор должен оставаться на том цвете, на котором он был в последний раз, пока кнопка не будет удерживаться снова. Мой код в настоящее время в основном обратный, но я не могу понять, что изменить. В настоящее время он циклически переключается между разными светодиодами, когда не нажата, и когда кнопка удерживается, она остается на последнем цвете, который был до нажатия кнопки. Я чувствую, что исправить это очень просто, но я просто не могу понять это. Нам предоставили таймер и сказали использовать тактовые циклы для нашей функции задержки, так как мы еще не научились использовать таймеры.
#include "msp.h"
#include <stdint.h>
/* defined macros */
#define _BIT(x) (1<<x)
#define RED_LED (_BIT(0) )
#define GREEN_LED (_BIT(1))
#define BLUE_LED (_BIT(2))
#define S1 (_BIT(1))
#define RGB_LED (_BIT(0)|_BIT(1)|_BIT(2))
/* function declarations */
void LED_sequence(int delay);
int debounce_btn1(int delay_time);
/* variable declarations */
const int time = 10;
/**
* main.c
*/
void main(void)
{
/* Stop watchdog timer */
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;
LED_sequence(time);
}
/* function definitions */
void LED_sequence(int delay) {
/* Name: LED_sequence
* Description: This function sequences LED's on MSP432 Launchpad
* on Port2 Pin1, Pin2, and Pin3 if pushbutton 1 is detected.
* IN: void
* OUT:void
*/
/* local variable */
int tracker = 0;
/* set P1.1 and P2.0, P2.1 and P2.2 as GPIO Pins */
P1->SEL0 &= ~RGB_LED;
P1->SEL1 &= ~RGB_LED;
P2->SEL0 &= ~S1;
P2->SEL1 &= ~S1;
/* Set P1.1 as input, with pullup resistor and set as high */
P1->DIR |= S1;
P1->REN |= S1;
P1->OUT |= S1;
/* Set P2.0, P2.1 and P2.2 as outputs */
P2->DIR |= RGB_LED;
/* Turn off all LED's */
P2->OUT &= ~RGB_LED;
while (1){
if (debounce_btn1(delay)) {
while(debounce_btn1(delay));
tracker++;
}
if (tracker == 1) {
/* turn on red LED and have all others off */
P2->OUT &= ~RGB_LED;
P2->OUT |= RED_LED;
__delay_cycles(3000000);
tracker++;
}
else if (tracker == 2) {
/* turn on green LED and have all others off */
P2->OUT &=~RGB_LED;
P2->OUT |= GREEN_LED;
__delay_cycles(3000000);
tracker++;
}
else if (tracker == 3) {
/* turn on blue LED and have all other off */
P2->OUT &=~RGB_LED;
P2->OUT |= BLUE_LED;
__delay_cycles(3000000);
tracker = 1;
}
/* to make sure that if tracker is assigned a random value, to
* defaulted to 0 to enter if statements
*/
else
tracker = 0;
}
}
int debounce_btn1(int delay_time) {
/* Name: debounce_btn1
* Description:
* IN:
* OUT:
*/
/* while loop acts like a debouncer to make sure that the signal is
* stable and not changing
*/
while (!(P1IN & S1));
__delay_cycles(30000);
/* if statement to make sure that user has actually pressed button */
if(!(P1IN & S1)) {
return 1;
}
else
return 0;
}