Hello All, I've made some alterations and put a lot of comments (in english this time :) in my frequency counting routine (code is on the end of this email), but it's still not working very well. When I put a square-wave of 1,000 Hz, it counts 2480 instructions for a complete cycle when the correct would be 2500. Also, it tells readings like: 2480, 2480, 2425, 2480, 2425 I think this is really strange. The reading are more weird with lower or higher frequencies. Can someone take a look at my routine? I think it deserves a good look, because It works on a different way from the common count-cycles-for-a-second approach. Best regards, Brusque ----------------------------------- Edson Brusque Research and Development C.I.Tronics Lighting Designers Ltda (47) 323-2685 / (47) 9993-6453 Blumenau - SC - Brazil www.citronics.com.br ----------------------------------- **************************************** * PIC Frequency Counting using CCS PIC-C **************************************** uns16 interrupt_counter; // This interrupt is used during the frequency counting routine to // implement a 32 bit counter #INT_GLOBAL void interrupt(void) { interrupt_counter++; PIR1.TMR1IF = 0; } // This routine counts the frequency by getting the time (in instructions) // it takes to complete 32 cycles uns16 get_freq(void) { uns8 x; uns8 counter; float total; // Now we're setting the Timer1 interrupt PIR1.TMR1IF = 0; PIE1.TMR1IE = 1; INTCON.PEIE = 1; INTCON.GIE = 1; // Now we're setting the Timer1 itself T1CON.T1CKPS1 = 0; T1CON.T1CKPS0 = 0; // T1CON.T1OSCEN = 1; T1CON.NOT_T1SYNC = 1; T1CON.TMR1CS = 0; T1CON.TMR1ON = 0; // Clearing the counter (we're, obviously, using the TMR1 for this) interrupt_counter = 0; TMR1H = 0; TMR1L = 0; // Setting the direction for the pin we'll use to detect the cycles TRISC.B7 = 1; // Waiting for the end of the actual cycle while(!PORTC.B7); while(PORTC.B7); // Counting the time (in instructions) that it takes to detect 32 cycles T1CON.TMR1ON = 1; for (x=0; x < 32; x++) { while(!PORTC.B7); while(PORTC.B7); } T1CON.TMR1ON = 0; // Calculating the frequency from the time in instructions total = ((float)interrupt_counter * 65536) + ((float)TMR1H * 256) + ((float)TMR1L); total /= 32; total = 2500000 / total; // 2500000 is for a 10MHz clock (2,500,000 instructions for sec) return total; } -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics