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

Trying to use to sample frequency counter on 16F688
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jul 27, 2010 3:49 am     Reply with quote

In the original code the timing for 1 second is based on counting the instruction times.
With a clock speed of 20MHz every clock pulse takes 0.05us.
Instructions take 4 clock pulses: 4 * 0.05us = 0.2us
So for 1 second you need to count 5 million instructions

In your code you have reduced the clock from 20MHz to 8MHz, but you are still counting 5 million instructions. That's why your result is a factor 2.5 too low.

The comments in the example source code provide enough information for you to make the required changes.
masterat



Joined: 17 Jul 2010
Posts: 26

View user's profile Send private message

PostPosted: Wed Jul 28, 2010 5:34 am     Reply with quote

On my last post code I modify delay cycle till I can read frequency from signal gen from 5Hz to 65535Hz, But when I increse signel gen to 710000 frequency readout is 5460 Hz. And my display still show 0 for freqc_high assume that no fire overflow correctly to #bit t1_overflow=0x0A.5 .On your hint is talk about timing for 1 secound base for 20MHz cpu clock speed need count to 5 million instruction so for 8 MHz cpu clock shuuld be 2 million instruction right .I think I'm already reduce delay cycle till readout loop in 1 secound but not sure.
Give me more suggestion or hint why overflow still not fire?
masterat



Joined: 17 Jul 2010
Posts: 26

View user's profile Send private message

PostPosted: Thu Jul 29, 2010 8:17 am     Reply with quote

Continue on math cal:
CPU .5us instruction
Total inner =((3+8+x+1+2)*255+4)*255 =2000000
x=16.75733948481353 if let x= 16 total instrution=1951770 full fill with 48230
Outer y=((48230-4)/255)-(3+1+0+0+1+2)
y=182.121568627
if y=182

z=31

So my code is :
Code:

#include <16F688.h>
#fuses INTRC_IO,NOWDT,PUT,NOPROTECT,NOBROWNOUT,MCLR,NOIESO
#use delay(clock=8000000)    //one instruction=0.5us
#rom  0xfff={0x3444}

#define LCD_DB4   PIN_C0
#define LCD_DB5   PIN_C1
#define LCD_DB6   PIN_C2
#define LCD_DB7   PIN_C5

#define LCD_E       PIN_A1
#define LCD_RS     PIN_A0
#define LCD_RW    PIN_A2

#include <flex_lcd.c>
#bit t1_overflow=0x0A.5

// #bit t1_overflow=0xF9E.0  (PIC18, Reminder)

void main() {
   int cycles8, cycles;
   int32 freq;
   long freqc_high;
   long freqc_low;
   lcd_init();       // Always call this first.

   while (TRUE) {
      cycles8=0;
      cycles=0;
      freqc_high=0;
      t1_overflow=0;
      set_timer1(0);
      setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
   
       /* ___ wait one second ___  */
      while (cycles!=0xFF)      //true=3, false=4
      {
       cycles8=0; //1 cycle
       //start inner loop
       while (cycles8!=0xFF)  //true=3, false=4
       {
             if (t1_overflow)           //true=2,false=3             //----|
            {t1_overflow=0;freqc_high++;}//6 cycles   //    |
         else                                         //    |-- 8 cycles
            {delay_cycles(5);}                        //----|
         delay_cycles(16); //x
         cycles8++; //1
        }
       delay_cycles(182);      //y
       cycles++;          ///1 cycle
      }
      delay_cycles(31);   //z
      /* ___ end waiting 1 second ___ */

      setup_timer_1(T1_DISABLED);   //turn of counter to prevent corruption while grabbing value
      if (t1_overflow)            //check one last time for overflow
          freqc_high++;
          freqc_low=get_timer1();      //get timer1 value as the least sign. 16bits of freq counter
          freq=make32(freqc_high,freqc_low);   //use new make32 function to join lsb and msb
   
          lcd_gotoxy(0,1);
            printf(lcd_putc,"%LU Hz",freq);
          lcd_gotoxy(0,2);
            printf(lcd_putc,"%LU  %LU",freqc_low,freqc_high);   

   }
}

Is these correct? But t1_overflow still not fire.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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