View previous topic :: View next topic |
Author |
Message |
agreement
Joined: 08 Oct 2010 Posts: 10
|
measure pulse width use timer1 |
Posted: Fri Oct 08, 2010 7:13 pm |
|
|
Hi. I want to measure pulse width use timer1, but I have a problem.
Pulse at pin CCP1, CCP2 then result be shown in LCD. But the result is 0.
Code as below:
Code: |
#include <16F877.h>
#use delay(clock=10000000)
#fuses HS,NOWDT
#include <lcd.c>
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
int overflow_count;
unsigned int32 start_time, end_time;
int32 pulse_ticks;
#int_TIMER1
void TIMER1_isr()
{
++overflow_count; //increment whenever an overflow occurs
}
#int_CCP2
void CCP2_isr()
{
end_time = CCP_1; //read captured timer ticks
start_time = CCP_2; //read captured timer ticks
//check for pulse time (in ticks) accounting for anyoverflow that may have occurred
pulse_ticks = (0x10000 * overflow_count) - start_time + end_time;
overflow_count = 0; //clear overflow counts
}
void main() {
setup_ccp1(CCP_CAPTURE_FE);
setup_ccp2(CCP_CAPTURE_RE);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);//set timer1 to run at system clock/4
enable_interrupts(INT_TIMER1); //unmask Timer1 overflow interrupt
enable_interrupts(INT_CCP2); //unmask capture event interrupt
enable_interrupts(global); //enable all unmasked interrupts
//set_timer1(0);
while(1){
LCD_init();
printf(lcd_putc,"\rdo rong: %u", pulse_ticks);
//as usual, the main() function does nothing
}
}
|
thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Oct 08, 2010 8:05 pm |
|
|
Try using this CCS example program. The input signal must be connected to both pins C1 and C2.
Quote: |
c:\program files\picc\examples\ex_ccpmp.c
|
If that doesn't work, then try the Pulse program in this thread.
Use the first program, which is for 16F PICs.
http://www.ccsinfo.com/forum/viewtopic.php?t=41877 |
|
|
agreement
Joined: 08 Oct 2010 Posts: 10
|
|
Posted: Fri Oct 08, 2010 8:29 pm |
|
|
thanks, but i have tryed the example but it still not work.:( |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Oct 08, 2010 8:44 pm |
|
|
Describe the input signal. What is the frequency ? What device makes
the signal ? What's the manufacturer and part number ? What is the
expected duration of the pulses (high level time duration) ? What are the
high and low level output voltages of the signal from the device (Voh and Vol) ? |
|
|
agreement
Joined: 08 Oct 2010 Posts: 10
|
|
Posted: Sat Oct 09, 2010 2:56 am |
|
|
PCM programmer wrote: | Describe the input signal. What is the frequency ? What device makes
the signal ? What's the manufacturer and part number ? What is the
expected duration of the pulses (high level time duration) ? What are the
high and low level output voltages of the signal from the device (Voh and Vol) ? |
I simulate it by Proteus. Pulse is provided by generator pulse. i have tryed with many frequency but the result still not change. i have attached the schematic file in: http://www.mediafire.com/?hgbm580t1k8aedt
thanks so much! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Oct 09, 2010 1:27 pm |
|
|
You may have a Proteus bug, or a configuration problem.
I don't have Proteus, so I can't do anything more on your problem. |
|
|
|