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

manual setting/clearing INTERRUPT BIT

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



Joined: 22 Sep 2012
Posts: 37

View user's profile Send private message

manual setting/clearing INTERRUPT BIT
PostPosted: Wed Jan 02, 2013 7:48 am     Reply with quote

hi,

For timing accuracy, I wish to use TIMER2 interrupt flag in the main loop.
such that each time the interrupt is set, the mc performs some opertations, for instance print some value in this case...
However, it seems that the following simple code doesn't work. It prints non-stop the string even though it should roughly print it every second.

Can someone please tell me why?
Thanks

Code:
#include <16F1783.h>
#bit  TIMER2IF=0x11.1
//#INCLUDE <stdlib.h>
#include "consts.c"
#fuses INTRC_IO, NOWDT, NOBROWNOUT, PUT,NOMCLR
#use delay(clock=1M)
#use rs232(baud=4800,xmit=pin_B7,rcv=pin_B6)


void main()
{
  setup_timer_2(T2_DIV_BY_64,200,16);
  enable_interrupts(INT_TIMER2);
  enable_interrupts(GLOBAL);
   
  while (TRUE)
  {
   if (TIMER2IF==1)
   {
      TIMER2IF==0;
      printf("abc");
   }
     
   
  }
 
 
     

}

asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Jan 02, 2013 7:54 am     Reply with quote

You have enabled a timer 2 interrupt but do not have a timer 2 interrupt handler. You are using polled mode to interrogate timer 2 status - this is valid. Simply remove the two interrupt enable lines.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
danielz85



Joined: 22 Sep 2012
Posts: 37

View user's profile Send private message

PostPosted: Wed Jan 02, 2013 8:07 am     Reply with quote

problem solved
thanks asmallri
Ttelmah



Joined: 11 Mar 2010
Posts: 19391

View user's profile Send private message

PostPosted: Wed Jan 02, 2013 8:16 am     Reply with quote

Use:
Code:

void main(void) {
   setup_timer_2(T2_DIV_BY_64,200,16);
   enable_interrupts(INT_TIMER2); //It is OK to enable this, but not
   //necessary. It _is_ needed if (for instance), you want this to wake
   //the chip
   //enable_interrupts(GLOBAL); //You must not enable this without
   //a handler 
   while (TRUE) {
      if (interrupt_active(INT_TIMER2)) { //This tests the flag for you
         clear_interrupts(INT_TIMER2); //This clears the interrupt
         printf("abc");
      }
   }
}


The handler, is called, in the following situation:

Timer2 interrupt flag is set.
and Timer2 interrupt enable is set
and global interrupt enable is set.

If you do not have a handler present, you must ensure that all three of these do not happen. If they do, the code will call the non existent handler - crash......

Just leave the global flag off. You don't actually have to enable the individual interrupt for this test operation, but you _do_, if you want the interrupt to do other things (like wake the chip). Only 'enabled' interrupts will do this. Hence it is common practice to always enable any interrupt you are 'using'.

Best Wishes
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Jan 02, 2013 10:51 am     Reply with quote

Code:
void main()
{
  setup_timer_2(T2_DIV_BY_64,200,16);
  enable_interrupts(INT_TIMER2);
  enable_interrupts(GLOBAL);
   
  while (TRUE)
  {
   if (TIMER2IF==1)
   {
      TIMER2IF==0;
      printf("abc");
   }
  }
}
I would not expect the line preceeding your printf statement to have the effect of clearing the interrupt flag.

Hence the continuous printing.

Mike
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