> I'm trying to grasp the functionality of tmr0 interrupt. > > I have written a basic tmr0 interrupt based c code. (I used hi-tech picc > compiler in my testings) > > ///////////////////////////////////////////////////////////////////////////////////////////////// > > #include > > __CONFIG(XT & WDTDIS); > > void interrupt tmr0_isr (void) { > RB0 ^= 1; > T0IF = 0; > TMR0 = 255; > } > > void main (void) { > > PSA = 1; // prescalar assigned to the WDT > T0CS = 0; // select internal clock > T0IE = 1; // enable timer interrupt > GIE = 1; // enable global interrupts > > TRISB0 = 0; > RB0 = 0; > > TMR0 = 255; // Load the tmr0 for the minimum interrupt rate > > while(1); > > } > > // All the calculations are based on 4 MHZ XT. > //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////// > > Above i would expecting the toggle rate of RB0 is about 1 us + 2us (synch > cycles) + a few more for interrupt latency which is totally about 5 us. But > when i tested the rate with MPLAB stopwatch i get an interrupt at each 18us. > So the tmr0_init_value = 256 - (delay_time * clock_freq/4) dont work with my > tmr0 calculations. (assumed no prescaler). > > So where am i misunderstanding, is this the problem of c compiler or me? There's over-head for bank switching, moving things around, and GIE will not be re-enabled until exit from your interrupt on RETFIE. In MPLAB click View, Program Memory to see the assembler generated by your C compiler. 0000 CLRF STATUS 0001 MOVLW 0 0002 MOVWF PCLATH 0003 GOTO 0x14 0004 MOVWF 0x70 ;<-- int vector 0005 MOVF STATUS, W 0006 CLRF STATUS 0007 MOVWF 0x20 0008 tmr0_isr BCF STATUS, 0x5 0009 MOVLW 0x1 000A XORWF PORTB, F 000B BCF INTCON, 0x2 000C MOVLW 0xff 000D MOVWF TMR0 000E BCF STATUS, 0x5 000F MOVF 0x20, W 0010 MOVWF STATUS 0011 SWAPF 0x70, F 0012 SWAPF 0x70, W 0013 RETFIE '<-- interrupts disabled until here 0014 CLRF STATUS 0015 GOTO main 03F5 main BSF STATUS, 0x5 03F6 BSF TMR0, 0x3 03F7 BCF TMR0, 0x5 03F8 BSF INTCON, 0x5 03F9 BSF INTCON, 0x7 03FA BCF PORTB, 0 03FB BCF STATUS, 0x5 03FC BCF PORTB, 0 03FD MOVLW 0xfe 03FE MOVWF TMR0 03FF GOTO 0x3ff Regards, Bruce -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist