Esempio n. 1
0
/**
* This function will initial board.
 */
void rt_hw_board_init()
{
    // Configure the device for maximum performance, but do not change the PBDIV clock divisor.
	// Given the options, this function will change the program Flash wait states,
	// RAM wait state and enable prefetch cache, but will not change the PBDIV.
    // The PBDIV value is already set via the pragma FPBDIV option above.
   	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

   	/* use DBPRINTF */
   	/* rt_hw_console_init(); */

	rt_hw_usart_init();
	rt_console_set_device("uart1");

   	rt_hw_show_info();

    // enable multi-vector interrupts
    INTEnableSystemMultiVectoredInt();
    rt_hw_interrupt_disable();

//    // STEP 2. configure the core timer
//    OpenCoreTimer(CORE_TICK_RATE);
//
//    // set up the core timer interrupt with a prioirty of 2 and zero sub-priority
//    mConfigIntCoreTimer((CT_INT_ON | CT_INT_PRIOR_2 | CT_INT_SUB_PRIOR_0));

    // STEP 2. configure Timer 1 using internal clock, 1:256 prescale
    OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_256, T1_TICK);
    // set up the timer interrupt with a priority of 2
    ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);

    /* Setup the software interrupt. */
	mConfigIntCoreSW0( CSW_INT_ON | CSW_INT_PRIOR_1 | CSW_INT_SUB_PRIOR_0 );
}
void PmodHB5_INIT(UART_MODULE uartID)
{
	PORTSetPinsDigitalOut(IOPORT_D, BIT_7); //HB5 Direction
	PORTSetPinsDigitalOut(IOPORT_D, BIT_1); //HB5 Enable
	PORTSetPinsDigitalIn(IOPORT_D, BIT_9);  //HB5 Sensor A
	PORTSetPinsDigitalIn(IOPORT_C, BIT_1);  //HB5 Sensor B

	hBridge.sensorAport = IOPORT_D;
	hBridge.sensorAportBit = BIT_9;
	hBridge.sensorBport = IOPORT_C;
	hBridge.sensorBportBit = BIT_1;
	hBridge.directionPort = IOPORT_D;
	hBridge.directionPortBit = BIT_7;
	hBridge.currentDirection = PMOD_HB5_DIR_CW;
	hBridge.newDirection = PMOD_HB5_DIR_CW;
	hBridge.ocChannel = 2;
	

    OpenOC2(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);
    OpenTimer2(T2_ON | T2_PS_1_256, SYSTEM_CLOCK/PB_DIV/PRESCALE/(TOGGLES_PER_SEC/2));
    
	OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_256, T1_TICK);
    ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
	INTEnableSystemMultiVectoredInt();
	
	UARTPutS("\r\nPmodHB5 init complete\r\n",uartID);

}
Esempio n. 3
0
/*
 * Setup a timer for a regular tick.
 */
