View previous topic :: View next topic |
Author |
Message |
Michaelc
Joined: 18 Nov 2003 Posts: 14 Location: Ireland
|
pulses |
Posted: Thu Apr 01, 2004 8:12 am |
|
|
How can i use ccp pins to count pulses? |
|
|
Kasper
Joined: 14 Jan 2004 Posts: 88 Location: Aurora, Ontario, Canada
|
|
Posted: Thu Apr 01, 2004 8:50 am |
|
|
could you perhaps use an interrupt on rising ( or falling) edges to increment a counter? |
|
|
Michaelc
Joined: 18 Nov 2003 Posts: 14 Location: Ireland
|
ccp and pulse |
Posted: Thu Apr 01, 2004 10:18 am |
|
|
Thanks
But iam not sure how to set up the ccp to interrupt on a rising edge. |
|
|
Kasper
Joined: 14 Jan 2004 Posts: 88 Location: Aurora, Ontario, Canada
|
|
Posted: Thu Apr 01, 2004 10:29 am |
|
|
I have not done such myself yet, but this is what the manual says:
Quote: | SETUP_CCP1()
SETUP_CCP2()
SETUP_CCP3()
SETUP_CCP4()
SETUP_CCP5()
Syntax: setup_ccp1 (mode)
Built-In Functions
149
setup_ccp2 (mode)
setup_ccp3 (mode)
setup_ccp4 (mode)
setup_ccp5 (mode)
Parameters: mode is a constant. Valid constants are in the devices
.h file and are as follows:
Disable the CCP:
CCP_OFF
Set CCP to capture mode:
CCP_CAPTURE_FE Capture on falling edge
CCP_CAPTURE_RE Capture on rising edge
CCP_CAPTURE_DIV_4 Capture after 4 pulses
CCP_CAPTURE_DIV_16 Capture after 16 pulses
Set CCP to compare mode:
CCP_COMPARE_SET_ON_MATCH Output high on
compare
CCP_COMPARE_CLR_ON_MATCH Output low on compare
CCP_COMPARE_INT interrupt on compare
CCP_COMPARE_RESET_TIMER Reset timer on compare
Set CCP to PWM mode:
CCP_PWM Enable Pulse Width Modulator
Returns: undefined
Function: Initialize the CCP. The CCP counters may be accessed
using the long variables CCP_1 and CCP_2. The CCP
operates in 3 modes. In capture mode it will copy the
timer 1 count value to CCP_x when the input pin event
occurs. In compare mode it will trigger an action when
timer 1 and CCP_x are equal. In PWM mode it will
generate a square wave. The PCW wizard will help to
set the correct mode
|
an example from the samples files is:EX_CCPMP.C
Code: |
/////////////////////////////////////////////////////////////////////////
//// EX_CCPMP.C ////
//// ////
//// This program will show how to use the built in CCP to ////
//// measure a pulse width. ////
//// ////
//// Configure the CCS prototype card as follows: ////
//// Connect a pulse generator to pin 3 (C2) and pin 2 (C1) ////
//// See additional connections below. ////
//// ////
//// This example will work with the PCM and PCH compilers. The ////
//// following conditional compilation lines are used to include a ////
//// valid device for each compiler. Change the device, clock and ////
//// RS232 pins for your hardware if needed. ////
/////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2003 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
/////////////////////////////////////////////////////////////////////////
#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12
#elif defined(__PCH__)
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12
#endif
long rise,fall,pulse_width;
#int_ccp2
void isr()
{
rise = CCP_1;
fall = CCP_2;
pulse_width = fall - rise; // CCP_1 is the time the pulse went high
} // CCP_2 is the time the pulse went low
// pulse_width/(clock/4) is the time
// In order for this to work the ISR
// overhead must be less than the
// low time. For this program the
// overhead is 45 instructions. The
// low time must then be at least
// 9 us.
void main()
{
printf("\n\rHigh time (sampled every second):\n\r");
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fall
setup_timer_1(T1_INTERNAL); // Start timer 1
enable_interrupts(INT_CCP2); // Setup interrupt on falling edge
enable_interrupts(GLOBAL);
while(TRUE) {
delay_ms(1000);
printf("\r%lu us ", pulse_width/5 );
}
}
|
See if this can get you started |
|
|
Michaelc
Joined: 18 Nov 2003 Posts: 14 Location: Ireland
|
pulses |
Posted: Thu Apr 01, 2004 10:58 am |
|
|
Thats brilliant i'll work on that!
Iam having a lot of problems setting up interupts, iam trying to use an
interrupt on portb pin b6, but it really doesn't seem to want to work. I also can't get the watchdog to reset. This is my first time working on interrupts and iam really not having much luck! I'd appreciate any more thoughts!
Thanks a mill!
#int_rb
rb_isr ( )
{
byte changes;
pos_cursor(0,1);
lcd_putc("is routine");
}
main()
{
int i;
enable_interrupts(INT_RB);
while(1)
{
i++; //I assume the interrupt should break this infinite loop
}
}
I have a switch on pin b6 with a pull up resistor! |
|
|
Kasper
Joined: 14 Jan 2004 Posts: 88 Location: Aurora, Ontario, Canada
|
|
Posted: Thu Apr 01, 2004 11:04 am |
|
|
you are missing the global interrupt enable.
Code: |
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
|
|
|
|
Michaelc
Joined: 18 Nov 2003 Posts: 14 Location: Ireland
|
pulses |
Posted: Thu Apr 01, 2004 12:53 pm |
|
|
That worked but now it just seems to interrupt the whole time, i tied pinb4 to pinb7 high with a pull up resistor, and the isr seems to run continuously!!
Whats happening here? |
|
|
Kasper
Joined: 14 Jan 2004 Posts: 88 Location: Aurora, Ontario, Canada
|
|
Posted: Thu Apr 01, 2004 1:16 pm |
|
|
If you are using the in circuit debugger, it is connected to port B also, so any change on the pins on the debugger ( which I am sure happens lots) will also trigger the interrupt. Try programming it without ICD, then it should be OK
btw. just for good order, what controller are you using, and compiler/ICD info too |
|
|
Michaelc
Joined: 18 Nov 2003 Posts: 14 Location: Ireland
|
|
Posted: Thu Apr 01, 2004 1:27 pm |
|
|
Iam not using a ICD, iam using an serial programmer! I had an LCD tied on those pins but have removed it. Its a 16f877 using IDE 2.32! I have the switch tied to b6 and the b4 b5 and b7 are tied high! What way should the port be setup i.e inputs or outputs! Also is it ok that they are all tied high and by pulling b6 low is that enough to run the isr? Or should the pins be tied low and then toggled to a high for the isr to run? |
|
|
Michaelc
Joined: 18 Nov 2003 Posts: 14 Location: Ireland
|
|
Posted: Thu Apr 01, 2004 1:57 pm |
|
|
i figured out what was wrong! you have to read the status of the port before you enable the interrupt!!!!
Thanks for the help i have to try and sort out the watchdog reset now!!! |
|
|
|