View previous topic :: View next topic |
Author |
Message |
RAZI Guest
|
Hz reading |
Posted: Fri Apr 10, 2009 7:23 am |
|
|
Hi.. I'm new in the ccs forum and working on ccs compiler in c language. I wish to ask, as I've seen from previous discussed forum, that you are using CCP/PWN method for reading 50 Hz. Is it possible, as I use too, to follow int_EXT on PIN_B0. I have just realized on Proteous simulation and my method is working for a limited pulse rate. What you suggest for me that I am on the right way or to adopt CCP/PWM method. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 10, 2009 12:19 pm |
|
|
The CCP capture method is better than the External interrupt because:
The compiler uses a specific amount of code to handle the interrupt.
The "interrupt dispatcher" code takes time to execute. This will limit
the maximum frequency of the input signal that you can measure.
For example, if you have a 4 MHz crystal, it takes 1 usec per instruction
cycle. It might take 50 instruction cycles to handle an interrupt.
That's 50 usec, which is a 20 KHz interrupt rate. Also, as the input
signal gets close to that maximum rate, most of the execution time in
your program is spent in handling the External interrupts. There is very
little time left to do other tasks in the main program.
The CCP module has a pre-scaler. You can set it to 1, 4, or 16. This
allows you to measure signals that are faster than your interrupt handling
rate. Also, the pre-scaler has a built-in "averaging" function since 4
or 16 cyles of the input signal are measured for each CCP capture.
Your EXT_INT method doesn't have any pre-scaler ability.
Also, the CCP capture is not affected by other interrupts that might be
in progress when the signal edge occurs. But your EXT_INT method
will be affected by other interrupts. For example, an INT_RDA
interrupt might be in progress. The program has to finish that interrupt
and exit from the interrupt handler, before it can handle the interrupt
for INT_EXT. This will cause an inaccurate reading of the input signal
period.
However, if you don't have a CCP available, it's possible that for certain
frequency ranges, the INT_EXT method might still produce acceptable
results. I have not tested this. |
|
|
mian_rizwan_azam
Joined: 10 Apr 2009 Posts: 4 Location: BCL Chakwal
|
thanks |
Posted: Sat Apr 11, 2009 3:47 am |
|
|
Thanks for your assistance and I shall surely adopt CCP/PWM method. I shall study to implement it. I am update about my weak point. Anyhow I am using 16F877 and it will work for this CCP/PWM purpose. Once I used CCP/PWM but again I shall focus myself in this sense for hz reading. If you tell me the hardware and software hints I shall be obliged.
Thanks and regards. _________________ Wish to get and share my knowledge with others for being literate. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
mian_rizwan_azam
Joined: 10 Apr 2009 Posts: 4 Location: BCL Chakwal
|
CCP/PWM |
Posted: Mon Apr 13, 2009 3:38 am |
|
|
I checked CCP/PWM for frequency reading and working perfect in proteous simulation but as far as i increase signal generator frequency above than 400 Hz display does blinking. Any how i used int_ccp1()
Code: | #int_ccp1()
int_isr(){
int16 hz_reading +=1; break;
} |
I am not using timer_1 because my counting is only for 50 Hz.
Is this correct or still my direction of work is wrong? _________________ Wish to get and share my knowledge with others for being literate. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 13, 2009 12:13 pm |
|
|
Quote: | #int_ccp1()
int_isr(){
int16 hz_reading +=1; break;
}
|
You're not using the tachometer code that I posted. You want to do it
some other way. Go ahead. |
|
|
mian_rizwan_azam
Joined: 10 Apr 2009 Posts: 4 Location: BCL Chakwal
|
CCP/PWM |
Posted: Tue Apr 14, 2009 1:26 am |
|
|
Ok thanks and hopefully it will be the last topic about CCP/PWM from my side that If using Tachometer and what i am using, i think logic behind is the same as i used in interrupt. I think there we shall use timer_1 for taking counting purpose.
I studied the example program you gave me that is for mainly square wave signal. Also there is timer _1 used for taking pulse counts then what is difference between my programming method and tachometer program. I just want comparison idea. Any how thanks and hope you are being bothered due to my questions.
Regards
Rizwan _________________ Wish to get and share my knowledge with others for being literate. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Apr 14, 2009 7:12 am |
|
|
The example program uses Timer1 to measure the time between pulses, i.e. whenever a CCP interrupt occurs the current value of the free running timer is copied to the CCP registers.
Your program is only counting how often the CCP interrupt occurs. In this it is not any different than when using the int_ext interrupt. Read the discussion above again about the advantages of why using the CCP versus int_ext.
Counting 50Hz is slow for a processor. The CCP method is great for high frequencies but becomes more complex at low frequencies as you have to extend the 16-bit Timer1 to 24-bits.
At 50 Hz or lower I think you can just as well use the INT_EXT method if that is what you want and understand.
Code: | #int_ccp1()
int_isr(){
int16 hz_reading +=1; break;
} | This code has two major problems:
1) hz_reading is declared as a local variable. As soon as the interrupt is finished the variable becomes invalid and you can not trust the contents any more, let alone increase the value at the next interrupt.
2) Because the value is a local variable, what do you want to do with it? You can not read it from the main loop...
A few minors:
1) The break command makes no sense. This command is only used to break out of a loop or switch command. Neither of these is presented here.
2) Why use an int16 when you are only counting 50Hz? An int16 takes more memory and processing power than an int8. Even with the int8 you have 5 seconds before it overflows.
I'd use something like: Code: | int8 hz_reading=0;
#int_ext
void int_isr(void)
{
hz_reading++;
} |
|
|
|
mian_rizwan_azam
Joined: 10 Apr 2009 Posts: 4 Location: BCL Chakwal
|
Hz_reading |
Posted: Wed Apr 15, 2009 11:20 pm |
|
|
Thanks and I did changing in the logic accordingly. My system for counting 50 Hz is working in proteous normal. Now I got interest to make Techo and as soon I shall complete my work on Techo I shall let you know the behaviour in proteous simulation.
Regards,
Rizwan _________________ Wish to get and share my knowledge with others for being literate. |
|
|
|