void prvSetupTimerInterrupt( void )
{
const unsigned long ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;

	OpenTimer1( ( T1_ON | T1_PS_1_8 | T1_SOURCE_INT ), ulCompareMatch );
	ConfigIntTimer1( T1_INT_ON | configKERNEL_INTERRUPT_PRIORITY );
}
void ServoInitFunc(void) {
    servo(0);

    ConfigIntTimer1(T1_INT_PRIOR_1 & T1_INT_ON);
    //1msec(4/80Mhz×1×200=0.01msec)
    OpenTimer1(T1_ON & T1_GATE_OFF & T1_PS_1_1 & T1_SYNC_EXT_OFF & T1_SOURCE_INT,200-1);
}
Esempio n. 5
0
extern void timerStart(timer * pTimer, const timerCalc * pTimerCalc)
{
  pTimer->m_TimerCalc = *pTimerCalc;

  const uint16_t        PrescalerBits = pTimer->m_TimerCalc.PrescalerBits;
  const uint16_t        PriorityBits  = pTimer->m_TimerCalc.PriorityBits;
  const uint16_t        Ticks         = pTimer->m_TimerCalc.Ticks;
  const timer_tCallback pCallback     = pTimer->m_pCallback;

  pTimer->m_OverflowCount = pTimer->m_TimerCalc.OverflowCount;

  switch (pTimer->m_TimerNumber) {
    case 1: 
      OpenTimer1(T1_ON | T1_SOURCE_INT | PrescalerBits, Ticks);
      ConfigIntTimer1((pCallback == NULL ? T1_INT_OFF : T1_INT_ON) | PriorityBits);
      break;
    case 2: 
      OpenTimer2(T2_ON | T2_SOURCE_INT | PrescalerBits, Ticks);
      ConfigIntTimer2((pCallback == NULL ? T2_INT_OFF : T2_INT_ON) | PriorityBits);
      break;
    case 3: 
      OpenTimer3(T3_ON | T3_SOURCE_INT | PrescalerBits, Ticks);
      ConfigIntTimer3((pCallback == NULL ? T3_INT_OFF : T3_INT_ON) | PriorityBits);
      break;
    case 4: 
      OpenTimer4(T4_ON | T4_SOURCE_INT | PrescalerBits, Ticks);
      ConfigIntTimer4((pCallback == NULL ? T4_INT_OFF : T4_INT_ON) | PriorityBits);
      break;
    case 5: 
      OpenTimer5(T5_ON | T5_SOURCE_INT | PrescalerBits, Ticks);
      ConfigIntTimer5((pCallback == NULL ? T5_INT_OFF : T5_INT_ON) | PriorityBits);
      break;
  }
}
Esempio n. 6
0
/* Specify Interrupt Priority Level = 2 */
void __ISR(_TIMER_1_VECTOR, IPL2) _Timer1Handler(void) {

    mT1ClearIntFlag();
    if(bitIndex == 0)
    {
        
        OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, (getEtu()/5));
        write(PIN_IO);
        bitIndex++;
    }
    else if(bitIndex == 39)
    {
        write(PIN_IO);
        bitIndex++;
        setFlag();
    }
    else if(bitIndex == 40)
    {
        bitIndex =0;
        ConfigINT2(EXT_INT_ENABLE | FALLING_EDGE_INT | EXT_INT_PRI_1);
        ConfigIntTimer1(T1_INT_OFF);
    }
    else
    {
        write(PIN_IO);
        bitIndex++;
    }
}
Esempio n. 7
0
int main(void)
{
  uint16_t i;
  FRESULT rc;

  map_io();
  init_port();

  InitRTCC();

  uart2_init();
  xdev_out(uart2_put);
  xdev_in(uart2_get);

  dbg_printf("$" PROJECT_NAME "\n");
  dbg_printf("$" __DATE__ " " __TIME__ "\n");

  rc = f_mount(&fatfs, "", 1);
  dbg_printf("$FF,f_mount,%s\n", get_rc(rc));

  OpenTimer1(T1_PS_1_256 & T1_GATE_OFF & T1_SOURCE_INT & T1_IDLE_CON &
    T1_ON & T1_SYNC_EXT_OFF, 0xFFFF);
  ConfigIntTimer1(T1_INT_ON & T1_INT_PRIOR_1);
  OpenCapture1(IC_IDLE_STOP & IC_TIMER1_SRC & IC_INT_1CAPTURE & IC_EVERY_RISE_EDGE,
    IC_CASCADE_DISABLE & IC_TRIGGER_ENABLE & IC_UNTRIGGER_TIMER & IC_SYNC_TRIG_IN_DISABLE);
  ConfigIntCapture1(IC_INT_ON & IC_INT_PRIOR_5);
  _IC1IF = 0;
  

  while (1) {
    while (_RTCSYNC == 0);
    while (_RTCSYNC == 1);
    if (gps_pr > 0) {
      _T1IE = 0;
      float f = (float) TMR1 / gps_pr;
      _T1IE = 1;
      xprintf("%u\n", (uint16_t) (f * 1000));
    }
    if (ngpslines > 0) {
      ngpslines--;
      if (xgets(gps_line, 128)) {
        xprintf("$GPS%s\n", gps_line);
      }
    }
  }

  while (0) {
    while (_RTCSYNC == 0);
    while (_RTCSYNC == 1);
    if (gps_pr > 0) {
      _T1IE = 0;
      float f = (float) TMR1 / gps_pr;
      _T1IE = 1;
      xprintf("%u\n", (uint16_t) (f * 1000));
    }
  }

  return (EXIT_SUCCESS);
}
Esempio n. 8
0
void InitTMR1(void)
{
    /* config: T1_OFF & T1_IDLE_CON & T1_SOURCE_INT & T1_PS_1_64 */
    // Interrupt period = 0.005 sec with a 64 prescaler
    OpenTimer1(T1_OFF & T2_GATE_OFF & T1_IDLE_CON & T1_PS_1_64 & T1_SOURCE_INT,3124);
    ConfigIntTimer1(T1_INT_PRIOR_6 & T1_INT_ON);
    T1CONbits.TON = 1; // Turn on timer 1
    return;
}
Esempio n. 9
0
/****************************************************************************
 Function
     TIMERS_Init

 Parameters
    none

 Returns
     None.

 Description
     Initializes the timer module
 Notes
     None.

 Author
     Max Dunne, 2011.11.15
 ****************************************************************************/
