mercredi 5 octobre 2016

C if else brackets

Good evening. So here's my C code. The idea behind my code is to press button1 for a (nickel) then LED1 is suppose to light up. I press button2 for a (dime) then LED2 lights up, etc. The problem I believe is else statements or the brackets. I looked up nested else statements and I tried placing appropriate brackets as suggested online. Like, am I thinking about this right? Last week I was working on assembly language. This week I am working on C. Any helpful input is appreciated. Thank you

int main()
{
    int button_in = 0;
    DeviceInit();
    DelayInit();

while(1)
{
    button_in = PORTReadBits (IOPORT_A, BIT_6|BIT_7);
    if (button_in != 0)
    {
        if ((button_in & 0x0040) != 0){ //1. Press button1. State goes to 001. 
        PORTWrite (IOPORT_B, BIT_10);
        DelayMs(10);}
        {
            if ((button_in & 0x0040) != 0) //1a. Press button1. State goes to 010
            PORTWrite (IOPORT_B, BIT_11);
            DelayMs(10);
            {
                if ((button_in & 0x0040) != 0) //1aa. Press button1. State goes to 011
                PORTWrite (IOPORT_B, BIT_10|BIT_11);
                DelayMs(10);
                {
                    if ((button_in & 0x0040) != 0) //1aaa. Press button1. State goes to 100
                    PORTWrite (IOPORT_B, BIT_12);
                    DelayMs(10);
                    {
                        if ((button_in & 0x0040) != 0) //1aaaa. Press button1. State goes to 1000
                        PORTWrite (IOPORT_B, BIT_13);
                        DelayMs(10);
                        {
                        else
                            {
                                ((button_in & 0x0080) != 0) //1aaab. Press button2. State goes to 1000
                                PORTWrite (IOPORT_B, BIT_13);
                                DelayMs(10);
                                PORTWrite (IOPORT_B, BIT_10|BIT_11|BIT_12); //State goes to 0111 to dispense change
                                DelayMs(10); //We may need to set for 0000?
                                }}}
                        else
                        {
                            ((button_in & 0x0080) != 0) //1aab. Press button2. State goes to 1000 
                            PORTWrite (IOPORT_B, BIT_13);
                            DelayMs(10);}}
                else
                {
                    if ((button_in & 0x0080) != 0) //1ab. Press button2. State goes to 100
                    PORTWrite (IOPORT_B, BIT_12);
                    DelayMs(10);
                        if ((button_in & 0x0040) != 0) //1aba. Press button1. State goes to 1000
                        PORTWrite (IOPORT_B, BIT_13);
                        DelayMs(10);
                            else
                            {
                                ((button_in & 0x0080) != 0) //1abb. Press button2. State goes to 1000
                                PORTWrite (IOPORT_B, BIT_13);
                                DelayMs(10);
                                PORTWrite (IOPORT_B, BIT_10|BIT_11|BIT_12); //State goes to 0111 to dispense change
                                DelayMs(10); //We may need to set for 0000?
                                }}}                  
            else
            {
                if((button_in & 0x0080) != 0) //1b. Press button2. State goes to 011
                PORTWrite (IOPORT_B, BIT_10| BIT_11);
                DelayMs(10);
                {
                    if ((button_in & 0x0040) != 0) //1ba. Press button1. State goes to 100
                    PORTWrite (IOPORT_B, BIT_12);
                    DelayMs(10);
                    {                            
                        if ((button_in & 0x0040) != 0) //1baa. Press button1. State goes to 1000
                        PORTWrite (IOPORT_B, BIT_13);
                        DelayMs(10);
                        {
                            else
                            {
                                ((button_in & 0x0080) != 0) //1bab. Press button2. State goes to 1000
                                PORTWrite (IOPORT_B, BIT_13);
                                DelayMs(10);
                                PORTWrite (IOPORT_B, BIT_10|BIT_11|BIT_12); //State goes to 0111 to dispense change
                                DelayMs(10); //We may need to set for 0000?
                                }}}
                        else
                        {
                            ((button_in & 0x0080) != 0) //1bb. Press button2. State goes to 1000
                            PORTWrite (IOPORT_B, BIT_13);
                            DelayMs(10);}}}}
        else
        {
            if ((button_in & 0x0080) != 0) //2. Press button2. State goes to 010
            PORTWrite (IOPORT_B, BIT_11);
            DelayMs(10);
            {
                if ((button_in & 0x0040) != 0) //2a. Press button1. State goes to 011
                PORTWrite (IOPORT_B, BIT_10);
                DelayMs(10);
                {
                    if ((button_in & 0x0040) != 0) //2aa. Press button1. State goes to 100
                    PORTWrite (IOPORT_B, BIT_12);
                    DelayMs(10);
                    {
                        if ((button_in & 0x0040) != 0) //2aaa. Press button1. State goes to 1000
                        PORTWrite (IOPORT_B, BIT_13);
                        DelayMs(10);
                        {    else
                            {
                            ((button_in & 0x0080) != 0) //2aab. Press button2. State goes to 1000
                            PORTWrite (IOPORT_B, BIT_13);
                            DelayMs(10);
                            PORTWrite (IOPORT_B, BIT_10|BIT_11|BIT_12); //State goes to 0111 to dispense change
                            DelayMs(10); //We may need to set for 0000?
                        }}}
                    else
                    {
                        ((button_in & 0x0080) != 0) //2ab. Press button2. State goes to 1000
                        PORTWrite (IOPORT_B, BIT_13);
                        DelayMs(10);}}

                else
                {
                    if((button_in & 0x0080) != 0) //2b. Press button2. State goes to 100
                    PORTWrite (IOPORT_B, BIT_10);
                    DelayMs(10);
                    {
                        if ((button_in & 0x0040) != 0) //2ba. Press button1. State goes to 1000
                        PORTWrite (IOPORT_B, BIT_13);
                        DelayMs(10);

                        else{
                        ((button_in & 0x0080) != 0) //2bb. Press button2. State goes to 1000
                        PORTWrite (IOPORT_B, BIT_13);
                        DelayMs(10);
                        PORTWrite (IOPORT_B, BIT_10|BIT_11|BIT_12); //State goes to 0111 to dispense change
                        DelayMs(10); //We may need to set for 0000?
                        }}}}}}
    DelayMs(100);
    PORTCLearBits(IOPORT_B, BIT_10|BIT_11|BIT_12|BIT_13);
 //Add Breakpoint here
}
}

Aucun commentaire:

Enregistrer un commentaire