This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problems with Timer 2 capture mode

I'm using philips 89c51RC+ MCU. And set timer 2 in capture mode. But I didn't get capture TL2 and TH2 values into RCAP2L/H registers.

I'm using following code to initialize Timer2 and reading registers:

void initialize_timer2()
{
unsigned int tl,th;

// Timer #2 to run 16 bit counter mode

        T2MOD = 0x02; 
	T2CON = 0x0D;

// clear 8 bit counter registers	

	TH2 = 0;			
	TL2 = 0;

// Turn on timer 2  (start counting...)
        
        T2 = 1;	

//Captures will occur on negative transitions at T2EX (p1.1).
(And that thing working well in my circuit...)
         

// Print a stream
		
th = RCAP2H<<8;
tl = RCAP2L + th;

printf("Captured value:% d\n\r",tl);	
	
}

Plz send me mail if anyone could help me with my problem or tell me where I can get support.

Thanks , Timo Saari

timo.saari@sanmina.com


  • th = RCAP2H<<8;
    tl = RCAP2L + th;

    BAM! You're screwed. You must set tl then th.

    Here's my macro:

    #define INCR_CAPTURE_REG(tmrh, tmrl, amt)           do {                                                    U16 data incr = ((U16) tmrh << 8 | tmrl) + amt;     tmrl          = (U8) incr;                          tmrh          = (U8) (incr >> 8);               } while (0)
    

    Also, you are writing '1' to T2, I don't think this is correct. Here's how I start Timer 2 counting:

    // PCA (Timer 2 extended operation)
    //    English Name          Data Address            Hardware NAme
    sbit  r_enablePcaIntr     = r_ie ^ 6;            // IE.PCA
    sfr   r_counterCtrl       = 0xD8;                // CCON   = 0xD8
    sfr   r_counterMode       = 0xD9;                // CMOD   = 0xD9
    sfr   r_counterLo         = 0xE9;                // CL     = 0xE9
    sfr   r_counterHi         = 0xF9;                // CH     = 0xF9
    sbit  r_counterOverflow   = r_counterCtrl ^ 7;   // CF
    sbit  r_counterRun        = r_counterCtrl ^ 6;   // CR
    //    Reserved            = r_counterCtrl ^ 5;   // RESERVED
    sbit  r_watchdog          = r_counterCtrl ^ 4;   // CCF4
    // sbit                   = r_counterCtrl ^ 3;   // CCF3
    // sbit                   = r_counterCtrl ^ 2;   // CCF2
    // sbit                   = r_counterCtrl ^ 1;   // CCF1
    sbit  r_systemTick        = r_counterCtrl ^ 0;   // CCF0
    
    void initPCA(void)
    {
        r_systemTickCtrl = MATCH_PCA | ENABLE_CAPTURE_INTR;
        r_systemTickLo   = (U8) SYSTEM_TICK;
        r_systemTickHi   = (U8) (SYSTEM_TICK >> 8);
        r_watchdogLo     = (U8) (SYSTEM_TICK + WATCHDOG_INIT_LAG);
        r_watchdogHi     = (U8) (SYSTEM_TICK + WATCHDOG_INIT_LAG >> 8);
    
        r_enablePcaIntr  = TRUE;
        r_counterRun     = TRUE;
    }
    Good luck.

    - Mark