void TIMERS_Init(void) {
    TimerActiveFlags = 0;
    TimerEventFlags = 0;
    FreeRunningTimer = 0;
    OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, F_PB / TIMER_FREQUENCY);
    ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_3);

    mT1IntEnable(1);
}
Esempio n. 10
0
void InitApp(void)
{
    /* Setup analog functionality and port direction */

    // enable multi-vector interrupts
    INTEnableSystemMultiVectoredInt();

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // STEP 2. configure Timer 1 using internal clock, 1:256 prescale

    OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, T1_TICK);

    // set up the timer interrupt with a priority of 2
    ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
    
    /* Zero to DELAY_COUNTER for Delay procedure. */
    StartTimer();

    /* Initialize peripherals */

    DisplayBacklightConfig();

//#if defined WANT_GOL_INIT
    GOLInit();
//#endif

//    TouchInit(NULL,NULL,NULL,NULL);

    DisplayBacklightOn();

    SPIFlashInit();
    
// initialize the components for Resistive Touch Screen
//#if defined WANT_TOUCH_INIT
//    TouchInit(NULL,NULL,NULL,NULL);
//#endif
//    SDCARDInit();
//    MP3Init();

    /* PORT Init                                                            */
    /* PORTD 0-3 bits inputs.                                               */
    /* PORTA 0-1 bits outputs.                                              */

    /* TODO Must be disable the JTAG port, that we are to appropriate using */
    /* RA0, and RA1 ports.  */

    DDPCONbits.JTAGEN = 0;  /* JTAG debug disabled */
    /* TODO D port output now !!!! */
    TRISDbits.TRISD14 = TRISDbits.TRISD15 = 0;
        /* Port D 14, 15, pins pull up resistor enabled */
//    CNPUEbits.CNPUE20 = CNPUEbits.CNPUE21 = 1;
    TRISAbits.TRISA0 = TRISAbits.TRISA1 = 0;

}
Esempio n. 11
0
//************************************************************************
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
{
uint32_t tonePeriod;
uint8_t port;

	// Should have an error check here for pin number out of range.
	//*	there is no standard on the number of pins. Since we want this to work on all versions of the PIC32
	//*	I have set it to 112 for now which is the largest I/O pin count on a pic32
	if ((frequency > 0) && (_pin < 112))
	{
			
		// If a tone is currently playing on a different pin, the function is
		// documented to have no effect. If playing on the same pin, change
		// the frequency. If not currently playing, initialize the timer.
		// This is currently hard coded to use timer1.
		if (tone_pin == 255)
		{
			// No tone currently playing. Init the timer.
			T1CON	=	T1_PS_1_256;
			mT1ClearIntFlag();
			ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_3 | T1_INT_SUB_PRIOR_1);
		}
		else if (_pin != tone_pin)
		{
			// Tone currently playing on another pin. ignore this call.
			return;
		}

		// Determine which port and bit are requested.
		tone_pin		=	_pin; 
		port			=	digitalPinToPort(_pin);
		tone_pin_port	=	portOutputRegister(port);
		tone_pin_mask	=	digitalPinToBitMask(_pin);

		// Ensure that the pin is a digital output
		pinMode(_pin, OUTPUT);

		// Duration 0 means to play forever until stopped. Other values
		// mean to play for that many milliseconds.
		if (duration > 0)
		{
			timer1_toggle_count	=	(2 * frequency * duration) / 1000;
		}
		else
		{
			timer1_toggle_count	=	-1;
		}

		TMR1		=	0;
		PR1			=	((F_CPU / 256) / 2 / frequency);
		T1CONSET	=	T1_ON;
	}
}
Esempio n. 12
0
/**********************************************************************
 * Function: Timer_init()
 * @return none
 * @remark Configures the timer module.
 **********************************************************************/
