PIC18f4431 int1 and int2

Thread Starter

dmta

Joined Mar 24, 2013
32
Hi all,

I am trying to configure the external interrupts int1 and int2 of a pic18f4431 without any success. The int0 and timer1 interrupts which are also running work just fine. Please look at my code and tell me what i am doing wrong.

I am using miKroC pro for PIC


Rich (BB code):
//******************************************************************************
//LCD connections
sbit LCD_RS at RD5_bit;
sbit LCD_EN at RD4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB0_bit;
sbit LCD_D4 at RD6_bit;

sbit LCD_RS_Direction at TRISD5_bit;
sbit LCD_EN_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB0_bit;
sbit LCD_D4_Direction at TRISD6_bit;
//******************************************************************************
//functions
void timer1_init();             // initialize TIMER1 
void inbuiltF_init();           // initialize inbuilt functions
void interrupts_init();         // initialize interrupts
void interrupt();
void display_lcd();
//******************************************************************************
// global variables
int calpid = 0;                 // pid loop run command
int ledon  = 0;
int x      = 0;
    
//******************************************************************************
void interrupt(){             
     if(PIR1.TMR1IF==1){                            
          calpid = 1;           // run PID loop 
          TMR1L         = 181;  //// preload timer1
          TMR1H         = 179;  ////                          
          PIR1.TMR1IF = 0;                                            
     }   
     if(INTCON.INT0IF==1){
          ledon = 1;                
          INTCON.INT0IF = 0;
     }
     if(INTCON3.INT1IF==1){
          ledon = 1;                                 
          INTCON3.INT1IF==0;
     }     
     if(INTCON3.INT2IF==1){       
          ledon = 1;                                          
          INTCON3.INT2IF==0;
     }              
}
//******************************************************************************
void main(){

     TRISC.F7 = 0;
     PORTC.F7 = 0;
     inbuiltF_init();  
     interrupts_init();       
     timer1_init();            
//******************************************************************************
     while(1) {
          while(ledon==1){
               Lcd_Out(1,1,"led on");
               Delay_ms(100);
               Lcd_Out(1,1,"      ");
               ledon = 0;
          }          
          while(calpid==1){
               x = x + 1;
               if(x<128){            
                    PORTC.F7 = 1;                   
               }
               else if(x<512){
                    PORTC.F7 = 0;
               }
               else if(x==512){
                    x = 0;
               }               
               calpid = 0;                            
          } 
     }
}
//******************************************************************************
void timer1_init(){                // initialized to give 256Hz interrupt for PID loop execution
     TMR1L         = 181;          // preload timer1
     TMR1H         = 179;
     
     T1CON.RD16    = 1;          // enable register read/write of TIMER1 in two 16-bit operations[7](1)
     T1CON.T1RUN   = 0;          // system clock is derived from another source                  [6](0)
     T1CON.T1CKPS1 = 0;          // ##increments with 1:2 prescale value                         [5](0)
     T1CON.T1CKPS0 = 1;          // ##                                                           [4](1)
     T1CON.T1OSCEN = 0;          // TIMER1 oscillator is shut-off                                [3](0)
     T1CON.TMR1CS  = 0;          // TIMER1 uses internal clock (FOSC/4)                          [1](0)
     T1CON.TMR1ON  = 1;          // enable TIMER1                                                [0](1)
}
//******************************************************************************
void inbuiltF_init(){
     Lcd_Init();
     Lcd_Cmd(_LCD_CLEAR);
     Lcd_Cmd(_LCD_CURSOR_OFF);
}
//******************************************************************************
void interrupts_init(){
     RCON.IPEN   = 0;           // disable priority levels on interrupts         (0)
     
     INTCON.GIE  = 1;           // global interrupts enabled                     (1)
     INTCON.PEIE = 1;           // enable all unmasked peripheral interrupts     (1)
     
     PIE1.TMR1IE = 1;           // TIMER1 overflow interrupt enabled             (1)
     IPR1.TMR1IP = 0;           // TIMER1 overflow interrupt set to low priority (0)
     PIR1.TMR1IF = 0;           // clear TIMER1 overflow interrupt flag          (0)     
     
     
     INTCON.INT0IE   = 1;       // RC3 external interrupt enable                 (1)
     INTCON2.INTEDG0 = 1;       // external interrupt0 RC3 on rising edge        (1)
     INTCON.INT0IF   = 0;       // clear RC3 INT0 interrupt flag     
     
     INTCON3.INT1IP  = 0;       // INT1 external interrupt priority - low        (0)
     INTCON2.INTEDG1 = 1;       // external interrupt1 RC4 on rising edge
     INTCON3.INT1IF  = 0;       // clear RC4 INT1 interrupt flag
     INTCON3.INT1IE  = 1;       // RC4 external interrupt enable      
     
     INTCON3.INT2IP  = 0;       // INT2 external interrupt priority - low         (0)
     INTCON2.INTEDG2 = 1;       // external interrupt2 RC5 on rising edge
     INTCON3.INT2IF  = 0;       // clear RC5 INT2 interrupt flag 
     INTCON3.INT2IE  = 1;       // RC5 external interrupt enable                              
}
regards
 
Last edited:

joeyd999

Joined Jun 6, 2011
5,181
Rich (BB code):
     if(INTCON3.INT1IF==1){
          ledon = 1;                                 
          INTCON3.INT1IF==0;
     }     
     if(INTCON3.INT2IF==1){       
          ledon = 1;                                          
          INTCON3.INT2IF==0;
     }
Perhaps you should set the interrupt flags to 0 rather than compare them with 0?
 

ErnieM

Joined Apr 24, 2011
8,376
I've seen many references in C style guides that one should always do comparisons with the constant on the left, as this WILL cause an error when the == is replaced by a single =

if (12 == A) // valid
if (12 = A) // always invalid


However... I don't see anything that would guard against this sort of typo. A second pair of eyeballs is a very effective tool. However, code walking with a simulator or a debugger will find this and many more bugs.
 

THE_RB

Joined Feb 11, 2008
5,438
I'm pretty sure that same compiler generates a warning message at the bottom of the screen for this plain assignment;
register == constant;

but dmta may have that panel at the screen bottom closed when compiling. It's the same panel that shows the build results.
 
Top