16F1827 interrupt hosed


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1

    Question 16F1827 interrupt hosed

    This is very strange. I have the interrupt working with a toggle on RB1 while main is generating in a loop a pulse on RB0.
    During power up both ports are putting out a toggle, but about 5 to 10 seconds after power on the interrupt generated toggle stops. Main still is toggling away. I went through and made sure other peripheral interrupt sources are off...

    what am I missing? Would someone please load this code and verify that the interrupt is stopping for some reason?



    Code:
    
    ' This code is for 16F1827 MCU with built in oscillator (Max Osc 32Mhz)
    ' Code requires PicBasic Pro 2.60 along with MPASM assembler
    
    ASM
        __config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
        __config _CONFIG2, _PLLEN_OFF & _LVP_OFF & _LVP_OFF & _STVREN_OFF
            
    ENDASM
    DEFINE OSC 8  
        
    ' Define ADCIN parameters
      DEFINE ADC_BITS 8   ' Set number of bits in result
      DEFINE ADC_CLOCK 4   ' Set clock source, look on data sheet for chart
      DEFINE ADC_SAMPLEUS 500   ' Set sampling time in uS 
      
      DEFINE  INTHAND myint                   ' Setup interrupt handler
       
    ;*******************************************************************************
    ;*******************************************************************************
    ; System hardware configuration
    ;*******************************************************************************
    ;*******************************************************************************
            OSCCON = %01110000   ' 8MHz internal
            ANSELA = %00000001   ' all digital. A/D disabled
            ANSELB = %00000000
            TRISB = %00000000    ' set port directions 0 = output, 1 = input
            TRISA = %00000001
            ADCON0 = %00000111  ' Set PORTA digital 1, analog 0
            APFCON0.0 = 1        ' CCP1 PWM output on RB0
            APFCON0.3 = 1        ' CCP2 PWM output on RA7
            APFCON1 = 1
            CCPR1L    = 64       ' Set PWM Duty-Cycle to 50%
            CCPR2L    = 64
            PR2 = 49             ' Set PWM frequency 
            CCP1CON = %00000000  ' Mode select = PWM off , to turn on %00001100
            CCP2CON = %00000000  ' Mode select = PWM off, to turn on %00001100
            T2CON = %00000110    ' %00000100 = TMR2 ON 1:1  pre-scale
                                 ' %00000101 = TMR2 ON 1:4  pre-scale
                                 ' %00000110 = TMR2 ON 1:16 pre-scale
                                 ' %00000111 = TMR2 ON 1:64 pre-scale
     
            
    ;*******************************************************************************
    ; Program variables 
    ;*******************************************************************************
            ADCVAL  var byte
          
            
            
    ;*******************************************************************************        
    ; Program constants
    ;*******************************************************************************
            
     
    
    ;******************************************************************************* 
    ; Power on initialization to known port states   
    ;*******************************************************************************
          
    PowerOn_init: 
            PortA = 0             ' force low to avoid glitches 
            POrtB = 0
            PIR1 = 0             ' clear TMR1 int flag
            PIE1 = %00000001     ' TMR1 int enabled
            INTCON = %11000000   ' global & peripheral ints enabled
            T1CON = %00010001    ' TMR1 1:2 prescale, timer1 on
    
    ;*******************************************************************************
    ;*******************************************************************************
     goto mainloop
    asm
    ; Save W, STATUS and PCLATH registers, if not done previously
    myint   
            ; retfie auto-restores w, status, bsr, fsr and pclath
      bcf   T1CON,TMR1ON ; stop timer 1
      bcf   PIR1,TMR1IF  ; clear over flow flag
      movlw 0xEF         ; load timer for 16,535 * 2 prescaler interrupt on 16 bit timer
      movwf TMR1H        ; load high byte
      movlw 0xEF         
      movwf TMR1L        ; load low byte
      bsf   T1CON,TMR1ON ; re-enable timer 1
      movlw 0x02
      xorwf PORTB,f      ; toggle RB1
      retfie             ; Return from interrupt
     
            retfie                        ; Return from interrupt
    endasm
     
     
    mainloop:
        adcin 0, adcval
        
        LATB.0 = 1
        pauseus (4*adcval) + 1000
        LATB.0 = 0 
        pause 15
        
        goto mainloop

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: 16F1827 interrupt hosed

    Try adding BSR=0 as the first asm instruction in your ISR. If this works, I will be happy to explain why. But if not the reason will confuse the issue.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: 16F1827 interrupt hosed

    Very strange...I did what you suggested but it only prolonged the end result. I am seeing two things when the interrupt crashes....either it hoses the main loop and the interrupt oscillates at a high frequency of 235 Khz or it completely stops and main keeps going with its toggle. This is what I added to the ISR.

    Code:
    asm
    ; Save W, STATUS and PCLATH registers, if not done previously
    myint   
            ; retfie auto-restores w, status, bsr, fsr and pclath
      
      bcf   BSR,0
      bcf   T1CON,TMR1ON ; stop timer 1
      bcf   PIR1,TMR1IF  ; clear over flow flag
      movlw 0xEF         ; load timer for 16,535 * 2 prescaler interrupt on 16 bit timer
      movwf TMR1H        ; load high byte
      movlw 0xEF         
      movwf TMR1L        ; load low byte
      bsf   T1CON,TMR1ON ; re-enable timer 1
      movlw 0x02
      xorwf PORTB,f      ; toggle RB1
      retfie             ; Return from interrupt
            
    endasm

  4. #4
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: 16F1827 interrupt hosed

    Try bsr=0. There maybe more bank select bit then just 1.Like this:
    Code:
    Myint
    Bsr=0
    asm
    Rest of isr
    OR
    Code:
    myint
    asm
    clrf bsr
    rest of isr
    Last edited by cncmachineguy; - 18th October 2011 at 05:43.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  5. #5
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: 16F1827 interrupt hosed

    here is what I think is happening:
    When the INT is called, the state of the BSR is unknown. If the main program is doing something that requires bsr to be other then 0, when you enter the ISR you can't manipulate the things in the ISR because the wrong bank is selected.

    When you added the BCF BSR,0 - This almost fixed it but not quite. When it stays in the ISR that is because you can't clear the flag (acting on wrong reg, NOT PIR) so the INT just fires forever. My guess is when you think the main is running without INT, that may be not true, it may be that you just aren't toggling the output so you think the ISR has not fired.

    The easiest way I know of to troubleshoot this is using MPLAB SIM inside of MPLAB. put a breakpoint at the first line of the ISR, then when it fires you can single step through while watching reg's like port b and even better BSR!
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: 16F1827 interrupt hosed

    Thanks for your help! I will try your suggestion. I did notice one thing before leaving the office last night, when I remove the ADC from main everything works well. When I rerun the test using ADC enabled it screws things up. Do I need to also do something with T1CON like select a bank of memory that it can write to that is common across banks? some how I think writing to values to the timer is somehow not being read out of the correct bank at some point after the interrupt is enabled upon exit....just my guess.

    Nick

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts