Beginner in need of help !!


Closed Thread
Results 1 to 40 of 72

Hybrid View

  1. #1
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Unhappy Re: Beginner in need of help !!

    Ive decided to go the interrupt route... I am stuck already. Here is my simple code, why isnt my interrupt working?

    Code:
    #CONFIG 
       __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    DEFINE OCS 4 '4mhz ocsillator
    ANSEL = 0 'all I/O digital
    CMCON0 = 7 'comparator off
    LED VAR GPIO.0 'LED pin 7
    LED2 VAR GPIO.1 'LED2 pin 6
    
    ON INTERRUPT GOTO mode1 'interrupt handler is mode1
    INTCON = %10001000 'enable gp3 interrupt
    
    main:
    ENABLE
    HIGH LED 'led on
    PAUSE 500 'delay 0.5 second
    LOW LED 'led off
    PAUSE 500 'delay 0.5 second
    GOTO main 'repeat
    
    DISABLE 'disable interrupts in handler
    mode1:
    HIGH LED2 'led2 on
    PAUSE 50
    LOW led2 'led2 off
    PAUSE 50
    RESUME 'return to where left off
    ENABLE 'enable interrupts

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Normally in the interrupt routine this is needed.
    INTCON.1 = 0
    But the data sheet for this chip reads like this is needed.
    INTCON.0 = 0

    Then at the beginning of your code below
    INTCON = %10001000
    add
    IOC.3=1

    I think...
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Quote Originally Posted by mackrackit View Post
    Normally in the interrupt routine this is needed.
    INTCON.1 = 0
    But the data sheet for this chip reads like this is needed.
    INTCON.0 = 0

    Then at the beginning of your code below
    INTCON = %10001000
    add
    IOC.3=1

    I think...
    Brilliant, that got it working. I take it IOC.3 = 1 tells the chip when to interrupt & on what pin?

    So INTCON = %10001000 tells it to enable interrupts on pin 3 & IOC.3 = 1 tells it to interrupt on pin 3 change ?

    Thanks for your help,
    Rob

  4. #4
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    This should work for you. It works in my simulator.

    Also, since PBP's ON INTERRUPT only checks to see if an interrupt has occurred between PBP commands, you may want to use a loop counter in your main routine and use short pauses. This is how I revised your code.

    Comments are added to the code below.

    Good Luck.

    Code:
    'PIC 12F683
    #CONFIG 
       __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    'DEFINE OCS 4 '4mhz ocsillator
    DEFINE OSC 4 '4mhz ocsillator
    ANSEL = 0 'all I/O digital
    CMCON0 = 7 'comparator off
    LED VAR GPIO.0 'LED pin 7
    LED2 VAR GPIO.1 'LED2 pin 6
    
    loop1 var byte  ' Loop 1 Counter
    
    ON INTERRUPT GOTO mode1 'interrupt handler is mode1
    INTCON = %10001000 'enable GIE and GPIE; clear GPIF
    IOC = %00001000 ' enable IOC3 (GPIO3 Interrupt on change)
    
    ENABLE 
    main:
    do
        for loop1 = 1 to 50
            HIGH LED 'led on
            'PAUSE 500 'delay 0.5 second
            pause 10    '10 ms
        next loop1
        
        for loop1 = 1 to 50
            LOW LED 'led off
            'PAUSE 500 'delay 0.5 second
            pause 10    '10 ms
        next loop1
    
    'GOTO main 'repeat
    loop
    
    DISABLE 'disable interrupts in handler
    mode1:
    if INTCON.0 = 1 then    'one of the GPIO<5:0> pins changed state (must be cleared in software)
    
        HIGH LED2 'led2 on
        PAUSE 50
        LOW led2 'led2 off
        PAUSE 50
    
    endif
    INTCON = %10001000  'enable GIE and GPIE; clear GPIF
    
    RESUME 'return to where left off
    ENABLE 'enable interrupts
    
    end 'got here by mistake
    Regards,
    TABSoft

  5. #5
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Quote Originally Posted by Tabsoft View Post
    This should work for you. It works in my simulator.

    Also, since PBP's ON INTERRUPT only checks to see if an interrupt has occurred between PBP commands, you may want to use a loop counter in your main routine and use short pauses. This is how I revised your code.

    Comments are added to the code below.

    Good Luck.

    Code:
    'PIC 12F683
    #CONFIG 
       __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    'DEFINE OCS 4 '4mhz ocsillator
    DEFINE OSC 4 '4mhz ocsillator
    ANSEL = 0 'all I/O digital
    CMCON0 = 7 'comparator off
    LED VAR GPIO.0 'LED pin 7
    LED2 VAR GPIO.1 'LED2 pin 6
    
    loop1 var byte  ' Loop 1 Counter
    
    ON INTERRUPT GOTO mode1 'interrupt handler is mode1
    INTCON = %10001000 'enable GIE and GPIE; clear GPIF
    IOC = %00001000 ' enable IOC3 (GPIO3 Interrupt on change)
    
    ENABLE 
    main:
    do
        for loop1 = 1 to 50
            HIGH LED 'led on
            'PAUSE 500 'delay 0.5 second
            pause 10    '10 ms
        next loop1
        
        for loop1 = 1 to 50
            LOW LED 'led off
            'PAUSE 500 'delay 0.5 second
            pause 10    '10 ms
        next loop1
    
    'GOTO main 'repeat
    loop
    
    DISABLE 'disable interrupts in handler
    mode1:
    if INTCON.0 = 1 then    'one of the GPIO<5:0> pins changed state (must be cleared in software)
    
        HIGH LED2 'led2 on
        PAUSE 50
        LOW led2 'led2 off
        PAUSE 50
    
    endif
    INTCON = %10001000  'enable GIE and GPIE; clear GPIF
    
    RESUME 'return to where left off
    ENABLE 'enable interrupts
    
    end 'got here by mistake
    Hi Tabsoft, that worked perfectly. I can see what you mean by using loops instead of pauses, the interrupt is much more reactive.

    Now I just need to know how to stay in that interrupt routine until I tap the button again.

    Thanks,
    Rob

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi Rob,
    Now I just need to know how to stay in that interrupt routine until I tap the button again.
    Even though that would work for this particular application that's generally not what you want to do (ie stay in the interrupt routine). Instead you do as I showed earlier, use a flag/semaphore in the interrupt service routine which the main code reads and determines what to do.

    The same thing applies to your scenario with ten states. You have a variable which you increment in the interrupt (ie once very time you press the button), once it gets to 10 you reset it to zero. Then, in the main program loop you simply check the value of this variable and act accordingly. With a bit of thought and some math you might even USE the variables value directly as the delay in your loop to blink the LED.

    As for the analog input it's important to understand that there is no mechanism to automatically run the ADC and detect (ie interrupt) when the voltage at the input changes. You need to write code to sample the input once every minute, second, millisecond or whatever the requirement may be.

    Finally, please don't quote the full message just posted. There's really no need to have the very latest post quoted in full.

    /Henrik.

  7. #7
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Rob,

    Glad it worked for you.

    I'm not sure if you want/need to stay in the ISR as I don't know your final objective.

    I would suggest using a flag to determine the state of the IOC.3 interrupt coming from your button.
    Then in the ISR change the state of the flag each time the interrupt occurs and then get out of the ISR.
    You can then setup a subroutine for what you wanted to accomplish in the ISR.
    Finally in your main loop check the state of the flag and gosub to the new subroutine.

    Here is a working example. Expanded on what I posted last time.

    Code:
    'PIC 12F683
    #CONFIG 
       __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    'DEFINE OCS 4 '4mhz ocsillator
    DEFINE OSC 4 '4mhz ocsillator
    ANSEL = 0 'all I/O digital
    CMCON0 = 7 'comparator off
    LED VAR GPIO.0 'LED pin 7
    LED2 VAR GPIO.1 'LED2 pin 6
    
    loop1 var word  ' Loop 1 Counter (word for 0 to 65535 increments)
    loop2 var byte  ' Loop 2 Counter (byte used for 0 to 255 increments)
    ModeFlag var bit   ' Bit to determine what state of IOC.3 is
    
    '*****Initlaze Vars*****
       loop1 = 0
       loop2 = 0
    ModeFlag = 0
    '***********************
    
    ON INTERRUPT GOTO mode1 'interrupt handler is mode1
    INTCON = %10001000 'enable GIE and GPIE; clear GPIF
    IOC = %00001000 ' enable IOC3 (GPIO3 Interrupt on change)
    
    ENABLE 
    main:
    do
        for loop1 = 1 to 500
            HIGH LED 'led on
            'PAUSE 500 'delay 0.5 second
            pause 1    '1 ms
            if ModeFlag = 1 then
            gosub moderoutine
        endif
        
        next loop1
        
        for loop1 = 1 to 500
            LOW LED 'led off
            'PAUSE 500 'delay 0.5 second
            pause 1    '1 ms
            if ModeFlag = 1 then
            gosub moderoutine
        endif
        
        next loop1
    
    'GOTO main 'repeat
    loop
    
    moderoutine:
        do while ModeFlag = 1
            high LED2   'LED2 On
            for loop2 = 0 to 50
                pause 1 '1ms
            next loop2
            
            low LED2    'LED2 Off
            for loop2 = 0 to 50
                pause 1 '1ms
            next loop2 
        loop    
    
        return
    
    DISABLE 'disable interrupts in handler
    mode1:
    if INTCON.0 = 1 then    'one of the GPIO<5:0> pins changed state (must be cleared in software)
    
        'HIGH LED2 'led2 on
        'PAUSE 50
        'LOW led2 'led2 off
        'PAUSE 50
    
        'Set the ModeFlag
        ModeFlag = ~ ModeFlag
    endif
    INTCON = %10001000  'enable GIE and GPIE; clear GPIF
    
    RESUME 'return to where left off
    ENABLE 'enable interrupts
    
    end 'got here by mistake
    Regards,
    TABSoft

Similar Threads

  1. Beginner help!
    By Recognize in forum General
    Replies: 14
    Last Post: - 26th April 2012, 14:55
  2. pic24 beginner
    By robertpeach in forum General
    Replies: 23
    Last Post: - 13th August 2009, 11:57
  3. need help! to beginner
    By emilhs in forum mel PIC BASIC Pro
    Replies: 27
    Last Post: - 6th May 2009, 18:44
  4. Beginner at PB
    By alphahr in forum Off Topic
    Replies: 1
    Last Post: - 21st April 2008, 17:43
  5. Can anyone help a beginner in a struggle?
    By douglasjam in forum mel PIC BASIC
    Replies: 1
    Last Post: - 5th May 2005, 23:29

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