Beginner in need of help !!


Closed Thread
Results 1 to 40 of 72

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi,
    No debounce may very well be part of the problem but in THIS case I think the contact bounce is what actually makes it sort of "work".
    The real issue, I believe, is that your "loops" looks for ModeFlag being equal to 1 and switches mode. But the flag is not being cleared. So, if you push the button once, the ISR sets the flag. If there are no bounce the flag will be set all the time and the program will keep cycling between the different modes since the flag is never cleared. That's a problem with the implementation.

    What's more severe is that you're using GOSUB without a matching RETURN. What you want in this case is GOTO.

    Make the ISR set (and only set) the ModeFlag, implement some debounce in the ISR.
    In the main loop you check the ModeFlag and IF it's set you first clear it and then act accordingly (ie. jump to the next mode).

    Also, there's no need to set the LED high or low 1000 times (ie do it inside the FOR_NEXT loop), one time is enough, I'd rewrite the loop like:
    Code:
    DO
        HIGH LED
        for loop1 = 1 to 1000
            pause 1    '1 ms
            IF ModeFlag = 1 THEN
              ModeFlag = 0
              GOTO Mode 2
            ENDIF    
         next loop1
        
        LOW LED 'led off
        for loop1 = 1 to 1000
            pause 1    '1 ms
            IF ModeFlag = 1 THEN
              ModeFlag = 0
              GOTO Mode 2
            ENDIF
        next loop1
    LOOP
    /Henrik.

  2. #2
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi Henrik, thanks for helping. I have changed to the code to do as you said: goto's instead of gosub's & made the ISR set flag only (i think).

    It all seems to work better now, however I still have a problem with the debounce as I think it is actually 'the problem' now instead of actually making the code work as you stated. The isr sees the release of the button as an interrupt & sets a flag again which skips modes. So how do I go about adding the debounce without slowing things down?

    Isnt there a way to only interrupt or tell the ISR to flag only if the button is depressed & not when released? Here is the updated code -

    Code:
    'PIC 12F683
    
    #CONFIG 
       __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _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
    
    loop1 var word  ' Loop 1 Counter (word for 0 to 65535 increments)
    ModeFlag var bit   ' Bit to determine what state of IOC.3 is
    
    '*****Initlaze Vars*****
       loop1 = 0
    ModeFlag = 0
    '***********************
    
    ON INTERRUPT GOTO modeselect 'interrupt handler is modeselect
    INTCON = %10001000 'enable GIE and GPIE; clear GPIF
    IOC = %00001000 ' enable IOC3 (GPIO3 Interrupt on change)
    
    ENABLE 
    mode1:
    do
        for loop1 = 1 to 1000
            HIGH LED 'led on
            'PAUSE 1000 'delay 1 second
            pause 1    '1 ms
            if ModeFlag = 1 then
            ModeFlag = 0
            goTO mode2
        ENDIF
        
        next loop1
        
        for loop1 = 1 to 1000
            LOW LED 'led off
            'PAUSE 1000 'delay 1 second
            pause 1    '1 ms
            if ModeFlag = 1 then
            Modeflag = 0
            goTO mode2
        endif
        
        next loop1
    
    'GOTO mode1 'repeat
    loop
    
    ENABLE 
    mode2:
    do
        for loop1 = 1 to 500
            HIGH LED 'led on
            'PAUSE 500 'delay 0.5 second
            pause 1    '1 ms
            if ModeFlag = 1 then
            modeflag = 0
            goTO mode3
        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
            modeflag = 0
            goTO mode3
        endif
        
        next loop1
    
    'GOTO mode2 'repeat
    loop
    
    ENABLE
    mode3:
    DO
        for loop1 = 1 to 50
            HIGH LED 'led on
            'PAUSE 50 'delay 0.05 second
            pause 1    '1 ms
            if ModeFlag = 1 then
            modeflag = 0
            goTO mode1
        endif
        
        next loop1
        
        for loop1 = 1 to 50
            LOW LED 'led off
            'PAUSE 50 'delay 0.05 second
            pause 1    '1 ms
            if ModeFlag = 1 then
            modeflag = 0
            goTO mode1
        endif
        
        next loop1
    
    'GOTO mode3 'repeat
    loop
        
    DISABLE 'disable interrupts in handler
    modeselect:
    if INTCON.0 = 1 then    'one of the GPIO<5:0> pins changed state (must be cleared in software)    
        'next mode
    
        'Set the ModeFlag
        ModeFlag = 1
        ENDIF
        
    INTCON = %10001000  'enable GIE and GPIE; clear GPIF
    RESUME 'return to where left off
    ENABLE 'enable interrupts
    
    end 'got here by mistake

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi,
    You're quite right, it's because you're using the IOC (Interrupt On Change), it'll trip the interrupt both when you push the button and when you release the button. (As well as on all the edges of the contact bounce). Also, when you're using IOC you need to remember to read the port (even if you're not using the value) in order to clear the mismatch condition.

    I'd wire the button to GP2 and use the "normal" INT instead of IOC.

    /Henrik.

  4. #4
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    So if I want to use IOC, I would need to read the button or any other type of input such as the adc?

    I'm only asking as I need to be able to use interrupt on like 5 inputs, not just 1. Although gp2 will use adc not any others if that would be easier.

    Thanks,
    Rob

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi Rob,
    The IOC trips the interrupt on any change of the state on a digital pin. An interrupt will trip when the pin goes low and again when the pin goes high - provided you read the state of the pin in between. So in the ISR you need to read the state of switch and determine if it was pushed or released (and don't forget about the contact bounce). If it's determined that the interrupt tripped because the button was released you don't set the MODEFLAG.

    And, once again, the ADC can NOT generate an interrupt when the analog voltage on the input changes. It won't work with IOC. Any pin configured as analog input will always read '1' and will never trip the IOC or INT interrupts.
    Depending on what you want to do you might be able to use the comparator, they can be setup to trip an interrupt when their respective output changes state.

    /Henrik.

  6. #6
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi Henrik, thanks for explaining that it makes things a lot clearer as to how I'm going to run the code. What I would like to do is have one of the inputs (gp2), taking a reading from ADC every say 0.5 - 1 seconds.

    Basically I want the chip to enter low power mode along with the device. When the device is off, one of the component pins goes to 0.00v, then when it is turned on it goes to 0.70v & stays at that. So I would like the ADC to monitor this, then when the voltage drops below 0.60v the chip is in low power mode, then when the voltage is higher than 0.60v I want it to come out of low power mode.

    I hope this can be done in some way,
    Rob
    Last edited by robbo78; - 22nd January 2015 at 20:23.

  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,

    I don't think the ADC module will accomplish what you are attempting as Henrik stated in his last reply.
    You should look into the Comparator of the PIC, again as Henrik suggested.
    Regards,
    TABSoft

  8. #8
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi guys, okay I've been having a mess today & I am quite happy with myself. I managed to use the comparator successfully, now I have added a button into the routine.

    It is all doing what I want it to but I am having this big issue with de-bouncing how do I sort this out, what is the best way? Do I need to read the button in a different way?

    Here is the code -

    Code:
    'PIC 12F683
    
    #CONFIG 
       __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    
    DEFINE OSC 4 '4mhz ocsillator
    ANSEL = %00000001 'pin 7 analog
    CMCON0 = %00011110 'comparator mode
    VRCON = %10100000 'voltage reference
    TRISIO = %00101111 'pin 2,4,5,6,7 input
    
    PAUSE 50 'wait for hardware to settle
    
    ON INTERRUPT GOTO int1 'interrupt handler is int1
    INTCON = %11001000 'enable GIE and GPIE; clear GPIF
    PIE1 = %00001000 'enable comparator interrupt
    IOC = %00001000 'pin 3 ioc
    
    RTPOT VAR CMCON0.6 'right trigger pot
    RTBITTEST var bit 'trigger debug test bit. Set/Clear in ISR and Check in main
    
    BUT VAR GPIO.3 'tactile button
    BUTBITTEST VAR BIT 'button test bit
    
    
    RTBITTEST = RTPOT 'initial reading
    
    BUTBITTEST = 0 'read 0 to start
    
    
    ENABLE
    main:
    
    IF RTBITTEST = 1 THEN
    GOTO RAPID
    ENDIF
    
    IF RTBITTEST = 0 THEN
    GOTO SLEEPY
    ENDIF
    
    GOTO main
    
    
    ENABLE
    RAPID:
    
    LOW GPIO.0 
    PAUSE 70
    TRISIO.0 = 1 
    PAUSE 70
    
    IF RTBITTEST = 0 THEN
    GOTO SLEEPY
    ENDIF
    
    IF BUTBITTEST = 1 THEN
    GOTO FLASH
    ENDIF
    
    GOTO RAPID
    
    
    ENABLE
    SLEEPY:
    
    HIGH GPIO.5
    PAUSE 1000
    TRISIO.5 = 1
    PAUSE 1000
    
    IF RTBITTEST = 1 THEN
    GOTO main
    ENDIF
    
    GOTO SLEEPY
    
    
    ENABLE
    FLASH:
    
    HIGH GPIO.5
    PAUSE 300
    TRISIO.5 = 1
    PAUSE 300
    
    IF BUTBITTEST = 1 THEN
    GOTO RAPID
    ENDIF
    
    GOTO FLASH
    
      
        
    DISABLE 'disable interrupts in handler
    int1:
        
    if PIR1.3 = 1 then  'Comparator Interrupt fired (must be cleared before resume)
    RTBITTEST = RTPOT
    ENDIF
    
    IF IOC.3 = 1 THEN
    BUTBITTEST = BUT
    ENDIF 
    
    INTCON =%11001000 
    PIR1 = %00000000 'reset CMIF
    IOC = %00000000 'reset IOC.3
    RESUME 'resume to where left off
    ENABLE 'enable interrupts
    Last edited by robbo78; - 31st January 2015 at 18:41.

  9. #9
    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,
    You really don't state what the problem is, only that you see a problem.

    A quick scan of the code leads to too many questions for me.
    What are you trying to accomplish in the different subroutines Rapid, Sleepy and Flash?
    You seem to be using two GPIO pins, one for Rapid (GPIO.0) and another (GPIO.5) for Sleepy and Flash.
    Another question is why in each of the subroutines are you setting the GPIO high, pausing and then configuring the same GPIO as an input?

    Without more info, it is futile to offer an opinion.
    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 : 1

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