void Timer_init(void) {
    timerActiveFlags = 0;
    timerEventFlags = 0;
    freeRunningTimer = 0;

    OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, F_PB / TIMER_FREQUENCY);
    ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_3);

    mT1IntEnable(1);

    timerInitialized = 1;
}
Esempio n. 13
0
/* Specify interupt priority 1, but stop the interupt once detected */
void __ISR(_EXTERNAL_2_VECTOR, IPL1) _Int2Handler(void) {

    uint state = getState();
    if(STATE_BEGINING == state)
    {
        /* STEP 1. configure the Timer1*/
        mT1ClearIntFlag();
        OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, 64000);
        /* STEP 2. set the timer interrupt to prioirty level 2 */
        ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
        writeBit(1);
        writeBit(1);
        setState(STATE_FIRST_EDGE);
    }
    else if( STATE_FIRST_EDGE == state)
    {
          ConfigIntTimer1(T1_INT_OFF);
          setEtu(ReadTimer1()/3);
          OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, getEtu()/5);
          /* STEP 2. set the timer interrupt to prioirty level 2 */
          ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
          /*Stopping the timer 1 and reading the value*/
          DisableINT2;
          
          /*the two first bits of ts are sets*/
          setState(STATE_TS);
          bitIndex = 10 ;
    }
    else
    {
        /* state == normal processing or no atr but an etu value is set*/
          DisableINT2;
          OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, getEtu());
          ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
    }
     /* The flag is cleared at the end to avoid stack overflow
    * PORTDbits.RD0 is the EXTERNAL_0_VECTOR*/

    mINT2ClearIntFlag();
}
Esempio n. 14
0
//Timer 1 setup function
int sysServiceConfigT1(unsigned int T1conval, unsigned int T1perval,
                        unsigned int T1intconval){
    //Todo: is there any way to have a compile time semaphore here?
    if(T1_already_confgured){
        return -1;
    } 
    else{
        T1_already_confgured = 1;
        OpenTimer1(T1conval, T1perval);
        ConfigIntTimer1(T1intconval);
        return 0;
    }
}
Esempio n. 15
0
void MainInit(void) {
  PORTSetPinsDigitalOut(IOPORT_D, PD_LED_HEARTBEAT);
  sysSec = 0;
  sysTicks = 0;
  sysTickEvent = 0;

	SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

  OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_64, T1_TICK);
  
  // set up the timer interrupt with a priority of 2
  ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
} // MainInit()
Esempio n. 16
0
/*
 * Function suspends program using timer1
 * inputs:  usec - delay time in useconds
 * returns: -1 - error
 * Tpr = Tcyc*PRE*PR
 * PR = Tpr/(Tcyc*PRE) = Tpr/(0.025*Pre)
 */
int delay_t1_us(unsigned int usec) {
    unsigned int ticks = 5 * usec; //ticks = Fcyc/Pre*usec, PRE = 8
    if (usec > DELAY_T1_US_MAX)
        return -1;

    ConfigIntTimer1(T1_INT_OFF);    //Disable timer interrupt
    IFS0bits.T1IF = 0; //Clear interrupt flag
    OpenTimer1(T1_ON | T1_PS_1_8, ticks); //Configure timer

    while (!IFS0bits.T1IF); //Wait for usec

    CloseTimer1();
    return 1;
}
Esempio n. 17
0
void SetupInterrupts(void)
{
    #if defined(__IMAGEPROC2)

        ConfigINT2(RISING_EDGE_INT & EXT_INT_ENABLE & EXT_INT_PRI_7); // Battery Supervisor

    #elif defined(__IMAGEPROC1)

        ConfigINT0(RISING_EDGE_INT & EXT_INT_ENABLE & EXT_INT_PRI_7); // Battery Supervisor

    #endif
    ConfigIntTimer1(T1_INT_PRIOR_4 & T1_INT_OFF);
    ConfigIntTimer2(T2_INT_PRIOR_4 & T2_INT_OFF);
}
Esempio n. 18
0
/** タイマ初期化.
 */
void timer_init(void)
{
	// タイマ1をオープン
	OpenTimer1(	T1_ON				/* Timer1 ON */
		 		& T1_IDLE_CON			/* operate during sleep */
		 		& T1_GATE_OFF			/* Timer Gate time accumulation disabled */
		 		& T1_PS_1_256			/* Prescaler 1:256 */
		 		& T1_SYNC_EXT_OFF		/* Do not synch external clk input */
		 		& T1_SOURCE_INT,		/* Internal clock source */
		 		TIMER_INTERVAL);		// 1ms interval

	// タイマ1割り込み設定
	ConfigIntTimer1(	T1_INT_PRIOR_4	/* 100 = Interrupt is priority 4 */
		 			& T1_INT_ON);		/* Interrupt Enable */
}
Esempio n. 19
0
/*
 * Function suspends program using timer1
 * int msec - desired CPU "sleep" time in milliseconds
 */
