Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need to make a simple stop-watch. Using an Educational BoosterPack MKII (BOOSTXL-EDUMKII) on Code Composer Studio. The picture below represents what should be displayed. when

Need to make a simple stop-watch. Using an Educational BoosterPack MKII (BOOSTXL-EDUMKII) on Code Composer Studio. The picture below represents what should be displayed. when you push button S1, the time runs and when pushed again it stops. when you push S2 the Laps goes up one.

image text in transcribed

Here is the code below

You need to complete all the TODO s in the starter code. If you like, you can add additional functions, but it is not needed. Adding the S2 button to count laps is a bonus.

// A simple stopwatch // The sys clock is modified in system_msp432p401r.c to be 48MHz

#include #include #include "LcdDriver/Crystalfontz128x128_ST7735.h"

// TODO: The below macro is used as the load value for a timer that waits 0.1s. // Based on all other values, choose the correct value for this macro #define TENTH_SEC_COUNT 0

#define PRESSED 0 #define RELEASED 1

// A new type that enumerates the debouncing states of a button // These will be used for debouncing FSM. // TODO: Add the missing states typedef enum {Stable_P, Tran_PtoR} debounce_state_t;

typedef struct { unsigned int tenth; unsigned int sec; unsigned int min; unsigned int hour; } time_t;

// Initialization functions void InitGraphics(Graphics_Context *); void InitFonts(); void Initialize(Graphics_Context *); void InitTimer();

// GPIO HAL functions void TurnOn_Launchpad_LED1(); void TurnOff_Launchpad_LED1(); void Toggle_Launchpad_LED1(); char SwitchStatus_BoosterPack_S1(); char SwitchStatus_BoosterPack_S2(); bool S2Pushed(); bool S1Pushed();

void startTimer0(); bool timer0Expired();

// This function gets "laps" as an integer and embeds in the string that already looks like "Laps: 00". // You can assume laps is less than 100. void makeLapsToString(unsigned int laps, int8_t *string) {

// Embeds the ones, i.e. the lower digit of laps, into the right character in the string // The % operator is modulo and gets the ones value as an integer. // adding '0', adds the ASCII value of 0, which is 48 to that number and therefore turns that number to its ASCII form. // In other words, if laps is 45, the below line assigns '5' to string[7]. string[7] = (laps % 10) + '0';

// TODO: write a similar line to turn the tens of laps into its ASCII form and assign it to the appropriate char in string }

// This function gets the pointer to a time_t struct and increases the time it represents by 0.1s

void increaseTime(time_t *time_p) {

time_p->tenth = time_p->tenth+1;

if (time_p->tenth == 10) { time_p->tenth = 0; time_p->sec = time_p->sec + 1; } //TODO: Finish this function }

// makeString gets a time_t struct and returns the string made up of them in 00:00:00:0 format. // Important note: char *string already has the space to hold these numbers and : are already there. // All you need is to embed the right ASCII numbers in the right place. // TODO: write this function. void makeTimeToString(time_t time, int8_t *string) {

}

int main(void) { Graphics_Context g_sContext; Initialize(&g_sContext);

time_t curTime = {0, 0, 0, 0}; int8_t timeString[12] = "00:00:00:0 ";

// TODO: initialize lapString to contain the proper string similar to timeString. Check the golden solution for guidance. // make sure to fix the size of the string array. // make sure you count \0 the NULL character at the end of the string when you size your array. int8_t lapString[1];

// TODO: initializes these two strings based on golden solution. Make sure to size the arrays. int8_t S1_String[1] = "S1: ..."; int8_t S2_String[1] = "S2: ...";

Graphics_drawString(&g_sContext, S1_String, -1, 10, 20, true); // TODO: Draw the other three strings on the display. It does not need to 100% match golden solution. // As long as your strings are reasonably displayed and look similar to golden solution, you recieve the points for this part.

// The boolean that holds the state of the stop-watch. If stop is 1, the stop-watch is stopped and is NOT running. bool stop = 1;

// The number of laps. unsigned int laps = 0;

while (1) { // Pressing S1 toggles the start/stop if (S1Pushed()) stop = !stop;

// Pressing S2 increments the lap if (S2Pushed()) { laps++;

// TODO: Using the functions you helped develop, update the string for lap and redraw it on the screen

}

// If the stop-watch is in the running mode and is not stopped and the timer for 0.1 is expired, time should be updated if ((!stop) && timer0Expired()) { // since the timer is in one-shot mode, we have to restart it to get a periodic behavior startTimer0();

// TODO: Using the functions you have helped develop, increase the time in curTime struct, update the timeString and redraw it

}

}

}

