#include "hal_LCD.h"
#include "gpio.h"
#include <stdlib.h>
#include <time.h>
#include <string.h>
// Define the GPIO pins corresponding to each emotion button
#define JOY_PIN BIT4 // P1.4
#define SAD_PIN BIT3 // P1.3
#define ANGRY_PIN BIT6 // P1.6
#define FEAR_PIN BIT7 // P1.7
#define MALICE_PIN BIT5 // P2.5
#define AMAZE_PIN BIT7 // P2.7
// Define maximum number of synonyms for each emotion
#define MAX_SYNONYMS 5
// Define synonyms for each emotion
const char* const joySynonyms[MAX_SYNONYMS] = {"HAPPY", "GLAD", "CHEER", "DELITE", "MIRTH"};
const char* const sadSynonyms[MAX_SYNONYMS] = {"GLOOM", "MELAN", "SORROW", "DESOL", "TEARS"};
const char* const angrySynonyms[MAX_SYNONYMS] = {"FUROR", "IRATE", "RAGE", "ANGER", "STRESS"};
const char* const fearSynonyms[MAX_SYNONYMS] = {"TERROR", "DREAD", "ANX", "ALARM", "PANIC"};
const char* const maliceSynonyms[MAX_SYNONYMS] = {"SPITE", "HATE", "VENOM", "MALICE", "SPURN"};
const char* const amazeSynonyms[MAX_SYNONYMS] = {"WONDER", "AMAZE", "ASTON", "AWING", "MARVEL"};
// Global variables to hold the display index, button index, and score
int displayIndex = 0;
int buttonIndex = 0;
// Function to display a word on the LCD
void displayWord(const char* word) {
clearLCD();
int len = strlen(word);
if (len <= 3) {
showChar(word[0], pos1);
if (len >= 2) {
showChar(word[1], pos2);
}
if (len == 3) {
showChar(word[0], pos1);
showChar(word[1], pos2);
showChar(word[2], pos3);
}
} else if (len == 4) {
showChar(word[0], pos1);
showChar(word[1], pos2);
showChar(word[2], pos3);
showChar(word[3], pos4);
} else if (len == 5) {
showChar(word[0], pos1);
showChar(word[1], pos2);
showChar(word[2], pos3);
showChar(word[3], pos4);
showChar(word[4], pos5);
} else if (len == 6){
showChar(word[0], pos1);
showChar(word[1], pos2);
showChar(word[2], pos3);
showChar(word[3], pos4);
showChar(word[4], pos5);
showChar(word[5], pos6);
}
}
// Function to display "START" on the LCD for 3 seconds
void displayStartScreen() {
clearLCD();
showChar('S', pos1);
showChar('T', pos2);
showChar('A', pos3);
showChar('R', pos4);
showChar('T', pos5);
__delay_cycles(3000000); // Delay for 3 seconds
}
void debounceButton(){
__delay_cycles(2000000);
}
void displayScore(int score) {
clearLCD();
showChar('P', pos1);
showChar('T', pos2);
showChar('S', pos3);
int hundreds = score/100;
int tens = score / 10;
int units = score % 10;
showChar(hundreds + '0', pos4);
showChar(tens + '0', pos4);
showChar(units + '0', pos5);
__delay_cycles(4000000);
WDTCTL = 0; //Trigger watchdog reset
}
// Function to initialize button pins
void initButtonPins() {
// Set button pins as inputs with internal pull-up resistors
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, JOY_PIN);
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, SAD_PIN);
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, ANGRY_PIN);
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, FEAR_PIN);
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P2, MALICE_PIN);
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P2, AMAZE_PIN);
}
// Function to configure interrupts for button pins
void configureButtonInterrupts() {
// Enable interrupts for each button pin
GPIO_selectInterruptEdge(GPIO_PORT_P1, JOY_PIN, GPIO_LOW_TO_HIGH_TRANSITION);
GPIO_selectInterruptEdge(GPIO_PORT_P1, SAD_PIN, GPIO_LOW_TO_HIGH_TRANSITION);
GPIO_selectInterruptEdge(GPIO_PORT_P1, ANGRY_PIN, GPIO_LOW_TO_HIGH_TRANSITION);
GPIO_selectInterruptEdge(GPIO_PORT_P1, FEAR_PIN, GPIO_LOW_TO_HIGH_TRANSITION);
GPIO_selectInterruptEdge(GPIO_PORT_P2, MALICE_PIN, GPIO_LOW_TO_HIGH_TRANSITION);
GPIO_selectInterruptEdge(GPIO_PORT_P2, AMAZE_PIN, GPIO_LOW_TO_HIGH_TRANSITION);
GPIO_clearInterrupt(GPIO_PORT_P1, JOY_PIN | SAD_PIN | ANGRY_PIN | FEAR_PIN);
GPIO_clearInterrupt(GPIO_PORT_P2, MALICE_PIN | AMAZE_PIN);
GPIO_enableInterrupt(GPIO_PORT_P1, JOY_PIN | SAD_PIN | ANGRY_PIN | FEAR_PIN);
GPIO_enableInterrupt(GPIO_PORT_P2, MALICE_PIN | AMAZE_PIN);
}
// Function to check if any button is pressed
int isAnyButtonPressed() {
return GPIO_getInputPinValue(GPIO_PORT_P1, JOY_PIN) ||
GPIO_getInputPinValue(GPIO_PORT_P1, SAD_PIN) ||
GPIO_getInputPinValue(GPIO_PORT_P1, ANGRY_PIN) ||
GPIO_getInputPinValue(GPIO_PORT_P1, FEAR_PIN) ||
GPIO_getInputPinValue(GPIO_PORT_P2, MALICE_PIN) ||
GPIO_getInputPinValue(GPIO_PORT_P2, AMAZE_PIN);
}
int main(void)
{
// Stop watchdog timer
WDTCTL = WDTPW + WDTHOLD;
// Initialize LCD
Init_LCD();
displayStartScreen();
PMM_unlockLPM5();
// Initialize button pins
initButtonPins();
// Enable interrupts for button pins
configureButtonInterrupts();
// Enable global interrupts
__enable_interrupt();
int score = 0; // Variable to store the score
while (1)
{
// Generate a random number to represent the emotion
srand(time(NULL)); // Seed the random number generator
int randomNumber = rand() % 6 + 1; // Generate a random number between 1 and 6
// Set the display index based on the random number
switch (randomNumber) {
case 1:
displayIndex = 1;
displayWord(joySynonyms[rand() % MAX_SYNONYMS]);
break;
case 2:
displayIndex = 2;
displayWord(sadSynonyms[rand() % MAX_SYNONYMS]);
break;
case 3:
displayIndex = 3;
displayWord(angrySynonyms[rand() % MAX_SYNONYMS]);
break;
case 4:
displayIndex = 4;
displayWord(fearSynonyms[rand() % MAX_SYNONYMS]);
break;
case 5:
displayIndex = 5;
displayWord(maliceSynonyms[rand() % MAX_SYNONYMS]);
break;
case 6:
displayIndex = 6;
displayWord(amazeSynonyms[rand() % MAX_SYNONYMS]);
break;
default:
break;
}
// Debounce Buttons
debounceButton();
// Wait until any button is pressed
while (!isAnyButtonPressed());
// Compare displayIndex and buttonIndex to update the score
if (displayIndex == buttonIndex) {
score++; // Increment score for correct button press
__delay_cycles(1000);
}else{
displayScore(score);
}
}
}
// Interrupt service routine for button presses on port 1
#pragma vector = PORT1_VECTOR
__interrupt void Port1_ISR(void) {
switch (__even_in_range(P1IV, P1IV_P1IFG7)) {
case P1IV_P1IFG4:
buttonIndex = 1; // JOY button
break;
case P1IV_P1IFG3:
buttonIndex = 2; // SAD button
break;
case P1IV_P1IFG6:
buttonIndex = 3; // ANGRY button
break;
case P1IV_P1IFG7:
buttonIndex = 4; // FEAR button
break;
default:
break;
}
// Clear the interrupt flag
P1IFG &= ~(JOY_PIN | SAD_PIN | ANGRY_PIN | FEAR_PIN);
}
// Interrupt service routine for button presses on port 2
#pragma vector = PORT2_VECTOR
__interrupt void Port2_ISR(void) {
switch (__even_in_range(P2IV, P2IV_P2IFG7)) {
case P2IV_P2IFG5:
buttonIndex = 5; // MALICE button
break;
case P2IV_P2IFG7:
buttonIndex = 6; // AMAZE button
break;
default:
break;
}
// Clear the interrupt flag
P2IFG &= ~(MALICE_PIN | AMAZE_PIN);
}
我正在为此挣扎。
在 CCS (Code Composure Studio) 中,在调试模式下,上述代码在我的 MSP-EXP430FR4133 微控制器上运行良好,但是当我将其与 PC 断开以进行调试模式并将电池连接到具有足够电压的电池时,它就无法运行与调试模式相同。
任何有关这方面的帮助将不胜感激,因为我正在大学项目的过程中。
buttonIndex
在 main() 之外被 ISR 更改,因此应将其声明为 volatile
。编译器没有指示为什么应该更改初始值,并且可能会优化变量读取。
在 displayScore() 中,您应该编写
int tens = (score / 10) % 10;
,而不是在同一位置打印数百和十。