void delay_t1(int msec) {
    int count = msec / 400;
    int i;
    for(i = 0; i <= count; i++) {
        OpenTimer1(T1_ON | T1_PS_1_64, ticks_from_ms(msec, 64));
        ConfigIntTimer1(T1_INT_OFF | T1_INT_PRIOR_1);
        while (1) {
            int dummy = IFS0bits.T1IF;
            if (dummy == 1)
                break;
        }

        CloseTimer1();
    }
}
Esempio n. 20
0
void timersInit()
{
    //T4 is input from encoder. Read from TMR4 to see number of ticks counted.
    T4CON = 0x0;                //T4 disabled
    T4CONSET = 0x0002;          //T4 External Clock Source, 1:1 prescaler
    TMR4 = 0x0;                 //Reset T4 counter
    PR4 = 0xffff;               //Set T4 period to max
    //mT4ClearIntFlag();          //clear interrupt flag
    //mT4SetIntPriority(7);       //set Timer4 Interrupt Priority
    //mT4IntEnable(1);            //enable timer4 interrupts
    T4CONSET = 0x8000;          //T4 enable

    //T5 is input from encoder. Read from TMR5 to see number of ticks counted.
    T5CON = 0x0;                //T5 disabled
    T5CONSET = 0x0002;          //T5 External Clock Source, 1:1 prescaler
    TMR5 = 0x0;                 //Reset T5
    PR5 = 0xffff;               //Set T5 period to max
    //mT5ClearIntFlag();          //clear T5 interrupt flag
    //mT5SetIntPriority(2);       //set Timer5 Interrupt Priority
    //mT5IntEnable(1);            //enable timer5 interrupts
    T5CONSET = 0x8000;          //T5 enable

        //The following code snippet opens and uses Timer1,2,3 as an interrupt.
        //Open Timer1 with 1:8 prescaler (80MHz -> 10MHz), with period of 1250, therefore tick = 8kHz.
        OpenTimer1(T1_ON | T1_PS_1_8 | T1_SOURCE_INT, 1000);
        ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);

        OpenTimer2(T2_ON | T2_PS_1_8 | T2_SOURCE_INT, 1000);
        ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);

        OpenTimer3(T3_ON | T3_PS_1_8 | T3_SOURCE_INT, 1000);
        ConfigIntTimer1(T3_INT_ON | T3_INT_PRIOR_2);

        INTEnableSystemMultiVectoredInt();

}
Esempio n. 21
0
void SetupTimer1(void)
{
    OpenTimer1(
        T1_ON           &
        T1_IDLE_CON     &
        T1_GATE_OFF     &
        T1_PS_1_256       &
        T1_SOURCE_INT,
        0xFFFF
    );
    ConfigIntTimer1(
           T1_INT_PRIOR_0   &
           T1_INT_OFF
    );
    return;
}
Esempio n. 22
0
void Timer1_Setup(void)
{
    OpenTimer1(
        T1_ON &
        T1_IDLE_CON &
        T1_GATE_OFF &
        T1_PS_1_256 &
        T1_SYNC_EXT_OFF &
        T1_SOURCE_INT,
        0x9897);

    ConfigIntTimer1(
        T1_INT_PRIOR_3 &
        T1_INT_ON
    );

    return;
}
Esempio n. 23
0
inline void Timer1_Setup(void)
{
    OpenTimer1(
            T1_OFF &
            T1_IDLE_CON &       
            T1_GATE_OFF &
            T1_PS_1_256 &
            T1_SYNC_EXT_OFF &
            T1_SOURCE_INT,
            39063); //for 250 ms.

    ConfigIntTimer1(
            T1_INT_PRIOR_3 &
            T1_INT_ON
                    );

    return;
}
Esempio n. 24
0
//--------------------------------------------------------------------------------------------------------------
// Init Timers
void Init_Steppers(){

	STEPPERS_TRIS	&= 0xFE00; 	// configure PORTB for output (STEPPERS)
	ADPCFG			 = 0xFFFF;	// configure PORTB for digital output
	MAIN_SWITCH		 = 0;		// turn on the main switch mosfet

/* Enable Timer1 Interrupt and Priority to "5" */
    ConfigIntTimer1(T1_INT_PRIOR_6 & T1_INT_ON);

    OpenTimer1(T1_ON & T1_GATE_OFF & T1_PS_1_8 & 
			 	T1_SOURCE_INT, 0xFFFF);

/* Enable Timer2 Interrupt and Priority to "5" */
    ConfigIntTimer2(T2_INT_PRIOR_6 & T2_INT_ON);

    OpenTimer2(T2_ON & T2_GATE_OFF & T2_PS_1_8 
				 & T2_SOURCE_INT, 0xFFFF);

}
int main(void)
{
//LOCALS
	unsigned int temp;
	unsigned int channel1, channel2;
	M1_stepPeriod = M2_stepPeriod = M3_stepPeriod = M4_stepPeriod = 50; // in tens of u-seconds
	unsigned char M1_state = 0, M2_state = 0, M3_state = 0, M4_state = 0;

	SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

/* TIMER1 - now configured to interrupt at 10 khz (every 100us) */
	OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, T1_TICK);
	ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
/* TIMER2 - 100 khz interrupt for distance measure*/
	OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_1, T2_TICK);
	ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_3); //It is off until trigger

/* PORTA b2 and b3 for servo-PWM */
	mPORTAClearBits(BIT_2 | BIT_3);
	mPORTASetPinsDigitalOut(BIT_2 | BIT_3);

/* ULTRASONICS: some bits of PORTB for ultrasonic sensors */
	PORTResetPins(IOPORT_B, BIT_8 | BIT_9| BIT_10 | BIT_11 );	
	PORTSetPinsDigitalOut(IOPORT_B, BIT_8 | BIT_9| BIT_10 | BIT_11); //trigger
