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

Newbie: Timer1 Problem

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



Joined: 21 Apr 2005
Posts: 8
Location: Wollongong, AUSTRALIA

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

Newbie: Timer1 Problem
PostPosted: Fri Jul 15, 2005 9:32 pm     Reply with quote

Hi

I posted regarding timer0:

http://www.ccsinfo.com/forum/viewtopic.php?t=23653

and now am trying timer1 on 16F876A, 16-bits, prescalar 8 ( maximum for 16F876A ).

According to info from previous post, to send message
"\n\r%u Hello World!." to terminal program on PC, should take around:

( 17 characters * 10 bits/char * 10 lines ) / 9600 bps ~= 177 ms

177ms/4us/tick = 44250 ticks

For timer1 with prescalar 8 (maximum for 16F876A ), on 16F876A overflow should occur
every 262ms

// Timer increments 8000000/(4*8) times per second
// Period = 1/f
// = 1/250000
// = 4 microseconds
// or once every 4 us

// Total # of seconds/overflow:
// = 65536*4E-6
// = 262.144 milliseconds

Question When my program executes, I get a really small execution time, suggesting that the timer has overflowed, even though the estimated execution time is within the overflow period.

This happens even when I print only two lines
(17 chars* 10bits/char *2)/9600bps ~= 35 milliseconds.



Code:

// This program demonstrates 16-bits timer/counter timer1
// on a 16F876A

#include <16F876A.h>

#fuses NOWDT, NOLVP, HS, PUT, NOPROTECT

#use delay( clock = 8000000 )

#use rs232( baud = 9600, xmit = PIN_C6, rcv = PIN_C7 )

# define INPUT_LINES 10

void main()
{
   float time ;
   int timer1_ticks, i ;
   
      
      setup_timer_1( T1_INTERNAL | T1_DIV_BY_8 ) ;
      
      // Timer increments 8000000/(4*8) times per second
      // Period = 1/f
      //         = 1/250000
      //        = 4 microseconds
      // or once every 4 us
      
      // Total # of seconds/overflow:
      //   = 65536*4E-6
      //  = 262.144 milliseconds
   
   
   
   while ( 1 == 1 )
   {
   
   delay_ms( 1000 ) ; // without this, terminal  program     
                                            // (br@y++)   spits out garbage instead of
                  // text message below
         
   printf( "\n\rPress any key to begin.\n\r" );
   getc();
   
      
   //clear the timer
   set_timer1( 0 ) ;
      
   for( i = 1; i <= INPUT_LINES; i++ )
   {            
      printf( "\n\r%u Hello World!.", i ) ;
   }
   
                //read the ticks elapsed
      
   timer1_ticks = get_timer1( ) ;
      
      
   // calculate elapsed time   
   
   time = ( 4E-6 ) * timer1_ticks ;
   
   
   
   // show the message
   printf("\n\rIt took %u timer ticks to print %u lines.", timer1_ticks, INPUT_LINES ) ;
   printf("\n\rIt took %4.3e seconds to print %u lines.", time, INPUT_LINES ) ;
   
   
   }
   
}


Output:
Code:

Press any key to begin.

1 Hello World!.
2 Hello World!.
3 Hello World!.
4 Hello World!.
5 Hello World!.
6 Hello World!.
7 Hello World!.
8 Hello World!.
9 Hello World!.
10 Hello World!.
It took 167 timer ticks to print 10 lines.
It took 6.680E-04 seconds to print 10 lines.
Press any key to begin.


Any suggestions most welcome
_________________
:-]

Cheers

Dale
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 15, 2005 11:30 pm     Reply with quote

Quote:
void main()
{
float time ;
int timer1_ticks, i ;

timer1_ticks = get_timer1( ) ;

In CCS, an "int" is an 8-bit unsigned value. So the problem is
that you're putting a 16-bit value from timer1 into an 8-bit variable.
Dale Stewart



Joined: 21 Apr 2005
Posts: 8
Location: Wollongong, AUSTRALIA

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

PostPosted: Fri Jul 15, 2005 11:50 pm     Reply with quote

Aha! Cool - that worked!

Thanks PCM for you time and effort. Smile

That's 2 I owe you! Wink
_________________
:-]

Cheers

Dale
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