On Interrupt question


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Posts
    130

    Question On Interrupt question

    Hi, im trying to understand how to use ON INTERRUPT, so I created this little program:

    Code:
    @ device pic16F628A, intrc_osc_noclkout, wdt_on, mclr_off, protect_off
    DEFINE OSC 4
    
    trisb   =%00110000
    trisa   =%00000000
    'ansel   =%00000001  
    cmcon   =%00000111  'Comparators Off
    vrcon   =%00000000
    intcon  =%10101000  'interrupts enable    
    OPTION_REG =%01010001     
    
    but1    var trisb.4
    but2    var trisb.5
    ledwork var trisa.0
    led1    var trisa.1
    led2    var trisa.2
    led3    var trisa.3
    rs      var trisa.5
    
    
    conta   var byte
    
    
    on interrupt goto interrupcion
    
    ;SerOut rs,2,[10,13]
    ;SerOut rs,2,["inicio"]
    ;SerOut rs,2,[10,13]
    
    enable
    loop:
    
    ;disable
    ;SerOut rs,2,[10,13]
    ;SerOut rs,2,["Loop"]
    ;SerOut rs,2,[10,13]
    ;enable
    
    for conta=0 to 255 step 16  'working led rise routine
        pwm ledwork, conta, 10  
    next conta
    high ledwork
    
    for conta =0 to 100
        pause 10
    next conta
    
    low led1    'set status buttons leds off
    low led2
    low led3
    
    for conta=0 to 255 step 16  'Fall routine
        pwm ledwork, 255-conta, 10 
    next conta
    low ledwork
    
    for conta =0 to 100
        pause 10
    next conta
    
    low led1   'set button status leds off
    low led2
    low led3
    
    goto loop
    
    end
    disable 
    interrupcion:
    ;SerOut rs,2,[10,13]
    ;SerOut rs,2,["Interrupt"]
    ;SerOut rs,2,[10,13]
        intcon.0=0
        intcon.1=0
        intcon.2=0
            if but1=1 then
                but1=0
                high led1
            endif
            if but2=1 then
                but2=0
                high led2
            endif
    high led3   
        resume
    enable
    The idea is to trigger an interrupt when any of the buttons (but1 & but2) are pressed, to show the status a led (led1 & led2) is turned on for a second and off again. led3 is used as a way to know an interruption has been triggered.

    To simulate a real loop the "payload" of the program is a PWM pulse on ledwork

    There are serouts all over the place, but since I cant make the basic version to work, these are commented out.


    IMHO this program should work, but it doesn't. Could someone guide me in the right direction to fully understand how this should work?

    Thanks!

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    begin by changing this
    Code:
    but1    var trisb.4
    but2    var trisb.5
    ledwork var trisa.0
    led1    var trisa.1
    led2    var trisa.2
    led3    var trisa.3
    rs      var trisa.5
    to
    Code:
    but1    var PORTB.4
    but2    var PORTB.5
    ledwork var PORTB.0
    led1    var PORTB.1
    led2    var PORTB.2
    led3    var PORTB.3
    rs      var PORTB.5
    Let's see what happen now. i didn't go deeper than this.. for now
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Feb 2005
    Posts
    130


    Did you find this post helpful? Yes | No

    Default

    I changed as you suggested but corrected the ports, I guess this is what you meant

    Code:
    but1    var PORTB.4
    but2    var PORTB.5
    ledwork var PORTA.0
    led1    var PORTA.1
    led2    var PORTA.2
    led3    var PORTA.3
    rs      var PORTA.5
    And before the loop starts I added:

    porta=0
    portb=0


    Now I can see the led1/led2 trigger after pressing for about a second, but the generic interrupt led3 is ON all the time which indicates me that the interrupt is being triggered constantly, regardless of the button presses.

    Any idea?


    Thanks


    Pablo

  4. #4
    Join Date
    Feb 2005
    Posts
    130


    Did you find this post helpful? Yes | No

    Default

    Im still puzzled by this, I know I must be near, but Im missing the finish line by a byte or two



    Thanks, help is appreciated

  5. #5
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default A quick look at your prog.

    Hi,

    You are turning on the LED3 in the interrupt routine and turning it off in the main loop. So technically it should work. Now consider this :

    Your buttons are not debounced so it may quite happen that another rising edge is setting up your interrupt. Since you have the buttons pressed while in interrupt you are being able to detect it and light led1 and led2.

    So introduce a pause inside your interrupt after turning on the LEDs and as soon as you inside the interrupt release the button. Then while getting out clear the interrupt flags. This might solve your problem.

    Regards

    Sougata

  6. #6
    Join Date
    Feb 2005
    Posts
    130


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sougata
    Hi,

    You are turning on the LED3 in the interrupt routine and turning it off in the main loop. So technically it should work. Now consider this :

    Your buttons are not debounced so it may quite happen that another rising edge is setting up your interrupt. Since you have the buttons pressed while in interrupt you are being able to detect it and light led1 and led2.

    So introduce a pause inside your interrupt after turning on the LEDs and as soon as you inside the interrupt release the button. Then while getting out clear the interrupt flags. This might solve your problem.

    Regards

    Sougata
    Hi Sougata,

    If I enter any pause in the interrupt routine (I just tried it) I severelly impair the PWM loops, and this is another indication that the interrupt is being triggered constantly.

    Here is the schematic:




    Thanks


    Pablo

Similar Threads

  1. Interrupt question
    By ultiblade in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 26th November 2009, 20:12
  2. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 02:35
  3. Help with Analog Interrupt
    By brid0030 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th February 2008, 18:14
  4. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07
  5. Interrupt question
    By stone in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th January 2005, 23:11

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