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

timer 0 help

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



Joined: 07 Jan 2004
Posts: 3
Location: Fresno, Ca

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

timer 0 help
PostPosted: Wed Jan 07, 2004 6:34 pm     Reply with quote

Hi everybody,
I am new to using timers on the PIC, and i am having a little trouble. I am using a PIC 16F877 (on the MP-LAB ICD demo board) and an 8 Mhz clock. I want the timer 0 interrupt to occur every 10 ms. So here are my calculations: 8000000/(4*128) == 15625 increments per second. So (15625 increments)/(1 second) == (X increments)/(10*10^-3 seconds).
Solve for X, and that gives me 156.25 increments/10ms. So in order to get 156 increments before overflow, i use the statement

set_rtcc(100);

Anyways, i set up a little test program to see if i was correct, and apparently i am missing something. I set up a counter in the timer0 interrupt, and so when the counter reaches 500, that should be 5 seconds, when it reaches 1500 that should be 15 seconds and so on. In the 5 second case, the actual time it is taking is 8.5 seconds. In the 15 second case the actual time it is taking is 25 seconds. So I will post my code below in case the problem can't be found in my above calculations. Thanks for any help you can give!

Code:

/************************
* includes
************************/
#include <16F877.h>         /* device file for demo board use */

/************************
* directives
************************/
//DEBUG remove unnecessary fuses after testing
#fuses XT,         \
       NOWDT,      \
       NOPROTECT, \
       NOLVP,      \
       DEBUG

#use delay (clock=8000000)          //DEBUG Use with Microchip ICD demo board

/************************
* enumerations
************************/
enum ADU_STATES
{
   STATE_LED_OUTPUT,
   STATE_ADU_STATE_MAX
};

/************************
* prototypes
************************/
void initialize();
void timeInit();
void LED_Output();
#int_rtcc
void ISRmbTimer();

/************************
* constants
************************/
/*LED output pins */
#define CRITICAL_LED_PIN      PIN_C2 /* 17 */
#define MAJOR_LED_PIN         PIN_C1 /* 16 */
#define MINOR_LED_PIN         PIN_C0 /* 15 */

/* Times in 10 ms increments */
#define TIME_ONE                500     // 5 seconds
#define TIME_TWO                1500    // 15 seconds
#define TIME_THREE             4500    // 45 seconds

/************************
* globals
************************/
ADU_STATES progState;
int32 timeCounter;


#int_rtcc
void ISRmbTimer()
{
   timeCounter++;
}

void timeInit()
{
   int8 i;

   timeCounter = 0;

   /* set real-time clock counter to interrupt every 10ms */
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);
   set_rtcc(100);   
   enable_interrupts(int_timer0);
   enable_interrupts(GLOBAL);
   progState = STATE_LED_OUTPUT;
}


void LED_Output()
{
   if (timeCounter > TIME_THREE)
   {
      output_High(CRITICAL_LED_PIN);
   }
   else
   {
      output_Low(CRITICAL_LED_PIN);
   }
   if (timeCounter > TIME_TWO)
   {
      output_High(MAJOR_LED_PIN);
   }
   else
   {
      output_Low(MAJOR_LED_PIN);
   }
   if (timeCounter > TIME_ONE)
   {
      output_High(MINOR_LED_PIN);
   }
   else
   {
      output_Low(MINOR_LED_PIN);
   }
   progState = STATE_LED_OUTPUT;
}


void main()
{
   timeInit();
   while (true)
   {
      switch(progState)
      {
         case STATE_LED_OUTPUT:
            LED_Output();
            break;

            default:
       }
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 07, 2004 7:15 pm     Reply with quote

Quote:
Anyways, i set up a little test program to see if i was correct,
and apparently i am missing something.


The RTCC does not automatically reload itself with the preset value.
You have to do it the isr. See the CCS examples, EX_SINE.C and
EX_DTMF.C.
seanyGood



Joined: 07 Jan 2004
Posts: 3
Location: Fresno, Ca

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

timer 0 help
PostPosted: Wed Jan 07, 2004 7:47 pm     Reply with quote

Right on the money PCM Programmer. Thanks.
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