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

Port B interrupt is not working with 16F1938. Help!

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



Joined: 26 Apr 2010
Posts: 56

View user's profile Send private message

Port B interrupt is not working with 16F1938. Help!
PostPosted: Thu Jul 21, 2011 5:57 pm     Reply with quote

This is an example code I got from PCM for 16F882. With some minor changes, it is NOT working with 16F1938.

The problem is that when an interrupt RB triggered by pushing a button, it will be triggered continuously and never stops.


Code:

#include <16F1938.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

#define SW_PIN   PIN_B1
#define LED_PIN  PIN_C7

int8 sleepFlag = 1;

#int_rb
void rb_isr(void)
{
   int8 value;
   
   output_toggle(LED_PIN);
   delay_ms(10);
   
   value = input(SW_PIN);
   //value = input_b( );   // even read twice, it is not working.
   if(value)
   {
      if(sleepFlag)
         sleepFlag = 0;
      else
         sleepFlag = 1;
   }
}

#INT_COMP
void Comp_int(void)
{
   sleepFlag = 0;
}

//============================
void main()
{
   int8 temp;
   
   output_low(LED_PIN);
   
   setup_comparator(CP1_A1_DAC|CP1_OUT_ON_A4|CP1_INT_H2L);
   setup_dac(DAC_VSS_VDD|DAC_OUTPUT);
   dac_write(3);

   temp = input(SW_PIN);
   clear_interrupt(INT_RB);
   enable_interrupts(INT_RB1);
   enable_interrupts(INT_COMP);
   enable_interrupts(GLOBAL);

   while(1)
   {
      delay_ms(100);
      if(sleepFlag)
         sleep();
     
   }
}


Please help. Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 22, 2011 3:36 pm     Reply with quote

Always post your compiler version. We don't remember your version
between posts and you might have updated it.

Also, I notice you have added all this comparator interrupt code and
DAC code. What external connections do you have on the PIC ?

Have you tested each hardware module and its code separately, and
proven that they work ? Have you tested the comparator code all
by itself in its own program ?

Before asking me for help, you need to do all this.
kongfu1



Joined: 26 Apr 2010
Posts: 56

View user's profile Send private message

PostPosted: Fri Jul 22, 2011 7:05 pm     Reply with quote

PCM programmer wrote:
Always post your compiler version. We don't remember your version
between posts and you might have updated it.

Also, I notice you have added all this comparator interrupt code and
DAC code. What external connections do you have on the PIC ?

Have you tested each hardware module and its code separately, and
proven that they work ? Have you tested the comparator code all
by itself in its own program ?

Before asking me for help, you need to do all this.


Compiler version: 4.122
MPLAB:8.73
ICD: PICKit3

Comparator is working fine and analog circuit is off during my test.
Analog signal is 0-3.3V and is connected to RA1 pin.

I just did a test by removing all the code related with comparator, the result is same.

Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 24, 2011 5:12 pm     Reply with quote

I found two bugs. There's one bug in the compiler and one in your code.
The compiler bug (vs. 4.122) is in the start-up code. It doesn't correctly
setup all the analog pins on the PIC to be digital i/o pins. But you can
fix it by adding the line shown in bold below.
Quote:

void main()
{
int8 temp;

setup_adc_ports(NO_ANALOGS);



The 2nd bug is in your code. For the 16F1938 and related PICs, you do
not clear the "change condition" by reading port B. You have to clear
a bit in the IOCBF register for the specific pin, or you can just clear the
whole register. I show how to do this below. Add the lines in bold:
Quote:

#include <16F1938.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

#byte IOCBF = 0x396
#define clear_IOC_flags() IOCBF = 0



Remove (or comment out) the input(SW_PIN) line, and add the line to
clear the IOC flags as shown below:
Quote:

#int_rb
void rb_isr(void)
{
int8 value;

output_toggle(LED_PIN);
delay_ms(10);

clear_IOC_flags();
//value = input(SW_PIN);



If you do all that, I think it should work.
kongfu1



Joined: 26 Apr 2010
Posts: 56

View user's profile Send private message

PostPosted: Mon Jul 25, 2011 11:29 am     Reply with quote

Hi PCM,

Thanks very much for the helps. I have talked with support of CCS. A call clear_interrupt should clear up IOCBF register. With 4.122 version, IOCBF is defined at address 0x393 instead of 0x396 for 16F1938. That is why the interrupt flag was not cleared. They have suggested same way as yours to fix it temporally. But they did not see first bug.

Thanks again.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 25, 2011 12:16 pm     Reply with quote

I was using the 16F1937 to test the problem in hardware. The 16F1937
has the ANSELx register bug in the start-up code. The 16F1938 does not.
The problem still exists in vs. 4.123. I emailed CCS about this today.
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