Beispiel #1
0
void delayus(unsigned t) {
    OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);
    while (t--) { // t x 1ms loop
        WriteTimer1(0);
        while (ReadTimer1() < SYS_FREQ / 256 / 1000000);
    }
    CloseTimer1();
}// Delayus
Beispiel #2
0
extern void timerStop(timer * pTimer)
{
  switch(pTimer->m_TimerNumber) {
    case 1: CloseTimer1(); break;
    case 2: CloseTimer2(); break;
    case 3: CloseTimer3(); break;
    case 4: CloseTimer4(); break;
    case 5: CloseTimer5(); break;
  }
}
Beispiel #3
0
void delayms(unsigned t)
// This uses Timer 1, can be changed to another timer.
{
    OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);
    while (t--) { // t x 1ms loop
        WriteTimer1(0);
        while (ReadTimer1() < SYS_FREQ / 256 / 1000);
    }
    CloseTimer1();
} // Delayms
/*
 * 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;
}
/*
 * 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();
    }
}
/*! **********************************************************************
 * Function: rangeUltrasonic(void)
 *
 * Include:
 *
 * Description: performs an ultrasonic range reading.
 * Pins:
 *
 * Arguments: None
 *
 * Returns: the average of the samples
 *
 * todo remove this function?
 *************************************************************************/
unsigned int rangeUltrasonic(void)
{
    unsigned int rng;

        configureRange();

        beginUS();

        rng = rangeUS(25);

        INIT_PIN = 0;
        CloseCapture1();
        CloseTimer1();

        return rng;

}
Beispiel #7
0
void adc_int_handler() {

    ADCValue = ReadADC();




    x[counter2] = ADCValue;

    if(counter2 < 299)
    counter2++;
    else
    {
        counter2 = 0;
        CloseTimer1();
//        ToMainHigh_sendmsg()
    }

}
Beispiel #8
0
void closeTimer(int timer)
{
    switch(timer)
    {
        case 1:
            CloseTimer1();
            break;

        case 2:
            CloseTimer2();
            break;

        case 3:
            CloseTimer3();
            break;

        case 4:
            CloseTimer4();
            break;
    }
}
Beispiel #9
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