CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Counting instruction cycles

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
GregEigsti



Joined: 12 Jan 2008
Posts: 12

View user's profile Send private message

Counting instruction cycles
PostPosted: Wed Jan 21, 2009 6:55 pm     Reply with quote

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 Shocked
Thanks!
Greg
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 21, 2009 9:29 pm     Reply with quote

With a 4 MHz oscillator, it's a 1 MHz instruction clock. Each instruction
cycle takes 1 usec. Most instructions execute in 1 instruction clock
but some take 2 or 3. Look at this table in your PIC data sheet. Look
in the "Cycles" column to see how many instruction cycles each
instruction takes:
Quote:
TABLE 26-2: PIC18FXXXX INSTRUCTION SET
GregEigsti



Joined: 12 Jan 2008
Posts: 12

View user's profile Send private message

PostPosted: Wed Jan 21, 2009 10:29 pm     Reply with quote

Thanks for the great info! I had seen the oscillator clock vs. instruction clock info before but it did not sink in. Now reading the excellent thread below to learn more.

Timers - what you should know

Thanks!
Greg
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group