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

Problem in reading and setting timer0

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



Joined: 18 Apr 2016
Posts: 4

View user's profile Send private message

Problem in reading and setting timer0
PostPosted: Mon Apr 18, 2016 5:32 am     Reply with quote

Hello to all,

i am very new to ccs service, i will come directly to point.
i am trying to set a timer0 with a maximum prescalar value as 256 to generate a 1 sec delay.

While generating the code through the graphic CCS code composer i have generated the timer0 code but the problem is its not running.

Below i have attached the code. Pls go through and reply me.
Code:

long int count=0;
int a;

void  TIMER0_isr(void)
{
  a=1;
 // clear_interrupt(INT_TIMER0);
  set_timer0(0);
 

}

void main()
{
unsigned int time;
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);      //13.1 ms overflow
   
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

   //
  // printf("timer enabled\n");

set_timer0(0);

 if(a==1)
   {
   printf("isr enabled");
   }

   while(TRUE)
   {
   
   
 
   
     

      //TODO: User Code
   }

}
temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Apr 18, 2016 5:39 am     Reply with quote

First place to look is in the manual ! There IS an example using Timer0. You can also scan thee dozens of working code examples that CCS supplies, in the examples folder.

Also when posting questions please post compiler version, pic type and use the 'code button'( just above 'font size') to post your code. It'll come up in a form that's easy to read.

And, your 'program' is not complete. Without seeing what clock you've chosen your ISR may not execute in 13.1 ms.

Hint....One of the entries in the FAQ section has details you need to read.

Jay
MVA



Joined: 18 Apr 2016
Posts: 4

View user's profile Send private message

Problem in reading and seting timer0
PostPosted: Mon Apr 18, 2016 5:48 am     Reply with quote

Thanks Jay for the reply..

i am using PIC 16F877a, 20MHZ, timer0(internal clock, prescalar=256)
ccs version IDE version 5.007 PCWHD Cmplier

Code:

#include <main.h>

#include <string.h>
#include <stdio.h>

#INT_TIMER0

long int count=0;
static int a;

void  TIMER0_isr(void)
{
  a=1;
 // clear_interrupt(INT_TIMER0);
  set_timer0(0);
 

}

void main()
{
unsigned int time;
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);      //13.1 ms overflow
   
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

   //
  // printf("timer enabled\n");

set_timer0(0);

while(TRUE)
{
 if(a==1)
   {
   printf("isr enabled");
   }
}
   

}
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Mon Apr 18, 2016 8:05 am     Reply with quote

OK.

First are you sure the chip is actually running?. You don't show your fuses etc., and the first thing always must be 'check the chip is actually working'.....

Then come comments inline:
Code:

//#include <main.h> You don't show the contents of this, so I'm
//setting up a typical chip
#include <18F4520.h>
#device ADC=10
#use delay(crystal=20000000)
#use RS232 (UART1, BAUD=9600, ERRORS)

#include <string.h>
#include <stdio.h>

unsigned int16 count=0;
int1 a=FALSE; //static is pointless except to force it to be cleared
//For a flag, just use int1. Generally avoid 'long' etc.. These have
//different meanings on different chips, so can lead to problems
//later if you change chips.

#INT_TIMER0 //This should be the line before the routine
void  TIMER0_isr(void)
{
    a=TRUE;
    count++;
    //The timer is already zero if it has wrapped
    //the compiler clears the interrupt unless you tell it not to.
}

void main(void)
{
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_BIT);
   //13.1 ms overflow - Implies 20MHz?...
   set_timer0(0);
   clear_interrupt(INT_TIMER0);
   //this way the timer starts from zero

   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

   while(TRUE)
   {
       if(a)
       {
           a=FALSE;
           printf("count = %ld`n",count);
           //Made it use 'count' since you are defining a count
       }
   }
}

This merrily runs and displays count=0 etc., with each output 13.1072mSec apart.

Ouch. I see you say 5.007. This was at best a beta version. The earliest truly working versions were around 5.012. However for a basic routine like this it should work.
MVA



Joined: 18 Apr 2016
Posts: 4

View user's profile Send private message

