Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Design a vending machine controller for a vending machine that sells an item for 25 cents. The types of coins the vending machine accepts are

Design a vending machine controller for a vending machine that sells an item for 25 cents. The types of coins the vending machine accepts are nickels, dimes, and quarters. The vending machine does not give change (that is, the customer will lose their money if more than 25 cents is inserted before the item is vended). You will design and implement a Finite State Machine (FSM) to control your vending machine. The FSM will keep track of the state of the system, that is how much money has been inserted, and once enough money has been inserted it will turn on the motor to vend the item. As coins are inserted your vending machine controller should also keep track of remaining amount due in order to display how much money is due to purchase the item. Your controller will have six parts: 1. Your FSM which will be implemented as an array of states. Each state is represented by a struct in C where the fields of the struct are the outputs of the controller (the vend motor control signal and the amount due) and the array of pointers to next states. 2. A main function which declares a state pointer, initializes the state pointer to a start state, and then continuously loops. In the infinite loop the following actions will take place: The output will be displayed for the current (present) state (part 4) The input will be read (part 6) The controller will transition to the next state based on the input value by updating the state pointer. 3. A function to generate the control signals for a 7segment controller based on the digit to be displayed. It should take as input the value of the digit and a period flag. The value of the digit can be 0 to 9. The value of the period flag should be 0 for no period and 1 for a period. The function should return the control signals expressed as hexadecimal numbers. To the right is a picture of a seven segment display with each segment assigned a letter. You can control the display by writing a 1 to the pin that controls the LED (light emitting diode) for that segment. Assume that the segments can be controlled with a byte (8bit) control signal where segment a is the most significant bit (msb) bit 7, segment b is bit 6, segment c is bit 5, etc. Bit 0 controls the period (P). So, to display a 0 the function input would be 0 for the digit and 0 for the period flag and it should return the following 8bit value: 11111100 (which is 0xFC in hex). To display a 0. the function input would be a 0 for the digit and a 1 for the period flag and it should return the following 8bit value: 11111101 (which is 1 0xFD). To display a 2 the function input would be 2 for the digit and 0 for the period flag and it should return the following 8bit value: 11011010 (which is 0xDA). 4. A function to display the output. This function will be called by the main function in part 2 to display the output. It should take as input the vend output value and the amount due output value. In the actual embedded system for the vending machine, the output signals would be connected to the motor to control when to vend (0 = motor off to not vend and 1 = motor on to vend) and the seven segment displays to display the amount due. For testing and debugging purposes, you can emulate the outputs by displaying them to the screen. You should display the following information: If the vend control signal is a one, display a message that the item is being vended. The amount due in decimal format. E.g., 0.20 (if 20 cents is due) The control signals to the 7segment controller. In order to control the three 7segment displays for the amount due, you should call the function from part 3 three times. Your function should display the returned control signal as a hexadecimal value. E.g. FD FC DA (if 20 cents is due). Part 3 above explains how these values are determined. 5. A function to encode the three sensor values to a 2bit value. There are three sensors used in your vending machine: nickel, dime, and quarter. The sensor value will be a one if that particular coin has been detected, otherwise the sensor value will be a zero. This function will take as input the three sensor values and return an encoded value: 00 (0) no coin when no sensors inputs are 1 01 (1) nickel when the nickel sensor input is a 1 10 (2) dime when the dime sensor input is a 1 11 (3) quarter when the quarter sensor input is a 1 6. A function to input the coin value and return the encoded input value for the FSM using the function from part 5. In an embedded system the sensors would detect the coin entered. For testing and debugging, to emulate the coin input, write a function to prompt the user for the value of the coin entered (0, 5, 10, 25). From this value you can generate the appropriate sensor input values to pass to the function in part 5. For example, if the user enters 10, then the function in part 5 should be passed a 0 for the nickel argument, a 1 for the dime argument, and a 0 for the quarter argument. The encoded value returned will be used by the main function to determine the next state. Submit the following two files: A pdf file with the following: o A drawing of your finite state machine for your vending machine showing the states (with the state labeled appropriately and the outputs shown in each state) and the transitions between states labeled with the appropriate inputs (no coin, nickel, dime, and quarter). o Flow charts for the functions in parts 2 6 above. Draw a separate flow chart for each function. 2 o The C code for your vending machine controller (parts 1 6 above) with good header comments for each function and the overall program and with comments throughout explaining your code. o A screen capture of sample test cases. Note, to make sure that your code is correct, you should exhaustively test (all states and all transitions from each state). However, you do not need to submit screen captures for all test cases. Instead, capture tests that make sure all states have been visited and that all inputs can be handled correctly (no coin, nickel, dime, and quarter). You will end up running through the FSM multiple times (vending multiple times) in order to test these different cases. Note, due to the infinite loop, you only have to start the program once and then provide inputs to cycle through the different states. Your C code (.c or .cpp file) for independent testing. You can submit in a word document or as a .c/.cpp file. Example FSM to control traffic light To help you with your project, below is the example FSM discussed in class. Additional comments have been added to the code to explain its functionality. Hardware Configuration: 3 Finite State Machine (FSM): C Code: const struct State { unsigned int Out; // output to control lights unsigned int Time; // time to delay in each state before changing light const struct State *Next[4]; // used to transition to next state based // on the 4 input values typedef const struct State SType; // declare a type for states to be used later #define goN &FSM[0] // use these defines to make code more readable by assigning #define waitN &FSM[1] // a name to each state #define goE &FSM[2] #define waitE &FSM[3] // The FSM is an array of 4 states SType FSM[4] = { { 0x21,30,{ goN,waitN,goN,waitN } }, // initialize the fields of each state { 0x22, 5,{ goE,goE,goE,goE } }, { 0x0C,30,{ goE,goE,waitE,waitE } }, { 0x14, 5,{ goN,goN,goN,goN } } }; // The light_signal function will be used for debugging/testing purposes }; // to display the value of each ligh (NS = North/South light and // goN: 100001 (0x21) makes it green on North and red on East // waitN: 100010 (0x22) makes it yellow on North and red on East // goE: 001100 (0x0C) makes it red on North and green on East // waitE: 010100 (0x14) makes it red on North and yellow on East void light_signal(unsigned int control) { switch (control) { case 0x21: printf("\t**NS Green and EW Red** "); break; case 0x22: printf("\t**NS Yellow and EW Red** "); break; case 0x0c: printf("\t**NS RED and EW Green** "); break; case 0x14: printf("\t**NS Red and EW Yellow** "); break; default: printf("Error: invalid control input "); } } EW = East/West light // The delay function will delay a specified number of seconds // For testing purposes, the delay value can be shortened if desired void delay(int number_of_seconds) { // Converting time into milli_seconds 4 } } return 0; } int milli_seconds = 100 * number_of_seconds; // adjust time change 1000 to 100 // Stroing start time clock_t start_time = clock(); // looping till required time is not acheived while (clock() < start_time + milli_seconds) ; // The read_senor function will prompt the user to enter the senor values this // is used for testing/debugging in order to emulate the sensor inputs unsigned int read_sensor() { unsigned int sensor_in; printf("Car sensor (0=None, 1=East, 2=North, 3=East&North): "); scanf_s("%d", &sensor_in); return sensor_in; } // The main function will use a pointer to point to the present state. The program will // initialize the pointer to the start state. The program will then loop continually, // In the loop the program will generate the outputs (display the lights), delay, read // the input values (sensors), and then based on the input, update the pointer to the // corresponding next state. int main() { SType *Pt; // state pointer unsigned int Input; // input sensors Pt = goN; // initialize Pt to point to the start state (north green, east red) while (1) { // loop continually light_signal(Pt>Out); // control the outputs for current (present) state delay(Pt>Time); // delay this is optional for FSMs Input = read_sensor(); // read input values (sensors) Pt = Pt>Next[Input]; // transition to next state based on input by // updating Pt

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

Database Horse Betting The Road To Absolute Horse Racing 2

Authors: NAKAGAWA,YUKIO

1st Edition

B0CFZN219G, 979-8856410593

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago