Exemple #1
0
void alarm() {
initPWM();
OC1RS+=0x8F; 
int j=0;
for (j=0;j<32*motor_position;j++)
{
PORTDCLR = 0x0010;
PORTDSET = 0x0002;
DelayMotor();
PORTDCLR = 0x0002;
PORTDSET = 0x0004;
DelayMotor();
PORTDCLR = 0x0004;
PORTDSET = 0x0008;
DelayMotor();
PORTDCLR = 0x0008;
PORTDSET = 0x0010;
DelayMotor(); 
}
initTimer2();
start=0;
first=1;
alm=1;

}
void main() {

  Start_TP();

  Init_GPIO();
  Init_SDIO();
  Init_Ext_Mem();
  Init_FAT();
  RTC_Init();
  
  I2C2_Init_Advanced(400000, &_GPIO_MODULE_I2C2_PF01);
  UART2_Init_Advanced(9600, _UART_8_BIT_DATA, _UART_NOPARITY, _UART_ONE_STOPBIT, &_GPIO_MODULE_USART2_PD5_PA3);
  MPU9150A_FSY = 0;
  MPU9150A_Init();
  MPU9150A_Detect();
  MAG_Detect();
  tmrTicks = 0;
  initTimer2();
  Timer2_On();
  MPU9150A_Read(); //initial read
  delay_ms(10);

  while (1) {
    DisableInterrupts();
    Check_TP();
    EnableInterrupts();
    //DrawScreen(&Boot);
    //DrawScreen(&Speedometer_graphics);
    Run_logger();

  }
}
Exemple #3
0
void initSymPWM2(uint16_t prescaler, _t_pwm_pol pol, uint8_t clkSrc)
{
	/* Override previous PWM init */
	initTimer2(prescaler, PWM, INT_OFF, clkSrc);
	/* DC = 0%*/
	pwm2Pins(pol);
}
int
main (void) {
  char msg[30];
  unsigned char c; int i=0;
  int counter[3];
ioinit();
  //initTimer();
  initTimer2();
  initPWM();
  initLCD();
  //initPWM();
  USART_Transmit_String("Restarting..\r\n");
        wait(10);
    //LCD_gotoXY(1,0);
    //LCD_Write("Out-PWM-BC1");
     while (1) {
      unsigned int en, en1;
      asm("nop");
      cli();
       en = topup_timer2;
       en1 = timestamp[0];
      sei();
      sprintf(msg, "%04x,%04x", en, en1);
      USART_Transmit_String(msg);
      LCD_gotoXY(0,0);
      LCD_Write(msg);
    }
    return 0;
}
Exemple #5
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;
}
Exemple #6
0
int main(void) {
    
    SYSTEMConfigPerformance(40000000);
    initKeypad();
    enableEnterruptKeypad();
    initTimer2();
    initLCD();
    enableInterrupts();
    moveCursorLCD(0,0);
    state = Wait;

    while (1) {
        switch (state) {
            case Wait:
                break;

            case Scan:
                key = scanKeypad();
                state = MoveCursor;
                break;
                
            case MoveCursor:
                if(count == 0) moveCursorLCD(0,0);
                else if (count == 9) moveCursorLCD(1,0);
                state = Print;
                break;
                
            case debounce1:
                delayUs(500);
                state = Scan;
                break;
                
            case debounce2:
                delayUs(500);
                state = MoveCursor;
                break;

            case Print:
                delayUs(100);
                if(key == 0) printCharLCD('0');
                else if(key == 1) printCharLCD('1');
                else if(key == 2) printCharLCD('2');
                else if(key == 3) printCharLCD('3');
                else if(key == 4) printCharLCD('4');
                else if(key == 5) printCharLCD('5');
                else if(key == 6) printCharLCD('6');
                else if(key == 7) printCharLCD('7');
                else if(key == 8) printCharLCD('8');
                else if(key == 9) printCharLCD('9');
                else if(key == 10) printCharLCD('*');
                else if(key == 11) printCharLCD('#');
                state = Wait;
                break;

        }
    }

    return 0;
}
Exemple #7
0
int main(void)
{
    enableInterrupts();
    initLEDs();
    initSW2();
    initTimer2();
    updateLEDState();
    
    
    while(1)
    {
        switch (currentState)
        {
            case wait:
                if(buttonState==risingEdge)//button is pressed
                {
                    
                    currentState=debouncePress;
                    buttonState=Idle;
                }
//                if(IFS1bits.CNDIF==1)
//                //if(PORTDbits.RD6==0) //!!! use change notifications, not polling
//                {
//                    currentState=debouncePress;
//                }
                break;
            case debouncePress:
                delayUs(50);
                currentState=waitForRelease;
                break;
            case waitForRelease:
                //!!! use change notifications, not polling
                if(buttonState==fallingEdge)//button released
                {
                    currentState=debounceRelease;
                    buttonState=Idle;
                }
//                if(IFS1bits.CNDIF==1)
//                //if(PORTDbits.RD6==1)//released
//                {
//                    currentState=debounceRelease;
//                }
                break;
            case debounceRelease:
                delayUs(50);
                updateLEDState();
                currentState=wait;
                break;
        }
        
    }
    
    return 0;
}
Exemple #8
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;
}
Exemple #9
0
int main(void)
{
    SYSTEMConfigPerformance(10000000);
    initTimer2();
    initLCD();
    
    while(1)
    {
        testLCD();
    }
    
    return 0;
}
Exemple #10
0
int main(void)
{
    SYSTEMConfigPerformance(40000000);
    enableInterrupts();
    initTimer2();
    //initLCD();
    testLCD();
    while(1)
    {
        //testLCD();
    }
    
    return 0;
}
int main(void)
{
	initTimer0();		// Timer0 - Lys/Lyd
	initTimer1();		// Timer1 - Lyd(OCR1A) og Delays
	initTimer2();		// Timer2 - Motorstyring
	initBackLEDPort();	
	initFrontLEDPort();
	
	sei();
	
	allLightsOn();	// Tænd alle lys
	
	

	while(1)
	{
		do 
		{
			ReflectionCount();
			DriveForward1();
			brakeLightOff();
			
			if( antalRefleksbrik == 3 )	// Brems ved bakken.
			{
				Brake();
				brakeLightOn();
			}
			
			if( antalRefleksbrik == 6 )	// Bremse og bak.
			{
				Brake();
				_delay_us(5000000);
				brakeLightOn();
				Reverse();
			}
			
			if( antalRefleksbrik == 8 )	// Når den holder ved refleksbrik nr 5.
			{
				Brake();
				_delay_us(5000000);
			}
		} while ( antalRefleksbrik >=0 && antalRefleksbrik <=11 );
		
		Brake();
		brakeLightNormal();
		StopLevelComplete();
	}
	
	return 0;
}
Exemple #12
0
int main(void)
{
	initTimer0();
	initTimer1();
	initTimer2();
	initSwitchPort();
	sei();
	
	//int antalRefleksbrik;
	
    while(1)
    {
			do
			{
				ReflectionCount();
				DriveForward1();

				/*if( StartReflection() == 1 )	// True = 1.
				{
					DetekCoin();
				}
				*/

				if( antal == 3 )	// Brems ved bakken.
				{
					Brake();
				}
	        
				if( antal == 6 )	// Bremse og bak.
				{
					Brake();
					_delay_us(5000000);
					Reverse();
				}
	        
				if( antal == 8 )	// Når den holder ved refleksbrik nr 5.
				{
					Brake();
					_delay_us(5000000);
				}
			
			} while ( antal >=0 && antal <=11 );
		
			Brake();		
    }
	
	return 0;
}
Exemple #13
0
void initSM()
 {
    ODCBbits.ODCB5    = 1;    // open drain   
    ODCBbits.ODCB10   = 1;    // open drain   
    ODCBbits.ODCB11   = 1;    // open drain   
    PORTBbits.RB5     = 0;    // das bleibt auf 0, geschaltet wird mit TRIS  
    PORTBbits.RB10    = 0;    //
    PORTBbits.RB11    = 0;    //  
    TRISBbits.TRISB5  = 1;    // step enable  
    TRISBbits.TRISB10 = 1;    // step  
    TRISBbits.TRISB11 = 1;    // dir
    LATBbits.LATB5    = 0;    // do NOT set the LAT to 1; this will set the pin to 3.3v and the pull pu will try to pull it to 5v.  
    LATBbits.LATB10   = 0;  
    LATBbits.LATB11   = 0;  
    initTimer2();
    enableSM(1);
}
Exemple #14
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;
}
Exemple #15
0
int main()
{
	uint8_t i;

	cli();

	wdt_enable(WDTO_60MS);

	for (i = 0; i < 8; i++) {
		PORTA = PORTA >> 1;
		PORTA |= ((readEEPROM(i) - '0') & 0x01) << 7;
	}

	for (i = 8; i < 16; i++) {
		PORTC = PORTC >> 1;
		PORTC |= ((readEEPROM(i) - '0') & 0x01) << 7;
	}

	DDRA = 0xff;
	DDRC = 0xff;

	for (stackTail = EEPROM_SIZE - 1; readEEPROM(stackTail) != 0xff; stackTail--);

	status = ((uint16_t) PORTC) << 8 | (uint16_t) PORTA;

	initUSART();

	setDuty();

	initTimer0();

	initTimer2();

	sei();

	printf("\nEntering the main loop\n");
	while (1) {
		wdt_reset();
	}

	return 0;
}
Exemple #16
0
int main() {
    SYSTEMConfigPerformance(10000000); //Does something with assembly to set clock speed
    enableInterrupts(); //Make interrupt work

    //initLEDs();
    //initTimer1();
    initTimer2();
    //initSW1();
    initLCD();
    delay(500);
    //printCharLCD(0);
    testLCD();
    
    moveCursorLCD(0,0);
    
    while (1) {
        printCharLCD('K');
                
    }
    return 0;
}
Exemple #17
0
void initPWM2(uint16_t prescaler, _t_pwm_pol pol, uint8_t clkSrc)
{
	/* Override previous PWM init */
	initTimer2(prescaler, FAST_PWM, INT_OFF, clkSrc);
	/* DC = 0%*/
	if ((pol == PWM_NORMAL_A)||(pol == PWM_INVERT_A))
	{
		set_pwmPol2A = 0;
		sbi(DDRB, DDB3);
		cbi(PORTB, PORTB3); /* out low */
	}
	if ((pol == PWM_NORMAL_B)||(pol == PWM_INVERT_B))
	{
		set_pwmPol2B = 0;
		sbi(DDRD, DDD3);
		cbi(PORTD, PORTD3); /* out low */
	}		
	pwm2Pins(pol);
	if (pol == PWM_NORMAL_A)
	{
		setDutyPWM2(0, PWM_A);
	}
	if (pol == PWM_NORMAL_B)
	{
		setDutyPWM2(0, PWM_B);
	}
	if (pol == PWM_INVERT_A)
	{
		setDutyPWM2(0xff, PWM_A);
	}
	if (pol == PWM_INVERT_B)
	{
		setDutyPWM2(0xff, PWM_B);
	}
	startTimer2();

}
Exemple #18
0
int main(void) {

    SYSTEMConfigPerformance(40000000);
    enableInterrupts();
    initTimer2();
    initLCD();
    clearLCD();
    initKeypad();
    
    while (1) {
        switch (state) {
            case Start:
                initLCD();
                clearLCD();
                state = ROW1;
                break;


            case ROW1:
                Row=1;
                //printCharLCD('1');
                LATGbits.LATG0 = DISABLED;
                LATFbits.LATF1 = DISABLED;
                LATDbits.LATD12 = DISABLED;
                LATGbits.LATG13 = ENABLED;                
                //delayUs2(70);
                state = ROW2;
                break;

            case ROW2:
                Row=2;
                //printCharLCD('2');
                LATDbits.LATD12 = DISABLED;
                LATFbits.LATF1 = DISABLED;
                LATGbits.LATG13 = DISABLED;
                LATGbits.LATG0 = ENABLED;
                //delayUs2(70);
                state = ROW3;
                break;

            case ROW3:
                Row=3;
                //printCharLCD('3');
                LATDbits.LATD12 = DISABLED;
                LATGbits.LATG13 = DISABLED;
                LATGbits.LATG0 = DISABLED;
                LATFbits.LATF1 = ENABLED;
                //delayUs2(70);
                state = ROW4;
                break;

            case ROW4:
                Row=4;
                LATGbits.LATG13 = DISABLED;
                LATGbits.LATG0 = DISABLED;
                LATFbits.LATF1 = DISABLED;
                LATDbits.LATD12 = ENABLED;
                //delayUs2(70);
                state = ROW1;
                break;

            case DebouncePress:
                delayUs2(700);
                state = WaitRelease;
                break;

            case WaitRelease:
                state = WaitRelease;
                break;

            case DebounceRelease:
                delayUs2(700);
                state = WriteLCD;
                break;

                
            case WriteLCD:       
                key=scanKeypad(Col, Row);
                printCharLCD(key);
                state = ROW1;
                break;
        }
    }

    return 0;
}
Exemple #19
0
/**@brief Chiamabile più volte per inizializzare moduli A e B */
uint8_t initCompare2 (uint16_t comp_val, _t_compare_sel comp, uint16_t prescaler, uint8_t timer_int,  uint8_t clkSrc)
{
	initTimer2(prescaler, COMPARE_MATCH, timer_int, clkSrc);
	timeout = 1000;
	while ((ASSR & TCR2A_BUSY_BIT) || (ASSR & TCR2B_BUSY_BIT) || (ASSR & OCR2A_BUSY_BIT) || (ASSR & OCR2B_BUSY_BIT))
	{
		timeout--;
		if (timeout == 0)
		{
			return 8;
		}
	}

	switch (comp)
	{
		case COMP_A_TOGGLE:
		sbi(TCCR2A, COM2A0);
		cbi(TCCR2A, COM2A1);
		sbi(DDRB, DDB3);
		OCR2A = comp_val;
		break;
		
		case COMP_A_CLEAR:
		cbi(TCCR2A, COM2A0);
		sbi(TCCR2A, COM2A1);
		sbi(DDRB, DDB3);
		OCR2A = comp_val;
		break;
		
		case COMP_A_SET:
		sbi(TCCR2A, COM2A0);
		sbi(TCCR2A, COM2A1);
		sbi(DDRB, DDB3);
		OCR2A = comp_val;
		break;
		
		case COMP_B_TOGGLE:
		sbi(TCCR2A, COM2B0);
		cbi(TCCR2A, COM2B1);
		sbi(DDRD, DDD3);
		OCR2B = comp_val;
		break;
		
		case COMP_B_CLEAR:
		cbi(TCCR2A, COM2B0);
		sbi(TCCR2A, COM2B1);
		sbi(DDRD, DDD3);
		OCR2B = comp_val;
		break;
		
		case COMP_B_SET:
		sbi(TCCR2A, COM2B0);
		sbi(TCCR2A, COM2B1);
		sbi(DDRD, DDD3);
		OCR2B = comp_val;
		break;
		
		case COMP_A_DISCONNECT:
		cbi(TCCR2A, COM2B0);
		cbi(TCCR2A, COM2B1);
		OCR2A = comp_val;
		break;
		
		case COMP_B_DISCONNECT:
		cbi(TCCR2A, COM2B0);
		cbi(TCCR2A, COM2B1);
		OCR2B = comp_val;
		break;
		
		default:
		/* lascia OCnx sconnesso come lo è da initTimer0*/
		cbi(TCCR2A, COM2B0);
		cbi(TCCR2A, COM2B1);
		cbi(TCCR2A, COM2A0);
		cbi(TCCR2A, COM2A1);
		break;
	}
	
	switch (timer_int)
	{
		case INT_ON:
		/* Non devo chiamare questa funzione se uso il normale */
		if ((comp == COMP_A_CLEAR) || (comp == COMP_A_SET) ||(comp == COMP_A_TOGGLE)||(comp == COMP_A_DISCONNECT))
		{
			sbi(TIMSK2, OCIE2A);
		}
		if ((comp == COMP_B_CLEAR) || (comp == COMP_B_SET) ||(comp == COMP_B_TOGGLE)||(comp == COMP_B_DISCONNECT))
		{
			sbi(TIMSK2, OCIE2B);
		}
		break;
		
		case INT_OFF:
		if ((comp == COMP_A_CLEAR) || (comp == COMP_A_SET) ||(comp == COMP_A_TOGGLE)||(comp == COMP_A_DISCONNECT))
		{
			cbi(TIMSK2, OCIE2A);
		}
		if ((comp == COMP_B_CLEAR) || (comp == COMP_B_SET) ||(comp == COMP_B_TOGGLE)||(comp == COMP_B_DISCONNECT))
		{
			cbi(TIMSK2, OCIE2B);
		}
		break;
		
		default:
		cbi(TIMSK2, OCIE2A);
		cbi(TIMSK2, OCIE2B);
		break;
	}
	return 0;
}
Exemple #20
0
int main() {
    SYSTEMConfigPerformance(10000000); //Does something with assembly to set clock speed
    enableInterrupts(); //Make interrupt work

    initLEDs();
    initTimer1();
    initTimer2();
    initTimer3();
    initTimer4();
    initSwitch();
    
    
    porta = PORTA;
    portd = PORTD;
    
    initLCD();
    delay(500);
    //printCharLCD(0);
    testLCD();
    
    state = InitState;
    nextState = InitState;

    while (1) {
        switch (state) {
            case RUN:
#ifdef _DEBUG_
                  LATDbits.LATD0=0;
                  LATDbits.LATD1=0;
                  LATDbits.LATD2=1;
#endif
                  LATGbits.LATG14=LedOFF; //TRD1
                  LATGbits.LATG12=LedON; //TRD2
                  moveCursorLCD(0,0);
                  printStringLCD("Running...");
                  if(counterCN==1){   // If the counter changed...
                      printTimeLCD(counter);
                      counterCN = 0;
                  }
                  if(AllowChange == 0){
                      delay(5000);
                      delay(5000);
                      delay(5000);
                      delay(5000);
                      AllowChange = 1;
                  }
                  if((ReqChange==1) && (AllowChange==1)){
                      state=WAIT1;
                      ReqChange=0;
                      AllowChange=0;  
                    //  CNCONAbits.ON = 1;
                  }
                  break;
                  
              case STOP:
#ifdef _DEBUG_
                  LATDbits.LATD0=1;
                  LATDbits.LATD1=0;
                  LATDbits.LATD2=0;
#endif
                  LATGbits.LATG14=LedON;
                  LATGbits.LATG12=LedOFF;
                  moveCursorLCD(0,0);
                  printStringLCD("Stopped");
                  if(AllowChange == 0){
                      delay(5000);
                      delay(5000);
                      delay(5000);
                      delay(5000);
                      AllowChange = 1;
                  }
                  if(AllowReset == 0){
                      delay(5000);
                      delay(5000);
                      delay(5000);
                      delay(5000);
                      AllowReset = 1;
                  }
                  if((ReqReset==1) && (AllowReset==1)){
                      AllowReset = 0;
                      ReqReset = 0;
                      counter = 0;
                      printTimeLCD(counter);
                  }
                  if((ReqChange==1) && (AllowChange==1)){
                      state=WAIT2;
                      ReqChange=0;
                      AllowChange=0;
                     // CNCONAbits.ON = 1;
                  }
                  break;
                  
              case WAIT1:
#ifdef _DEBUG_
                  LATDbits.LATD0=0;
                  LATDbits.LATD1=1;
                  LATDbits.LATD2=1;
#endif
                  T1CONbits.ON = 0; // Turn off counter
                  if(AllowChange == 0){
                      delay(5000);
                      delay(5000);
                      delay(5000);
                      delay(5000);
                      AllowChange = 1;
                  }
                  if((ReqChange==1) && (AllowChange==1)){
                      state=STOP;
                      ReqChange=0;
                      AllowChange=0;
                      ReqReset = 0; // So we don't reset as soon as we stop...
                      AllowReset = 0;   // See previous line
                     // CNCONAbits.ON = 1;
                  }
                  
                  break;
              case WAIT2:
#ifdef _DEBUG_
                  LATDbits.LATD0=1;
                  LATDbits.LATD1=1;
                  LATDbits.LATD2=0;
#endif
                  T1CONbits.ON = 1; // Turn on counter
                  if(AllowChange == 0){
                      delay(5000);// The sad part is this didn't even work.
                      delay(5000);
                      delay(5000);
                      delay(5000);
                      AllowChange = 1;
                  }
                  if((ReqChange==1) && (AllowChange==1)){   // Button Changed and De-bouncing period is done
                      state=RUN;
                      ReqChange=0;
                      AllowChange=0;
                      //CNCONAbits.ON = 1;
                  }
                  break;
              
              case InitState:// Only happens once
                   state=STOP;
                   clearLCD();
                   counter = 0;
                   printTimeLCD(counter);
                   T1CONbits.ON = 0;
                   TMR1 = 0;

                  break;
              default:// Should never happen. How did you get here?
                   state=InitState;
                   ReqChange = 0;
                   AllowChange=0;
                  break;
        }
    }
    return 0;
}
Exemple #21
0
void initSevenSeg() {
	DDRB = 0xFF;		//7 seg port
	DDRC |= 0b00001100; //7 seg enable pins
	initTimer2();		//Enable 7 seg timer
}
Exemple #22
0
void main(void)
{
	PORTA=0x00;
	DDRA=0x00;

	PORTB=0x00;
	DDRB=0x00;

	PORTC=0x00;
	DDRC=0x00;

	PORTD=0x00;
	DDRD=0x00;

	PORTE=0x00;
	DDRE=0x00;

	PORTF=0x00;
	DDRF=0x00;

	PORTG=0x00;
	DDRG=0x00;

	initTimer0();
    initTimer1();
    initTimer2();
    initTimer3();

	enableTimers();

	// External Interrupt(s) initialization
	// INT0-INT7: Off
	EICRA=0x00;
	EICRB=0x00;
	EIMSK=0x00;

	// Analog Comparator initialization
	// Analog Comparator: Off
	// Analog Comparator Input Capture by Timer/Counter 1: Off
	ACSR=0x80;
	SFIOR=0x00;

	// ====================== TRUE INITIALIZATION =============================

    initLeds();
	//initJumpers();

	InitFans();
	// configure Fans
    FanSetPower(&fan1, 5);
	FanSetPower(&fan2, 0);
	FanSetPower(&fan3, 0);
	FanSetPower(&fan4, 0); // max is 27

	FanEnable(&fan2);
	FanEnable(&fan3);
	FanEnable(&fan4);




	InitRailControl();

	InitPsuControl();

	InitPullDownControl();

	//InitTemperature(); // no temperature-based control yet // depend on jumper J1

	InitSignalControl();

	InitADC();
	InitBatChargeControl();

	initPPC_State();

    initUSART();


	#asm("sei")

	while(1)
	{
		// read jumpers
		// read inputs
		// update fans
		// update outputs
		// enter sleep

		/*
		// test fan speed debug
		if (getTicksDelta(lastFanStepTickCount) > FAN_STEP_INTERVAL)
		{
			lastFanStepTickCount = getTickCount();
			debug_fan_power_step++;
			if (debug_fan_power_step > PWR_FAN_MAX)
				debug_fan_power_step = PWR_FAN_MIN;
            FanSetPower(&fan1, debug_fan_power_step);
		};
		*/

        BatChargeUpdate();
		//batChargeState.all_batteries_charged = 1; // !!!
		SignalsIndicateCharged(batChargeState.all_batteries_charged);

		#if 1
        SignalReadUpdate();


		switch (ppcState.mode)
		{
		case(PPC_MODE_WAITING):
		{
			if (InputsTransientHigh(SIGNAL_INDEX_SPINT_POWER))
			{
				setPPC_Mode(PPC_MODE_STARTING);
			};
			break;
		}; // waiting mode state

		case(PPC_MODE_STARTING):
		{
            FanEnable(&fan1);
            switch(ppcState.stage)
			{
			case(0):
                PsuSet(0,0,1,0);
				PsuUpdateOutput();
				PORTA = railState.port_value | psuState.port_value | pullDownState.portA_value;
				delay_ms(200);

				break;

			case(1):
                PsuSet(1,0,1,0);
				PsuUpdateOutput();
				PORTA = railState.port_value | psuState.port_value | pullDownState.portA_value;
				delay_ms(200);

				break;

			case(2):
                PsuSet(1,0,1,1);
				PsuUpdateOutput();
				PORTA = railState.port_value | psuState.port_value | pullDownState.portA_value;
				delay_ms(200);

				break;

			case(3):
                RailSet(1,0);
				RailUpdateOutput();
				PORTA = railState.port_value | psuState.port_value | pullDownState.portA_value;
				delay_ms(200);

				break;

			case(4):
                RailSet(1,1);
				RailUpdateOutput();
				PORTA = railState.port_value | psuState.port_value | pullDownState.portA_value;
				delay_ms(1000);

				break;

			case(5):
                PullDownSet(1,0,0);
				PullDownUpdateOutput();
				PORTA = railState.port_value | psuState.port_value | pullDownState.portA_value;
				delay_ms(100);

				break;

			case(6):
                PullDownSet(0,0,0);
				PullDownUpdateOutput();
				PORTA = railState.port_value | psuState.port_value | pullDownState.portA_value;
				delay_ms(100);

				break;

			default:
				setPPC_Mode(PPC_MODE_ACTIVE);
				break;

			};
			ppcState.stage++;
			break;
		}; // starting mode state

		case(PPC_MODE_ACTIVE):
		{
			FanEnable(&fan1);

			if (signalState.current_signal_state[SIGNAL_INDEX_SPINT_POWER] == 0)
			{
				setPPC_Mode(PPC_MODE_PARKING);
			};

			//if (InputsTransientLow(SIGNAL_INDEX_SPINT_POWER))

			break;
		}; // acitve mode state

		case(PPC_MODE_PARKING):
		{
			FanDisable(&fan1);
			switch(ppcState.stage)
			{
			case(0): // turn off PSU power
                PsuSet(0,0,0,0);
				PsuUpdateOutput();
				PORTA = railState.port_value | psuState.port_value | pullDownState.portA_value;
				delay_ms(200);

				break;
			case(1): // park computer (pull-in)
                PullDownSet(1,0,0);
				PullDownUpdateOutput();
				PORTA = railState.port_value | psuState.port_value | pullDownState.portA_value;
				delay_ms(100);

				break;
			case(2): // park computer (pull-out)
                PullDownSet(0,0,0);
				PullDownUpdateOutput();
				PORTA = railState.port_value | psuState.port_value | pullDownState.portA_value;
				delay_ms(1000); // wait before clamping power rail

				break;

			case(3): // turn-off rails
                RailSet(0,0);
				RailUpdateOutput();
				PORTA = railState.port_value | psuState.port_value | pullDownState.portA_value;
				delay_ms(200);

				break;
			default:
				setPPC_Mode(PPC_MODE_WAITING);
				break;
			};

			ppcState.stage++;
			break;
		}; // Parking mode state

		}; // MODE Switch




		// This function locks for 0.2 second minimum
		// This must be read in the end of control loop
		/*
		TemperatureReadUpdate();
		if (temperatureState.temperature1 != 0)
		{
			if (temperatureState.temperature1 <= 30)
			{
				FanDisable(&fan1);
			}
			else
			{
				FanEnable(&fan1);
				FanSetPower(&fan1,(unsigned char)(PWR_FAN_MIN + ((signed int)(PWR_FAN_MAX - PWR_FAN_MIN))*temperatureState.temperature1/60));
			};
		}
		else
		{
			FanDisable(&fan1);
		};
		*/


		RailUpdateOutput();
		PsuUpdateOutput();
		PullDownUpdateOutput();

		networkUpdate();

		PORTA = railState.port_value | psuState.port_value | pullDownState.portA_value;

        #endif


		/*
		if (ppcState.mode == PPC_MODE_WAITING)
		{
			#asm("sleep")
		}
		*/
	}



} // main
Exemple #23
0
int main(void)
{
	static uint8_t oldPosition;
	static uint8_t divVal, modVal;

	cli();
	wdt_disable();
	set_sleep_mode( SLEEP_MODE_IDLE );
	initDevice();

	DDRD = 0b11111111;
	DDRB = 0b11111111;

	initTimer2();
	initTimer0();
	sei();
	startTimer2();
	startTimer0();
	
	frame	= buf1;
	buffer 	= buf2;

	CLEAR_BUFFER( frame );
	CLEAR_BUFFER( buffer );

	setAnimationFunction( anim_fillDot, TRUE, TRUE );
	animation.loop = TRUE;

    while ( 1 )
    {
		if( animation.tick )
		{
			animationTick();
			clearTick();
		}

		if( currentScanPixel == 0 )
		{
			if( animation.nextFrameReady )
			{
				animationBufferSwap();
			}
		}

		if( oldPosition != currentScanPixel )
		{
			modVal = currentScanPixel % 8;
			
			//we need to change columns only so often
			if( modVal == 0 )
			{
				divVal = currentScanPixel / 8;
				COL_PIXEL( divVal );
			}
			
			if( frame[ divVal ] & _BV( modVal ) )
			{
				ROW_PIXEL( modVal );
			}
			else
			{
				ROW_OFF();
			}
			oldPosition	 = currentScanPixel;
		}

		//if( TIME TO SLEEP ) { sleep_mode(); }

    }

}
Exemple #24
0
int main(void)
{
    SYSTEMConfigPerformance(10000000);
    enableInterrupts();
   initTimer2();
   initKeypad();
   initLCD();
   
    char password1[5] = {'a','a','a','a','\0'}; //2D array of passwords 
 char password2[5] = {'a','a','a','a','\0'};
 char password3[5] = {'a','a','a','a','\0'};
 char password4[5] = {'a','a','a','a','\0'};
 char tempPassword[5] = {'\0','\0','\0','\0','\0'}; //password being entered

 char keyPressed = 'f';
 int row = 1;
 int position = 0; 
 int counter = 1;
 int numPasswords = 0; //number of passwords 
 int i =0; 
 int j =0;
 int set = 0; 
 
   //TRISA.RA7
   TRISAbits.TRISA7 = 0;
   
   //TRISCbits.TRISC4 = 0;
    while(1)
    {
       
        
        switch(state){
            
            case Wait: 
                            
                if( set == 1){
//                moveCursorLCD(1,1); //first line 
//                printStringLCD("Set Mode");
                }              
                else {
                    
                moveCursorLCD(1,1); //first line 
                printStringLCD("Enter"); 
                }
                
               
                break;
                
            case Wait2:
                
                
                if((PORTBbits.RB10 == 1) && (PORTBbits.RB12 == 1) && (PORTBbits.RB14 == 1) ) { //waits for button release
         
                   
                state = DeBounceRelease;
                break;
                }
                
                else state = Wait2;
                break;
                
            case DeBounce: 
                 
                //moveCursorLCD(1,1); //first line 
                //printStringLCD("DeBounce State");
                
                delayUs(500);
                state = SearchKeypad;
                
                break;
                
            case DeBounceRelease: 
                 
                //moveCursorLCD(1,1); //first line 
                //printStringLCD("DeBounce State");
                
                delayUs(500);
               // moveCursorLCD(1,2); //first line 
               // printStringLCD("Button Released");
                state = EnterPassword;
                //IEC1bits.CNBIE = ON;
                break;
                
            case Set:
                moveCursorLCD(1,1); //first line 
                printStringLCD("Set Mode");
                
                moveCursorLCD(1,2); // clears second line 
                    clearLCD;
                set = 1; 
                state = Wait; 
                
                break; 
                
            case EnterPassword:
                
                                                
                if(i == 4){
                    i = 0;
                                        
                }
               tempPassword[i] =  keyPressed;
               i = i + 1;
               
               if( i == 2){
                        
                        //if((passwords[0][numPasswords] == '*') && (passwords[1][numPasswords] == '*')) {
                         if((tempPassword[0] == '*') && (tempPassword[1] == '*')) {   
                            i = 0;
                            state = Set;
                        } 
                    }
               
               state = WriteLCD;
                break;
                
              case CheckNewPassword:
                        
                for(j = 0; j < 4; j++){ //checks all stored passwords 
                    
                    if ((tempPassword[j] == '*') || (tempPassword[j] == '#')){
                        moveCursorLCD(1,1); //first line 
                        printStringLCD("Invalid"); //print valid
                        delayUs(2000000);
                        moveCursorLCD(1,2); // clears second line 
                        clearLCD;
                        set = 0;
                        state = Wait;
                 
                        }
                }
                
                
                    moveCursorLCD(1,1); //first line 
                    printStringLCD("Valid"); //print invalid
                    delayUs(2000000);
                    moveCursorLCD(1,2); // clears second line 
                    clearLCD;
                    
                    numPasswords = numPasswords +1;
                    
                      if(numPasswords == 4){
                        numPasswords = 0;
                    }
                    
                    if(numPasswords == 0){
                        
                        for(j =0; j < 5; j++){
                            password1[j] = tempPassword[j];
                        }
                    }
                    
                    else if(numPasswords == 1){
                        
                        for(j =0; j < 5; j++){
                            password2[j] = tempPassword[j];
                        }
                    }
                    
                    else if(numPasswords == 2){
                        
                        for(j =0; j < 5; j++){
                            password3[j] = tempPassword[j];
                        }
                    }
                    
                    else if(numPasswords = 3){
                        
                        for(j =0; j < 5; j++){
                            password4[j] = tempPassword[j];
                        }
                    }
                    set = 0;
                    
                    state = Wait;
                
                    
                        
                        
                break;
                
            case CheckPassword:
                        
                                  
                    if (strcmp(password1, tempPassword) == 0){
                        moveCursorLCD(1,1); //first line 
                        printStringLCD("Good"); //print valid
                        delayUs(20000);
                        moveCursorLCD(1,2); // clears second line 
                        clearLCD;
                 
                        state = Wait;
                 
                        }
                     if (strcmp(password2, tempPassword) == 0){
                        moveCursorLCD(1,1); //first line 
                        printStringLCD("Good"); //print valid
                        delayUs(20000);
                        moveCursorLCD(1,2); // clears second line 
                        clearLCD;
                 
                        state = Wait;
                 
                        }
                    
                     if (strcmp(password3, tempPassword) == 0){
                        moveCursorLCD(1,1); //first line 
                        printStringLCD("Good"); //print valid
                        delayUs(20000);
                        moveCursorLCD(1,2); // clears second line 
                        clearLCD;
                 
                        state = Wait;
                 
                        }
                    
                     if (strcmp(password4, tempPassword) == 0){
                        moveCursorLCD(1,1); //first line 
                        printStringLCD("Good"); //print valid
                        delayUs(20000);
                        moveCursorLCD(1,2); // clears second line 
                        clearLCD;
                 
                        state = Wait;
                 
                        }
                
                
                
                    moveCursorLCD(1,1); //first line 
                    printStringLCD("Bad"); //print invalid
                    delayUs(20000);
                    moveCursorLCD(1,2); // clears second line 
                    clearLCD;
                    
                    
                    state = Wait;
                
                    
                        
                        
                break;
                        
            case SearchKeypad: 
                
                //moveCursorLCD(1,1); //first line 
                //printStringLCD("Search Keypad");
                //delayUs(5000);
                
              //  IEC1bits.CNBIE = OFF;// turn off ISR 
                
                CNCONBbits.ON = 0; //turn change notifications off 
                
                LATEbits.LATE0 = 1;// open drain collector for outputs
                LATEbits.LATE2 = 1;
                LATEbits.LATE4 = 1;
                LATEbits.LATE6 = 1;
                
                keyPressed = scanKeypad();
                
                LATEbits.LATE0 = 0;// open drain collector for outputs
                LATEbits.LATE2 = 0;
                LATEbits.LATE4 = 0;
                LATEbits.LATE6 = 0;
                
                CNCONBbits.ON = 1; // CN on 
                
               state = Wait2;
               //state = WriteLCD;
                break;
                
            case WriteLCD: 
                
                if (counter == 9){
//                    row = ~row;
                    counter = 1;
                }
                
                moveCursorLCD(counter,2);
                  delayUs(2000);
                printCharLCD(keyPressed);
                delayUs(200);
                counter = counter + 1;
                  
                if(i == 3){
                    
                    if(set == 1){
                        state = CheckNewPassword;
                    }
                    state = CheckPassword;
                }
                        state = Wait; 
                
               //IEC1bits.CNBIE = ON;
                break;
            
            
            
        }
    }
    
    return 0;
}
Exemple #25
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;
}
Exemple #26
0
int main(void)
{
    OSCTUN = 0b0111; // Tune internal FRC: 9.7MHz+3% to have 10MHz => 20MIPS
    
    initPWM();
    initTimer2();
    initADC();
    initTimer1(); 
    initLedOutputs();
    initUART();
    
    
    /* starting state */
    send_ping = true;
    
    
    /* Enable ADC */
    ADCONbits.ADON = 1; /* Start the ADC module*/
    
    /* Enable Timer 1 */
    T1CONbits.TON = 1;
    
    unsigned int fetched_RTT1_overflows, fetched_RTT2_overflows, fetched_RTT1_time, fetched_RTT2_time;
    unsigned int i = 0;
    unsigned int j = 0;
    Point point;
    point.x = 0;
    point.y = 0;
    point.z = 0;
    
    while(1) {
//        LATBbits.LATB2 = RTT1_received;
//        LATBbits.LATB3 = RTT2_received;
        
        IEC0bits.ADIE = 0;
        fetched_RTT1_overflows = RTT1_overflows;
        fetched_RTT2_overflows = RTT2_overflows;
        fetched_RTT1_time = RTT1_time;
        fetched_RTT2_time = RTT2_time;
        IEC0bits.ADIE = 1;
        
        ++i;
//        if (i == 65000) {
//            ++j;
//            i = 0;
//        }
        if (i == 5000) { 
            
            //LATBbits.LATB3 = !LATBbits.LATB3;
            if (RTT1_received && RTT2_received) {
                unsigned int RTT1 = fetched_RTT1_overflows*3277 + (fetched_RTT1_time/20) - 16000;
                unsigned int RTT2 = fetched_RTT2_overflows*3277 + (fetched_RTT2_time/20) - 16000;
                int error = track(RTT1, RTT2, &point);
                if (error == 0) {
//                    send_debug("RTTs:");
//                    send_coord(RTT1, RTT2);
//                    send_debug("Point:");
                    int x = (int) point.x;
                    int y = (int) point.y;
                    send_coord(x, y);
//                    if (point.x < 0) {
////                        send_debug("X NEGATIF");
//                      
//                    }
//                    send_coord(-1,-1);
                } else {
//                    send_debug("BUG!");
//                    send_coord(error, 1);
                }
            }

            i = 0;
        }
    }
}
Exemple #27
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;
}
Exemple #28
0
int main(void)
{
    SYSTEMConfigPerformance(SYS_CLOCK);
    
    enableInterrupts();
    initADC();
    initTimer2(); //for PWM
    initPWM();
    initLCD();
    initTimer4(); //for pwm
    initSW();
    
    int track = 1;
    int SpeedR = 0;
    int SpeedL = 0;
    int ones = 0;
    int dec = 0;
    float voltage = 0;

    //Actual main state machine loop
    while(1)
    {
        Speed = ADC1BUF0;
        
        moveCursorLCD(0,0);
        voltage = Speed * ((double)3.3/1023);
        ones = floor(voltage);
        dec = (voltage - ones) * 10;
        printCharLCD('0'+ones);
        printCharLCD('.');
        printCharLCD('0'+dec);
        printCharLCD('V');
        
       delayUs(10000); //10ms - Just to help the LCD not strobe too much
        
        switch(state)
        {
            case wait_Press: //handled by ISR
                break;
            case debouncePress:
                state = wait_Release;
                break;
            case wait_Release: //handled by ISR
                break;
            case debounceRelease:
                if (track == 1){
                    state = forward;
                }
                if (track == 2){
                    state = idle2;
                }
                if (track == 3){
                    state = reverse;
                }
                if (track == 4){
                    state = idle1;
                }
                break;
            case idle1:
                track = 1;
                motorsOff();
                state = wait_Press;
                break;
            case forward:
                track = 2;       
                if(Speed == 511){
                    SpeedR = 1023;
                    SpeedL = 1023;
                }
                else if(Speed < 511){
                    SpeedR = 1023;
                    SpeedL = (2*Speed);
                }
                else if(Speed > 511){
                    SpeedR = 2*(1023 - Speed);
                    SpeedL = 1023;
                }
                else if (Speed == 1023){
                    SpeedR = 0;
                    SpeedL = 1023;
                }
                else if (Speed == 0){
                    SpeedR = 1023;
                    SpeedL = 0;
                }                
                RightMotor_Write(SpeedR);
                LeftMotor_Write(SpeedL);
                break;
            case idle2:
                track = 3;
                motorsOff();
                state = wait_Press;
                break;
            case reverse:
                track = 4;
                if(Speed == 511){
                    SpeedR = 1023;
                    SpeedL = 1023;
                }
                else if(Speed < 511){
                    SpeedR = 1023;
                    SpeedL = 2*Speed;
                }
                else if(Speed > 511){
                    SpeedR = 2*(1023 - Speed);
                    SpeedL = 1023;
                }
                else if (Speed == 1023){
                    SpeedR = 1023;
                    SpeedL = 0;
                }
                else if (Speed == 0){
                    SpeedR = 0;
                    SpeedL = 1023;
                }

                
                RightMotor_Write(0-SpeedR); //maybe it needs the zero to be interpreted as negative?
                LeftMotor_Write(0-SpeedL);
                break;                       
        }
    }
    
    return 0;
}
Exemple #29
0
int main() {
first=1;
start=0;
int k=0;
for (k=0;k<=7;k++) {ref_distance1[k]=0;ref_distance2[k]=0;cur_distance1[k]=0;cur_distance2[k]=0;}
initIntGlobal();
initCN();
initTimer2();
initTimer4();
initPort();
LCD_init(); 
while(1)
{
while(start)
{
int i=0,j=0;
for (i=0;i<=7;i++)
{
for (j=0;j<32;j++)
{
PORTDCLR = 0x0002;
PORTDSET = 0x0010;
DelayMotor();
PORTDCLR = 0x0010;
PORTDSET = 0x0008;
DelayMotor();
PORTDCLR = 0x0008;
PORTDSET = 0x0004;
DelayMotor();
PORTDCLR = 0x0004;
PORTDSET = 0x0002;
DelayMotor(); 
}
TMR2=0; TMR4=0;
PORTDSET=0x0021;
DelayUsec(15);
PORTDCLR=0x0021;
DelayMsec(30);
DisplayTMR2();
DisplayTMR4();
if (!first) 
{cur_distance1[i]=TMR2; cur_distance2[i]=TMR4;} 
else {ref_distance1[i]=TMR2;ref_distance2[i]=TMR4;
cur_distance1[i]=TMR2; cur_distance2[i]=TMR4;}
if (cur_distance1[i]-ref_distance1[i]>300 || ref_distance1[i]-cur_distance1[i]>300
||cur_distance2[i]-ref_distance2[i]>300 || ref_distance2[i]-cur_distance2[i]>300)
{PORTDbits.RD7=1;}
DelayMsec(20);
PORTDbits.RD7=0;
} 
first=0;

for (i=0;i<=7;i++)
{
for (j=0;j<32;j++)
{
PORTDCLR = 0x0010;
PORTDSET = 0x0002;
DelayMotor();
PORTDCLR = 0x0002;
PORTDSET = 0x0004;
DelayMotor();
PORTDCLR = 0x0004;
PORTDSET = 0x0008;
DelayMotor();
PORTDCLR = 0x0008;
PORTDSET = 0x0010;
DelayMotor(); 
}   
TMR2=0; TMR4=0;
PORTDSET=0x0021;
DelayUsec(15);
PORTDCLR=0x0021;
DelayMsec(30);
DisplayTMR2();
DisplayTMR4(); 
cur_distance1[7-i]=TMR2; cur_distance2[7-i]=TMR4;
if (cur_distance1[7-i]-ref_distance1[i]>300 || ref_distance1[i]-cur_distance1[7-i]>300
||cur_distance2[7-i]-ref_distance2[i]>300 || ref_distance2[i]-cur_distance2[7-i]>300)
{PORTDbits.RD7=1;}
DelayMsec(20);
PORTDbits.RD7=0;
}
}
}
}
Exemple #30
0
int main(void) {
    SYSTEMConfigPerformance(10000000);
    enableInterrupts(); //This function is necessary to use interrupts.
    
    initTimer2();
    initTimer3();
    switch1();
    initPWM();
    
    initADC();
    initLCD();
    
    state = IDLE_1;
    
    volatile float printbuffer = 0; //maybe change to float
   // double voltPOT = 0;
    char str[16];
    
    while(1){
//           clearLCD();
//           snprintf(str, sizeof(str), "%0.2f", (float)potVoltage/1023.0*5.0);
//           printStringLCD(str);
//           moveCursorLCD(0,0);
       switch(state){
           voltage = potVoltage*1.0;
            case IDLE_1:
                setLeftForward(1);
                setRightForward(1);
                setLeftWheelSpeed(0);
                setRightWheelSpeed(0);
                break;
           case D_IDLE_1:
                delayUs(100);
                state = FORWARD;
                break;
            case FORWARD:
                CalculatedSpeed();
                AD1CON1bits.SAMP = 1;
                break;
            case D_FORWARD:
                delayUs(100);
                state = IDLE_2;
                break;
            case IDLE_2:
                setLeftWheelSpeed(0);
                setRightWheelSpeed(0);
                break;
            case D_IDLE_2:
                delayUs(100);
                state = BACKWARD;
                break;
            case BACKWARD:
                setLeftForward(0);
                setRightForward(0);
                CalculatedSpeed();
                AD1CON1bits.SAMP = 1;
                break; 
            case D_BACKWARD:
                delayUs(100);
                state = IDLE_1;
                break;
        }
    }
    return 0;
}