Problem in reading and seting timer0
PostPosted: Tue Apr 19, 2016 12:31 am     Reply with quote

Thanks for the Reply

i have reconfigured my code as mentioned above

but still i am not getting the count in ISR rountine

i am getting as output:
value=102
value=169
value=235
value=46
value=92
value=138
value=205
value=15
value=62
value=108
value=174
value=241
value=51
value=98
value=144
value=210
value=21
value=67
value=113
value=180
value=246
value=57
value=103
value=170
value=236
value=47
value=93
value=139
value=206
value=16
value=63
value=109
value=175
value=242
value=52
value=99
value=145
value=211
value=22
value=68
value=114
value=181
value=247
value=58
value=104
value=171
Code:

//********************* header files and include files **********************//

#include <main.h>
#include <string.h>
#include <stdio.h>

#INT_TIMER0

//********************* header files and include files **********************//




//******************* global variable declartion *******************//

unsigned int16 count=0,value;
int1 a=FALSE;

//******************* global variable declartion *******************//


//****************** timer0 ISR ***************************//

void  TIMER0_isr(void)
{

  //a=TRUE;
  count++;
  //set_timer0(0);
 // clear_interrupt(INT_TIMER0);
             
              if(count>=76)
              {
                  a=TRUE;
                 // printf("count=%ld\n",count);
             
              }
 
 
}

//****************** timer0 ISR ***************************//

//******************** Main ptogram *******************************//

void main()

{
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);      //13.1 ms overflow
   set_timer0(0);
   clear_interrupt(INT_TIMER0);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

      while(TRUE)
            {
                   
               value=get_timer0();
               delay_ms(100);
               printf("value=%ld\n",value);
           
                if(a)
                {
                   
                   printf("count=%ld\n",count);
                     
                }     
                     

            }
 

}


//******************** Main ptogram *******************************//
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Tue Apr 19, 2016 12:52 am     Reply with quote

Er.

You are ignoring just about everything you are being told.

_Read the comments_.

You are now outputting the timer value, not the count, and so will get almost random numbers as your reading moment 'beats' with the timer count. Duh...
You are also seperating the #INT declaration from it's routine again.

Add just the fuses etc., for your processor, and run the code as posted. _Then_ start to play once you understand what it is doing.
MVA



Joined: 18 Apr 2016
Posts: 4

View user's profile Send private message

Problem in reading and seting timer0
PostPosted: Tue Apr 19, 2016 1:04 am     Reply with quote

Thanks friend

i have corrected my code and i have got my count value too..
thanks a lot

One more what's the significance of defining #INT_TIMER0 before the isr,why is it so...


Code:

//********************* header files and include files **********************//

#include <main.h>
#include <string.h>
#include <stdio.h>



//********************* header files and include files **********************//




//******************* global variable declartion *******************//

unsigned int16 count=0,value;
int1 a=FALSE;

//******************* global variable declartion *******************//


//****************** timer0 ISR ***************************//

#INT_TIMER0
void  TIMER0_isr(void)
{

  //a=TRUE;
  count++;
  //set_timer0(0);
 // clear_interrupt(INT_TIMER0);
             
              if(count>=76)
              {
                  a=TRUE;
                 // printf("count=%ld\n",count);
             
              }
 
 
}

//****************** timer0 ISR ***************************//

//******************** Main ptogram *******************************//

void main()

{
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);      //13.1 ms overflow
   set_timer0(0);
   clear_interrupt(INT_TIMER0);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

      while(TRUE)
            {
                   
              /* value=get_timer0();
               delay_ms(100);
               printf("value=%ld\n",value);*/
           
                if(a)
                {
                   
                   printf("count=%ld\n",count);
                     
                }     
                     

            }
 

}


//******************** Main ptogram *******************************//
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Tue Apr 19, 2016 8:03 am     Reply with quote

Read the manual....

From #INT_XXXX

"These directives specify the following function is an interrupt function. Interrupt functions may not have any parameters. Not all directives may be used with all parts. See the devices .h file for all valid interrupts for the part or in PCW use the pull down VIEW | Valid Ints"

It's a pre-processor command to say that the function following, is the interrupt handler for that particular interrupt.
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