-1

Okay, so for the following code once the debugger enters the switch statement it transitions from

line 41 --> line 38 --> line 26

Then it oscillates between lines 38 and 26 perpetually. It doesn't even enter the first case statement which confuses me as I have been working on assignments until early morning. It shouldnt enter any other cases as PORTA are initialized to all zeros and the default should direct the debugger to the first case. You can even try it yourself and see what I mean.

#include <avr/io.h>


int main(void)
{
    DDRA = 0x00; //PORTA = 0x00;
    DDRB = 0xFF; //PORTB = 0x00;

    typedef enum {
        wtf, //wait for PA3 to be pressed
        wth, //wait for PA2 to be pressed
        dooropens, //yay
        }doorstate;

    doorstate state = wtf;

    while (1) 
    {
        switch(state) 
        {
            case wtf:
                if (PORTA == 0x04)  {
                    state = wth; 
                    PORTA = 0x00; //releases button
                }
                else 
                    state = wtf;  
                break;
            case wth:
                if (PORTA == 0x02)  
                    state = dooropens; 
                else 
                    state = wtf;  
                break; 
            case dooropens:
                PORTB = 0x01;
                if (PORTA == 0x80) {
                    state = wtf;
                    PORTB = 0x00;
                }
                break;
            default:
                state = wtf;
        }
    }
}


Here are the instructions if you need a better understanding of what it's supposed to do.

A household has a digital combination deadbolt lock system on the doorway. The system has buttons on a keypad. Button 'X' connects to PA0, 'Y' to PA1, and '#' to PA2. Pressing and releasing '#', then pressing 'Y', should unlock the door by setting PB0 to 1. Any other sequence fails to unlock. Pressing a button from inside the house (PA7) locks the door (PB0=0). For debugging purposes, give each state a number, and always write the current state to PORTC (consider using the enum state variable). Also, be sure to check that only one button is pressed at a time

Code4life
  • 95
  • 4

1 Answers1

0
PORTA == 0x04) 
{
state = wth; 
PORTA = 0x00; //releases button

Not sure you do what you are trying to do. You don't release a button with PORTA = 0x00

What do you think you'll be reading on PORTA if the state of 1 pin on PORTA changes?

  • change (PORTA == something) by (PORTA & something)

  • remove the line PORTA = 0x00; //releases button

Note:

  • DDRA = 0x00; //make port a as input

  • PORTA = 0x00; //disable pull-ups and make it tri state

Guillaume D
  • 2,202
  • 2
  • 10
  • 37