示例#1
0
文件: timer.c 项目: tmitchell/oosmos
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;
  }
}
示例#2
0
void Init_Ultrasons (void)
{
    PIN_CN_ULTRASON_AR_IE = 0;     // desactivation de l'IT
    PIN_CN_ULTRASON_AV_IE = 0;     // desactivation de l'IT
    TRIS_ULTRASON_AV = 1;          // mise en input de la pin
    TRIS_ULTRASON_AR = 1;          // mise en input de la pin
    
    CloseTimer4();
    __delay_ms(5); // attente de 5 ms pour bien tuer l'IT
    Etat_Ultrason = U_ETAT_OFF;
    nb_Coups_Timers = 0;
    
    Ultrason_AV_Detect = 0;
    Ultrason_AR_Detect = 0;
    Threshold_US = ULTRASON_THRESOLD;
    
    OpenTimer4(T4_ON & T4_GATE_OFF & T4_PS_1_8 & T4_SOURCE_INT, 0xFFFF );
    // FCY = 40Meg   prescaler à 8 donc F timer = 5Meg
    // 1 coup = 200 ns = 0.2us
    // max = 65535 => 13.107 ms     // ce qui correspondra à de l'overshoot

    // configuration des interruptions
    ConfigIntTimer4(T4_INT_PRIOR_2 & T4_INT_ON); 
    Init_CN();
    Etat_Ultrason = U_ETAT_FOR_SEND1;

}
void setupTimer4()
{
    //1 click of timer 4 is 3 200 nS (clock on 80 Mhz is 12,5 nS and prescaller is 256)
    OpenTimer4(T4_OFF | T4_SOURCE_INT | T4_PS_1_256, 0);
    ConfigIntTimer4(T4_INT_ON | T4_INT_PRIOR_4);
    T4CONbits.ON = 0;
}
示例#4
0
void InitCRModule(void){
    WORD TiempoT4 = 1; //En mseg;
    WORD Prescaler = 32; //Selecciono el prescaler, si lo
                                   //cambio revisar opentimer.
    WORD CuentaT4 = (TiempoT4)*(CLOCK_FREQ/((1<<mOSCGetPBDIV())*Prescaler*1000));

    OpenTimer4(T4_ON | T1_IDLE_CON | T4_GATE_OFF | T4_PS_1_32 | T4_32BIT_MODE_OFF | T4_SOURCE_INT, CuentaT4);
    /*Pongo la int de nivel 3 porque de nivel 4 fastidia las interrupciones del transceptor. Dentro de nivel 3
    le doy maxima subprioridad.*/
    ConfigIntTimer4(T4_INT_ON | T4_INT_PRIOR_3 | T4_INT_SUB_PRIOR_3);

    mCNOpen(CN_ON | CN_IDLE_CON, CN14_ENABLE, CN_PULLUP_DISABLE_ALL);
    PORTSetPinsDigitalIn(IOPORT_D, BIT_5);
    mPORTDRead();
    ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_3);//6);

    CRM_Optm_Init();
    CRM_Poli_Init();
    CRM_AccCtrl_Init();
    //Creamos una entrada en la tabla de permisos.
    ACCCTRL_MSSG_RCVD PeticionCrearEntrada;
    PeticionCrearEntrada.Action = ActAddEntry;
    #if defined NODE_1
        //BYTE EUI_Permiso[] = {0x01, EUI_1, EUI_2, EUI_3, EUI_4, EUI_5, EUI_6, EUI_7};
        BYTE EUI_Permiso[] = {EUI_0, EUI_1, EUI_2, EUI_3, EUI_4, EUI_5, EUI_6, 0x22};
    #elif defined NODE_2
        //BYTE EUI_Permiso[] = {0x05, EUI_1, EUI_2, EUI_3, EUI_4, EUI_5, EUI_6, EUI_7};
        BYTE EUI_Permiso[] = {EUI_0, EUI_1, EUI_2, EUI_3, EUI_4, EUI_5, EUI_6, 0x11};
    #endif
    PeticionCrearEntrada.DirecOrigen = EUI_Permiso;
    CRM_Message(NMM, SubM_AccCtrl, &PeticionCrearEntrada);
    //Fin de la creacion de entrada de la tabal de permisos.
    NodoCerrado = FALSE; //Para que pueda aceptar peticiones de acciones de otros nodos.
    CRM_Repo_Init();
}
示例#5
0
/*********************************************************************
* Function:         void InitSymbolTimer()
*
* PreCondition:     none
*
* Input:		    none
*
* Output:		    none
*
* Side Effects:	    TMR0 for PIC18 is configured for calculating
*                   the correct symbol times.  TMR2/3 for PIC24/dsPIC
*                   is configured for calculating the correct symbol
*                   times
*
* Overview:		    This function will configure the UART for use at 
*                   in 8 bits, 1 stop, no flowcontrol mode
*
* Note:			    The timer interrupt is enabled causing the timer
*                   roll over calculations.  Interrupts are required
*                   to be enabled in order to extend the timer to
*                   4 bytes in PIC18.  PIC24/dsPIC version do not 
*                   enable or require interrupts
********************************************************************/
void InitSymbolTimer()
{
#if defined(__18CXX)
    TMR_CON = 0b00000000 | CLOCK_DIVIDER_SETTING;
    TMR_IP = 1;
    TMR_IF = 0;
    TMR_IE = 1;
    TMR_ON = 1;

    timerExtension1 = 0;
    timerExtension2 = 0;
#elif defined(__dsPIC30F__) || defined(__dsPIC33F__) || defined(__PIC24F__) || defined(__PIC24FK__) || defined(__PIC24H__)
    T2CON = 0b0000000000001000 | CLOCK_DIVIDER_SETTING;
    T2CONbits.TON = 1;
#elif defined(__PIC32MX__)
    #if defined (SECOND_PROTOTYPE) //Timer 3 freed for ADC rjk
        CloseTimer4();
        WriteTimer4(0x00);
        WriteTimer5(0x00);
        WritePeriod5(0xFFFF);
        OpenTimer4((T4_ON|T4_32BIT_MODE_ON|CLOCK_DIVIDER_SETTING),0xFFFFFFFF);
    #else
        CloseTimer2();
        WriteTimer2(0x00);
        WriteTimer3(0x00);
        WritePeriod3(0xFFFF);
        OpenTimer2((T2_ON|T2_32BIT_MODE_ON|CLOCK_DIVIDER_SETTING),0xFFFFFFFF);
    #endif
#else
    #error "Symbol timer implementation required for stack usage."
#endif
}
示例#6
0
文件: blinker.c 项目: nsaspook/mbmc
void Blink_Init(void) {
    LEDS.out_byte = 0xff;
    V.blink = 0;
    V.blink_out = 0;
    V.blink_alt = 0;
    // High priority interrupt for Blink timer. Called 1/2sec.
    ConfigIntTimer4(T4_INT_ON | T4_INT_PRIOR_7 | T4_INT_SUB_PRIOR_3);
    OpenTimer4(T4_ON | T4_SOURCE_INT | T4_PS_1_8, (50000000 / 2 / 64 / 2)); // for 2hz
}
void CommunicationLoop_initialize()
{
    FIFOUART1_initialize();

    //Setup Timer4
    INTClearFlag(INT_T4);
    INTSetVectorPriority(INT_TIMER_4_VECTOR, INT_PRIORITY_LEVEL_4);
    INTSetVectorSubPriority(INT_TIMER_4_VECTOR, INT_SUB_PRIORITY_LEVEL_0);
    INTEnable(INT_T4, INT_ENABLED);
    //Turn on clock
    OpenTimer4(T4_ON | T4_SOURCE_INT | T4_PS_1_64, 0x4FFF);

}
示例#8
0
//Timer 4 setup function
int sysServiceConfigT4(unsigned int T4conval, unsigned int T4perval,
                        unsigned int T4intconval){
    //Todo: is there any way to have a compile time semaphore here?
    if(T4_already_confgured){
        return -1;
    }
    else{
        T4_already_confgured = 1;
        OpenTimer4(T4conval, T4perval);
        ConfigIntTimer4(T4intconval);
        return 0;
    }
}
示例#9
0
void fis_Timer4_config(unsigned int period){
    //                      7654321076543210
    unsigned int config = 0b1000000000110000; //T4_ON & T4_GATE_ON & T4_IDLE_CON & T4_PS_1_1 & T4_SOURCE_INT;
    //                      7654321076543210
    //config = T4_ON & T4_GATE_OFF & T4_IDLE_CON & T4_PS_1_8 & T4_SOURCE_INT & T4_32BIT_MODE_OFF;
    //unsigned int period2 = 0b0000000001001011;
    WriteTimer4(0x0000);
    OpenTimer4( config, period );

    //ConfigIntTimer4(T4_INT_ON & T4_INT_PRIOR_3);
    EnableIntT4;

    //printf("t4_config\n");
}
示例#10
0
inline void Timer4_Setup(void)
{
    OpenTimer4(
            T4_OFF &
            T4_IDLE_CON &
            T4_GATE_OFF &
            T4_PS_1_256 &
            T4_32BIT_MODE_OFF &
            T4_SOURCE_INT,
            39063); //for 250 ms.

    ConfigIntTimer4(
            T4_INT_PRIOR_3 &
            T4_INT_ON
                    );

    return;
}
示例#11
0
void setupTimer4(unsigned int frequency) {

    unsigned int con_reg, period;

    // prescale 1:64
    con_reg =     T4_ON & 
    T4_IDLE_STOP & 
    T4_GATE_OFF & 
    T4_PS_1_64 & 
    T4_SOURCE_INT &
    T4_32BIT_MODE_OFF;

    // period value = Fcy/(prescale*Ftimer)
    period = FCY/(64*frequency); 

    OpenTimer4(con_reg, period);
    ConfigIntTimer4(T4_INT_PRIOR_5 & T4_INT_ON);

}
示例#12
0
int main(int argc, char** argv) {

    /*Configuring POSC with PLL, with goal FOSC = 80 MHZ */
    // Configure PLL prescaler, PLL postscaler, PLL divisor
    // Fin = 8 Mhz, 8  * (40/2/2) = 80
    PLLFBD = 18; // M=40          // change to 38 for POSC 80 Mhz - this worked only on a single MCU for uknown reason
    CLKDIVbits.PLLPOST = 0; // N2=2
    CLKDIVbits.PLLPRE = 0; // N1=2

    // Initiate Clock Switch to Primary Oscillator with PLL (NOSC=0b011)
    //__builtin_write_OSCCONH(0x03);

    // tune FRC
    OSCTUN = 23;  // 23 * 0.375 = 8.625 % -> 7.37 Mhz * 1.08625 = 8.005Mhz
    // Initiate Clock Switch to external oscillator NOSC=0b011 (alternative use FRC with PLL (NOSC=0b01)
    __builtin_write_OSCCONH(0b011);
    __builtin_write_OSCCONL(OSCCON | 0x01);

    // Wait for Clock switch to occur
    while (OSCCONbits.COSC!= 0b011);
    // Wait for PLL to lock
    while (OSCCONbits.LOCK!= 1);

     // local variables in main function
    int status = 0;
    int i = 0;
    int ax = 0, ay = 0, az = 0;
    int statusProxi[8];
    int slowLoopControl = 0;
    UINT16 timerVal = 0;
    float timeElapsed = 0.0;
    //extern UINT8 pwmMotor;
    extern UINT16 speakerAmp_ref;
    extern UINT16 speakerFreq_ref;
    extern UINT8 proxyStandby;
    UINT16 dummy = 0x0000;

    setUpPorts();
    delay_t1(50);

    PWMInit();
    delay_t1(50);

    ctlPeltier = 0;
    PeltierVoltageSet(ctlPeltier);
    FanCooler(0);
    diagLED_r[0] = 100;
    diagLED_r[1] = 0;
    diagLED_r[2] = 0;
    LedUser(diagLED_r[0], diagLED_r[1],diagLED_r[2]);

    // Speaker initialization - set to 0,1
    spi1Init(2, 0);
    speakerAmp_ref = 0;
    speakerAmp_ref_old = 10;
    speakerFreq_ref = 1;
    speakerFreq_ref_old = 10;
    int count = 0;
    UINT16 inBuff[2] = {0};
    UINT16 outBuff[2] = {0};

    while (speakerAmp_ref != speakerAmp_ref_old) {
        if (count > 5 ) {
            // Error !
            //LedUser(100, 0, 0);
            break;
        }

        inBuff[0] = (speakerAmp_ref & 0x0FFF) | 0x1000;

        chipSelect(slaveVib);
        status = spi1TransferWord(inBuff[0], outBuff);
        chipDeselect(slaveVib);

        chipSelect(slaveVib);
        status = spi1TransferWord(inBuff[0], &speakerAmp_ref_old);
        chipDeselect(slaveVib);

        count++;
    }

    count = 0;

    while (speakerFreq_ref != speakerFreq_ref_old) {
        if (count > 5 ) {
            // Error !
            //LedUser(0, 100, 0);
            break;
        }

        inBuff[0] = (speakerFreq_ref & 0x0FFF) | 0x2000;

        chipSelect(slaveVib);
        status = spi1TransferWord(inBuff[0], outBuff);
        chipDeselect(slaveVib);

        chipSelect(slaveVib);
        status = spi1TransferWord(inBuff[0], &speakerFreq_ref_old);
        chipDeselect(slaveVib);

        count++;
    }

    accPin = aSlaveR;
    accPeriod = 1.0 / ACC_RATE * 1000000.0;  // in us; for ACC_RATE = 3200 Hz it should equal 312.5 us
    status = adxl345Init(accPin);
    ax = status;
    delay_t1(5);

    /* Init FFT coefficients */
    TwidFactorInit(LOG2_FFT_BUFF, &Twiddles_array[0],0);
    delta_freq = (float)ACC_RATE / FFT_BUFF;

    // read 100 values to calculate bias
    int m;
    int n = 0;
    for (m = 0; m < 100; m++) {

        status = readAccXYZ(accPin, &ax, &ay, &az);
        if (status <= 0) {
            //
        }
        else {
            ax_b_l += ax;
            ay_b_l += ay;
            az_b_l += az;
            n++;
        }
        delay_t1(1);
    }

    ax_b_l /= n;
    ay_b_l /= n;
    az_b_l /= n;

    _SI2C2IE = 0;
    _SI2C2IF = 0;

    // Proximity sensors initalization
    I2C1MasterInit();
    status = VCNL4000Init();

    // Cooler temperature sensors initalization
    status = adt7420Init(0, ADT74_I2C_ADD_mainBoard);
    delay_t1(1);
    muxCh = I2C1ChSelect(1, 6);
    status = adt7420Init(0, ADT74_I2C_ADD_flexPCB);

    // Temperature sensors initialization
    statusTemp[0] = adt7320Init(tSlaveF, ADT_CONT_MODE | ADT_16_BIT);
    delay_t1(5);
    statusTemp[1] = adt7320Init(tSlaveR, ADT_CONT_MODE | ADT_16_BIT);
    delay_t1(5);
    statusTemp[2] = adt7320Init(tSlaveB, ADT_CONT_MODE | ADT_16_BIT);
    delay_t1(5);
    statusTemp[3] = adt7320Init(tSlaveL, ADT_CONT_MODE | ADT_16_BIT);
    delay_t1(5);

    // Temperature estimation initialization
    for (i = 0; i < 50; i++) {
        adt7320ReadTemp(tSlaveF, &temp_f);
        delay_t1(1);
        adt7320ReadTemp(tSlaveL, &temp_l);
        delay_t1(1);
        adt7320ReadTemp(tSlaveB, &temp_b);
        delay_t1(1);
        adt7320ReadTemp(tSlaveR, &temp_r);
        delay_t1(1);
    }

    tempBridge[0] = temp_f;
    tempBridge[1] = temp_r;
    tempBridge[2] = temp_b;
    tempBridge[3] = temp_l;

    if (statusTemp[0] != 1)
        temp_f = -1;
    if (statusTemp[1] != 1)
        temp_r = -1;
    if (statusTemp[2] != 1)
        temp_b = -1;
    if (statusTemp[3] != 1)
        temp_l = -1;

    // CASU ring average temperature
    temp_casu = 0;
    tempNum = 0;
    tempSensors = 0;

    for (i = 0; i < 4; i++) {
        if (statusTemp[i] == 1 && tempBridge[i] > 20 && tempBridge[i] < 60) {
            tempNum++;
            temp_casu += tempBridge[i];
            tempSensors++;
        }
    }

    if (tempNum > 0)
        temp_casu /= tempNum;
    else
        temp_casu = -1;

    temp_casu1 = temp_casu;
    temp_wax = temp_casu;
    temp_wax1 = temp_casu;
    temp_model = temp_wax;

    temp_old[0] = temp_f;
    temp_old[1] = temp_r;
    temp_old[2] = temp_b;
    temp_old[3] = temp_l;
    temp_old[4] = temp_flexPCB;
    temp_old[5] = temp_pcb;
    temp_old[6] = temp_casu;
    temp_old[7] = temp_wax;

    for (i = 0; i < 4; i++) {
        uref_m[i] = temp_wax;
    }

    // Configure i2c2 as a slave device and interrupt priority 5
    I2C2SlaveInit(I2C2_CASU_ADD, BB_I2C_INT_PRIORITY);

    // delay for 2 sec
    for(i = 0; i < 4; i ++) {
        delay_t1(500);
        ClrWdt();
    }

    while (i2cStarted == 0) {
        delay_t1(200);
        ClrWdt();
    }

    dma0Init();
    dma1Init();

    CloseTimer4();
    ConfigIntTimer4(T4_INT_ON | TEMP_LOOP_PRIORITY);
    OpenTimer4(T4_ON | T4_PS_1_256, ticks_from_ms(2000, 256));

    CloseTimer5();
    ConfigIntTimer5(T5_INT_ON | FFT_LOOP_PRIORITY);
    OpenTimer5(T5_ON | T5_PS_1_256, ticks_from_ms(1000, 256));

    diagLED_r[0] = 0;
    diagLED_r[1] = 0;
    diagLED_r[2] = 0;
    LedUser(diagLED_r[0], diagLED_r[1],diagLED_r[2]);

    start_acc_acquisition();

    while(1) {

        ConfigIntTimer2(T2_INT_OFF);    // Disable timer interrupt
        IFS0bits.T2IF = 0;              // Clear interrupt flag
        OpenTimer2(T2_ON | T2_PS_1_256, 65535); // Configure timer

        if (!proxyStandby) {
            statusProxi[0] = I2C1ChSelect(1, 2);            // Front
            proxy_f = VCNL4000ReadProxi();
            delay_t1(1);
            statusProxi[1] = I2C1ChSelect(1, 4);            // Back right
            proxy_br = VCNL4000ReadProxi();
            delay_t1(1);
            statusProxi[2] = I2C1ChSelect(1, 3);            // Front right
            proxy_fr = VCNL4000ReadProxi();
            delay_t1(1);
            statusProxi[3] = I2C1ChSelect(1, 5);            // Back
            proxy_b = VCNL4000ReadProxi();
            delay_t1(1);
            statusProxi[4] = I2C1ChSelect(1, 0);            // Back left
            proxy_bl = VCNL4000ReadProxi();
            delay_t1(1);
            statusProxi[5] = I2C1ChSelect(1, 1);            // Front left
            proxy_fl = VCNL4000ReadProxi();
            delay_t1(1);
        }
        else {
            proxy_f = 0;            // Front
            proxy_br = 0;            // Back right
            proxy_fr = 0;            // Front right
            proxy_b = 0;            // Back
            proxy_bl = 0;            // Back left
            proxy_fl = 0;            // Front left
        }

        if (timer4_flag == 1) {
            // every 2 seconds
            CloseTimer4();
            ConfigIntTimer4(T4_INT_ON | TEMP_LOOP_PRIORITY);
            timer4_flag = 0;

            if (dma_spi2_started == 0) {
                OpenTimer4(T4_ON | T4_PS_1_256, ticks_from_ms(2000, 256));
                skip_temp_filter++;
                tempLoop();
            }
            else {
                OpenTimer4(T4_ON | T4_PS_1_256, ticks_from_ms(50, 256));
            }
        }

        if (dma_spi2_done == 1) {
            fftLoop();
            dma_spi2_done = 0;
        }
        if ((timer5_flag == 1) || (new_vibration_reference == 1)) {
            // every 1 seconds
            CloseTimer5();
            ConfigIntTimer5(T5_INT_ON | FFT_LOOP_PRIORITY);
            OpenTimer5(T5_ON | T5_PS_1_256, ticks_from_ms(1000, 256));

            timer5_flag = 0;
            if (new_vibration_reference == 1) {
            //if(1){
                CloseTimer3();
                dma0Stop();
                dma1Stop();
                spi2Init(2, 0);
                dma0Init();
                dma1Init();
                chipDeselect(aSlaveR);
                IFS0bits.DMA0IF = 0;
                delay_t1(30); // transient response
            }
            new_vibration_reference = 0;

            start_acc_acquisition();
        }

        // Cooler fan control
        if (fanCtlOn == 1) {
            if (temp_pcb >= 25 && fanCooler == FAN_COOLER_OFF)
                fanCooler = FAN_COOLER_ON;
            else if (temp_pcb <= 24 && fanCooler == FAN_COOLER_ON)
                fanCooler = FAN_COOLER_OFF;
            // In case of I2C1 fail turn on the fan
            if ((proxy_f == 0xFFFF) && (proxy_fr == 0xFFFF) && (proxy_br == 0xFFFF) && (proxy_b == 0xFFFF) && (proxy_bl == 0xFFFF) && (proxy_fl == 0xFFFF))
                fanCooler = FAN_COOLER_ON;
        }
        else if (fanCtlOn == 2)
            fanCooler = FAN_COOLER_ON;
        else
            fanCooler = FAN_COOLER_OFF;

        //TEST
//        temp_f = temp_model;
//        if (temp_ref < 30) {
//            temp_r = smc_parameters[0] * 10;
//        }
//        else {
//            temp_r = smc_parameters[0] / 2.0 * 10.0;
//        }
//        temp_r = alpha*10;
//        temp_b = sigma_m * 10;
//        temp_l = sigma * 10;
        //temp_flexPCB = temp_ref_ramp;
/*
        proxy_f = dma_spi2_started;
        proxy_fl = dma_spi2_done;
        proxy_bl = new_vibration_reference;
        proxy_b = timer5_flag;
        proxy_br = timer4_flag;
*/
        int dummy_filt = 0;
        for (i = 0; i < 8; i++) {
            if (index_filter[i] > 0){
                dummy_filt++;
            }
        }

        if (dummy_filt > 0) {
            filtered_glitch = dummy_filt;
            //for (i = 0; i< 8; index_filter[i++] = 0);
        }
        else {
            filtered_glitch = 0;
        }

        updateMeasurements();

        timerVal = ReadTimer2();
        CloseTimer2();
        timeElapsed = ms_from_ticks(timerVal, 256);
        //if (timeElapsed < MAIN_LOOP_DUR)
        //    delay_t1(MAIN_LOOP_DUR - timeElapsed);

        ClrWdt(); //Clear watchdog timer

    } // end while(1)
    return (EXIT_SUCCESS);
}
示例#13
0
void main(void) {
    uint16_t v;
    uint8_t i;
    int16_t pad;
    uint32_t padsNow=0;
    uint32_t padsLast=0;
    int8_t  lastKey;
    uint8_t cnt=0;
    uint16_t lastEncValue;
    uint8_t encStep;
    int16_t senseDelta;
    uint8_t easteregg;

    SetupBoard();
    SetupCTMU();

    encStep=1;
    encValue=0;
    lastEncValue=0;
    octave=3;
    encMin=0;
    encMax=14;
    senseDelta=99;
    OpenTimer4(TIMER_INT_ON & T4_PS_1_1 & T4_POST_1_1); // Timer4 - Rotary Encoder polling
    WriteTimer4(0xFF);

    TMR4IF=0;
    OpenTimer2(TIMER_INT_ON & T2_PS_1_4 & T2_POST_1_8); // Timer2 = Display Refresh
    WriteTimer2(0xFF);
    TMR2IF=0;

    GIE=1;
    PEIE=1;
    ei();

    disp[0]=0x00;
    disp[1]=0x00;
    disp[2]=0x00;

    keys=0;
    easteregg=0;

    leds=0x7fff;
    __delay_ms(25);
    leds=0x0000;
    CalibratePads();
    leds=0x7fff;
    __delay_ms(25);
    leds=0x0000;


//
//    encMin=1;
//    encMax=24;
//    encValue=1;
//    for (;;) {
//        pad=ReadPad(encValue);
//        DispValue(pad);
//        leds=0;
//        if ((encValue>=1) && (encValue<=9))   {setbit(leds,encValue-1);   setbit(leds,10);}
//        if ((encValue>=10) && (encValue<=19)) {setbit(leds,encValue-10);setbit(leds,11);}
//        if ((encValue>=20) && (encValue<=29)) {setbit(leds,encValue-20);setbit(leds,12);}
//        __delay_ms(25);
//    }



    for (;;) {
        // Check for tones H+A+D
        if (padsLast==0b000000000000101000000100) EasterEgg(1);
        if (padsLast==0b101000000100000000000000) EasterEgg(2);

        padsNow=0;
        for (i=0; i<PADS; i++) {
            pad=ReadPad(i);
            if (GetPadBaseValue(i)-pad>senseDelta) {
                setbit(padsNow,i);
            }
        }
        for (i=0; i<PADS; i++) {
            if (testbit(padsNow,i) != testbit(padsLast,i)) {
                if (testbit(padsNow,i)) {
                    SendNoteOn(octave*12+i);
                } else {
                    SendNoteOff(octave*12+i);
                }
            }
        }
        padsLast=padsNow;

        for (i=0; i<KEYS; i++) {
            if (testbit(keys,i)) {
                disp[0]=charmap[butSettings[i].txt[0]-32];
                disp[1]=charmap[butSettings[i].txt[1]-32];
                disp[2]=charmap[butSettings[i].txt[2]-32];
                lastKey=i;
                encMin=butSettings[i].min;
                encMax=butSettings[i].max;
                encValue=butSettings[i].value;
                encStep=butSettings[i].stepSize;
                lastEncValue=encValue;
                break;
            }
        }

        if ((encValue!=lastEncValue) && (lastKey!=-1)) {
            lastEncValue=encValue;
            if (butSettings[lastKey].cc==127) {  // OCTAVE
                DispValue(encValue*encStep);
                octave=encValue;
                butSettings[lastKey].value=encValue;
                DispValue(encValue*encStep);
            } else if (butSettings[lastKey].cc==126) {  // SENSE DELTA
                senseDelta=encValue;
                butSettings[lastKey].value=senseDelta;
                DispValue(senseDelta);
                if (senseDelta&1) setbit(leds,0); else clrbit(leds,0);
            } else {
                SendCC(butSettings[lastKey].cc, encValue*encStep);
                DispValue(encValue*encStep);
            }
        }


        __delay_ms(10);

    }
}
示例#14
0
void SystickInit() {
    OpenTimer4(T4_ON | T4_IDLE_CON | T4_PS_1_8 | T4_32BIT_MODE_OFF | T4_SOURCE_INT, pb_clock/8/TICK_FREQUENCY);
    INTSetVectorPriority(INT_TIMER_4_VECTOR, INT_PRIORITY_LEVEL_2);
    INTSetVectorSubPriority(INT_TIMER_4_VECTOR, INT_SUB_PRIORITY_LEVEL_2);
    INTEnable(INT_T4, INT_ENABLED);
}
示例#15
0
文件: tmr.c 项目: Griffindog/tiny-DDS
/**
 * @function TmrSetFrequency
 * @brief configure a given timer with a desired frequency
 * @param tmr_t tmr_id: timer id
 * @param uint32_t desiredFrequency: desired frequency, in Hz
 * @return int8_t: 0 sucess, otherwise error
 */
