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

Timer0 based interrupt

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



Joined: 21 Apr 2007
Posts: 44

View user's profile Send private message

Timer0 based interrupt
PostPosted: Mon Jul 30, 2007 9:53 am     Reply with quote

I wish to use 16F84 with FOSC=8MHz for controlling a servo.

At 8 Mhz, Timer0 will run at 0.5 us / tick and with 1:1 prescaling, the 8 bit counter will rollover in:
0.5 us/tick x 256= 128us.
In order to create a pulse width of 20 ms, 156 rollovers shall be required because 128us x 156 = 19968 us = 20 ms.
The problem is that with this setting, I am getting a pulse width of 40 ms and not 20ms.
Desparately need help to understand...Pls.

Code:
Code:
#include "16F84a.H"
#fuses XT, NOWDT, NOPROTECT
#use delay(clock = 8000000)
#use fast_io(A)
#use fast_io(B)

#define PWM_PIN  PIN_A0
#define LOOPCNT 78

void main(void)
{   
   SET_TRIS_A(0);
   SET_TRIS_B(0);   


   PORTA=0;
   PORTB=0;

   delay_ms(100); //let the system stabilize
   
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1|RTCC_8_BIT);
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);
     
   while(TRUE);
}


#INT_RTCC
void tick_interrupt(void)
{
   if(--loop == 0)
     {
      loop = LOOPCNT;
      pulse = width;
     }
   
   if(pulse)
     {
      output_high(PWM_PIN);
      pulse--;
     }
   else
     {
      output_low(PWM_PIN);
     }
}
AdamkT1



Joined: 21 Apr 2007
Posts: 44

View user's profile Send private message

PostPosted: Mon Jul 30, 2007 9:55 am     Reply with quote

A correction Pls.
#define LOOPCNT 156 and not 78.
Sorry...
ckielstra



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

View user's profile Send private message

PostPosted: Mon Jul 30, 2007 1:06 pm     Reply with quote

Compiler version?
Declaration and init value for pulse and width variables?

Are you sure your processor is running at 8MHz? The XT fuse is only for external crystals up to 4MHz, change this into HS.
AdamkT1



Joined: 21 Apr 2007
Posts: 44

View user's profile Send private message

PostPosted: Tue Jul 31, 2007 9:12 pm     Reply with quote

Thank you very much.
My problem is solved.
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