Beispiel #1
0
/* There are three possibilities for the step rate calculation that keep the
   step rate error to under 0.2% of the desired value, from within 0.5 to 2KHz:
   from 78 to 20Khz, Prescale is 1:8, Rollover = (F_PB/8-1)/PPS
   from 10 to 77Hz, Prescale is 1:64, Rollover = (F_PB/64-1)/PPS
   from 0.5 to 10Hz, Set rollover for a constant 1Khz, and increment to reach
   the desired time, Prescale is 1:8, Rollover = (F_PB/8-1)/1000 and the number
   of times to repeat is (1000-1)/PPS + 1                                     */
char Stepper_Init(unsigned short int rate)
{
    unsigned short int overflowPeriod;
    if (rate > TWENTY_KILOHERTZ) return ERROR;
    dbprintf("\nInitializing Stepper Module");
    stepCount = 0;
    stepperMode = full;
    stepperSpeed = rate;
    overflowReps = 0;
    // Initialize hardware (no current flow)
    COIL_A_DIRECTION = 1;
    COIL_B_DIRECTION = 1;
    ShutDownDrive();
    TRIS_COIL_A_DIRECTION = 0;
    TRIS_COIL_A_ENABLE = 0;
    TRIS_COIL_B_DIRECTION = 0;
    TRIS_COIL_B_ENABLE = 0;
    // Calculate overflow time and prescalar
    overflowPeriod = CalculateOverflowPeriod(rate);
    dbprintf("\nOverflow Period: %u",overflowPeriod);
    dbprintf("\nNumber of Reps: %u",overflowReps);
    // Setup timer and interrupt
    OpenTimer3(T3_ON | T3_SOURCE_INT | T3_PS_1_8, overflowPeriod);
//    OpenTimer5(T5_ON | T5_IDLE_STOP | T5_GATE_OFF | T5_PS_1_8 | T5_SOURCE_INT,overflowPeriod);
//    OpenTimer5(T5_ON | T5_PS_1_8 | T5_SOURCE_INT, overflowPeriod);
    ConfigIntTimer3(T3_INT_ON | T3_INT_PRIOR_3 );
    mT3IntEnable(1);
    stepperState = inited;
    return SUCCESS;
}
/*********************************************************************
* Function: void TouchInit(void)
*
* PreCondition: none
*
* Input: none
*
* Output: none
*
* Side Effects: none
*
* Overview: sets ADC 
*
* Note: none
*
********************************************************************/
void TouchInit(void){
#define TIME_BASE (GetPeripheralClock()*SAMPLE_PERIOD)/4000000

    // Initialize ADC
	AD1CON1 = 0x080E0;  			// Turn on, auto-convert
	AD1CON2 = 0;					// AVdd, AVss, int every conversion, MUXA only
	AD1CON3 = 0x1FFF;			    // 31 Tad auto-sample, Tad = 256*Tcy
#if defined(__dsPIC33F__) || defined(__PIC24H__)
	AD1CHS0 = ADC_POT;
	AD1PCFGL = 0;                   // All inputs are analog  
	AD1PCFGLbits.PCFG11 = AD1PCFGLbits.PCFG12 = 1;    
#else
    AD1CHS = ADC_POT;  
    AD1PCFG = 0;                    // All inputs are analog   
#endif 
	AD1CSSL = 0;					// No scanned inputs
  
#ifdef __PIC32MX__
    OpenTimer3(T3_ON | T3_PS_1_8, TIME_BASE);
    ConfigIntTimer3(T3_INT_ON | T3_INT_PRIOR_4);
#else
    // Initialize Timer3
    TMR3 = 0;
    PR3 = TIME_BASE;
    T3CONbits.TCKPS = 1;         // Set prescale to 1:8
    IFS0bits.T3IF = 0;           // Clear flag
    IEC0bits.T3IE = 1;           // Enable interrupt
    T3CONbits.TON = 1;           // Run timer  
#endif
}
Beispiel #3
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;
  }
}
Beispiel #4
0
void InitTMR3(void)
{
    //On ouvre le Timer2 qui gère l'asservissement toutes les 10ms
    OpenTimer2(T3_OFF & T3_GATE_OFF & T3_IDLE_CON & T3_SOURCE_INT & T3_PS_1_64, 6249);
    ConfigIntTimer3(T3_INT_PRIOR_4 & T3_INT_ON); //Interruption ON et priorité 3
    T3CONbits.TON = 1; // Turn on timer 3
    return;
}
Beispiel #5
0
/*  Function:
    void SYSTEM_TickInit(void)
    Initialize Timer3 timer for 500usec system timer
    *****************************************************************************/
