示例#1
0
文件: ir.c 项目: nevers/preamp
void decodeIR(void) {
    WriteTimer1(1);
    invertEdge();

    if (necState == invalid)
        return;

    if (necState == inactive) {
        necState = lead_pulse;
        T1CONbits.TMR1ON = 1;
        T3CONbits.TMR3ON = 1;
        return;
    }

    if (necState == lead_pulse) {
        if (CCPR1H > NEC_LEAD_PULSE_MIN || CCPR1H < NEC_LEAD_PULSE_MAX)
            necState = lead_space;
        else
            necState = invalid;
        return;
    }

    if (necState == lead_space) {
        if (CCPR1H > NEC_LEAD_SPACE_MIN && CCPR1H < NEC_LEAD_SPACE_MAX) {
            necState = data_pulse;
            withinFrame = 1;
        } else if (CCPR1H > NEC_REPEAT_SPACE_MIN && CCPR1H < NEC_REPEAT_SPACE_MAX)
            necState = repeat_pulse;
        else
            necState = invalid;
        return;
    }

    if (necState == repeat_pulse && withinFrame) {
        if (CCPR1H > NEC_SHORT_MIN && CCPR1H < NEC_SHORT_MAX) {
            ir.decoded = 1;
            ir.repeated = 1;
            WriteTimer3(0);
        } else {
            necState = invalid;
        }
        return;
    }

    if (necState == data_pulse) {
        if (CCPR1H > NEC_SHORT_MIN && CCPR1H < NEC_SHORT_MAX)
            necState = data_space;
        else
            necState = invalid;
        return;
    }

    if (necState == data_space) {
        // Received a 0
        if (CCPR1H > NEC_SHORT_MIN && CCPR1H < NEC_SHORT_MAX) {
            capture.data = capture.data >> 1;
            capture.bitcount++;
            necState = data_pulse;
        // Received a 1
        } else if (CCPR1H > NEC_LONG_MIN && CCPR1H < NEC_LONG_MAX) {
示例#2
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
}
示例#3
0
void TMR3_init(unsigned int reload , void (*func)())
{
	// Enagle TMR1 interrrupt,16 bit counter, with internal clock, No prescalar
	OpenTimer3(TIMER_INT_ON & T3_16BIT_RW & T3_SOURCE_INT & T3_PS_1_1 & T3_SYNC_EXT_OFF );
	// Reload value for 1ms overflow
	WriteTimer3(reload);
	// Make timer1 interrupt high priority
  	//IPR2bits.TMR3IP = 1;

	tmr[2].reload = reload;
	tmr[2].func = func;

}
示例#4
0
/**
 * Configura o gerador de sinais, a partir dos parâmetros recebidos
 * 
 * Recebe do PC:
 *  Pulso (descida -> subida) a receber na entrada de sinal. TEMPO DESCONHECIDO.
 *  Valor de K
 *  Valor da velocidade
 * 
 * A4 85 00 05 (1BYTE VENSAIO) + (4BYTES K_ENSAIO) CRC16
 * A6 85 00 08 (4BYTES TEMPO32) + (4BYTES N_PULSO) CRC16
 * 
 *  Se velocidade = 0, desligar interrupção para a geração de sinal.
 *  Se houve alteração nos parâmetros de configuração:
 *      reinicia medição;
 *      altera o gerador.
 *      Gera evento EVENT_NOVA_CALIBRACAO.
 *  Se a solicitação mantiver os parâmetros:
 *      não altera a medição.
 *      Gera evento  EVENT_NONE_CALIBRACAO.
 * 
 *  NÃO HÁ AJUSTE NO FATOR DE CORREÇÃO. O TEMPO DE ENTRADA NÃO É CONHECIDO O DAVI.
 *  Apresentar o valor medido, com correção no tempo de atraso. Usar fator de correção
 * na rotina de aquisição de tempo.(interrupção).
 *  Gerar velocidade a partir do valor informado de velocidade/K.
 *  Interromper gerador ao comando Abortar.
 */
void calibra_pwm(void)
{
    uint32_t k_temp;
    uint8_t v_temp;
    uint8_t i;
    
    /*armazena os valores temporariamente*/
    k_temp = param_ensaio.CALIBRA.K_ENSAIO;
    v_temp = param_ensaio.CALIBRA.V_ENSAIO;

    /*recebe e armazena os valores dos parametros recebidos pelo comando CALIBRA*/
    for (i = 0; i <  SIZE_COMMAND_CALIBRACAO_DAVI ; i++)
    param_ensaio.CALIBRA_A4[i] = mensagem_Davi.payload.parameter[i];
    
    /*coleta valor de k*/
    param_ensaio.CALIBRA.K_ENSAIO = 
            endian_shortint(param_ensaio.CALIBRA.K_ENSAIO);
    /*V_ENSAIO não precisa de correção*/

    /*converter valor de entrada, se estiver em BCD*/
    if (mensagem_Davi.overhead.format == FORMAT_BCD_DAVI) {
        param_ensaio.CALIBRA.K_ENSAIO =
                Converter_BCD_to_HEX((uint32_t)param_ensaio.CALIBRA.K_ENSAIO);
    }

    /*verifica se houve mudança nos parâmetros*/
    if ((param_ensaio.CALIBRA.V_ENSAIO != v_temp) ||
            (param_ensaio.CALIBRA.K_ENSAIO != k_temp)) {
        /*habilita a contagem de tempo e
                     redefine o valor inicial do contador de tempo*/
                    di();
                    odometro_in = 0;
                    PIE2bits.TMR3IE = false;
                    WriteTimer3(0);
                    PIR2bits.TMR3IF = false;
                    PIE2bits.TMR3IE = true;
                    ConfigPulseIn(true);/*habilita a contagem de pulsos*/
                    ei();

        /*reinicia contadores do ensaio*/
        odometro_out = 0; /*pulsos de saída total do gerador*/
        odometro_in = 0;/*número de janelas de medição*/
        flag_int.tempo_fracao = false;
        /*se velocidade > 0 prepara gerador*/
        /*se velocidade = 0 desliga gerador*/
        gera_velocidade(&param_ensaio.CALIBRA.V_ENSAIO, &param_ensaio.CALIBRA.K_ENSAIO);
    }    
}
示例#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)
    T0CON = 0b00000000 | CLOCK_DIVIDER_SETTING;
    INTCON2bits.TMR0IP = 1;
    INTCONbits.TMR0IF = 0;
    INTCONbits.TMR0IE = 1;
    T0CONbits.TMR0ON = 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__)
    CloseTimer2();
    WriteTimer2(0x00);
    WriteTimer3(0x00);
    WritePeriod3(0xFFFF);
    OpenTimer2((T2_ON|T2_32BIT_MODE_ON|CLOCK_DIVIDER_SETTING),0xFFFFFFFF);     
#else
    #error "Symbol timer implementation required for stack usage."
#endif
}
示例#6
0
void ReStart_T3()
{
    T3CONbits.TON = 0;
    WriteTimer3(0);
    T3CONbits.TON = 1;
}
示例#7
0
void main(void) {

    unsigned char i,j;

    /* Output configurations */

    ADCON1 |= 0x0F;    // All possible analog input pins config as digital I/O
    CMCON = 0x07;    // Comparators disabled

    /* Natural interaction expansion port configuration    */
    TRISAbits.TRISA4 = 1;    // A4 Botton 3
    TRISAbits.TRISA3 = 1;    // A3 Botton 2
    TRISAbits.TRISA2 = 1;    // A2 Botton 1
    TRISAbits.TRISA1 = 1;    // A1 Potentiometer 2
    TRISAbits.TRISA0 = 1;    // A0 Potentiometer 1

    TRISB = 0;    // B0..B6    Serial input for the Shift Registers
    TRISD = 0;    // D0..D3    Shift Registers control inputs: SCK, RCK, _SCL, _G
    TRISEbits.TRISE0 = 1; //Input button PORTEbits.RE0

    LATA = 0;    // Disable expansion port
    _SCL = 1;    // Disable Shift Register _SCL (Global Clear)
    _G = 0;        // Enables Shift Regusters outputs _G

    // Resetting variables
    for (i = 0; i <= MAX_INDEX_G_BUFFER_GREYSCALE; i++) {
        gBufferGreyscale[i] = 0;
        gPreBufferGreyscale[i] = 0;
    }
    for (i = 0; i <= MAX_INDEX_M_BUFFER_MATRIX; i++) {
        mBufferMatrix[i] = RESET_M_BUFFER_MATRIX;
    }
    iGreyscale = 0;
    iTimer1 = 0;
    iMenu = 0;
    FIRST = 0;
    SECOND = 0;
    THIRD = 0;
    FOURTH = 0;
    FIFTH = 0;
    SIXTH = 0;
    pwm = 0;

    /* Timer 0 Configuration */
    // Used to trigger the refresh matrix printed data routine
    OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_1);
    WriteTimer0(0);

    /* Timer 1 Configuration */
    // Used to periodically check the input data (external buttons)
    OpenTimer1(TIMER_INT_ON & T1_SOURCE_INT &  T1_PS_1_8 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF);
    WriteTimer1( 0x00 & 0x00 );

    /* Timer 3 Configuration */
    // Used to create delays within the different menus without blocking with delays
    OpenTimer3( TIMER_INT_ON & T3_16BIT_RW & T3_SOURCE_INT & T3_PS_1_8 & T3_SYNC_EXT_OFF);
    WriteTimer3( 0x00 & 0x00 );

    /* A/D configuration */
    //ADCON1
    ADCON1bits.VCFG1 = 0;    // Voltage Reference Configuration bit (Vref-) = Vss
    ADCON1bits.VCFG0 = 0;    // Voltage Reference Configuration bit (Vref+) = Vdd
    ADCON1bits.PCFG3 = 1;    // PCFG = "1110" enables AN0 and AN1
    ADCON1bits.PCFG2 = 1;
    ADCON1bits.PCFG1 = 1;
    ADCON1bits.PCFG0 = 0;

    //ADCON0
    ADCON0bits.ADON = 1;    // A/D converter module is enabled
    ADCON0bits.CHS0 = 0;    // CHS = "0000" AN0 selected
    ADCON0bits.CHS1 = 0;
    ADCON0bits.CHS2 = 0;
    ADCON0bits.CHS3 = 0;

    //ADCON2
    ADCON2bits.ADCS0 = 0;    // A/D Adquisition Clock Select bits
    ADCON2bits.ADCS1 = 1;    // Tad = conversion time per bit. The A/D conversion requires 11 Tad
    ADCON2bits.ADCS2 = 0;    // "010" = 32 * Tosc

    ADCON2bits.ACQT0 = 0;    // A/D Adquisition time bits    "000" = Manual adquisition
    ADCON2bits.ACQT1 = 0;
    ADCON2bits.ACQT2 = 0;
    ADCON2bits.ADFM = 0;    // Left justified     . . .ADRESH . . : . . ADRESL. . .
    //                    7 6 5 4 3 2 1 0 : 7 6 5 4 3 2 1 0
    //                    X X X X X X X X . X X . . . . . . <-Left Justified


    /* Enabling interrups */
    INTCONbits.TMR0IE = 1;    // Enables interrupts for TIMER0
    PIE1bits.TMR1IE = 1;    // Enables interrupts for TIMER1
    PIE2bits.TMR3IE = 1;    // Enables interrupts for TIMER3
    INTCONbits.PEIE = 1;    // Peripherial interrupt enabled
    INTCONbits.GIE = 1;    // Global interrupt enabled


    /* Main Loop */
    while(1)
    {

        /*    Main MENU includes the different modes that the table can show
            Switching between menus is done using external button(RE0):

                0 - Fixed light dimmed with external control
                1 - Slow square
                2 - Slow chess board
                3 - Message
                4 - Invaders
                5 - Party (Dirty)

        */
        switch(iMenu) {

        /******************************************************************/
        /* 0 - Fixed light dimmed with external control                   */
        /******************************************************************/
        case 0:
            if (FIRST == 0) {
                deleteMatrix();
                (FIRST = 1);
            }
            drawLine(1,1,1,5,pwm);    //dirty way to draw the all pixels at the same time
            drawLine(2,1,2,5,pwm);
            drawLine(3,1,3,5,pwm);
            drawLine(4,1,4,5,pwm);
            drawLine(5,1,5,5,pwm);
            break;

        /******************/
        /* 1- Slow square */
        /******************/
        case 1:
            if (SECOND == 0) {
                deleteMatrix();
                (SECOND = 1);
            }
            for(j = 130, i = 254; j<=254; j++, i--) {
                drawSquare(1,1,5,5,j);
                drawSquare(2,2,4,4,i);
                drawPoint(3,3,j);
                Delay10KTCYx(100);
                if(iMenu != 1) {
                    break;
                }
            }
            if(iMenu != 1) {
                break;
            }
            for(j = 130, i = 254; j<=254; j++, i--) {
                drawSquare(1,1,5,5,i);
                drawSquare(2,2,4,4,j);
                drawPoint(3,3,i);
                Delay10KTCYx(100);
                if(iMenu != 1) {
                    break;
                }
            }
            if(iMenu != 1) {
                break;   // It allows to break the case during the executation
            }

        /***********************/
        /* 2- Slow Chess board */
        /***********************/
        case 2:
            if (THIRD == 0) {
                deleteMatrix();
                (THIRD = 1);
            }
            for(j = 130, i = 254; j<=254; j++, i--) {
                drawPoint(1,5,j);
                drawLine(1,3,3,5,j);
                drawLine(1,1,5,5,j);
                drawLine(3,1,5,3,j);
                drawPoint(5,1,j);

                drawLine(1,4,2,5,i);
                drawLine(1,2,4,5,i);
                drawLine(2,1,5,4,i);
                drawLine(4,1,5,2,i);
                Delay10KTCYx(100);
                if(iMenu != 2) {
                    break;
                }
            }
            if(iMenu != 2) {
                break;
            }
            for(j = 130, i = 254; j<=254; j++, i--) {
                drawPoint(1,5,i);
                drawLine(1,3,3,5,i);
                drawLine(1,1,5,5,i);
                drawLine(3,1,5,3,i);
                drawPoint(5,1,i);

                drawLine(1,4,2,5,j);
                drawLine(1,2,4,5,j);
                drawLine(2,1,5,4,j);
                drawLine(4,1,5,2,j);
                Delay10KTCYx(100);
                if(iMenu != 2) {
                    break;
                }
            }
            if(iMenu != 2) {
                break;   // It allows to break the case during the executation
            }


        /**************/
        /* 3- Message */
        /**************/
        case 3:
            if (FOURTH == 0) {
                deleteMatrix();
                (FOURTH = 1);
            }
            //knightRider(4);
            scrollText((rom unsigned char *)&Nino[0], TRANS_RIGHT_2_LEFT);

            break;



        /***************/
        /* 4- Invaders */
        /***************/
        case 4:
            if (FIFTH == 0) {
                deleteMatrix();
                (FIFTH = 1);
            }
            for(i = 1; i <= INVADERS_PAIR_REPETITIONS; i++) {
                drawFrame((rom unsigned char *)&invaders[0]);
                Delay10KTCYx(DELAY_INVADERS);
                if(iMenu != 4) {
                    break;
                }
                drawFrame((rom unsigned char *)&invaders[1]);
                Delay10KTCYx(DELAY_INVADERS);
                if(iMenu != 4) {
                    break;
                }
            }// end for
            for(i = 1; i <= INVADERS_PAIR_REPETITIONS; i++) {
                drawFrame((rom unsigned char *)&invaders[2]);
                Delay10KTCYx(DELAY_INVADERS);
                if(iMenu != 4) {
                    break;
                }
                drawFrame((rom unsigned char *)&invaders[3]);
                Delay10KTCYx(DELAY_INVADERS);
                if(iMenu != 4) {
                    break;
                }
            }// end for
            for(i = 1; i <= INVADERS_PAIR_REPETITIONS; i++) {
                drawFrame((rom unsigned char *)&invaders[4]);
                Delay10KTCYx(DELAY_INVADERS);
                if(iMenu != 4) {
                    break;
                }
                drawFrame((rom unsigned char *)&invaders[5]);
                Delay10KTCYx(DELAY_INVADERS);
                if(iMenu != 4) {
                    break;
                }
            }// end for

            break;


        /********************/
        /* 5- Party (Dirty) */
        /********************/
        case 5:
            if (SIXTH == 0) {
                deleteMatrix();
                drawLine(1,5,5,5,254);
            }
            for(j = 100, i = 254; j<=254; j++, i--) {
                drawSquare(1,1,5,5,j);
                drawSquare(2,2,4,4,i);
                drawPoint(3,3,j);
                if(iMenu != 5) {
                    break;
                }
            }
            if(iMenu != 5) {
                break;
            }
            for(j = 100, i = 254; j<=254; j++, i--) {
                drawSquare(1,1,5,5,i);
                drawSquare(2,2,4,4,j);
                drawPoint(3,3,i);
                if(iMenu != 5) {
                    break;
                }
            }

            if(iMenu != 5) {
                break;    // It allows to break the case during the executation
            }

            //SOLVES A BUG: because when TMR1F is called from this case, FIFTH is
            //reset but when we come back is set one because we are in case 4 not 1,
            if (iMenu == 5)(SIXTH = 1);
            break;

        default:
            drawPoint(3,3, 180);

        }// End switch iMenu
    }// End while

    CloseTimer0();
    CloseTimer1();
}//end main