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 support@ccsinfo.com

Timer 2 woes...

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



Joined: 15 Jun 2004
Posts: 21
Location: Nottingham UK

View user's profile Send private message

Timer 2 woes...
PostPosted: Sat Nov 20, 2004 8:46 am     Reply with quote

Hello all.
Tearing my hair out here
I have the following:
16LF877A at 10MHz, 3.35V
PCM 3.202
Trying to get Timer 2 to wiggle the E0 line at 96KHz (with a view to using this interrupt handler to poll kbhit for a software UART).
Here is my code:


Code:

#include <16F877A.H>
#fuses HS, NOWDT, NOPROTECT, PUT, NOBROWNOUT, NOLVP
#use delay(clock=10000000)

void main()
{
   enable_interrupts(global);
   
   // we need 96kHz, and the xtal is 10MHz.
   // Timer2 gets fosc/4 = 2.5MHz
   // So no further divide, and use 26th overflow
   setup_timer_2(T2_DIV_BY_1, 26, 1);
   enable_interrupts(int_timer2);

   for(;;)
   {
      // ...
   }
}

#int_timer2
void timer2_isr()
{
   output_high(PIN_E0);
   output_low(PIN_E0);
}



When I stick a scope on pin E0 I see pulses 1.6us wide and 25us apart. It should be 96kHz which is 11us apart or so!!!

Furthermore when I change the values for the period on the setup timer call, the E0 output period stays the same!

With

Code:

setup_timer_2 ( T2_DIV_BY_4, 0xc0, 2);


stolen from the help page I get 612us not the expected 910us.

Yes my scope is in calibration. Yes the chip *is* running at 10MHz. I run this program

Code:

void main()
{
   for(;;)
   {
      output_high(PIN_E0);
      delay_us(10);
      output_low(PIN_E0);
   }
}


and the E0 pin goes high for 11us, which with the setup time for the output is about right.

Any ideas?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Sat Nov 20, 2004 8:59 am     Reply with quote

Code:
setup_timer_2 ( T2_DIV_BY_4, 0xc0, 2);


10MHz / 4 = 2.5MHz is the timer2 increment rate.

Now you prescale with div by 4

2.5MHz / 4 = 625KHz

Now a post scale of 2

625KHz / 2 = 312.5KHz

and finally a period of 0xC0

312.5KHz / 0xC0 = 1.627KHz for a period of 614.62814 us

Code works just fine!

Now your orginal problem has to do with the overhead in handling ints. Simple answer, you just can't do it 11us or 27.5 instructions. Take a look at the lst file and see how many instructions the int handler consumes.
Gizz



Joined: 15 Jun 2004
Posts: 21
Location: Nottingham UK

View user's profile Send private message

Latency!Aaargh!
PostPosted: Sat Nov 20, 2004 9:32 am     Reply with quote

Mark, thanks for that. Damn! Forgot about the dreaded interrupt setup time!

I guess the only way to poll kbhit at '10 times the bit rate' for a 9600 baud soft UART is to do it in a tight loop?
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