GregEigsti
Joined: 12 Jan 2008 Posts: 12
|
Counting instruction cycles |
Posted: Wed Jan 21, 2009 6:55 pm |
|
|
Sorry for the simple question; I just want to verify my thinking.
I am working on a timing critical project (IR learning/playback) and want to further tune my code so that I get/play IR bursts with as accurate as possible timing. I am using an 18F2550 clocked at 4mhz (external crystal) and would like to 'calculate' the offset between when my ISR is called and when I stuff away timer values (there are a few lines of management code between the two). I glanced through Microchip's PDF for this chip and see that an instruction takes 4 clock cycles but because of pipelining it is reduced to 1 cycle. I just want to make sure that I am reading my .lst file correctly.
I have to admit complete ignorance as to what the internal speed of my PIC clock is (as applies to timer1). At one time I had this figured out but have lost that knowledge... USB CDC and bootloader are working for this project so I know that I have one of the compatible USB internal clock speeds (though I am confused by the 18F2550 doc at this point). A couple years ago I found a great thread on this forum about the different fuses values as they pertain to USB clock speeds - but I cannot find it now. If anyone is familiar with this thread I'd love to see it!
My question is this... If I want to divine the timer1 value when my ext_isr is called is it a simple matter of each instruction line (between the top of ext_isr and my call to get_timer1()) counting for 1 cycle? If so, by my calculations, I would need to subtract 31 cycles from the return value of get_timer1().
Code: |
//fuses and delay from the C source
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL1,CPUDIV1,VREGEN
#use delay(clock=4000000)
...
//setup external interrupt for IR RX
ext_int_edge(0, H_TO_L);
enable_interrupts(INT_EXT);
//TIMER1 - time/cycles used to time high/low times for IR RX
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
...
//snipped of my ext_isr routine from my generated .lst file
.................... #INT_EXT
.................... void ext_isr() {
....................
.................... output_toggle(STATUSPIN);
*
2740: BCF F93.7
2742: BTG F8A.7
....................
.................... if(FRAMES > g_frame_iter) {
2744: MOVF x8B,W
2746: SUBLW 01
2748: BNC 278C
274A: BNZ 2752
274C: MOVF x8A,W
274E: SUBLW 8F
2750: BNC 278C
.................... g_frame_buff[g_frame_iter++] = get_timer1();
2752: MOVFF 8B,03
2756: MOVF x8A,W
2758: INCF x8A,F
275A: BTFSC FD8.2
275C: INCF x8B,F
275E: MOVLB 3
2760: MOVWF xD8
2762: MOVFF 03,3D9
2766: BCF FD8.0
2768: RLCF xD8,W
276A: MOVWF 02
276C: RLCF 03,W
276E: MOVWF 03
2770: MOVF 02,W
2772: ADDLW 8C
2774: MOVWF FE9
2776: MOVLW 00
2778: ADDWFC 03,W
277A: MOVWF FEA
277C: MOVF FCE,W
277E: MOVWF FEF
2780: MOVFF FCF,FEC
|
Any links to pertinent info or tips are greatly appreciated. Paying work has my brain too tired to fully concentrate on this for fun project
Thanks!
Greg |
|