View previous topic :: View next topic |
Author |
Message |
m@rie Guest
|
pulse count... |
Posted: Tue Jul 22, 2008 8:07 pm |
|
|
hi people.. i have a simple question. i hope the kind hearted people of this forum will help me with this. i am using a virtual picdem2+ with PIC16f877
and placed a virtual pulse generator in pin c2 and set it to 60 hz.. what i want to do is count the number of pulses that the pulse generator generates.. i come up with this code..
Code: |
#include <16f877.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES PUT //Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <stdlib.h>
#include <string.h>
int finData = 0;
void main(){
setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
do{
delay_ms(1000);
printf("pulse count = %i\r",finData);
}while(1);
}
#int_CCP1
void CCP1_isr()
{
finData++;
}
|
i cant seem to figure out why its output is always "pulse count = 0". Is something wrong with what i did here? please help.. thanks in advance |
|
|
Ttelmah Guest
|
|
Posted: Wed Jul 23, 2008 2:43 am |
|
|
Hurrah!....
If it was in my power to give an award, you'd be in the running. Except for one thing (compiler version?), almost a perfectly done 'question'.
Example code, nice and small, and complete.
Description of hardware.
etc. etc..
First possiblity: '%i'. Check the list of printf format specifiers. Though 'i' is an acceptable alternative for 'd' in K&R, I don't know if CCS do correctly handle this.
Second possibility: The emulator. There are quite a few problems at times with these.
Third possibility: A problem with the compiler version. There are some faults on particular versions with some settings for things like the CCP being 'wrong'. However not on any reasonably 'recent' version, for a chip as 'mainstream' as this (most are on chips with more exotic peripheral setups).
The code as posted, runs perfectly under MPLAB (compiled using 3.249), so I'd suspect the second possibility is the most likely.
Best Wishes |
|
|
vijay s
Joined: 20 Oct 2007 Posts: 17 Location: coimbatore,india
|
|
Posted: Wed Jul 23, 2008 7:04 am |
|
|
There is another simple way to measure external pulse.. you can use external clock counter feature of TIMERs.. just configure the clock source for timer1 as external..now the timer1 registers contains the value equal to number of pulses you have applied..you can use this feature to measure the frequency too,by measuring number of pulses per second.. _________________ with regards
vijay s |
|
|
m@rie Guest
|
|
Posted: Wed Jul 23, 2008 7:24 pm |
|
|
Quote: |
Hurrah!....
If it was in my power to give an award, you'd be in the running. Except for one thing (compiler version?), almost a perfectly done 'question'. Smile
Example code, nice and small, and complete.
Description of hardware.
etc. etc..
|
thanks.. and thank you also for answering my question.. i have read some of your post in this forum and have learned many things because of the thorough explanations you gave. keep up the good work.. you really are an expert on this field. i hope one day i could be as good as you..
Quote: |
There is another simple way to measure external pulse.. you can use external clock counter feature of TIMERs.. just configure the clock source for timer1 as external..now the timer1 registers contains the value equal to number of pulses you have applied..you can use this feature to measure the frequency too,by measuring number of pulses per second..
|
hi.. can you show me how to do this? sorry for my ignorance.. im still a newbie.. i hope you can share your expertise.. |
|
|
m@rie Guest
|
|
Posted: Wed Jul 23, 2008 7:52 pm |
|
|
hi i have tried it with an actual board and waveform generator and set it to 60 hz. placed source of generator in pin C2 but its pulse count is still zero.. i have no clue as to why it behaves this way.. please help.. here is my code.. i used a 20Mhz clock since its the one in my board.
Code: |
#include <16f877.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES PUT //Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <stdlib.h>
#include <string.h>
int16 finData = 0;
void main(){
delay_ms(3000);
setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
output_float(PIN_C2);
do{
delay_ms(1000);
printf("pulse count = %ld\r",finData);
}while(1);
}
#int_CCP1
void CCP1_isr()
{
finData++;
}
|
|
|
|
Ttelmah Guest
|
|
Posted: Thu Jul 24, 2008 2:14 am |
|
|
Obvious thing. Add the HS fuse to the fuses. You need this if it is a 20MHz crystal (it'll run without this if you have an external oscillator)..
Best Wishes |
|
|
m@rie Guest
|
|
Posted: Thu Jul 24, 2008 2:51 am |
|
|
thank you so much.. i have another problem.. i used an 18f4525 with 8mhz clock.. i post it in another thread named (ccp help).. it still in relation with counting pulses.. would you be so kind as to look at my problem and help me.. thank you.. |
|
|
|