void SYSTEM_TickInit(void)
{
  OpenTimer3(T3_ON | T3_PS_1_256, TICK_PERIOD);
  ConfigIntTimer3(T3_INT_ON | T3_INT_PRIOR_1);

/*  OpenTimer4(T4_ON | T4_PS_1_256, TICK4_PERIOD);
  ConfigIntTimer4(T4_INT_ON | T4_INT_PRIOR_1);*/

}
Beispiel #6
0
// Exported functions
void buzzer_setup()
{
  pinMode(BUZZER_PIN, OUTPUT);
  pin_write(BUZZER_PIN, LOW);
  buzzing = false;
  is_buzzer_on = false;
  alarm = 1;

  // There are two timers capable of PWM, 2 and 3. We are using 2 for the modem,
  // so use 3 for the buzzer.
  OpenTimer3(T3_ON | T3_PS_1_8, PWM_PERIOD);
  ConfigIntTimer3(T3_INT_ON | T3_INT_PRIOR_5);
}
Beispiel #7
0
//Timer 3 setup function
int sysServiceConfigT3(unsigned int T3conval, unsigned int T3perval,
                        unsigned int T3intconval){
    //Todo: is there any way to have a compile time semaphore here?
    if(T3_already_confgured){
        return -1;
    }
    else{
        T3_already_confgured = 1;
        OpenTimer3(T3conval, T3perval);
        ConfigIntTimer3(T3intconval);
        return 0;
    }
}
Beispiel #8
0
void TickInit(void)
{

    // Initialize Timer4
    #ifdef __PIC32MX__
    OpenTimer3(T3_ON | T3_PS_1_8, TICK_PERIOD);
    ConfigIntTimer3(T3_INT_ON | T3_INT_PRIOR_4);
    #else
    TMR3 = 0;
    PR3 = TICK_PERIOD;
    IFS0bits.T3IF = 0;  //Clear flag
    IEC0bits.T3IE = 1;  //Enable interrupt
    T3CONbits.TON = 1;  //Run timer
    #endif
    
}
Beispiel #9
0
inline void Timer3_Setup(void)
{
    OpenTimer3(
            T3_OFF &
            T3_IDLE_CON &
            T3_GATE_OFF &
            T3_PS_1_256 &
            T3_SOURCE_INT,
            39063); //for 250 ms.

    ConfigIntTimer3(
            T3_INT_PRIOR_3 &
            T3_INT_ON
                    );

    return;
}
Beispiel #10
0
void vInitialiseTimerForIntQueueTest( void )
{
    /* Timer 1 is used for the tick interrupt, timer 2 is used for the high
    frequency interrupt test.  This file therefore uses timers 3 and 4. */

    T3CON = 0;
    TMR3 = 0;
    PR3 = ( unsigned short ) ( configPERIPHERAL_CLOCK_HZ / timerINTERRUPT3_FREQUENCY );

    /* Setup timer 3 interrupt priority to be above the kernel priority. */
    ConfigIntTimer3( T3_INT_ON | ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 1 ) );

    /* Clear the interrupt as a starting condition. */
    IFS0bits.T3IF = 0;

    /* Enable the interrupt. */
    IEC0bits.T3IE = 1;

    /* Start the timer. */
    T3CONbits.TON = 1;


    /* Do the same for timer 4. */
    T4CON = 0;
    TMR4 = 0;
    PR4 = ( unsigned short ) ( configPERIPHERAL_CLOCK_HZ / timerINTERRUPT4_FREQUENCY );

    /* Setup timer 4 interrupt priority to be above the kernel priority. */
    ConfigIntTimer4( T4_INT_ON | ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) );

    /* Clear the interrupt as a starting condition. */
    IFS0bits.T4IF = 0;

    /* Enable the interrupt. */
    IEC0bits.T4IE = 1;

    /* Start the timer. */
    T4CONbits.TON = 1;
}
Beispiel #11
0
char Stepper_ChangeStepRate(unsigned short int rate)
{
    unsigned short int overflowPeriod;
    if ((rate > TWENTY_KILOHERTZ)||(stepperState == off)) return ERROR;
    dbprintf("\nChanging step rate");
    //T5CONbits.ON = 0; // halt timer5
    //overflowPeriod = CalculateOverflowPeriod(rate);
    //WritePeriod5(overflowPeriod);
    //if (stepperState != halted) {
    //    T3CONbits.ON = 1; // restart timer3
    //}
    // Calculate overflow time and prescalar
    overflowPeriod = CalculateOverflowPeriod(rate);
    dbprintf("\nOverflow Period: %u",overflowPeriod);
    dbprintf("\nNumber of Reps: %u",overflowReps);
    // Setup timer and interrupt
    OpenTimer3(T3_ON | T3_SOURCE_INT | T3_PS_1_8, overflowPeriod);
//    OpenTimer5(T5_ON | T5_IDLE_STOP | T5_GATE_OFF | T5_PS_1_8 | T5_SOURCE_INT,overflowPeriod);
//    OpenTimer5(T5_ON | T5_PS_1_8 | T5_SOURCE_INT, overflowPeriod);
    ConfigIntTimer3(T3_INT_ON | T3_INT_PRIOR_3 );
    mT3IntEnable(1);
    stepperSpeed = rate;
    return SUCCESS;
}
Beispiel #12
0
int setupTimer(int timer, int frequency, int priority)
{
    int tX_on;
    int tX_source_int;
    int tX_int_on;
    int tX_ps_1_X;

    long countTo = SYS_FREQ / frequency;

    switch (timer)
    {
        case 1:
            tX_on = T1_ON;
            tX_source_int = T1_SOURCE_INT;
            tX_int_on = T1_INT_ON;

            while (1)
            {
                if (countTo >= 65536)
                {
                    countTo = countTo / 8;
                    if (countTo >= 65536)
                    {
                        countTo = countTo / 8;
                        if (countTo >= 65536)
                        {
                            countTo = countTo / 8;
                            if (countTo >= 65536)
                            {
                                return 0;
                            }
                            tX_ps_1_X = T1_PS_1_256;
                            break;
                        }
                        tX_ps_1_X = T1_PS_1_64;
                        break;
                    }
                    tX_ps_1_X = T1_PS_1_8;
                    break;
                }
                else
                {
                    tX_ps_1_X = T1_PS_1_1;
                    break;
                }
            }
            OpenTimer1(tX_on | tX_source_int | tX_ps_1_X, countTo);
            ConfigIntTimer1(tX_int_on | priority);
            break;

        case 2:
            tX_on = T2_ON;
            tX_source_int = T2_SOURCE_INT;
            tX_int_on = T2_INT_ON;

            while (1)
            {
                if (countTo >= 65536)
                {
                    countTo = countTo / 2;
                    if (countTo >= 65536)
                    {
                        countTo = countTo / 2;
                        if (countTo >= 65536)
                        {
                            countTo = countTo / 2;
                            if (countTo >= 65536)
                            {
                                countTo = countTo / 2;
                                if (countTo >= 65536)
                                {
                                    countTo = countTo / 2;
                                    if (countTo >= 65536)
                                    {
                                        countTo = countTo / 2;
                                        if (countTo >= 65536)
                                        {
                                            countTo = countTo / 2;
                                            if (countTo >= 65536)
                                            {
                                                return 0;
                                            }
                                            tX_ps_1_X = T2_PS_1_256;
                                            break;
                                        }
                                        tX_ps_1_X = T2_PS_1_64;
                                        break;
                                    }
                                    tX_ps_1_X = T2_PS_1_32;
                                    break;
                                }
                                tX_ps_1_X = T2_PS_1_16;
                                break;
                            }
                            tX_ps_1_X = T2_PS_1_8;
                            break;
                        }
                        tX_ps_1_X = T2_PS_1_4;
                        break;
                    }
                    tX_ps_1_X = T2_PS_1_2;
                    break;
                }
                else
                {
                    tX_ps_1_X = T2_PS_1_1;
                    break;
                }
            }
            OpenTimer2(tX_on | tX_source_int | tX_ps_1_X, countTo);
            ConfigIntTimer2(tX_int_on | priority);
            break;

        case 3:
            tX_on = T3_ON;
            tX_source_int = T3_SOURCE_INT;
            tX_int_on = T3_INT_ON;

            while (1)
            {
                if (countTo >= 65536)
                {
                    countTo = countTo / 2;
                    if (countTo >= 65536)
                    {
                        countTo = countTo / 2;
                        if (countTo >= 65536)
                        {
                            countTo = countTo / 2;
                            if (countTo >= 65536)
                            {
                                countTo = countTo / 2;
                                if (countTo >= 65536)
                                {
                                    countTo = countTo / 2;
                                    if (countTo >= 65536)
                                    {
                                        countTo = countTo / 2;
                                        if (countTo >= 65536)
                                        {
                                            countTo = countTo / 2;
                                            if (countTo >= 65536)
                                            {
                                                return 0;
                                            }
                                            tX_ps_1_X = T3_PS_1_256;
                                            break;
                                        }
                                        tX_ps_1_X = T3_PS_1_64;
                                        break;
                                    }
                                    tX_ps_1_X = T3_PS_1_32;
                                    break;
                                }
                                tX_ps_1_X = T3_PS_1_16;
                                break;
                            }
                            tX_ps_1_X = T3_PS_1_8;
                            break;
                        }
                        tX_ps_1_X = T3_PS_1_4;
                        break;
                    }
                    tX_ps_1_X = T3_PS_1_2;
                    break;
                }
                else
                {
                    tX_ps_1_X = T3_PS_1_1;
                    break;
                }
            }
            OpenTimer3(tX_on | tX_source_int | tX_ps_1_X, countTo);
            ConfigIntTimer3(tX_int_on | priority);
            break;

        case 4:
            tX_on = T4_ON;
            tX_source_int = T4_SOURCE_INT;
            tX_int_on = T4_INT_ON;

            while (1)
            {
                if (countTo >= 65536)
                {
                    countTo = countTo / 2;
                    if (countTo >= 65536)
                    {
                        countTo = countTo / 2;
                        if (countTo >= 65536)
                        {
                            countTo = countTo / 2;
                            if (countTo >= 65536)
                            {
                                countTo = countTo / 2;
                                if (countTo >= 65536)
                                {
                                    countTo = countTo / 2;
                                    if (countTo >= 65536)
                                    {
                                        countTo = countTo / 2;
                                        if (countTo >= 65536)
                                        {
                                            countTo = countTo / 2;
                                            if (countTo >= 65536)
                                            {
                                                return 0;
                                            }
                                            tX_ps_1_X = T4_PS_1_256;
                                            break;
                                        }
                                        tX_ps_1_X = T4_PS_1_64;
                                        break;
                                    }
                                    tX_ps_1_X = T4_PS_1_32;
                                    break;
                                }
                                tX_ps_1_X = T4_PS_1_16;
                                break;
                            }
                            tX_ps_1_X = T4_PS_1_8;
                            break;
                        }
                        tX_ps_1_X = T4_PS_1_4;
                        break;
                    }
                    tX_ps_1_X = T4_PS_1_2;
                    break;
                }
                else
                {
                    tX_ps_1_X = T4_PS_1_1;
                    break;
                }
            }
            OpenTimer4(tX_on | tX_source_int | tX_ps_1_X, countTo);
            ConfigIntTimer4(tX_int_on | priority);
            break;
    }
    return 1;
}
Beispiel #13
0
void timer_init(void) {
    OpenTimer3(T3_ON | T3_SOURCE_INT | T3_PS_1_256, TIMER_VAL);
    ConfigIntTimer3(T3_INT_ON | T3_INT_PRIOR_3);
    INTEnableSystemMultiVectoredInt();
}