We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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); }
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)
// 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; }