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

Interrupt re-entrance

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



Joined: 01 Jul 2014
Posts: 41

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

Interrupt re-entrance
PostPosted: Fri Jul 25, 2014 7:22 am     Reply with quote

Hello,

I am using pic16f1946. Just trying to get interrupt from RS485 line. After first interrupt occurs it starts to re-entering interrupt continuously and Led starts blinking. What can be the reason? Here is my code.
Code:

#INCLUDE <16f1946.h>

#FUSES INTRC_IO      // Internal RC clock (OSC1 and OSC2 pins are normal I/O)
#FUSES NOWDT         // Watch Dog Timer disabled
#FUSES PUT           // Power Up Timer enabled
#FUSES NOMCLR        // Master Clear pin is used for I/O
#FUSES PROTECT       // Code protected from reads
#FUSES CPD           // Data EEPROM code protected
#FUSES NOBROWNOUT    // No Brown_Out reset
#FUSES NOCLKOUT      // Disable clock output on OSC2
#FUSES NOIESO        // Internal External Switch Over Mode disabled
#FUSES NOFCMEN       // Fail-safe clock monitor disabled
#FUSES WRT           // Program memory write protected                                             
#FUSES NOLVP         // Low Voltage Programming disabled
#FUSES NODEBUG       // No debug mode for ICD

#USE   DELAY(clock = 32000000)
#USE RS232(stream=RS485,baud=38400,xmit=PIN_G1,rcv=PIN_G2,ENABLE=PIN_G3,parity=N,bits=8,stop=1)

// Led pin
#DEFINE LED          PIN_E2             //output               // Led used in debugging
#INT_RDA2
void set_RS485_flag(){
   output_toggle(LED);
   delay_ms(1000);
}


void main()
{
   // Set I/O states of the ports
   //           76543210
   set_tris_a(0b00000000);
   set_tris_b(0b11000000);
   set_tris_c(0b00000000);
   set_tris_d(0b00000000);
   set_tris_e(0b00000000);
   set_tris_f(0b11110000);
   set_tris_g(0b11100100);
   
   output_low(LED);
   
   enable_interrupts(global);
   enable_interrupts(INT_RDA2);
   
   while(true)
   {
   }
jeremiah



Joined: 20 Jul 2010
Posts: 1319

View user's profile Send private message

PostPosted: Fri Jul 25, 2014 7:50 am     Reply with quote

hardware generates interrupts. You have to "clear" the reason for the interrupt in most of the interrupts, including the one you are using. Look up what RDA2 is for. You must complete the operation it expects or it will keep firing.

Also, no need for TRIS settings. Compiler does it for you.
ckielstra



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

View user's profile Send private message

PostPosted: Fri Jul 25, 2014 7:58 am     Reply with quote

Easy.
When the interrupt is triggered it is up to you to clear the interrupt condition, i.e. when the reason for triggering the interrupt is still there on interrupt exit, a new interrupt will be triggered immediately again. Depending on the interrupt there are different ways to accomplish this, see the datasheet. For portB interrupt for example you have to do a read operation on the port. For UARTs you have to read the received data byte.

Code:
#INT_RDA2
void set_RS485_flag(){
   fgetc(RS485);     // dummy read to clear the interrupt condition
   output_toggle(LED);
   delay_ms(1000);
}


Note 1: it is bad practise to have a long delay in your ISR. For purpose of your demonstration it is allowed but otherwise get out of the ISR as quick as possible.
Note 2: Always add the 'errors' directive to the #RS232 line. With this setting the compiler will create extra code for clearing error conditions in the hardware UART. As it is now the UART will block after 3 characters receiver buffer overflow and appears to be blocked until you reset the PIC.
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