| View previous topic :: View next topic |
| Author |
Message |
stijn023
Joined: 28 Sep 2010 Posts: 49
|
|
Posted: Thu Jan 13, 2011 8:43 am |
|
|
Hi,
Does anyone know how to make a pulse counter with the RA4 pin of the PIC16F876 and the clock source is external.
Thanks for the help |
|
 |
stijn023
Joined: 28 Sep 2010 Posts: 49
|
|
Posted: Mon Jan 17, 2011 1:54 pm |
|
|
| MPX is working fine |
|
 |
stijn023
Joined: 28 Sep 2010 Posts: 49
|
| anemometer |
Posted: Tue Jan 18, 2011 11:42 am |
|
|
Hi, can someone help me with this code:
| Code: | #include <16F876.h>
#device adc=10 *=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#use delay(clock=4000000)
#include <lcd_driver.c>
int counter;
int speed;
void main()
{
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
lcd_init();
while(1);
{
if(counter==5)
{
speed=10;
counter=0;
}
printf(lcd_putc,"Speed: %3.1f km/h",speed);
delay_ms(1000);
}
} |
I use the RA4 pin as an external counter input for the speed of the wind but I didn't know if it's ok. The counter need to be reset.
Thanks for the help |
|
 |
stijn023
Joined: 28 Sep 2010 Posts: 49
|
|
Posted: Tue Jan 18, 2011 2:00 pm |
|
|
I need to know how many pulses there are in one second to know the speed. All I have is this:
| Code: | #include <16F876.h>
#device adc=10 *=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#use delay(clock=4000000)
#include <lcd_driver.c>
int time;
void main()
{
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1|RTCC_8_BIT);
set_timer0(0);
time=get_timer0();
} |
|
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 18, 2011 2:11 pm |
|
|
Use this online calculator for Timer0:
http://eng-serve.com/pic/pic_timer.html
Change the frequency box from 8 MHz to 4 MHz and press the
Calculate button. It will show the frequency in Hz of the Timer0 interrupt rate. |
|
 |
stijn023
Joined: 28 Sep 2010 Posts: 49
|
|
Posted: Wed Jan 19, 2011 10:54 am |
|
|
Hi,
I don't quite understand what I need to do.
I'm very sorry but I don't understand to count the pulses in 1 second.
Does anyone know it?
It would be very gratefull. |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
 |
stijn023
Joined: 28 Sep 2010 Posts: 49
|
|
Posted: Wed Jan 19, 2011 12:20 pm |
|
|
Thanks
Tomorrow I will try to understand is but can I make it also with interrupts?
So I count the number of interrupts in 1 second of B0 and the 1 second I time with the timer 0.
I mean that I write a code that generate an interrupt on B0 when it change and do count+1. When timer 0 is 1 second it need to make an interrupt and then I look how many counts I have and I convert than the number of counts to m/s or km/h.
Is this possible because on the first sight the program from this
http://www.ccsinfo.com/forum/viewtopic.php?t=41877
is difficult but I will do my best.
And again thanks for the help |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 19, 2011 12:48 pm |
|
|
| I can't help any more. |
|
 |
stijn023
Joined: 28 Sep 2010 Posts: 49
|
|
Posted: Thu Jan 20, 2011 12:08 pm |
|
|
Hi,
can someone confirm this is a good program to measure the speed in m/s?
| Code: | //pulsteller op RB0
//1s wordt gegenereerd door tmr0 interrupt ... te laten overlopen
#include <16F876.h>
#device adc=10 *=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#use delay(clock=4000000)
#include <lcd_driver.c>
unsigned int usec=0;
unsigned int count=0;
unsigned int speed=0;
unsigned int pi=3.14159265;
unsigned int jet=5;
#int_TIMER0
void TIMER0_isr(void) //interrupt om de 1ms
{
usec++;
}
#int_EXT
void EXT_isr(void) //interrupt op RB0
{
count++;
}
void main()
{
lcd_init();
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4); //overflow timer0 = 1ms
set_timer0(0); //reset timer0
enable_interrupts(INT_EXT); //externe interrupts enabled
enable_interrupts(INT_TIMER0); //interrupt timer0 enabled
enable_interrupts(GLOBAL); //globale interrupts enabled
while(1)
{
if(usec==1000)
{
usec=0;
speed=(2*pi*jet)*count;
count=0;
lcd_gotoxy(1,1);
printf(lcd_putc,"Speed: %3.1f m/s",speed);
}
}
} |
Thanks for the help |
|
 |
