Quantcast
Channel: MEL PICBASIC Forum
Viewing all articles
Browse latest Browse all 4747

Help Needed with Darrel's Interrupt on Change Routine

$
0
0
Hi All,

I have been going around in circles all day trying to get Darrel's Instant Interrupt routine (Interrupt on Change) working on two PORTB pins of a PIC16F1826. I want to increment or decrement a byte size variable (Level) between 1 and 8 using switches on RB1 and RB2. Each pin has a 10k pull down resistor and I have tried to set the IOCBP register to read rising edge triggers. The variable is displayed on a 16 x 2 LCD using LCDOUT command. I can display the variable (default is 3) but cannot get the switches to work.

My code is as follows:

Code:

#CONFIG
    __config _CONFIG1,    _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
    __config _CONFIG2,    _WRT_OFF & _PLLEN_ON & _STVREN_OFF & _BORV_25 & _LVP_OFF
#ENDCONFIG       

DEFINE OSC 32                      'Internal RC Oscillator with PLL

' Connect LCD DB4 to RA0 (Pin 17)
' Connect LCD DB5 to RA1 (Pin 18)
' Connect LCD DB6 to RA2 (Pin 1)
' Connect LCD DB7 to RA3 (Pin 2)
' Connect LCD R/S to RA6 (Pin 15)
' Connect LCD E  to RA7 (Pin 16)
' Connect Reset Button to MCLR (Pin 4)
' Connect Up Switch to RB1 (Pin 7) with 10k pulldown resistor
' Connect Down Switch to RB2 (Pin 8) with 10k pulldown resistor


INCLUDE "DT_INTS-14.bas"    ' Base Interrupt System
INCLUDE "ReEnterPBP.bas"    ' Include if using PBP interrupts

ASM
INT_LIST macro            ; IntSource,        Label,  Type, ResetFlag?
    INT_Handler    IOC_INT,  _PortBInterrupts,  PBP,  yes
    endm
    INT_CREATE              ; Creates the interrupt processor
ENDASM

@    INT_ENABLE  IOC_INT    ; enable PortB change interrupts


' Define LCD registers and bits
Define        LCD_DREG        PORTA
Define        LCD_DBIT        0
Define        LCD_RSREG        PORTA
Define        LCD_RSBIT        6
Define        LCD_EREG        PORTA
Define        LCD_EBIT        7
Define        LCD_BITS        4
Define        LCD_LINES        2

INTCON = %10001000                  'Enable Global Interupts and IOC interrupt
OSCCON= %11110000                  'PLL enabled, Internal RC-8MHz and PLL x 4

CM1CON0.7 = 0                              'Disable comparator 1
CM2CON0.7 = 0                      'Disable comparator 2

PORTB = 0:PORTA=0                  'Set all outputs to 0
TRISB = %00000110                  'Set PORTB.1 & 2 as digital input
                                    'All other PORTB pins as digital outputs
TRISA = %00100000                  'Set PORTA.5 as digital inputs
                                    'All other PORTA pins as digital outputs
                                    '(PORTA.5 can only be an input)
                                   
ANSELA = %00000000                  'Disable PORTA analog inputs
ANSELB = %00000000                  'Disable PORTB analog inputs

ADCON0 = %00000000                  'Enable ADC and select AN9

                                   
FVRCON = %00000000                  'Disable FVR
IOCBP = %00000110                  'Set IOC on rising edge for RB1 & RB2
IOCBN = 0                          'Disable IOC falling

;-- Place a copy of these variables in your Main program -------------------
;--  The compiler will tell you which lines to un-comment                --
;--  Do Not un-comment these lines                                      --
;---------------------------------------------------------------------------
wsave  VAR BYTE    $20    SYSTEM      ' location for W if in bank0
;wsave  VAR BYTE    $70    SYSTEM      ' alternate save location for W
                                        ' if using $70, comment wsave1-3

' --- IF any of these three lines cause an error ?? ------------------------
'      Comment them out to fix the problem ----
' -- Which variables are needed, depends on the Chip you are using --
wsave1  VAR BYTE    $A0    SYSTEM      ' location for W if in bank1
wsave2  VAR BYTE    $120    SYSTEM      ' location for W if in bank2
;wsave3  VAR BYTE    $1A0    SYSTEM      ' location for W if in bank3


Level  var    byte                'Level setting 1 - 8   
Level = 3                          'Set starting level to 1

pause 1000                          'Pause 1 second
LCDOUT $FE, 1                      'Clear screen

Again:
    lcdout $FE, $80, "Level = ", dec Level, "      "'Display Level
 
    goto Again


PortBInterrupts:

    if IOCBF.1 = 1  then
        Level = Level + 1
        If Level > 8 then
            Level = 8
        endif
    endif
   
    if IOCBF.2 = 1  then
        Level = Level - 1
        If Level < 1 then
            Level = 1
        endif
    endif
 
@ INT_RETURN
end

I have used the Instant Interrupt routine before (with some help from other forum members) but this time it has me perplexed. The process of commenting out variable wsave etc confuses me as on this occasion the compiler gives no error messages. I have tried a number of different combinations - all to no avail. I must admit, I have been clutching at straws for the last few hours!

Can somebody please look at the code and tell me what I am doing wrong?

Cheers
Barry
VK2XBP

Viewing all articles
Browse latest Browse all 4747

Trending Articles