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 support@ccsinfo.com

Again about Frequency Measure

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



Joined: 09 Jul 2004
Posts: 40
Location: Europe

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

Again about Frequency Measure
PostPosted: Sat Jul 10, 2004 8:50 pm     Reply with quote

Anybody can tell me what is max frequency in it my example.
Don't very critic me I'm beginner ( locker ).
Or can anybory give me sample without PORTB interrupt
dir CCS\exmpl\ - Please don't

Code:

#include <16f876.h>
//#device ICD=TRUE
//#device adc=10
#use delay(clock=4000000)
#fuses NOWDT,XT, NOPUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT//, DEBUG

#BYTE PORTA = 0x05
#BYTE PORTBN = 0x06
#BYTE PORTCN = 0x07
#BIT  RB4   = PORTBN.4
#BIT  RB5   = PORTBN.5
#define DelPerTimer0 122        // For 1 Second Interrupt

#include <lcd.h>
#define LCD_BLANK()        {lcd_send_byte(0,1);delay_ms(2);}

int hc;
byte last_b ;
int32 freq,freq_to_lcd;

//************************************************** 1Sec / 122 Interrupt
#int_rtcc
void isr()
   {

      if(--hc==0)
         {
          freq_to_lcd=freq; 
          freq=0; 
          hc=DelPerTimer0;
         }                     
   }
//**************************************************

//************************************************** Button Press
#int_rb
   rb_isr ( ) {
      byte changes;
      changes = last_b ^ portbn;
      last_b = portbn;
      if (bit_test(changes,4 )&& !bit_test(last_b,4)){
      Freq++;
      }
   }

//***************************************************


void main() {
  hc = DelPerTimer0;

  set_rtcc(4);
  set_tris_a(0);
  set_tris_b(0xf0);
  set_tris_c(0xf0);

  portc=0;

  setup_counters( RTCC_INTERNAL, RTCC_DIV_32);
  enable_interrupts(INT_RTCC);
  enable_interrupts(INT_RB);
  enable_interrupts(GLOBAL);

  Lcd_Init(); 

  while (TRUE) {
    last_b=PORTBN;
    LCD_gotoxy( 1,1 );
    printf ( LCD_PutC, "%ld Hz  ", freq_to_lcd );       
   
  }

}

[img]
http://icd2.front.ru/freqm.jpg
[/img]
ckielstra



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

View user's profile Send private message

PostPosted: Sun Jul 11, 2004 7:10 pm     Reply with quote

Thanks for providing the schematic! This makes understanding your problem much easier.

Your processor is running at 4MHz, or 1us per instruction. The handling of your port B interrupt takes:
55 instructions for the register saving and restoring.
21 instructions in the rb_isr()
You have two interrupts for every pulse, total of 152 instructions. The maximum frequency you can meassure will then be around 6500Hz.

A small note
Code:
      changes = last_b ^ portbn;
      last_b = portbn;

You are here reading portB two times. There is a small theoretical chance that the value has changed in between these two reads which will cause a wrong result in your code.

Why do you make it hard on yourself by using PB4? The PB4 interrupt will occur on each edge but from your code it looks like you only want to count the frequency. Using the EXT interrupt on PB0 you will only get an interrupt on the positive or negative edge (selectable), so you get half the number of interrupts and are able to measure up to double the frequency.

Change your portB interrupt to:
Code:
//************************************************** Button Press
#int_ext
void ext_isr ( )
{
  Freq++;
}

Total 55 + 12 = 67 instructions. Your maximum frequency goes up to 14.900Hz.

For even higher frequencies you could use the hardware CCP unit which was designed just to do this, count pulses. See the description of the setup_ccp1() function in the manual.

I'm just wondering, in your code you mention that this function is for detecting "Button Press". How are you going to handle switch bouncing?
Userrr



Joined: 09 Jul 2004
Posts: 40
Location: Europe

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

Thank ckielstra
PostPosted: Mon Jul 12, 2004 2:48 pm     Reply with quote

Thanks for correct my errors
Please tell me max frequency :
using CCP
using RB0
using EXT_Timer
Userrr



Joined: 09 Jul 2004
Posts: 40
Location: Europe

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

How About
PostPosted: Tue Aug 03, 2004 11:58 am     Reply with quote

How about this circuit to measure 50MHZ?
Does anyboby see for CCS?
This is for 74HC132 and PIC with RA4 - T0CKI
[img]
http://icd2.front.ru/fm.jpg
[/img]
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 03, 2004 12:34 pm     Reply with quote

You can't measure 50Mhz with a PIC.

However, you could divide this signal down and measure it. Since you know how much the signal was divided by, you then multiply the value measured by that value. You'll lose the lower significant digits but the difference between 50.0009MHz and 50.0000MHz might not matter to you.

What exactly is your goal?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 03, 2004 12:44 pm     Reply with quote

http://www.siliconchip.com.au/cms/A_30706/article.html

and here is a project that you are probably trying to do something very similar too:

http://www.raveen.net/projects/fcounter.pdf
Userrr



Joined: 09 Jul 2004
Posts: 40
Location: Europe

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

How
PostPosted: Wed Aug 04, 2004 10:17 am     Reply with quote

How about asm code in PDF on CCS
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Aug 04, 2004 11:02 am     Reply with quote

The manual explains how to embed asm code into your C file. Look for the #asm in the manual. Somehow I get the feeling that this is a school project which should be about learning and not copying someone else's work.
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