stijn023
Joined: 28 Sep 2010 Posts: 49
|
|
Posted: Mon Jan 24, 2011 11:18 am |
|
|
Does anyone know how to make a counter with interrupt?
Thanks in advance |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9658 Location: Greensville,Ontario
|
|
Posted: Mon Jan 24, 2011 11:53 am |
|
|
First, that program will not give correct answers....
Hint, variable type declarations.... what is PI ???
Second, There are hundreds of 'pulse counting, RPM, tachometer' programs on the net INCLUDING one in the examples folder of CCS PCM compiler.
Third, Do NOT use Proteus or other 'simulator'. They are ALL full of 'bugs' and will not work properly if at all. |
|
 |
stijn023
Joined: 28 Sep 2010 Posts: 49
|
|
Posted: Mon Jan 24, 2011 1:41 pm |
|
|
After research I think I'm using the CCP module.
But will it works with something like this.
| Code: | Vcc
|
| | ___________________
| | |
|____| CCP1 PIC16F876
| |___________________
SENSOR
|
|
GND
|
I use this scheme from the website:
http://www.electronicslab.ph/forum/index.php?topic=6771.0
It measure the change from Vcc to GND |
|
 |
stijn023
Joined: 28 Sep 2010 Posts: 49
|
|
Posted: Tue Jan 25, 2011 11:44 am |
|
|
I use this program:
| Code: |
#include <16F876.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#include <lcd_driver.c>
// This global variable holds the time interval
// between two consecutive rising edges of the
// input signal.
int16 isr_ccp_delta;
// When a rising edge occurs on the input signal,
// the CCP1 will 'capture' the value of Timer1
// at that moment. Shortly after that, a CCP1
// interrupt is generated and the following isr
// is called. In the isr, we read the 'captured'
// value of Timer1. We then subtract from it the
// Timer1 value that we 'captured' in the previous
// interrupt. The result is the time interval
// between two rising edges of the input signal.
// This time interval is then converted to a frequency
// value by code in main().
#int_ccp1
void ccp1_isr(void)
{
int16 current_ccp;
static int16 old_ccp = 0;
// Read the 16-bit hardware CCP1 register
current_ccp = CCP_1; // From 16F877.H file
// Calculate the time interval between the
// previous rising edge of the input waveform
// and the current rising edge. Put the result
// in a global variable, which can be read by
// code in main().
isr_ccp_delta = current_ccp - old_ccp;
// Save the current ccp value for the next pass.
old_ccp = current_ccp;
}
//=======================
void main()
{
int16 current_ccp_delta;
int16 frequency;
lcd_init();
// Setup Timer1 and CCP1 for Capture mode so that
// we can measure the input signal's frequency.
// The input signal comes from the CCP2 pin, which
// is connected to the CCP1 pin with a wire.
set_timer1(0);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
setup_ccp1(CCP_CAPTURE_RE);
// Clear the CCP1 interrupt flag before we enable
// CCP1 interrupts, so that we don't get an unwanted
// immediate interrupt (which might happen).
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
while(1)
{
// Now calculate the frequency.
// Get a local copy of the latest ccp delta from the isr.
// We have to disable interrupts when we read a global
// isr variable that is larger than a single byte.
disable_interrupts(GLOBAL);
current_ccp_delta = isr_ccp_delta;
enable_interrupts(GLOBAL);
// To calculate the frequency of the input signal,
// we take the number of clocks that occurred
// between two consecutive edges of the input signal,
// and divide that value into the number of Timer1
// clocks per second. Since we're using a 4 MHz
// crystal, the Timer1 clock is 1 MHz (Timer1 runs
// at the instruction cycle rate, which is 1/4 of the
// crystal frequency). For example, suppose the
// the input waveform has a frequency of 244 Hz.
// 244 Hz has a period of about 4098 usec.
// Timer1 is clocked at 1 MHz, so between two
// consecutive rising edges of the input signal,
// it will count up by 4098 clocks. To find the
// frequency, we divide 4098 into the number of
// clocks that occur in 1 second, which is 1000000.
// This gives 1000000 / 4098 = 244 Hz.
frequency = (int16)(250000L / current_ccp_delta);
// Display the calculated frequency
lcd_gotoxy(1,1);
printf(lcd_putc,"\f%lu Hz", frequency);
delay_ms(500);
}
}
|
But do you need to connect the pulse immediately to the CCP1 input or may I made it active low (see post above) ?
Thanks for the help |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 25, 2011 5:06 pm |
|
|
You just need a series of pulses to arrive at the CCP1 pin on the PIC.
The voltage level of the pulses should be from 0v to 4v (or higher).
It will not work if the pulses only go from 0 to 3v. |
|
 |
|