int8_t TmrSetFrequency(tmr_t tmr_id, uint32_t desiredFrequency) {

  uint16_t tmrValue = 0xFFFF;
  uint16_t prescale;
  uint32_t cfg;
  int8_t res = -1;

  
    switch(tmr_id) {
    
      case TMR_1:
        if(SetTimerFrequency(TMR_TYPE_A, desiredFrequency, &tmrValue, &prescale) == 0) {
          TmrStop(TMR_1);
          mT1ClearIntFlag();
          ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_1 | T1_INT_SUB_PRIOR_1);
          cfg = T1_SOURCE_INT | T1_IDLE_CON;
          if(prescale == 1) cfg |= T1_PS_1_1;
          else if(prescale == 8) cfg |= T1_PS_1_8;
          else if(prescale == 64) cfg |= T1_PS_1_64;
          else cfg |= T1_PS_1_256;
          OpenTimer1(cfg, tmrValue);
          timer1Configured = 1;
          res = 0;
        }
        break;
        
      case TMR_4:
        if(SetTimerFrequency(TMR_TYPE_B, desiredFrequency, &tmrValue, &prescale) == 0) {
          TmrStop(TMR_4);
          mT4ClearIntFlag();
          ConfigIntTimer4(T4_INT_ON | T4_INT_PRIOR_4 | T4_INT_SUB_PRIOR_1);
          cfg = T4_SOURCE_INT | T4_IDLE_CON;
          if(prescale == 1) cfg |= T4_PS_1_1;
          else if(prescale == 2) cfg |= T4_PS_1_2;
          else if(prescale == 4) cfg |= T4_PS_1_4;
          else if(prescale == 8) cfg |= T4_PS_1_8;
          else if(prescale == 16) cfg |= T4_PS_1_16;
          else if(prescale == 32) cfg |= T4_PS_1_32;
          else if(prescale == 64) cfg |= T4_PS_1_64;
          else cfg |= T4_PS_1_256;
          OpenTimer4(cfg, tmrValue);
          timer4Configured = 1;
          res = 0;
        }
        break;
        
      case TMR_5:
        if(SetTimerFrequency(TMR_TYPE_B, desiredFrequency, &tmrValue, &prescale) == 0) {
          TmrStop(TMR_5);
          mT5ClearIntFlag();
          ConfigIntTimer5(T5_INT_ON | T5_INT_PRIOR_5 | T5_INT_SUB_PRIOR_1);
          cfg = T5_SOURCE_INT | T5_IDLE_CON;
          if(prescale == 1) cfg |= T5_PS_1_1;
          else if(prescale == 2) cfg |= T5_PS_1_2;
          else if(prescale == 4) cfg |= T5_PS_1_4;
          else if(prescale == 8) cfg |= T5_PS_1_8;
          else if(prescale == 16) cfg |= T5_PS_1_16;
          else if(prescale == 32) cfg |= T5_PS_1_32;
          else if(prescale == 64) cfg |= T5_PS_1_64;
          else cfg |= T5_PS_1_256;
          OpenTimer5(cfg, tmrValue);
          timer5Configured = 1;
          res = 0;
        }
        break;
        
      default:
        break;
    }
  return res;
}
示例#16
0
文件: Timer.c 项目: gpeal/Sherman
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;
}