ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 20, 2008 6:48 pm |
|
|
| Quote: | As you mentioned, 2us*5000 counts have to correspond 10ms and 2us*10000 have to correspond 20us. But here i think 2us*10000 counts
coresponds to 10ms and 2us*20000 corresponds to 20us. | You 'think' the timer is running at double speed?
I already asked you before, why do you think so? How are you doing the measurement? I mean, what signal are you testing, with what equipment and what are the results?
I noticed several potential problems:
1) A 50Hz signal generates 100 zero crossings per second. What does your zero crossing circuit look like exactly? Is it possible you get 100 rising edges on the A4 pin instead of the 50 you suspect? Can you post a schematic?
2) The original timing configuration varies from 28ms (minPERIOD) to 39ms (maxPERIOD). A 50Hz test signal will trigger Timer0 every 20ms, or every 10ms depending on you zero cross circuit. That is before the CCP2 compare will fire and this will mess up your program.
3) Timer1 is not stopped after the second pulse so theoretically if it takes long for Counter0 to fire again you could get false CCP1 and CCP2 interrupts.
Here is a minimized version of your code.
- I removed most code that is not important for testing the pulse generation.
- For sake of simplicity I only use hard coded values, the pulse width is not adjustable any more.
- The initialization is improved a bit to prevent unwanted interrupts at startup.
- I don't trust your zero crossing circuit, that's why I adjusted the timings to 4ms for the first pulse and 5ms for the seconds pulse. This way you will get good results with both 50 and 100 Timer0 pulses per second.
- I stop Timer1 after the second pulse.
What this program does is:
- it waits for a single L-H edge on pin A4 to cause an overflow of Timer0.
- The Timer0 interrupt starts Timer1 and sets values for CCP1 and CCP2.
- After 2000 Timer1 clock ticks (4ms) the CCP1 interrupt will fire, generating a pulse on pin E0.
- Another 500 Timer1 ticks (1ms) later the CCP2 interrupt will generate a second pulse on pin E0. You will have to test these results with an oscilloscope.
| Code: | #include <16F877A.H>
#fuses HS, NOWDT, NOPUT, PROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=16000000)
#int_TIMER0
void timer0_isr()
{
set_timer0(255); // Arm Timer 0 to generate interrupt at next external pulse
// Start Timer1
set_timer1(0);
setup_timer_1( T1_INTERNAL | T1_DIV_BY_8 );
}
#int_CCP1
void pos_trig()
{
output_high(pin_e0);
delay_us(1);
output_low(pin_e0);
}
#int_CCP2
void neg_trig()
{
output_high(pin_e0);
delay_us(1);
output_low(pin_e0);
// Stop Timer1
setup_timer_1( T1_DISABLED );
}
void init_MCU()
{
SETUP_TIMER_0(RTCC_EXT_L_TO_H | RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_CCP1( CCP_COMPARE_INT );
setup_CCP2( CCP_COMPARE_INT );
disable_interrupts( GLOBAL );
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_CCP1);
enable_interrupts(INT_CCP2);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
set_timer0(255); // Arm external trigger
CCP_1=2000; // First pulse at 2000 * 2us = 4ms after trigger
CCP_2=CCP_1 + 500; // Second pulse 1ms after pulse 1
set_timer1(0); // ensures that no false CCP interrupt occurs!
clear_interrupt( INT_CCP1 );
clear_interrupt( INT_CCP2 );
}
void main()
{
init_MCU();
enable_interrupts( GLOBAL );
while(1) {};
} |
Oops, the code still doesn't fit on a single page.
Good luck in testing. |
|