Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

can't able to use timer0 and timer 2 at same time

Status
Not open for further replies.

raushankumar586

Junior Member level 1
Joined
Jun 26, 2017
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
191
Hello guys,

I am using two timers timers i.e. timer0 and timer2.
implementation code given following:

For Timer 0 :
Code:
void Timer0Setup() {
    T0CON = 0x00; // using a 16 bit timer0  with pre-scaler 4
    T0CONbits.PSA = 0;
    TMR0H = 0x01;
    TMR0L = 0x00;
    T0CONbits.TMR0ON = 1;
    INTCONbits.TMR0IE = 1;
}

For Timer 2 :
Code:
void Timer2Setup() {

    PIR1bits.TMR2IF = 0;
    PIE1bits.TMR2IE = 1;
    T2CON = 0b01111100;
    TMR2 = 0x00;
    PR2 = 0xFF;
    T2CONbits.TMR2ON = 1;

}

Now the ISR :
Code:
if (timer0InterruptFired()) {
        T0CON = 0x01;
        TMR0H = 0x00;
        TMR0L = 0x01;
        T0CONbits.TMR0ON = 1;
        INTCONbits.TMR0IF = 0;
        readIt();
    }

    if (timer2InterruptFired()) {

        T2CONbits.TMR2ON = 0;
        T2CON = 0b01111100;
        TMR2 = 0x00;
        PR2 = 0xFF;
        Count++;
        ledCount++;
        if (ledCount == blinkTime) { // blinkTime = 600;
            ledCount = 0;
            blinkLed();
        }
        if (Count > 100) {

            Count = 0;
            readIt();
        }

        T2CONbits.TMR2ON = 1;
        PIR1bits.TMR2IF = 0;

    }


interrupt settings :
Code:
void InterruptSetup() {
    INTCONbits.PEIE = 1; //periferal interrupt enable 
    INTCONbits.GIE = 1;
    INTCON2 = 0x00; // Set Falling Edge Trigger
   
}



My issue is , both timers are not working simultaneously , help me to solve that ...
 

Hi,

Maybe you should write what microcontroller you use.
And what compiler.

Some remarks in the code makes it easier to understand.

Klaus
 

Hi,

Maybe you should write what microcontroller you use.
And what compiler.

Sone remarks in the code makes it easier to understand.

Klaus

Sorry, I forgot to mention that , I am using Pic18f4520 with MPlab- XC8 Compiler
 

You did not show what is within readIt() function.
 

You did not show what is within readIt() function.
Code:
void readIt() {
    if(SimMsgReceived <=5){
        printToArduino(buffer);
        SimMsgReceived = 0;
        
    }
    clearbuffer();
}

Code:
void printToArduino(unsigned char *printOutput) {
    I2C_Master_Start(); //Start condition
    I2C_Master_WriteC(0x12); //7 bit address + Write (000001-0)(SELECT ADDRESS TO COMMUNICATE )
    I2C_Master_WriteS(printOutput); //Write data
    I2C_Master_Stop(); //Stop condition
}

sorry I forgot to mention those, now I think it would be easy to understand my situation ...
 

sorry I forgot to mention those, now I think it would be easy to understand my situation ...

Now it is clear your sitiation, you are doing a lot of things within interrupt vector.
You should just handle flags and/or increment/decrement counters;

Code:
readIt() {
...
printToArduino(buffer);
...
}

This is a function that once executed, keeps the program at a local loop while the last byte of the buffer string has not been sent.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top