CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

tocki and timer1
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue May 20, 2008 6:48 pm     Reply with quote

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. Smile
Good luck in testing.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group