Exemplo n.º 1
0
int main() {
    
    //This function is necessary to use interrupts. 
    enableInterrupts();
    initSwitch1();
    initLEDs();
    
    while(1){
        switch (state){
            case led1:
                if(PORTDbits.RD7 == 0){
                    delayMs();
                    i=2;
                    wait2();
                    state = debounceRelease; 
                }
                break; 
            case led2:
                if(PORTDbits.RD7 == 0){
                    delayMs();
                    i=3;
                    wait2();
                    state = debounceRelease;
                }
                break; 
            case led3:
                if(PORTDbits.RD7 == 0){
                    delayMs();
                    i=1;
                    wait2();
                    state = debounceRelease;   
                }
                break; 
        }
        
        if(state == debounceRelease){
            if(PORTDbits.RD7 == 1){
                if(IFS0bits.T2IF == 1){ 
                i=i+1;
                if(i == 4) i=1; 
                turnOffTimer2();
                } 
                delayMs();
                if(i == 3){ 
                    turnOnLED(3);
                    state = led3;  
                }
                else if(i == 1){
                    turnOnLED(1);
                    state = led1;
                }
                else { 
                    turnOnLED(2);
                    state = led2; 
                }
            }       
        }
    }
    return 0;
}
Exemplo n.º 2
0
int main() {
    
    //This function is necessary to use interrupts. 
    enableInterrupts();
    
    //initialize the switch, all three LEDs, and the main timer
    initSwitch1();
    initLEDs();
    initTimer2();
    initTimer1();
    
    stateCurrent = wait; //set the starting state to no led's on
    
    while(1){
        if(PORTDbits.RD6 != 0){
            TMR1 = 0;
            T1CONbits.TON = 0;
            if(status == 1){
                status = 0;
                stateCurrent = stateNext;
            }
            switch(stateCurrent){
            case(wait):
                turnOnLED(0);
                stateNext = led1;
                break;
            case(led1):
                turnOnLED(1);
                stateNext = led2;
                break;
            case(led2):
                turnOnLED(2);
                stateNext = led3;
                break;
            case(led3):
                turnOnLED(3);
                stateNext = led1;
                break;
            default:
                stateCurrent = led1;
                break;
            }
        }
        else if(PORTDbits.RD6 == 0){
            if(status == 0){
                T1CONbits.TON = 1;
                status = PRESSED; //button was pressed
            }
        }
    }
    
    return 0;
}
Exemplo n.º 3
0
int main() {
    
    //This function is necessary to use interrupts. 
    enableInterrupts();
    
    //TODO: Write each initialization function
    initSwitch1();
    initLEDs();
    initTimer2();
    initTimer1();
    
    while(1){

        //TODO: Implement a state machine to create the desired functionality
        
    }
    
    return 0;
}
int main() {
    SYSTEMConfigPerformance(10000000);    //Configures low-level system parameters for 10 MHz clock
    enableInterrupts();                   //This function is necessary to use interrupts.

    //TODO: Write each initialization function
    initLEDs();
    initSwitch1();
    
    
    state = red;
    
    while(1){
        
        //TODO: Implement a state machine to create the desired functionality
       switch (state){
       
        case red:
            LATDbits.LATD0=ON;
            LATDbits.LATD1=OFF;
            LATDbits.LATD2=OFF;
            
            break;
        
        case yellow:
            LATDbits.LATD0=OFF;
            LATDbits.LATD1=ON;
            LATDbits.LATD2=OFF;
            break;
            
        case green:
            LATDbits.LATD0=OFF;
            LATDbits.LATD1=OFF;
            LATDbits.LATD2=ON;
         break;
    } 
        
        
    }
    
    return 0;
}
Exemplo n.º 5
0
int main() {
    //Create current LED placeholders
    int currLED = 1;

    //This function is necessary to use interrupts. 
    //enableInterrupts();

    //Initialize SW1
    initSwitch1();
    
    //Initialize all LEDs
    initLEDs();
    
   //Initialize Timer 2
    initTimer2();
    
    //Initialize Timer 1
    initTimer1();
    
    //Turn on LED 1 before starting the state machine
    turnOnLED(1);

    while (1) {
        
        //See state descriptions above
        switch (state) {

            case wait:

                if (SW1 == PRESSED) {
                    startTimer1();
                    state = debouncePress;
                }

                break;

            case debouncePress:

                delayMs(100);
                state = wait2;
                break;

            case wait2:
                if (IFS0bits.T1IF == FLAGUP){
                    stopTimer1();
                    state = debounceRelease2;
                }
                if (SW1 == RELEASED) {
                   stopTimer1();
                   state = debounceRelease;

                }
                break;

            case debounceRelease:

                delayMs(100);
                if (currLED == 1) {
                    state = led2;

                } else if (currLED == 2) {
                    state = led3;

                } else if (currLED == 3) {
                    state = led1;

                }
                break;

            case debounceRelease2:

                if (SW1 == RELEASED) {
                    delayMs(100);
                    if (currLED == 1) {
                        state = led3;

                    } else if (currLED == 2) {
                        state = led1;

                    } else if (currLED == 3) {
                        state = led2;

                    }
                }
                break;


            case led1:
              
                turnOnLED(1);
                turnOffLED(currLED);

                currLED = 1;
                state = wait;

                break;

            case led2:
               
                turnOnLED(2);
                turnOffLED(currLED);

                currLED = 2;
                state = wait;

                break;

            case led3:
           
                turnOnLED(3);
                turnOffLED(currLED);

                currLED = 3;
                state = wait;

                break;
        }
    }

    return 0;
}
Exemplo n.º 6
0
int main() {
    //This function is necessary to use interrupts.
    enableInterrupts();

    //TODO: Write each initialization function
    initSwitch1();
    initLEDs();
    //initTimer2();
    initTimer1();
    int led;
    int i;
    while(1) {
        switch(state)
        {
        //init case
        case init:
            led = 0;
            turnOnLED(0);//turn the top LED on
            state = waitPush;//move on to the next state
            break;
        //waitPush state
        case waitPush:
            if(PORTDbits.RD6 == 0)//check if the switch is pushed
            {
                //for(i=0;i<10000;i++);
                TMR1CLR = 0xFFFF;; // Clear the timer interrupt flag
                state = waitRelease;
                T1CONbits.TON = 1;//enable timer
                IFS0bits.T1IF = 0;//put the flag down
            }
            else state = waitPush;//stay in waitPush if the switch is not pushed
            break;
        //waitRelease state
        case waitRelease:
            if(PORTDbits.RD6 == 1)//check if the switch is pushed
            {
                if(IFS0bits.T1IF == 1)//check if the flag is on
                {
                    state = prevLED;//move to the previous state if the flag is on when hold more than 2 seconds
                    T1CONbits.TON = 0;//clear the timer when not in use
                }
                else {
                    state = nextLED;//move to the next state
                    T1CONbits.TON = 0;//clear the timer when not in use
                }
            }
            else {
                state = waitRelease;//move to waitRelease
            }
            break;
        //go back to previous LED state
        case prevLED:
            if(led == 0)led=2;
            else if(led == 1)led=0;
            else if(led == 2)led=1;

            turnOnLED(led);
            IFS0bits.T1IF = 0;
            state = waitPush;
            T1CONbits.TON = 0;
            break;
        //go to next LED state
        case nextLED:
            if(led==0) led=1;
            else if(led == 1)led=2;
            else if (led == 2) led=0;

            turnOnLED(led);
            IFS0bits.T1IF = 0;
            state = waitPush;
            T1CONbits.TON = 0;
            break;

        default:
            break;
        }
    }

    return 0;
}
Exemplo n.º 7
0
int main() {
    //This function is necessary to use interrupts. 
    enableInterrupts();
    state = led1;
    //TODO: Write each initialization function
    initSwitch1();
    initLEDs();
    initTimer2();
    initTimer1();
    //This 2 lines stop the timer 1 and reset it, so at the start of the FSM it's equal to 0
    T1CONbits.TON = 0; //Turn the timer 1 OFF   
    IFS0bits.T1IF = 0; //Put the flag down
    
    while (1) {

        //TODO: Implement a state machine to create the desired functionality
        switch (state) {

            case led1:                     //Turn on Led1, and changes state when the button is pressed
                turnOnLED(1);
                if(SW1 == PRESSED) {
                    state = debouncePress;
                }
                break;

            case led2:                  //Turn on Led2, and changes state when the button is pressed
                turnOnLED(2);
                if (SW1 == PRESSED) {
                    state = debouncePress;
                }
                break;

            case led3:                  //Turn on Led3, and changes state when the button is pressed
                turnOnLED(3);
                if (SW1 == PRESSED) {
                    state = debouncePress;
                }
                break;

            case timer:                //Wait until the button is released and based on the timer
                                       // it either goes to debounceRelease or debounceRelease2;
                if ((SW1 == RELEASED) && (IFS0bits.T1IF == 0)) {
                    state = debounceRelease;
                    IFS0bits.T1IF = 0; //Put the flag down
                    T1CONbits.TON = 0; //Turn the timer 1 OFF
                }

                if ((SW1 == RELEASED) && (IFS0bits.T1IF == 1)) {
                    state = debounceRelease2;
                    IFS0bits.T1IF = 0; //Put the flag down
                    T1CONbits.TON = 0; //Turn the timer 1 OFF
                }
                break;

            case debouncePress:     //Debounces for 20ms , reset and start timer 1
                delayMs(20);
                state = timer;
                TMR1 = 0;           //Clear the timer 1
                T1CONbits.TON = 1;  //Turn the timer 1 ON
                break;

            case debounceRelease:   //Debounces for 20ms, and depending on the led that is on
                delayMs(20);        //it changes state to the next one
                if (LATDbits.LATD0 == ON) {
                    state = led2;
                }
                if (LATDbits.LATD1 == ON) {
                    state = led3;
                }
                if (LATDbits.LATD2 == ON) {
                    state = led1;
                }
                break;

            case debounceRelease2:      //Debounces for 20ms, and depending on the led that is on
                delayMs(20);            //it changes state to the next one (going backwards)
                if (LATDbits.LATD0 == ON) {
                    state = led3;
                }
                if (LATDbits.LATD1 == ON) {
                    state = led1;
                }
                if (LATDbits.LATD2 == ON) {
                    state = led2;
                }
                break;
        }
    }

    return 0;
}
Exemplo n.º 8
0
int main() {
    SYSTEMConfigPerformance(10000000);    //Configures low-level system parameters for 10 MHz clock
    enableInterrupts();                   //This function is necessary to use interrupts.

    //TODO: Write each initialization function--done!
    initLEDs();
    //initTimer1();
   initTimer2();
    initSwitch1();
    
    
    while(1){         
        switch(state)
       {
                case led1:
                    //state=2;
                    if(up==1)
                    {
                        state=led3;
                    }
                    else if(switch1==1)
                    {
                        state=led2;
                    }
                    
                    up=0;
                    switch1=0;
                     delayMs(10);
                    break;
                
                case led2:
                    //state=3;
                    if(up==1)
                    {
                        state=led1;
                    }
                    else if(switch1==1)
                    {
                        state=led3;
                    }
                    
                    up=0;
                    switch1=0;
                      delayMs(10);
                    break;
                     
                case led3:
                    //state=1;
                    if(up==1)
                    {
                        state=led2;
                    }
                    else if(switch1==1)
                    {
                        state=led1;
                    }
                    
                    up=0;
                    switch1=0;
                       delayMs(10);
                    break;
        }
          switch(state)
          {
            case led1:
                 LATDbits.LATD0=1;
                    LATDbits.LATD1=0;
                    LATDbits.LATD2=0;
                    
                    break;
            case led2:
                LATDbits.LATD0=0;
                    LATDbits.LATD1=1;
                    LATDbits.LATD2=0;
                    
                    break;
            case led3:
                 LATDbits.LATD0=0;
                    LATDbits.LATD1=0;
                    LATDbits.LATD2=1;
                    
                    break;
          }
              
        
        
    }
    
    return 0;
}
Exemplo n.º 9
0
int main() {
    
    SYSTEMConfigPerformance(10000000);    //Configures low-level system parameters for 10 MHz clock
    enableInterrupts();                   //This function is necessary to use interrupts.
    initLEDs();                           //Initialize LED
    initTimer1();                         //Initialize Timer
    initSwitch1();                        //Initialize dem switches yo  
    
    while(1){                             //Finite State Machine Implementation
        switch (state){
                case init:
                    led = 1;
                    turnOnLED(1);
                    break;
                    
                case nextLED:
                    if (led == 1)
                    {
                        led = 2;
                    }
                    
                    else if (led == 2)
                    {
                        led = 3;
                    }
                    
                    else if (led == 3)
                    {
                        led = 1;
                    }
                    
                    turnOnLED(led);
                    IFS0bits.T1IF = 0;
                    T1CONbits.TON = 0;
                    break;
                    
                case previousLED:
                     if (led == 1)
                    {
                        led = 3;
                    }
                    
                    else if (led == 2)
                    {
                        led = 1;
                    }
                    
                    else if (led == 3)
                    {
                        led = 2;
                    }
                    
                    turnOnLED(led);
                    IFS0bits.T1IF = 0;
                    T1CONbits.TON = 0;
                    break;
                    
                case waitForButtonPush:
                    break;
                    
                case waitForButtonRelease:
                    break;
        }    
    }
    return 0;
}
Exemplo n.º 10
0
int main() {
    SYSTEMConfigPerformance(10000000);
    enableInterrupts();
    initSwitch1();
    initLEDs();
    initTimer1();
    initTimer2();
     int PrevState=0;  
    while(1)
    {
            switch(state){
              case  led1:
                    LED1=ON;
                    LED2=OFF;
                    LED3=OFF;
                    PrevState=1;
                  if(PORTDbits.RD6==0)
                  {
                      state=debouncePress;
                  }
                    break;
               case led2:
                    LED1=OFF;
                    LED2=ON;
                    LED3=OFF;
                    PrevState=2;
                 if(PORTDbits.RD6==0)
                  {
                      state=debouncePress;
                  }
                    break;
               case led3:
                    LED1=OFF;
                    LED2=OFF;
                    LED3=ON;
                    PrevState=3;
                if(PORTDbits.RD6==0)
                  {
                      state=debouncePress;
                  }
                    break;
     
              case debouncePress:
                  delayMs(10);
                  state=wait;
                  break;
    
              case debounceRelease:
                  delayMs(10);
                  state=wait2;
                  break;
   
                case wait:
                    if(PORTDbits.RD6==0)
                    {
                        state=wait;
                    }
                    else
                    {
                        state=debounceRelease;
                    }
                    break;
                case wait2:
                    if (longerthan==1)
                    {
                        if(PrevState==1)
                        {
                            longerthan=0;
                            TMR1=0;
                            state=led3;
                        }
                        else if(PrevState==2)
                        {
                            longerthan=0;
                            TMR1=0;
                            state=led1;
                        }
                        else if(PrevState==3)
                        {
                            longerthan=0;
                            TMR1=0;
                            state=led2;
                        }
                    }
                    else
                    {
                        if(PrevState==1)
                        {
                            longerthan=0;
                            TMR1=0;
                            state=led2;
                        }
                        else if(PrevState==2)
                        {
                            longerthan=0;
                            TMR1=0;
                            state=led3;
                        }
                        else if(PrevState==3)
                        {   
                            longerthan=0;
                            TMR1=0;
                            state=led1;
                        }
                    }
                    break;
            }
        }
    
    
    return 0;
}