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

Timer0 Counter Problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
bells_electronics



Joined: 05 Dec 2009
Posts: 40

View user's profile Send private message

Timer0 Counter Problem
PostPosted: Fri Feb 04, 2011 9:54 am     Reply with quote

hey guys,
I need to use timer0 as a counter to count pulses. After counting 100,000 or > pluses it should give one high pulse on PortC_0,
now I'm not able to get proper results.
Code:

#include <16F876A.h>
#FUSES NOWDT, HS, PUT, PROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD
#use delay(clock=20MHz , crystal)                   
unsigned int32 x,y;

#int_timer2                 
void timer2_isr(void)
 {
    x = GET_TIMER0();
    set_timer0(0);
   
    y=y+x;
   
 }

void main()
 {
  x=0;
  y=0;
  set_tris_c (0x00);
  set_timer0(0);             
  setup_timer_0(RTCC_DIV_1 | RTCC_EXT_L_TO_H );
   
  setup_timer_2(T2_DIV_BY_1, 249, 16); 
  enable_interrupts(INT_TIMER2);
  enable_interrupts(GLOBAL);   
 
  while(TRUE)
  {     
   delay_ms(500);
   if(y>100000)
    {
     y=0;
     output_high(PIN_C0);
     delay_ms(1000);
    }
    else
     output_low(PIN_C0);
     output_high(PIN_C0);
  }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 10:55 am     Reply with quote

Currently, C0, is basically going to be high all the time, except for dropping for two instruction times every half second.
If the count gets to > 100000, it'll stay high for one second, without the two instruction time low pulse.
Get rid of the second 'output_high', and then you might see the 'high pulse'.
_But_ you really need to disable interrupts, copy the y value into a 'local' variable, re-enable the interrupts, and do a test on this local value, or you may well get 'spurious' results if the value updated during the test.
How fast are the pulse likely to be?. T0, on the 876, is only an 8bit counter. T2, interrupts every 4000 instructions, so every 0.8mSec. If pulses arrive faster than every 3.125uSec, T0, will wrap, before the T2 interrupt. Also remember that a count can be missed between reading the timer value, and clearing the timer.
You'd probably be better to use timer1, and just increment a 17th bit value when this interrupts, rather than using T2, and T0.

Best Wishes
bells_electronics



Joined: 05 Dec 2009
Posts: 40

View user's profile Send private message

PostPosted: Fri Feb 04, 2011 12:28 pm     Reply with quote

Sir I'm asked to use only timer0, I can't use timer1.
Code:

#include <16F876A.h>
#FUSES NOWDT, HS, PUT, PROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD
#use delay(clock=20MHz , crystal)                   
unsigned int32 x,y;

void main()
 {
  x=0;
  y=0;
  set_tris_c (0x00);
  set_timer0(0);             
  setup_timer_0(RTCC_DIV_1 | RTCC_EXT_L_TO_H );   
 
  while(TRUE)
  {     
   delay_ms(500);
   x = GET_TIMER0();
    set_timer0(0);     
    y=y+x;
   
   if(y>100000)
    {
     y=0;
     output_high(PIN_C0);
     delay_ms(1000);
    }
    else
     output_low(PIN_C0);
  }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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