PIC18f4431 int1 and int2

Discussion in 'Embedded Systems and Microcontrollers' started by dmta, Sep 2, 2013.

  1. dmta

    Thread Starter Member

    Mar 24, 2013
    32
    1
    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 ( (Unknown Language)):
    1. //******************************************************************************
    2. //LCD connections
    3. sbit LCD_RS at RD5_bit;
    4. sbit LCD_EN at RD4_bit;
    5. sbit LCD_D7 at RB5_bit;
    6. sbit LCD_D6 at RB2_bit;
    7. sbit LCD_D5 at RB0_bit;
    8. sbit LCD_D4 at RD6_bit;
    9.  
    10. sbit LCD_RS_Direction at TRISD5_bit;
    11. sbit LCD_EN_Direction at TRISD4_bit;
    12. sbit LCD_D7_Direction at TRISB5_bit;
    13. sbit LCD_D6_Direction at TRISB2_bit;
    14. sbit LCD_D5_Direction at TRISB0_bit;
    15. sbit LCD_D4_Direction at TRISD6_bit;
    16. //******************************************************************************
    17. //functions
    18. void timer1_init();             // initialize TIMER1
    19. void inbuiltF_init();           // initialize inbuilt functions
    20. void interrupts_init();         // initialize interrupts
    21. void interrupt();
    22. void display_lcd();
    23. //******************************************************************************
    24. // global variables
    25. int calpid = 0;                 // pid loop run command
    26. int ledon  = 0;
    27. int x      = 0;
    28.    
    29. //******************************************************************************
    30. void interrupt(){            
    31.      if(PIR1.TMR1IF==1){                            
    32.           calpid = 1;           // run PID loop
    33.           TMR1L         = 181;  //// preload timer1
    34.           TMR1H         = 179;  ////                          
    35.           PIR1.TMR1IF = 0;                                            
    36.      }  
    37.      if(INTCON.INT0IF==1){
    38.           ledon = 1;                
    39.           INTCON.INT0IF = 0;
    40.      }
    41.      if(INTCON3.INT1IF==1){
    42.           ledon = 1;                                
    43.           INTCON3.INT1IF==0;
    44.      }    
    45.      if(INTCON3.INT2IF==1){      
    46.           ledon = 1;                                          
    47.           INTCON3.INT2IF==0;
    48.      }              
    49. }
    50. //******************************************************************************
    51. void main(){
    52.  
    53.      TRISC.F7 = 0;
    54.      PORTC.F7 = 0;
    55.      inbuiltF_init();  
    56.      interrupts_init();      
    57.      timer1_init();            
    58. //******************************************************************************
    59.      while(1) {
    60.           while(ledon==1){
    61.                Lcd_Out(1,1,"led on");
    62.                Delay_ms(100);
    63.                Lcd_Out(1,1,"      ");
    64.                ledon = 0;
    65.           }          
    66.           while(calpid==1){
    67.                x = x + 1;
    68.                if(x<128){            
    69.                     PORTC.F7 = 1;                  
    70.                }
    71.                else if(x<512){
    72.                     PORTC.F7 = 0;
    73.                }
    74.                else if(x==512){
    75.                     x = 0;
    76.                }              
    77.                calpid = 0;                            
    78.           }
    79.      }
    80. }
    81. //******************************************************************************
    82. void timer1_init(){                // initialized to give 256Hz interrupt for PID loop execution
    83.      TMR1L         = 181;          // preload timer1
    84.      TMR1H         = 179;
    85.      
    86.      T1CON.RD16    = 1;          // enable register read/write of TIMER1 in two 16-bit operations[7](1)
    87.      T1CON.T1RUN   = 0;          // system clock is derived from another source                  [6](0)
    88.      T1CON.T1CKPS1 = 0;          // ##increments with 1:2 prescale value                         [5](0)
    89.      T1CON.T1CKPS0 = 1;          // ##                                                           [4](1)
    90.      T1CON.T1OSCEN = 0;          // TIMER1 oscillator is shut-off                                [3](0)
    91.      T1CON.TMR1CS  = 0;          // TIMER1 uses internal clock (FOSC/4)                          [1](0)
    92.      T1CON.TMR1ON  = 1;          // enable TIMER1                                                [0](1)
    93. }
    94. //******************************************************************************
    95. void inbuiltF_init(){
    96.      Lcd_Init();
    97.      Lcd_Cmd(_LCD_CLEAR);
    98.      Lcd_Cmd(_LCD_CURSOR_OFF);
    99. }
    100. //******************************************************************************
    101. void interrupts_init(){
    102.      RCON.IPEN   = 0;           // disable priority levels on interrupts         (0)
    103.      
    104.      INTCON.GIE  = 1;           // global interrupts enabled                     (1)
    105.      INTCON.PEIE = 1;           // enable all unmasked peripheral interrupts     (1)
    106.      
    107.      PIE1.TMR1IE = 1;           // TIMER1 overflow interrupt enabled             (1)
    108.      IPR1.TMR1IP = 0;           // TIMER1 overflow interrupt set to low priority (0)
    109.      PIR1.TMR1IF = 0;           // clear TIMER1 overflow interrupt flag          (0)    
    110.      
    111.      
    112.      INTCON.INT0IE   = 1;       // RC3 external interrupt enable                 (1)
    113.      INTCON2.INTEDG0 = 1;       // external interrupt0 RC3 on rising edge        (1)
    114.      INTCON.INT0IF   = 0;       // clear RC3 INT0 interrupt flag    
    115.      
    116.      INTCON3.INT1IP  = 0;       // INT1 external interrupt priority - low        (0)
    117.      INTCON2.INTEDG1 = 1;       // external interrupt1 RC4 on rising edge
    118.      INTCON3.INT1IF  = 0;       // clear RC4 INT1 interrupt flag
    119.      INTCON3.INT1IE  = 1;       // RC4 external interrupt enable      
    120.      
    121.      INTCON3.INT2IP  = 0;       // INT2 external interrupt priority - low         (0)
    122.      INTCON2.INTEDG2 = 1;       // external interrupt2 RC5 on rising edge
    123.      INTCON3.INT2IF  = 0;       // clear RC5 INT2 interrupt flag
    124.      INTCON3.INT2IE  = 1;       // RC5 external interrupt enable                              
    125. }
    regards
     
    Last edited: Sep 3, 2013
  2. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,677
    2,727
    Code ( (Unknown Language)):
    1.      if(INTCON3.INT1IF==1){
    2.           ledon = 1;                                
    3.           INTCON3.INT1IF==0;
    4.      }    
    5.      if(INTCON3.INT2IF==1){      
    6.           ledon = 1;                                          
    7.           INTCON3.INT2IF==0;
    8.      }              
    9.  
    Perhaps you should set the interrupt flags to 0 rather than compare them with 0?
     
    dmta likes this.
  3. dmta

    Thread Starter Member

    Mar 24, 2013
    32
    1
    That was IT. Thank you !!!!!!!!!!!!!!!!!!!!11
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  5. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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.
     
Loading...