#include enum States {Start, Init, LED_Lit1} state; unsigned char button; #defin
ID: 3747428 • Letter: #
Question
#include
enum States {Start, Init, LED_Lit1} state;
unsigned char button;
#define button PINA & 0x01
void tick_LED()
{
switch(state) { // Transitions
case Start: // Start transition
state = Init;
break;
case Init: // Initial transition
if(button == 1){ // If PINA0 is 1 state goes to led_lit in LED_Lit1
state = LED_Lit1;
}
else { // If PINA0 is 0 state remains in initial
state = Init;
}
break;
case LED_Lit1: // LED_Lit1 Transition
if(button == 0){ // If PA0 is 1 state remains in LED_Lit1
state = LED_Lit1;
}
else {
state = Init; // if PA1 is 1 state goes to initial
}
break;
default:
state = Start;
break;
}
switch(state) { // Actions
case Start:
PORTB = 0x01; // PB0 set to 1
break;
case Init:
PORTB = 0x01; // PB0 set to 1
break;
case LED_Lit1:
PORTB = 0x02; // PB1 set to 1
break;
}
}
int main(void)
{
DDRA = 0x00; PORTA = 0xFF; // Configure port A's 8 pins as inputs
DDRB = 0xFF; PORTB = 0x00; // Configure port B's 8 pins as outputs,
while (1)
{
state = Start;
tick_LED();
}
}
Hi, could someone please help me debug this code....I've created a simple state machine but it wont transition into different states....I would like it so when I press the button PINA0 is 1 and it transitions into the LED_LIT1 state and the output of PORTB becomes 0x02 instead of the default 0x01. this is done in Atmel studio 7.0 using Atmega32 using C programming
Explanation / Answer
Answer:
I would like to elaborate the final expected result from the question.
As per the issue described
a) The current program is not transitioning to multiple states as expected and always getting struck in the same state.
b) The actual output is not as per the expected one. Expected output is, when PINA0 becomes 01 program should transition to LED_LIT1 state and output of PORTB should be 0x02 and remain in this state till PINA0 becomes 0.
Issues observed in the provided program and fixes:
1. In the original program, inside infinite loop state is always getting reassigned to “Start”. When tick_LED() function is being called, the first switch case will make value of "state" to "Init". And second switch case will make PORTB = 0x01. Function call will be returned to original infinite loop, where "state" is again changed back to "Start". Due to this infinite loop will not transition to new state.
Fix to this issue: “state = Start;” initialization should be moved out of the infinite while loop. This will ensure state transitioning happens from “Init” to “LED_Lit1” properly.
2. In the function “tick_LED()”, case LED_Lit1: makes comparison of (button == 0), as this condition will be false during event of PINA0 = 0x01, state will transition to Init from LED_Lit1. Due to this, "state" will always toggles between “Init” and “LED_Lit1” when PINA0 = 0x01. However, as per the expectation state should remain at “LED_Lit1” during the event of PINA0 = 0x01.
Fix to this issue: Inside case LED_Lit1: comparison of (button == 1) should be done to ensure PORTB remains at 0x02
Updated Program as below:
#include <stdio.h>
enum States {Start, Init, LED_Lit1} state;
unsigned char button;
#define button PINA & 0x01
void tick_LED()
{
switch(state) { // Transitions
case Start: // Start transition
state = Init;
break;
case Init: // Initial transition
if(button == 1){ // If PINA0 is 1 state goes to led_lit in LED_Lit1
state = LED_Lit1;
}
else { // If PINA0 is 0 state remains in initial
state = Init;
}
break;
case LED_Lit1: // LED_Lit1 Transition
if(button == 1){ // If PA0 is 1 state remains in LED_Lit1
state = LED_Lit1;
}
else {
state = Init; // if PA1 is 1 state goes to initial
}
break;
default:
state = Start;
break;
}
switch(state) { // Actions
case Start:
PORTB = 0x01; // PB0 set to 1
break;
case Init:
PORTB = 0x01; // PB0 set to 1
break;
case LED_Lit1:
PORTB = 0x02; // PB1 set to 1
break;
}
}
int main(void)
{
DDRA = 0x00; PORTA = 0xFF; // Configure port A's 8 pins as inputs
DDRB = 0xFF; PORTB = 0x00; // Configure port B's 8 pins as outputs,
state = Start;
while (1)
{
tick_LED();
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.