void InitGraphics(Graphics_Context *g_sContext_p) {

Graphics_initContext(g_sContext_p, &g_sCrystalfontz128x128, &g_sCrystalfontz128x128_funcs); Graphics_setForegroundColor(g_sContext_p, GRAPHICS_COLOR_YELLOW); Graphics_setBackgroundColor(g_sContext_p, GRAPHICS_COLOR_BLUE); Graphics_setFont(g_sContext_p, &g_sFontCmtt16);

InitFonts(); Graphics_setFont(g_sContext_p, &g_sFontCmss18b);

Graphics_clearDisplay(g_sContext_p); }

void InitFonts() { Crystalfontz128x128_Init(); Crystalfontz128x128_SetOrientation(LCD_ORIENTATION_UP); }

void InitTimer() {

// This timer is used for the stop-watch. It will be in one-shot mode. Timer32_initModule(TIMER32_0_BASE, TIMER32_PRESCALER_1, TIMER32_32BIT, TIMER32_PERIODIC_MODE);

// We start it the first time, to make sure the timer reading is 0 when we press the Start/Stop button for the first time Timer32_setCount(TIMER32_0_BASE, 1); Timer32_startTimer(TIMER32_0_BASE, true); // start the timer in one-shot mode. This means the timer stops when it reaches 0

// TODO: Initialize the second timer32 module to be used for button debouncing. No need to start the timer at this time.

}

void Initialize(Graphics_Context *g_sContext_p) { // Stop the watchdog timer WDT_A_hold(WDT_A_BASE);

// Initialize the graphics InitGraphics(g_sContext_p);

// Initialize the timer InitTimer();

// S1, S2 and LED1 initialization GPIO_setAsInputPin(GPIO_PORT_P3, GPIO_PIN5); GPIO_setAsInputPin(GPIO_PORT_P5, GPIO_PIN1); GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0); }

bool S1Pushed() { char cur_status; static char prev_status = RELEASED;

// TODO: Assign an intial state to this variable. // TODO: An important keyword is missing in this declaration. Add it. debounce_state_t debounce_state;

// The default value of the returned value bool isPushed = false;

// The first input of the FSM char rawButtonStatus = SwitchStatus_BoosterPack_S1();

// The second input of the FSM bool timerExpired = (Timer32_getValue(TIMER32_1_BASE) == 0);

// outputs of the FSM bool debouncedButtonStatus; bool startTimer = false;

// The button struct holds info on the debouncing state of the button // e.g. it knows if the button is in transition or not switch (debounce_state) { case Stable_P: // The output of both arcs is the same so we can put this statement outside the transition arcs debouncedButtonStatus = PRESSED; if (rawButtonStatus != PRESSED) { // Change state debounce_state = Tran_PtoR;

// Update outputs, if different from default startTimer = true;

} break;

}

// If the output of the FSM says "start the timer", we should take action and do so if (startTimer) { Timer32_setCount(TIMER32_1_BASE, TENTH_SEC_COUNT); Timer32_startTimer(TIMER32_1_BASE, true);

}

// This is a small state machine that we call the push-button machine cur_status = debouncedButtonStatus; if ((cur_status == PRESSED) && (prev_status==RELEASED)) isPushed = true; prev_status = cur_status;

return isPushed; }

// Bonus TODO: Add S2Pushed() function here.

void TurnOn_Launchpad_LED1() { GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0); } void TurnOff_Launchpad_LED1() { GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0); }

void Toggle_Launchpad_LED1() { GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0); } char SwitchStatus_BoosterPack_S2() { return (GPIO_getInputPinValue(GPIO_PORT_P3, GPIO_PIN5)); } char SwitchStatus_BoosterPack_S1() { return (GPIO_getInputPinValue(GPIO_PORT_P5, GPIO_PIN1)); }

void startTimer0() { Timer32_setCount(TIMER32_0_BASE, TENTH_SEC_COUNT); Timer32_startTimer(TIMER32_0_BASE, true); }

bool timer0Expired() { return (Timer32_getValue(TIMER32_0_BASE) == 0); }

Sl: Start/Stop S2: Lap 00:00:09:4 Laps: 17 Sl: Start/Stop S2: Lap 00:00:09:4 Laps: 17

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

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions

Question

What are the different types of short sales?

Answered: 1 week ago