Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC18f4431 int1 and int2

Status
Not open for further replies.

dmta

Member
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


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:
You are calling the "inbuiltF_init();" -function twice. And you are not calling the "interrupts_init();" -function anywhere.
 
Last edited:
Thank you misterT for your reply. But it is not the problem. It was just a mistake that I made when I was copying this code. I have corrected it in the post now.
 
OOOOOHHHHHHHHHH found it!!!!!!!!!!!!!!!
Code:
if(INTCON3.INT2IF==1){       
          ledon = 1;                                          
          INTCON3.INT2IF==0;
     }

I am not setting the flag to 0!!!!!!!!!!!
 
OOOOOHHHHHHHHHH found it!!!!!!!!!!!!!!!
Code:
if(INTCON3.INT2IF==1){       
          ledon = 1;                                          
          INTCON3.INT2IF==0;
     }

I am not setting the flag to 0!!!!!!!!!!!

Haha.. yes. Silly mistakes are the hardest ones to find. Some programmers write comparisons the other way i.e. if(1==INTCON3.INT2IF) So if you miss one "=" then the compiler gives an error.. but I think this is the first time I see a comparison "==" where assignment was intended.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top