int main (void)
{
	uint16_t heading;
	int16_t pitch, roll;
	uint16_t headingIntegerPart, headingFractionPart;
	int16_t pitchIntegerPart, pitchFractionPart;
	int16_t rollIntegerPart, rollFractionPart;

	// initialize the hardware stuff we use
	InitTimer ();
	InitUART ();
	i2c_init ();

	fdevopen (UART0_PutCharStdio, UART0_GetCharStdio);

	// set the LED port for output
	LED_DDR |= LED_MASK;

	printf ("\nHoneywell HMC6343 I2C Compass Test\n\n");

	// Flash the LED for 1/2 a second...
	turnOnLED ();
	ms_spin (500);
	turnOffLED ();

	while (1)	// outer loop is once every 250 ms (more or less)
	{
		// send the HEADING command to the compass
		i2c_start_wait (COMPASS_ADDRESS_WRITE);
		i2c_write (COMPASS_HEADING_COMMAND);

		// now read the response
		i2c_rep_start (COMPASS_ADDRESS_READ);
		heading = (i2c_readAck () * 256) + i2c_readAck ();
		pitch = (i2c_readAck () * 256) + i2c_readAck ();
		roll = (i2c_readAck () * 256) + i2c_readNak ();
		i2c_stop ();

		headingIntegerPart = heading / 10;
		headingFractionPart = heading - (headingIntegerPart * 10);
		pitchIntegerPart = pitch / 10;
		pitchFractionPart = pitch - (pitchIntegerPart * 10);
		if (pitchFractionPart < 0)
			pitchFractionPart *= -1;
		rollIntegerPart = roll / 10;
		rollFractionPart = roll - (rollIntegerPart * 10);
		if (rollFractionPart < 0)
			rollFractionPart *= -1;

		printf ("Heading: %3d.%1d   Pitch: %3d.%1d   Roll: %3d.%1d\n", headingIntegerPart, headingFractionPart, pitchIntegerPart, pitchFractionPart, rollIntegerPart, rollFractionPart);

		turnOnLED ();
		ms_spin (100);
		turnOffLED ();
		ms_spin (150);
	}

	// we'll never get here...
	return 0;
}
Beispiel #2
0
int main() {
    SYSTEMConfigPerformance(10000000);    //Configures low-level system parameters for 10 MHz clock
    enableInterrupts();                   //This function is necessary to use interrupts.

    initLEDs();
    initTimer1();
    
    while(1){
        switch(state) {
            case INIT:
                next_state = LED_0;
                break;
            case LED_0:
                turnOnLED(0);
                next_state = LED_1;
                break;
            case LED_1:
                turnOnLED(1);
                next_state = LED_2;                
                break;
            case LED_2:
                turnOnLED(2);
                next_state = LED_0;
                break;
        }
    }
    
    return 0;
}
Beispiel #3
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;
}
Beispiel #4
0
static void blinkNumLockLED(void) {
	static int counter = 0;
	const int countMAX = 100;
	//on off on off
	if(ledBlinkNumLockCount > 0){
		counter++;
		if(counter > countMAX){
			if(ledBlinkNumLockCount == 5 || ledBlinkNumLockCount == 3){ 
				turnOnLED(LEDNUM); //PORTLEDS |= (1 << LEDCAPS);
			}else if(ledBlinkNumLockCount == 4 || ledBlinkNumLockCount == 2){ 
				turnOffLED(LEDNUM); //PORTLEDS &= ~(1 << LEDCAPS);
			}else{
				if(isBeyondFnLedEnabled()){
					if(isBeyondFN()){
						turnOnLED(LEDNUM);
					}
				}else{
					if((getLEDState() & LED_STATE_NUM)){
						turnOnLED(LEDNUM);
					}
				}
			}
			counter = 0;

			ledBlinkNumLockCount--;
		}
	}else{
		counter = 0;
	}
}
Beispiel #5
0
void setLEDIndicate(void) {
	static bool prevNum = false;

	if(isBeyondFnLedEnabled()){
		ledBlinkNumLockCount = getLedBlnik(LED_STATE_NUM, isBeyondFN(), &prevNum);
	}else{
		if (LEDstate & LED_STATE_NUM) { // light up num lock
	        turnOnLED(LEDNUM);//PORTLEDS |= (1 << LEDNUM);	//
	    } else {
	        turnOffLED(LEDNUM);//PORTLEDS &= ~(1 << LEDNUM);	//
	    }
	}

    if (LEDstate & LED_STATE_CAPS) { // light up caps lock
        turnOnLED(LEDCAPS); //PORTLEDS |= (1 << LEDCAPS);	// 
    } else {
        turnOffLED(LEDCAPS); //PORTLEDS &= ~(1 << LEDCAPS);	//
    }

#ifdef LEDSCROLL
    if (LEDstate & LED_STATE_SCROLL) { // light up scroll lock
        turnOnLED(LEDSCROLL); //PORTLEDS |= (1 << LEDCAPS);	// 
    } else {
        turnOffLED(LEDSCROLL); //PORTLEDS &= ~(1 << LEDCAPS);	//
    }
#endif

#ifndef SCROLL_LOCK_LED_IS_APART   
	static bool prevScroll = false; 
	ledBlinkScrollLockCount = getLedBlnik(LED_STATE_SCROLL, (LEDstate & LED_STATE_CAPS), &prevScroll);
#endif
	
}
Beispiel #6
0
int main(void)
{
    SYSTEMConfigPerformance(10000000);
    initTimer1();
    initSW2();
    initLEDs();
    enableInterrupts();
    state = led1;

    while(1)
    {
        //TODO: Using a finite-state machine, define the behavior of the LEDs
        //Debounce the switch
        switch(state) {
        case led1:
            turnOnLED(1);
            stateNext = led2;
            break;

        case led2:
            turnOnLED(2);
            stateNext = led1;
            break;

        case deBounce1:
            delayUs(10000);
            break;
        case deBounce2:
            delayUs(10000);
            break;
        }
    }

    return 0;
}
Beispiel #7
0
void setLed(uint8_t xLed, bool xBool){
	if(xBool){
		if(xLed == LED_STATE_NUM){
			turnOnLED(LEDNUM);
		}else if(xLed == LED_STATE_CAPS){
			turnOnLED(LEDCAPS);
		}
#ifdef LEDSCROLL
		else if(xLed == LED_STATE_SCROLL){
			turnOnLED(LEDSCROLL);
		}
#endif
	}else{
		if(xLed == LED_STATE_NUM){
			turnOffLED(LEDNUM);
		}else if(xLed == LED_STATE_CAPS){
			turnOffLED(LEDCAPS);
		}
#ifdef LEDSCROLL
		else if(xLed == LED_STATE_SCROLL){
			turnOffLED(LEDSCROLL);
		}
#endif
	}
}
Beispiel #8
0
static void blinkScrollLockLED(void) {
	static int counter = 0;
	const int countMAX = 100;
	//on off on off
	if (ledBlinkScrollLockCount > 0) {
		counter++;
		if (counter > countMAX) {
			if (ledBlinkScrollLockCount == 5 || ledBlinkScrollLockCount == 3) {
				turnOnLED(LEDCAPS); //PORTLEDS |= (1 << LEDCAPS);
			} else if (ledBlinkScrollLockCount == 4
					|| ledBlinkScrollLockCount == 2) {
				turnOffLED(LEDCAPS); //PORTLEDS &= ~(1 << LEDCAPS);
			} else {
				if ((getLEDState() & LED_STATE_CAPS)) {
					turnOnLED(LEDCAPS);
				}
			}
			counter = 0;

			ledBlinkScrollLockCount--;
		}
	} else {
		counter = 0;
	}
}
void ledSM(LedData *data)
{
    switch(data->state)
    {
        case LED_OFF:
            turnOffLED(PORTG, LED3);
            if(msg == CHANGE_MODE)
            {
                data->state = LED_BLINKING_ON;
                msg = DO_NOTHING;
            }
            break;
        case LED_ON:
            turnOnLED(PORTG, LED3);
            if(msg == CHANGE_MODE)
            {
                data->state = LED_OFF;
                msg = DO_NOTHING;
            }
            break;
        case LED_BLINKING_ON:
            turnOnLED(PORTG, LED3);
            if(msg == CHANGE_MODE)
            {
                data->state = LED_ON;
                msg = DO_NOTHING;
            }
            else
            {
                if(isTimerExpire(FIVE_HUND_MILISEC, &data->time))
                    data->state = LED_BLINKING_OFF;
            }
            break;
        case LED_BLINKING_OFF:
            turnOffLED(PORTG, LED3);
            if(msg == CHANGE_MODE)
            {
                data->state = LED_ON;
                msg = DO_NOTHING;
            }
            else
            {
                if(isTimerExpire(FIVE_HUND_MILISEC, &data->time))
                    data->state = LED_BLINKING_ON;
            }
            break;
        default :
			#ifdef TEST
            printf("Error: Unknown state encounter in LedSM: %d\n", data->state);
			#endif
            break;
    }
}
Beispiel #10
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;
}
Beispiel #11
0
void __ISR(_CHANGE_NOTICE_VECTOR, IPL7SRS) _CNInterrupt(){    //interrupt service routines that manage the change of states
    IFS1bits.CNDIF = 0;
    int i;
    i = PORTD;
    if (SW1 == NOTPRESSED){
        turnOnLED(1);
    }
    
    else{
        turnOnLED(0);
    }
}
Beispiel #12
0
updateLEDState()
{
    switch (currentLED)
    {
            case RUN: //turn on led1, turn off led2
                turnOnLED(1);
                currentLED=STOP;
            break;
            case STOP: //turn off led1, turn on led2
                turnOnLED(2);
                currentLED=RUN;
            break;
    }
}
Beispiel #13
0
//State Diagram for LED1
void SDLED1(State *state, int *previousTime)	{

	switch (*state)
	{
		case INITIAL:
			turnOffLED();
			*state = ON_LED1;
			break;

		case ON_LED1:
			if(getCurrentTime() - *previousTime > ONE_HUNDRED_TWENTY_MILI_SEC)	{
				*previousTime = getCurrentTime();
				turnOnLED();
				*state = OFF_LED1;
			}
			break;

		case OFF_LED1:
			if(getCurrentTime() - *previousTime > ONE_HUNDRED_TWENTY_MILI_SEC)	{
				*previousTime = getCurrentTime();
				turnOffLED();
				*state = FINAL;
			}
			break;

		case FINAL:
			*state = ON_LED1;
			break;

		default:
			break;
	}
}
Beispiel #14
0
int main(void)
{
    //Initialize new interrupt fix
    SYSTEMConfigPerformance(40000000);
    
   
#ifdef part1
   initSW(); 
   initLED(RUN_LED);
   initLED(STOP_LED);
   initTimer2();
   enableInterrupts();
   
   // initialize the lights         
   state = runToggle; 
   turnOffLED(STOP_LED);
   turnOnLED(RUN_LED);
   
    while(1){
        
        switch(state){
            // the state that toggles the LEDs 
            case runToggle:
                // switch the led's
                toggleAllLEDs();
                prevState = runToggle;
                
                state = waitForPress; //Go to debounce press state
                break;
                
            // wait for user input i.e. button press
            case waitForPress:
                
                while (state == waitForPress);
                
                break;
                
            // once the button has been pressed 
            case dbPress:
                
                delayUs(DBdelayTime); // Delay for 5ms
              
                while(state == dbPress );
               
                break;
            
            // once the button has been released 
            case dbRelease:
                
                delayUs(DBdelayTime); //Delay for 5ms

                 state = runToggle;
                 
                break;
        } 
    }
#endif
    return 0;
}
Beispiel #15
0
static void blinkCapsLockLED(void) {
	static int gCounter = 0;
	static int gDelayCounter = 0;
	static uint8_t gLEDState = 1;
	const uint8_t gCountMAX = 200;
	uint8_t led = LEDCAPS;
	uint8_t gIsOn = 0;

#ifdef ENABLE_BOOTMAPPER
	if(isBootMapper()){
		gIsOn = 1;
	}
#endif	

	if(gIsOn == 1){
		++gDelayCounter;
		if(gDelayCounter > blinkLedCountDelay){
			gCounter++;
			if(gCounter > gCountMAX){
				if(getLEDState() & LED_STATE_CAPS){	// Caps Lock이 켜져 있을때는 커졌다 켜지고;
					if(gLEDState == 1){
						turnOffLED(led);
					}else{
						turnOnLED(led);
						gDelayCounter = 0;
					}
				}else{	// Caps Lock이 꺼져 있을 때는 켜졌다 꺼진다.
					if(gLEDState == 1){
						turnOnLED(led);
					}else{
						turnOffLED(led);
						gDelayCounter = 0;
					}
				}
				gLEDState ^= 1;
				gCounter = 0;
			}
		}
	}else{
		gCounter = 0;
		gLEDState = 1;
	}
}
Beispiel #16
0
void changelight(char buffer[4]){
	
	if(buffer[0] == '!' || buffer[0] == '?'){
		if(buffer[2] == '0'){
			turnOffLED(buffer[1]);			
		}
		else if(buffer[2] == 'f'){
			turnOnLED(buffer[1]);			
		}		
	}	
}
Beispiel #17
0
void setLEDIndicate(void) {
	static uint8_t prevLEDstate;

	if (getBeyondFnLed() == BEYOND_FN_LED_NL) {
		getLedBlink(LED_STATE_NUM, getBeyondFN(), &prevLEDstate, &ledBlinkCount);
	} else {
		if (LEDstate & LED_STATE_NUM) { // light up num lock
			turnOnLED(LEDNUM); //PORTLEDS |= (1 << LEDNUM);	//
		} else {
			turnOffLED(LEDNUM); //PORTLEDS &= ~(1 << LEDNUM);	//
		}
	}

	if (LEDstate & LED_STATE_CAPS) { // light up caps lock
		turnOnLED(LEDCAPS); //PORTLEDS |= (1 << LEDCAPS);	//
	} else {
		turnOffLED(LEDCAPS); //PORTLEDS &= ~(1 << LEDCAPS);	//
	}

#ifdef LEDSCROLL
	if (getBeyondFnLed() == BEYOND_FN_LED_SL) {
		getLedBlink(LED_STATE_SCROLL, getBeyondFN(), &prevLEDstate, &ledBlinkCount);
	} else {
		if (LEDstate & LED_STATE_SCROLL) { // light up scroll lock
			turnOnLED(LEDSCROLL);//PORTLEDS |= (1 << LEDCAPS);	//
		} else {
			turnOffLED(LEDSCROLL); //PORTLEDS &= ~(1 << LEDCAPS);	//
		}
	}
#endif

#ifndef SCROLL_LOCK_LED_IS_APART   
	getLedBlink(LED_STATE_SCROLL, (LEDstate & LED_STATE_CAPS), &prevLEDstate, &ledBlinkScrollLockCount);
#endif

	prevLEDstate = LEDstate;

}
Beispiel #18
0
void buttonAndLED(TaskState *self){
	startCoroutine();
	while(1){
		turnOffLED();
		while(isButtonPressed()==FALSE){
			yield();
		}
		turnOnLED();
		while(isButtonPressed()==TRUE){
			yield();
		}
		while(isButtonPressed()==FALSE){
			yield();
		}
		endCoroutine();
	}
}
//State Diagram LED3
int LED3_SM(State state){
	static uint32_t timer = 0;
	int button = pressedButton();
	static uint32_t previousTime = 0;
	if(timer != 5){
		if(button == GPIO_PIN_SET)
			timer = 0;
	}else if(timer == 5){
		if(button == GPIO_PIN_SET)
			return 0;
	}
	switch(state){
		case INITIAL:
			initRearLED1();
			state = STATEA;
		break;
		case STATEA:
			if(timer != 4){
				if(getCurrentTime()-previousTime >  two_hundred_millisec){
					turnOnLED(PORTB,LED1);
					previousTime = getCurrentTime();
					state = STATEB;
					timer ++ ;
				}
			}
		break;
		case STATEB:
			if(timer==5){
				turnOffLED(PORTB,LED1);
			}else{
			if(getCurrentTime()-previousTime >  two_hundred_millisec){
				turnOffLED(PORTB,LED1);
				previousTime = getCurrentTime();
				state = FINAL;
			}
			}
		break;
		case FINAL:
			state = INITIAL;
		break;
		}
		return state;
}
Beispiel #20
0
void blinkOnce(const int xStayMs){
	if(isBeyondFnLedEnabled()){
		if (isBeyondFN()) { // light up num lock on FN2 toggle
	        turnOffLED(LEDNUM);//PORTLEDS |= (1 << LEDNUM);	//
	    } else {
	        turnOnLED(LEDNUM);//PORTLEDS &= ~(1 << LEDNUM);	//
	    }
	}else{
		if (LEDstate & LED_STATE_NUM) { // light up num lock
	        turnOffLED(LEDNUM);//PORTLEDS &= ~(1 << LEDNUM);	//
	    }else{
	        turnOnLED(LEDNUM);//PORTLEDS &= ~(1 << LEDNUM);	//    	
	    }
	}
    if (LEDstate & LED_STATE_CAPS) { // light up caps lock
        turnOffLED(LEDCAPS); //PORTLEDS &= ~(1 << LEDCAPS);	//
    } else {
        turnOnLED(LEDCAPS); //PORTLEDS |= (1 << LEDCAPS);	// 
    }
	/*
		_delay_ms()에 xStayMs를 인자로 넣으면 hex 파일의 용량이 0x1000가량 증가한다. 
		매뉴얼 펑션으로 _delay_ms(1)을 ms 만큼 루프시키도록 만들어서 사용;
	*/
	__delay_ms(xStayMs);	
	
	if(isBeyondFnLedEnabled()){
		if (isBeyondFN()) { // light up num lock on FN2 toggle
	        turnOnLED(LEDNUM);//PORTLEDS |= (1 << LEDNUM);	//
	    } else {
	        turnOffLED(LEDNUM);//PORTLEDS &= ~(1 << LEDNUM);	//
	    }
	}else{
		if (LEDstate & LED_STATE_NUM) { // light up num lock
	        turnOnLED(LEDNUM);//PORTLEDS &= ~(1 << LEDNUM);	//   
	    }else{
	        turnOffLED(LEDNUM);//PORTLEDS &= ~(1 << LEDNUM);	// 	
	    }
	}
    if (LEDstate & LED_STATE_CAPS) { // light up caps lock
        turnOnLED(LEDCAPS); //PORTLEDS |= (1 << LEDCAPS);	// 
    } else {
        turnOffLED(LEDCAPS); //PORTLEDS &= ~(1 << LEDCAPS);	//
    }	
}
Beispiel #21
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;
}
Beispiel #22
0
void TunRun_JoyStk()
{
	
	switch(TRJ_state) //transitions
	{
		case TRJ_START:
		TRJ_state = TRJ_WAIT; break;
		
		case TRJ_WAIT:
		if((~PINB & 0x01) == 0x00)
		{
			TRJ_state = TRJ_WAIT; break;
		}
		else
		{
			TRJ_state = TRJ_PLAY; break;
		}
		
		case TRJ_PLAY:
		if(QUIT == 1 || TR_LOSE == 1)
		{
			TRJ_state = TRJ_PAUSE; break;
		}
		else
		{
			TRJ_state = TRJ_PLAY; break;
		}
		
		case TRJ_PAUSE:
		if((~PINB & 0x01) == 0x00)
		{
			TRJ_state = TRJ_WAIT; break;
		}
		else
		{
			TRJ_state = TRJ_PAUSE; break;
		}
		
		default:
		break;
	}
	
	switch(TRJ_state) //state actions
	{
		case TRJ_START:
		break;
		
		case TRJ_WAIT:
		break;
		
		case TRJ_PLAY:
		convert();
		TR_TEMP = ADC;
		if(TR_TEMP > 600)
		{
			if(Y1 <= 0);
			
			else
			{
				turnOffLED(7, Y1);
				turnOffLED(7, y2);
				--Y1;
				--y2;
			}
		}
		else if(TR_TEMP < 350)
		{
			if(y2 >= 7);
			
			else
			{
				turnOffLED(7, Y1);
				turnOffLED(7, y2);
				++Y1;
				++y2;
			}
			
		}
		turnOnLED(7, Y1);
		turnOnLED(7, y2);
		break;
		
		case TRJ_PAUSE:
		break;
		
		default:
		break;
		
		
	}
}
void SM_LED(State *state,int LED,int *Time)
{
	static int currentTime[4] = {} ;
	static int counter = 0, triggered = 0 ;
	int UserInput = 0;

	switch(*state)
	{
			case START :
							turnOffLED(LED);
							if (LED == LED3)
								*state = LED_OFF ; //LED3 doesn;t have to care about button
							else
								*state = CHECK_BUTTON ;
							break ;

			case CHECK_BUTTON :
							UserInput = readUserInput(); //for LED4 and LED5 only
							if(UserInput == 1)
							{
								triggered = 1 ; //set triggered to 1 to remember that the user had already pressed the button

								*state = BUTTON_PRESSED ; //
							}
							else
								*state = BUTTON_NOT_PRESSED ;
							break ;

			case LED_ON :
							turnOnLED(LED);
							//if (getCurrentTime() - currentTime[LED] >= *Time)
							//{
							//	currentTime[LED] = getCurrentTime();
								turnOffLED(LED);

								if (LED == LED3)
									*state = LED_OFF ; //LED3 doesn;t have to care about button
								else
									*state = CHECK_BUTTON ;

								if (LED == LED5)
									counter ++ ;
							//}

							break ;

			case LED_OFF:
							turnOffLED(LED);
							//if (getCurrentTime() - currentTime[LED] >= *Time)
							//{
							//	currentTime[LED] = getCurrentTime();
								*state = LED_ON ;
							//}

							break ;

			case BUTTON_PRESSED:
							if (LED == LED4)
								*Time = _100ms;
							else if (LED == LED5)
							{
								if (counter == 5) // LED5 blinked 5 times
								{
									*state = START ;
									break ;
								}
							}
							*state = LED_OFF ;
							break ;
			case BUTTON_NOT_PRESSED :

							if (LED == LED4) //restore LED4 to original blinking rate
								*Time = _1s ;
							else if (LED == LED5)
								{
									if (triggered) // button was pressed
									{
										if (counter == 5 ) // check if LED5 had blinked 5 times
										{
											triggered  = 0 ;
											counter = 0 ;
										}
										else //if not will force LED5 to blink till 5 times
										{
											*state = LED_OFF ;
											break;
										}

									}

									*state = CHECK_BUTTON ; // button was not pressed previously and go and check it again
									break ;
								}

							*state = LED_OFF ;
							break ;
	}
}
Beispiel #24
0
void TUNRUN_LINES()
{
	switch(Lines_state) //transitions
	{
		case TRL_START:
		Lines_state = TRL_WAIT;
		break;
		case TRL_WAIT:
		if((~PINB & 0x01) == 0x00)
		{
			Lines_state = TRL_WAIT; break;
		}
		else
		{
			Lines_state = TRL_PLAY; break;
		}
		
		case TRL_PLAY:
		if(QUIT == 1 || TR_LOSE == 1)
		{
			Lines_state = TRL_PAUSE; break;
		}
		else
		{
			Lines_state = TRL_CHECK; break;
		}
		
		case TRL_CHECK:
		if(TR_LOSE == 1)
		{
			Lines_state = TRL_PAUSE; break;
		}
		else
		{
			Lines_state = TRL_PLAY; break;
		}
		
		case TRL_PAUSE:
		{
			if((~PINB & 0x01) == 0)
			{
				Lines_state = TRL_WAIT; break;
			}
			else
			{
				Lines_state = TRL_PAUSE; break;
			}
		}
		
		default:
		break;
	}
	
	switch(Lines_state) //state actions
	{
		case TRL_START:
		break;
		
		case TRL_WAIT:
		clearLED();
		SetLINE(L_E7);
		for(unsigned char a = 0; a < 8; ++a)
		{
			turnOnLED(CurrLine.start_x, a);
		}
		turnOffLED(CurrLine.start_x, CurrLine.off_y1);
		turnOffLED(CurrLine.start_x, CurrLine.off_y2);
		Y1 = 3;
		y2 = 4;
		turnOnLED(7, Y1);
		turnOnLED(7, y2);
		break;
		
		case TRL_PLAY:
		for(unsigned char a = 0; a < 8; ++a)
		{
			if(CurrLine.start_x > 0)
			{
				turnOffLED(CurrLine.start_x - 1, a);
			}
			else if(CurrLine.start_x == 0)
			{
				turnOffLED(7,a);
			}
			turnOnLED(CurrLine.start_x, a);
		}
		turnOffLED(CurrLine.start_x, CurrLine.off_y1);
		turnOffLED(CurrLine.start_x, CurrLine.off_y2);
		break;
		
		case TRL_CHECK:
		++CurrLine.start_x;
		if(CurrLine.start_x > 7)
		{
			SetLINE(Generate_RanLINE());
		}
		break;
		
		case TRL_PAUSE:
		break;
		
		default:
		break;
	}
}
Beispiel #25
0
void SnakeBody()
{
	switch(bodyState)
	{
		case BODY_START:
		{
			bodyState = BODY_WAIT;
			break;
		}
		case BODY_WAIT:
		{
			if((~PINB & 0x01) == 0x01)
			{
				Generate_RanSnack();
				turnOnLED(bitA,bitB);
				bodyState = BODY_PLAY;
				break;
			}
			else
			{
				bodyState = BODY_WAIT;
				break;
			}
		}
		case BODY_PLAY:
		{
			if((SNAKE_QUIT == 1) || (SNAKE_LOSE == 1))
			{
				bodyState = BODY_PAUSE;
				break;
			}
			else
			{
				bodyState = BODY_CHECK;
				break;
			}
		}
		case BODY_CHECK:
		{
			if(SNAKE_LOSE == 1)
			{
				bodyState = BODY_PAUSE;
				break;
			}
			else
			{
				bodyState = BODY_PLAY;
				break;
			}
		}
		case BODY_PAUSE:
		{
			if((~PINB & 0x01) == 0x00)
			{
				bodyState = BODY_WAIT;
				break;
			}
			else
			{
				bodyState = BODY_PAUSE;
				break;
			}
		}
		default:
		break;
	}
	switch(bodyState)
	{
		case BODY_START:
		break;
		case BODY_WAIT:
		{
			clearLED();
			Snek_head.x = 6;
			Snek_head.y = 4;
			turnOnLED(Snek_head.x,Snek_head.y);
			movement = 0x01;
			body_period = 500;
			break;
		}
		case BODY_PLAY:
		{
			turnOffLED(Snek_head.x,Snek_head.y);
			if(movement == 1)
			{
				--Snek_head.x;
			}
			else if(movement == 3)
			{
				++Snek_head.x;
			}
			else if(movement == 4)
			{
				--Snek_head.y;
			}
			else
			{
				++Snek_head.y;
			}
			turnOnLED(Snek_head.x,Snek_head.y);
			break;
		}
		case BODY_CHECK:
		{
			if((bitA == Snek_head.x) && (bitB == Snek_head.y))
			{
				Generate_RanSnack();
				turnOnLED(bitA,bitB);
				++SNEK_SIZE;
				if(SNEK_SIZE == 5)
				{
					SNEK_SIZE = 0;
					if(body_period > 300)
					{
						body_period -= 150;
					}
					else if (body_period <= 100)
					{
						body_period -= 10;
					}
					else if (body_period <= 300)
					{
						body_period -=50;
					}
					
				}
				break;
			}
			break;
		}
		case BODY_PAUSE:
		break;
		default:
		break;
	}
}
Beispiel #26
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;
}
Beispiel #27
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;
}
Beispiel #28
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;
}