Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with the structure of my code, its not outputting correctly and i am unsure where exactly its having issues? / * *

I need help with the structure of my code, its not outputting correctly and i am unsure where exactly its having issues?
/** EX: E6.0
TASK:
Your task is to write code which handles a sequence of input characters
from the UART interface, and responds with the output specified below.
On receipt of the character sequence:
- "foo" your program should print '0'.
- "bar" your program should print '1'.
- "foobar" your program should not print either '0' or '1' as specified
above, but should instead print a linefeed '
' character.
NOTE:
It is strongly recommended that you design a state machine to complete
this task.
Your solution should use a baud rate of 9600, and 8N1 frame format. Your
solution MUST NOT use qutyio.o or qutyserial.o.
EXAMPLES:
INPUT: ...foo.bar.foo.barfoobarfood
OUTPUT: 0101
0
(START)0101
0(END)
INPUT: barsfoosbarforbarfoobarrforfoobarfoobarfood
OUTPUT: 1011
0
(START)1011
0(END)
*/
#include
void uart_init(void){
PORTB.DIRSET = PIN2_bm; // Enable PB2 as output (USART0 TXD)
USART0.BAUD =1389; //9600 baud @ 3.3 MHz
USART0.CTRLB = USART_RXEN_bm | USART_TXEN_bm; // Enable Tx/Rx
}
void uart_putc(char data){
while (!(USART0.STATUS & USART_DREIF_bm)); // Wait for the Data Register to be empty
USART0.TXDATAL = data; // Transmit the data
}
uint8_t uart_getc(void){
while (!(USART0.STATUS & USART_RXCIF_bm)); // Wait for received data
return USART0.RXDATAL;
}
typedef enum {
START,
F1, FO1, FOO1, FO2, FOA2, FOR2,
B1, BA1, BAR1
} state;
int main(void){
uart_init();
state current_state = START;
int foobar_triggered =0;
while (1){
char received_char = uart_getc(); // Get input character from UART
switch (current_state){
case START:
if (received_char =='f') current_state = F1;
else if (received_char =='b') current_state = B1;
else current_state = START;
foobar_triggered =0;
break;
case F1:
if (received_char =='o') current_state = FO1;
else current_state = START;
break;
case FO1:
if (received_char =='o') current_state = FOO1;
else current_state = START;
break;
case FOO1:
if (received_char =='b') current_state = FO2;
else {
uart_putc('0');
current_state =(received_char =='f')? F1 : START;
}
break;
case FO2:
if (received_char =='a') current_state = FOA2;
else {
uart_putc('0');
current_state =(received_char =='f')? F1 : START;
}
break;
case FOA2:
if (received_char =='r') current_state = FOR2;
else {
uart_putc('0');
current_state =(received_char =='f')? F1 : START;
}
break;
case FOR2:
uart_putc('
');
foobar_triggered =1;
current_state = START;
break;
case B1:
if (received_char =='a') current_state = BA1;
else {
current_state =(received_char =='f')? F1 : START;
}
break;
case BA1:
if (received_char =='r'){
uart_putc('1');
if (!foobar_triggered) current_state = START;
} else {
current_state =(received_char =='f')? F1 : START;
}
break;
default:
current_state = START;
break;
}
}
}
Ex E6.0(0/1)
FAIL.
Input: .foo.bar.foobar.ffoo.bbar.ffoobar.ffoobbar.ffoob.ffooba.obarfoobafooba.abarbarbafoobfoobabarfoofobarfoobaabarofobafoobarafoobarbarfofofoobfofbarbarafoobfoobfoobafofoobfobabbafoobafoofofofoobba.
Expected: 01
01
0100100110010101
10110000000
(START)01
01
0100100110010101
10110000000(END)
Observed: 01
11001100001
011000000
(START)01
11001100001
011000000(END)

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2018 Dublin Ireland September 10 14 2018 Proceedings Part 1 Lnai 11051

Authors: Michele Berlingerio ,Francesco Bonchi ,Thomas Gartner ,Neil Hurley ,Georgiana Ifrim

1st Edition

3030109240, 978-3030109240

Students also viewed these Databases questions