View Full Version : Interupt for RB1 & RB2
  
itmsoil
- 15th March 2004, 09:48
Help is needed guys.
   i am trying to implement this program below but using either 
  RB1 or RB2 as RB0 on my pic is already being used. i am using a 
  pic18f452
  Could another help out .
' On Interrupt - Interrupts in BASIC Using 18f452
'  Turn LED on.  Interrupt on PORTB.0 (INTE) turns LED off.
'  Program waits .5 seconds and turns LED back on.
led     var     PORTB.7
INTCON2.7 = 0       ' Enable PORTB pullups
 INTCON2.6 = 0      'interupt on falling edge        
        On Interrupt Goto myint ' Define interrupt handler
        INTCON = $90            ' Enable INTE interrupt
loop:   High led                ' Turn LED on
        Goto loop               ' Do it forever
' Interrupt handler
        Disable                 ' No interrupts past this point
myint:  Low led                 ' If we get here, turn LED off
        Pause   500             ' Wait .5 seconds
        INTCON.1 = 0            ' Clear interrupt flag
        Resume                  ' Return to main program
        Enable
 Itm
Dave
- 15th March 2004, 12:18
You need to look at your data sheet for the 18F452 page 76 & 77 as they tell you what bits to be set for RB1 & RB2. INTCON2 bit 4 & 5 set the edge to trigger the interrupt. INTCON3 bits 6 & 7 set the interrupt priority, INTCON3 bits 3 & 4 set the enable mask and INTCON3 bits 0 & 1 are the interrupt flags set for the ports RB1 & RB2. It's all in the document.....
itmsoil
- 15th March 2004, 14:59
Dave Thank for the pointer
  i have manage to get just RB0 and RB2 working but i do get strange results for RB1
 when i press the button connected to Rb1, the the message Rb0 pressed displayed before displaying Rb1 pressed.
 Could someone please help me with this
 DEFINE LOADER_USED  1  ' uses a bootloader
  INCLUDE "Modedefs.Bas" 
 
' ** Setup Crystal Frequency in mHz **
	DEFINE 	OSC		20		' Set Xtal Frequency
DEFINE  LCD_DREG         PORTD
DEFINE  LCD_DBIT         4
DEFINE  LCD_RSREG        PORTD
DEFINE  LCD_RSBIT        1
DEFINE  LCD_EREG         PORTD
DEFINE  LCD_EBIT         0
DEFINE LCD_RWREG         PORTD	' PORTD for R/w bit
DEFINE LCD_RWBIT 2	    ' PORTD-1 for LCD's R/W line
DEFINE  LCD_BITS         4
DEFINE  LCD_LINES        2
DEFINE LCD_COMMANDUS 	2000' Command Delay (uS)				
DEFINE LCD_DATAUS 		50	' Data Delay (uS)	
	I			CON	254		' Control Byte
	Clr			CON	1		' Clear the display
	Line1		CON	128		' Point to beginning of line 1
	Line2		CON	192		' Point to beginning of line 2
' ** Define the bits within the ADCON1 register **
index VAR BYTE	
R0    VAR BYTE
R1    VAR BYTE
R2    VAR BYTE
TRISA=%00111111
TRISB=%00100111 
TRISC=%10100110 
TRISD=%00001000 
TRISE.0=1
TRISE.1=1
TRISE.2=1
        INTCON2.7=0  'Enable portB pullups
        INTCON2.6=0  '%Interupt on falling edge
       
        
	Main:
 Pause 1000  ' delay for lcd
 
 Loop:
'==== ================================================== ======================== 
 LCDOut I,Clr	
 LCDOut I,Line1,"Power on"  
 For index =1 TO 500
 Pause 1
 Next index
  
  ON INTERRUPT GoTo myint1  ' Define interrupt handler
  INTCON=%10010000          ' Enable RB0 interupt
  
  'The RB0 interupts works ok but RB1 & RB2 don't
  'Dummy code
  'kkkkkkkkkkkkkkkkkkk
  
  ON INTERRUPT GoTo myint2 ' Define interrupt handler
    INTCON2.4=0: INTCON2.5=0   ' Enable RB2 interupt
    INTCON3.7=1:INTCON3.6=1
    INTCON3.4=0:INTCON3.3=1
   
   ON INTERRUPT GoTo myint3 ' Define interrupt handler
     
    INTCON2.4=0: INTCON2.5=0   ' Enable RB2 interupt
    INTCON3.7=1:INTCON3.6=1
    INTCON3.4=1:INTCON3.3=0
   
   
       GoTo Loop
  
 End
 
' RB0 Interrupt handler
' ===========================
        
myint1:
       Disable                 ' No interrupts past this point  
       LCDOut I,Clr	
       LCDOut I,Line1,"RB0 Pressed"      
       For R0 = 1 TO 500        ' allow user to see
       Pause 1
       Next
       INTCON.1 = 0             ' Clear interrupt flag
        Resume                  ' Return to main program
        Enable
        
        
' RB1 Interrupt handler
' ===========================
myint2:        
        Disable                 ' No interrupts past this point
       LCDOut I,Clr	
       LCDOut I,Line1,"RB1 Pressed"  
       For R1 = 1 TO 500        ' allow user to see
       Pause 1
       Next
      
        INTCON3.0 = 0           ' Clear interrupt flag
        INTCON3.1 = 0 
        Resume                  ' Return to main program
        Enable
' RB2 Interrupt handler
' ===========================
myint3:      
        Disable                 ' No interrupts past this point
        
       LCDOut I,Clr	
       LCDOut I,Line1,"RB2 Pressed"  
       For R2 = 1 TO 500        ' allow user to see
       Pause 1
       Next
        INTCON3.0 = 0  
        INTCON3.1 = 0            ' Clear interrupt flag
        Resume                   ' Return to main program
        Enable
Itm
Dave
- 15th March 2004, 17:11
Well you almost have it right. There is only 1 interrupt vector or in other words there can only be 1 interrupt routine. Therefore after entering the interrupt routine you must check each interrupt flag to see which interrupt occured and service that interrupt. After this you must clear the approprate interrupt flag then return.
Dave Purola,
 
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.