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

Example - Timer Interrupt duration (SOLVED)

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



Joined: 29 Aug 2012
Posts: 17

View user's profile Send private message

Example - Timer Interrupt duration (SOLVED)
PostPosted: Mon Sep 03, 2012 8:01 am     Reply with quote

I am trying the following example in which the timer interrupt is being called every time and the overflow duration is not being considered at all.
The code is
Code:

#INT_TIMER1
void TimerOverflow()
{
    printf("Timer Overflowed\r\n");
}

void main()
{

   long time;
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_TIMER1);
   set_timer1(8000); //count till 2000 then overflow
   time = get_timer1();
   while(1)
   {
   }
 
}

I expect the overflow method to be called approximately every 8 seconds but it seems that this duration is not being considered. Any suggestion what I might be doing wrong ?


Last edited by rajeshkhan808 on Mon Sep 03, 2012 8:46 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Mon Sep 03, 2012 8:26 am     Reply with quote

Start by telling us your clock rate....

However, Timer1 in general, is a 16bit counter. You are feeding it off the CPU instruction clock (OSC/4). It overflows when it reaches 65536 (when it wraps from 65535 back to 0), so to take 8 seconds from start (ignoring your '8000', which makes no sense at all), would require the instruction clock to be running at 8192 instructions per second, and Fosc, to be 32768Hz.
You'd not be able to do serial at any sensible rate from such a slow clock, so I suspect something is deeply 'wrong' in your calculations.
Now, assuming the Fosc is something more reasonable, like 4Mhz, then with the biggest prescaler (/8), the timer is still going to wrap every (4*8*65536)/4000000 second. Just over 0.5 second. To get an 8 second timeout, you'd need to be running off a much slower clock (or just use the timer as a 'tick', and only do your task every sixteen calls for example).

You then set the timer 'to' a value, and talk about it counting to 2000. The value you set it 'to' is just a count. The timer wraps when it reaches 65536, so if you wanted 2000 counts, you'd have to load the timer with 65536-2000. However you'd have to do this every time the interrupt is called, and given that this is happening much faster than you want, you probably don't want to do this.....

Best Wishes
rajeshkhan808



Joined: 29 Aug 2012
Posts: 17

View user's profile Send private message

PostPosted: Mon Sep 03, 2012 8:37 am     Reply with quote

Thanks that helped , So while going through the example

Code:
// 20 mhz clock, no prescaler, set timer 0
// to overflow in 35us
set_timer0(81);        // 256-(.000035/(4/20000000))

What is 256 ? Where did that come from ?

Also is the parameter of set_timer0 microseconds ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Mon Sep 03, 2012 8:40 am     Reply with quote

Timer0, is an 8 bit counter on most chips. Hence wraps at 255 to 0, and does 256 counts. Even on chips where it offers 16bit operation, it can be turned 'down' to run in 8bit mode. It is running in 8bit mode.
In your code, you are using Timer1, which is 16bit by default.
rajeshkhan808



Joined: 29 Aug 2012
Posts: 17

View user's profile Send private message

PostPosted: Mon Sep 03, 2012 8:46 am     Reply with quote

@Ttelmah thanks for clearing that up.
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