/* Input Capture pins for echo signals */
	//interrupt on every risging/falling edge starting with a rising edge
	PORTSetPinsDigitalIn(IOPORT_D, BIT_8| BIT_9| BIT_10| BIT_11); //INC1, INC2, INC3, INC4 Pin
	mIC1ClearIntFlag();
	OpenCapture1(  IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//front
	ConfigIntCapture1(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3);
	OpenCapture2(  IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//back
	ConfigIntCapture2(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3);
	OpenCapture3(  IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//left
	ConfigIntCapture3(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3);
	OpenCapture4(  IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//right
	ConfigIntCapture4(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3);

/* PINS used for the START (RD13) BUTTON */
    PORTSetPinsDigitalIn(IOPORT_D, BIT_13);
	#define CONFIG          (CN_ON | CN_IDLE_CON)
	#define INTERRUPT       (CHANGE_INT_ON | CHANGE_INT_PRI_2)
	mCNOpen(CONFIG, CN19_ENABLE, CN19_PULLUP_ENABLE);
	temp = mPORTDRead();

/* PORT D and E for motors */
	//motor 1
	mPORTDSetBits(BIT_4 | BIT_5 | BIT_6 | BIT_7); 		// Turn on PORTD on startup.
	mPORTDSetPinsDigitalOut(BIT_4 | BIT_5 | BIT_6 | BIT_7);	// Make PORTD output.
	//motor 2
	mPORTCSetBits(BIT_1 | BIT_2 | BIT_3 | BIT_4); 		// Turn on PORTC on startup.
	mPORTCSetPinsDigitalOut(BIT_1 | BIT_2 | BIT_3 | BIT_4);	// Make PORTC output.
	//motor 3 and 4
	mPORTESetBits(BIT_0 | BIT_1 | BIT_2 | BIT_3 |
					BIT_4 | BIT_5 | BIT_6 | BIT_7); 		// Turn on PORTE on startup.
	mPORTESetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3 |
					BIT_4 | BIT_5 | BIT_6 | BIT_7);	// Make PORTE output.

// UART2 to connect to the PC.
	// This initialization assumes 36MHz Fpb clock. If it changes,
	// you will have to modify baud rate initializer.
    UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);
    UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
    UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
    UARTSetDataRate(UART2, GetPeripheralClock(), BAUD);
    UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
	// Configure UART2 RX Interrupt
	INTEnable(INT_SOURCE_UART_RX(UART2), INT_ENABLED);
    INTSetVectorPriority(INT_VECTOR_UART(UART2), INT_PRIORITY_LEVEL_2);
    INTSetVectorSubPriority(INT_VECTOR_UART(UART2), INT_SUB_PRIORITY_LEVEL_0);


/* PORTD for LEDs - DEBUGGING */
	mPORTDClearBits(BIT_0 | BIT_1 | BIT_2);
	mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2);

	

// Congifure Change/Notice Interrupt Flag
	ConfigIntCN(INTERRUPT);
// configure for multi-vectored mode
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
// enable interrupts
    INTEnableInterrupts();


	counterDistanceMeasure=600; //measure ULTRASONICS distance each 60 ms

	while (1) {
	
/***************** Robot MAIN state machine *****************/
		unsigned char ret = 0;
		switch (Robo_State) {
			case 0:
				MotorsON = 0;
				Robo_State = 0;

				InvInitialOrientation(RESET);
				TestDog(RESET);
				GoToRoom4short(RESET);
				BackToStart(RESET);
				InitialOrientation(RESET);
				GoToCenter(RESET);
				GoToRoom4long(RESET);
				break;
			case 1:
				ret = InvInitialOrientation(GO);
				if (ret == 1) {
					Robo_State = 2;
				}
				break;
			case 2:
				ret = TestDog(GO);
				if (ret == 1) {
					Robo_State = 3;		//DOG not found
				} else if (ret == 2) {
					Robo_State = 4;		//DOG found
				}
				break;
			case 3:
				ret = GoToRoom4short(GO);
				if (ret == 1) {
					Robo_State = 0;
				}
				break;
			case 4:
				ret = BackToStart(GO);
				if (ret == 1) {
					Robo_State = 5;
				}
				break;
			case 5:
				ret = GoToCenter(GO);
				if (ret == 1) {
					Robo_State = 6;
				}
				break;
			case 6:
				ret = GoToRoom4long(GO);
				if (ret == 1) {
					Robo_State = 0;
				}
				break;
		}

		if (frontDistance < 30 || backDistance < 30 || leftDistance < 30 || rightDistance < 30)
			mPORTDSetBits(BIT_0);
		else 
			mPORTDClearBits(BIT_0);
/***************************************************************/


/***************** Motors State Machine ************************/

		if (MotorsON) {
			/****************************
			MOTOR MAP
				M1 O-------------O M2   ON EVEN MOTORS, STEPS MUST BE INVERTED
					|	 /\		|			i.e. FORWARD IS BACKWARD
					|	/  \	|
					|	 || 	|
					|	 ||		|
				M3 O-------------O M4
			*****************************/
			if (M1_counter == 0) {
				switch (M1_state) {
					case 0: // set 0011
						step (0x3 , 1);
						if (M1forward)
							M1_state = 1;
						else
							M1_state = 3;
						break;
					case 1: // set 1001
						step (0x9 , 1);
						if (M1forward)
							M1_state = 2;
						else
							M1_state = 0;
						break;
					case 2: // set 1100
						step (0xC , 1);
						if (M1forward)
							M1_state = 3;
						else
							M1_state = 1;
						break;
					case 3: // set 0110
					default:
						step (0x6 , 1);
						if (M1forward)
							M1_state = 0;
						else
							M1_state = 2;
						break;	
				}
				M1_counter = M1_stepPeriod;
				step_counter[0]--;
				if (directionNow == countingDirection)
					step_counter[1]--;
			}
			
			if (M2_counter == 0) {
				switch (M2_state) {
					case 0: // set 0011
						step (0x3 , 2);
						if (M2forward)
							M2_state = 1;
						else
							M2_state = 3;
						break;
					case 1: // set 0110
						step (0x6 , 2);
						if (M2forward)
							M2_state = 2;
						else
							M2_state = 0;
						break;
					case 2: // set 1100
						step (0xC , 2);
						if (M2forward)
							M2_state = 3;
						else
							M2_state = 1;
						break;
					case 3: // set 1001
					default:
						step (0x9 , 2);
						if (M2forward)
							M2_state = 0;
						else
							M2_state = 2;
						break;	
				}
				M2_counter = M2_stepPeriod;
			}

			if (M3_counter == 0) {
				switch (M3_state) {
					case 0: // set 0011
						step (0x3 , 3);
						if (M3forward)
							M3_state = 1;
						else
							M3_state = 3;
						break;
					case 1: // set 1001
						step (0x9 , 3);
						if (M3forward)
							M3_state = 2;
						else
							M3_state = 0;
						break;
					case 2: // set 1100
						step (0xC , 3);
						if (M3forward)
							M3_state = 3;
						else
							M3_state = 1;
						break;
					case 3: // set 0110
					default:
						step (0x6 , 3);
						if (M3forward)
							M3_state = 0;
						else
							M3_state = 2;
						break;	
				}
				M3_counter = M3_stepPeriod;
			}
			
			if (M4_counter == 0) {
				switch (M4_state) {
					case 0: // set 0011
						step (0x3 , 4);
						if (M4forward)
							M4_state = 1;
						else
							M4_state = 3;
						break;
					case 1: // set 0110
						step (0x6 , 4);
						if (M4forward)
							M4_state = 2;
						else
							M4_state = 0;
						break;
					case 2: // set 1100
						step (0xC , 4);
						if (M4forward)
							M4_state = 3;
						else
							M4_state = 1;
						break;
					case 3: // set 1001
					default:
						step (0x9 , 4);
						if (M4forward)
							M4_state = 0;
						else
							M4_state = 2;
						break;	
				}
				M4_counter = M4_stepPeriod;
			}
		} else {
			//motors off
			mPORTDSetBits(BIT_4 | BIT_5 | BIT_6 | BIT_7);
			mPORTCSetBits(BIT_1 | BIT_2 | BIT_3 | BIT_4);
			mPORTESetBits(BIT_0 | BIT_1 | BIT_2 | BIT_3 |
					BIT_4 | BIT_5 | BIT_6 | BIT_7);
		}
/************************************************************/
		

/******* TEST CODE, toggles the servos (from 90 deg. to -90 deg.) every 1 s. ********/
/*		if (auxcounter == 0) {
			
			servo1_angle = 0;

			if (servo2_angle == 90)
				servo2_angle = -90;
			else
				servo2_angle = 90;

			auxcounter = 20000;		// toggle angle every 2 s.
		}
*/

		servo1_angle = 0;
		servo2_angle = -90;
	/*
		if (frontDistance > 13 && frontDistance < 17) {
			servo2_angle = 90;
		}
		else
			servo2_angle = -90;
	*/
/*******************************************************************/


/****************** SERVO CONTROL ******************/
		/*
			Changing the global servoX_angle at any point in the code will 
			move the servo to the desired angle.
		*/
		servo1_counter = (servo1_angle + 90)*(18)/180 + 6; // between 600 and 2400 us
		if (servo1_period == 0) {
			mPORTASetBits(BIT_2);
			servo1_period = SERVOMAXPERIOD; 		/* 200 * 100us = 20000us period  */
		}

		servo2_counter = (servo2_angle + 90)*(18)/180 + 6; // between 600 and 2400 us
		if (servo2_period == 0) {
			mPORTASetBits(BIT_3);
			servo2_period = SERVOMAXPERIOD; 		/* 200 * 100us = 20000us period  */
		}
/*****************************************************/
	
	} /* end of while(1)  */
		
	return 0;
}
Esempio n. 26
0
//
// Main application entry point.
//
int main(void)
{
	static TICK t = 0;
	static DWORD dwLastIP = 0;
	
	// Initialize application specific hardware
	InitializeBoard();

	// Initialize stack-related hardware components that may be 
	// required by the UART configuration routines
    TickInit();
	MPFSInit();

	// Initialize Stack and application related NV variables into AppConfig.
	InitAppConfig();    

	// Initialize core stack layers (MAC, ARP, TCP, UDP) and
	// application modules (HTTP, SNMP, etc.)
    StackInit();

	// Initialize any application-specific modules or functions/
	// For this demo application, this only includes the
	// UART 2 TCP Bridge

	//************** PIC32-WEB ********************
	//***** Additional initializations Start: *****
	// 1. UART1
	USART_Init(115200,(GetPeripheralClock()));
	USART_Test_Menu_Begin();
	// 2. Timer1
	// configure Timer 1 using external clock(32768Hz), 1:1 prescale, 
	// period 0x8000, thus set interrupt on every 1sec
	// Blink LED0 (right most one) with frequency 2Hz at every one Timer1 interrupt.
    OpenTimer1(T1_ON | T1_SOURCE_EXT | T1_PS_1_1, 0x8000);
    // set up the timer interrupt with a priority of 3
    ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_3);
	//CloseTimer1(); // Switches off the Timer1
	mPORTASetPinsDigitalOut(BIT_2);	// Set RA2 like out -> LED1

	// 3. Enable interrupts at the end of initialization
	// enable multi-vector interrupts
    INTEnableSystemMultiVectoredInt();
	//***** Additional initializations End: *****
	

	// Now that all items are initialized, begin the co-operative
	// multitasking loop.  This infinite loop will continuously 
	// execute all stack-related tasks, as well as your own
	// application's functions.  Custom functions should be added
	// at the end of this loop.
    // Note that this is a "co-operative mult-tasking" mechanism
    // where every task performs its tasks (whether all in one shot
    // or part of it) and returns so that other tasks can do their
    // job.
    // If a task needs very long time to do its job, it must be broken
    // down into smaller pieces so that other tasks can have CPU time.

	while(1){

	// Run all enabled web demo applications:
		
    // This task performs normal stack task including checking
    // for incoming packet, type of packet and calling
    // appropriate stack entity to process it.
    StackTask();
        
    // This tasks invokes each of the core stack application tasks
    StackApplications();

	// Process application specific tasks here.
	// For this demo app, this will include the Generic TCP 
	// client and servers, and the SNMP, Ping, and SNMP Trap
	// demos.  Following that, we will process any IO from
	// the inputs on the board itself.
	// Any custom modules or processing you need to do should
	// go here.
		
	#if defined(STACK_USE_ICMP_CLIENT)
	PingDemo();
	#endif
		
	#if defined(STACK_USE_SNMP_SERVER) && !defined(SNMP_TRAP_DISABLED)
	SNMPTrapDemo();
	if(gSendTrapFlag)
		SNMPSendTrap();
	#endif
		
	#if defined(STACK_USE_BERKELEY_API)
	BerkeleyTCPClientDemo();
	BerkeleyTCPServerDemo();
	BerkeleyUDPClientDemo();
	#endif

	ProcessIO();

    // If the local IP address has changed (ex: due to DHCP lease change)
    // write the new IP address to the LCD display, UART, and Announce 
    // service
	if(dwLastIP != AppConfig.MyIPAddr.Val)
	{
		#if defined(STACK_USE_ANNOUNCE)
			AnnounceIP();
		#endif
	}
  }       
}
Esempio n. 27
0
void inline __attribute__((always_inline)) timer1_init()
{
    ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2); // configure interrupt
    INTEnableSystemMultiVectoredInt();
}
Esempio n. 28
0
timer1Init()
{
    OpenTimer1(T1_ON | T1_PS_1_8 | T1_SOURCE_INT, 1000);
    ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
    INTEnableSystemMultiVectoredInt();
}
Esempio n. 29
0
/*****************************************************************************
  Function:
	void TickInit(void)

  Summary:
	Initializes the Tick manager module.

  Description:
	Configures the Tick module and any necessary hardware resources.

  Precondition:
	None

  Parameters:
	None

  Returns:
  	None

  Remarks:
	This function is called only one during lifetime of the application.
  ***************************************************************************/
void TickInit(void)
{
    OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_256, T1_TICK);
    ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_7);
}
void hal_tick_init(void)
{
	ConfigIntTimer1(T1_INT_ON|T1_INT_PRIOR_3);
	OpenTimer1(T1_ON|T1_PS_1_64,0x7a12);
}