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

    Default Re: Beginner in need of help !!

    Hi Tabsoft, I got the comparator working perfectly so I decided to move onto adding an interrupt, which believe it or not hasn't worked. Here is the code I have fully written, maybe you can help where I have gone wrong?

    Thanks,
    Rob

    You will probably have a laugh at this, as its probably completely wrong lol


    Code:
    'PIC 12F683
    
    #CONFIG 
       __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    PAUSE 20 'wait for hardware to settle
    
    DEFINE OSC 4 '4mhz ocsillator
    ANSEL = %00000010 'pin 6 analog
    CMCON0 = %00000100 'comparator mode
    VRCON = %10101000 'voltage reference
    TRISIO = %00000010 'pin 6 input
    
    ON INTERRUPT GOTO SLSELECT 'interrupt handler is slselect
    INTCON = %11001000 'enable GIE and GPIE; clear GPIF
    IOC = %00000010 ' enable IOC1 (GPIO.1 Interrupt on change)
    PIE1 = %00001000 'enable comparator interrupt
    
    POT1 VAR CMCON0.6 'read potentiometer
    LED VAR GPIO.2 'led pin 5
    
    ENABLE
    main:
    HIGH LED 'light led
    GOTO main 'repeat
    
    DISABLE 'disable interrupts in handler
    SLSELECT:
    IF POT1 = 1 THEN 'if <1.1v sleep
    NAP 1
    ELSE
    ENDIF
    
    INTCON = %10001000  'enable GIE and GPIE; clear GPIF
    PIR1 = %00000000 'reset CMIF
    RESUME 'where left off
    ENABLE 'enable interrupts

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


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Glad to hear if Rob.

    You did not say what is happening only that it is not working.
    I took a look at your code and several things jump out at me.

    1. You are jumping into the deep end of the pool with testing "nap"
    If would take a smaller step first. Set a flag in the ISR and resume. Then in the main act on the flag you set in the ISR.
    This way you can see the effects of the ISR logic.
    2. You have IOC.1 enabled, which is GP1, which you are using for the comparator. You need to comment out IOC.1.
    3. You are not testing for which Interrupt is firing inside of your ISR. It is almost always a good practice to determine what interrupt caused the jump to the ISR.
    4. At the end of your ISR you are not setting the INTCON register they way you think. I believe you have a typo in the value you are assigning to it.


    I changed your code to the following which shows the changes.

    Code:
    'PIC 12F683
    
    #CONFIG 
       __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    PAUSE 20 'wait for hardware to settle
    
    DEFINE OSC 4 '4mhz ocsillator
    ANSEL = %00000010 'pin 6 analog
    CMCON0 = %00000100 'comparator mode
    VRCON = %10101000 'voltage reference
    TRISIO = %00000010 'pin 6 input
    
    ON INTERRUPT GOTO SLSELECT 'interrupt handler is slselect
    INTCON = %11001000 'enable GIE and GPIE; clear GPIF
    'IOC = %00000010 ' enable IOC1 (GPIO.1 Interrupt on change)
    PIE1 = %00001000 'enable comparator interrupt
    
    POT1 VAR CMCON0.6 'read potentiometer
    LED VAR GPIO.2 'led pin 5
    
    bitTest var bit 'Debug test bit.  Set/Clear in ISR and Check in maiin
    
    bitTest = 0
    
    ENABLE
    main:
        
        'HIGH LED 'light led
        LED = bitTest
        
    GOTO main 'repeat
    
    DISABLE 'disable interrupts in handler
    SLSELECT:
        'IF POT1 = 1 THEN 'if <1.1v sleep
            'NAP 1
        'ELSE
        'ENDIF
        if PIR1.3 = 1 then  'Comparator Interrupt fired (must be cleared before resume)
            bitTest = POT1
        endif
    
        'INTCON = %10001000  'enable GIE and GPIE; clear GPIF
        INTCON =%11001000 
        PIR1 = %00000000 'reset CMIF
        RESUME 'where left off
        ENABLE 'enable interrupts
    See if this gets you along further.
    Regards,
    TABSoft

  3. #3
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi Tabsoft, okay thanks for all that. I will go back to flagging. I think I was
    Getting a bit ahead of myself lol.

    Let you know how it goes

  4. #4
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi, okay I've been playing about with comparator interrupt now & think I have got the hang of it using flags. The code below is something little I came up with

    Now I think I would like to implement the nap command now, or do you think I should learn something else before jumping to the nap command?

    Thanks again,
    Rob

    Code:
    'PIC 12F683
    
    #CONFIG 
       __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    PAUSE 50 'wait for hardware to settle
    
    DEFINE OSC 4 '4mhz ocsillator
    ANSEL = %00000010 'pin 6 analog
    CMCON0 = %00000100 'comparator mode
    VRCON = %10101000 'voltage reference
    TRISIO = %00000010 'pin 6 input
    
    ON INTERRUPT GOTO int1 'interrupt handler is int1
    INTCON = %11001000 'enable GIE and GPIE; clear GPIF
    PIE1 = %00001000 'enable comparator interrupt
    
    POT1 VAR CMCON0.6 'read potentiometer
    LED VAR GPIO.2 'led pin 5
    loop1 var word 'loop1 counter
    
    bitTest var bit 'Debug test bit.  Set/Clear in ISR and Check in main
    
    bitTest = 0
    Loop1 = 0
    
    ENABLE
    main:
    
    IF bittest = 1 THEN 
    GOTO FLASH
    ENDIF
        
    LOW LED
    
    GOTO main
    
    
    ENABLE
    FLASH:
    
    
    
    do
    for loop1 = 1 to 1000
    HIGH LED
    PAUSE 1
    
    NEXT LOOP1
    
    for loop1 = 1 to 1000
    LOW LED
    PAUSE 1
    
    NEXT LOOP1
    
    IF bittest = 0 THEN
    GOTO main
    ENDIF
    
    LOOP
      
        
    DISABLE 'disable interrupts in handler
    int1:
        
    if PIR1.3 = 1 then  'Comparator Interrupt fired (must be cleared before resume)
    BITtest = pot1
    ENDIF 
    
    INTCON =%11001000 
    PIR1 = %00000000 'reset CMIF
    RESUME 'where left off
    ENABLE 'enable interrupts

  5. #5
    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,

    Looks like you got it working.

    One little thing though.
    The Pause command is based upon the "DEFINE OSC" value to calculate the pause interval correctly.
    It is a good practice to have your "DEFINE OSC" statement up top in the source code before you use any time sensitive commands such as "Pause x".

    The way you have your program setup the following occurs, which I presume you have seen in practice on your physical system.
    The LED on GP2(pin5) will stay LOW (OFF) while the voltage input on GP1(pin6) is above your reference voltage.
    The LED GP2(pin5) will blink in 1sec intervals while the voltage input on GP1(pin6) is below your reference voltage.

    This works fine on my simulator.

    Good job!

    It looks like you're ready for a "nap" now.
    Regards,
    TABSoft

  6. #6
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Hi Tabsoft, thanks for comfirming also thanks again for all your help, I wouldnt have got to this point if it wasnt for you. I havent got a simulator so I just test straight on the BB.

    I will drop the pause below the osc value, thanks for that tip

    Do I implement the nap command in the same way as flashing an LED? So if bittest = 1 : nap 1, if bittest = 0 : goto main? Something like that ?

    Thanks again,
    Rob

  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,

    Why don't you check this out.

    Code:
    'PIC 12F683
    
    #CONFIG 
       __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    
    DEFINE OSC 4 '4mhz ocsillator
    ANSEL = %00000010 'pin 6 analog
    CMCON0 = %00000100 'comparator mode
    VRCON = %10101000 'voltage reference
    TRISIO = %00000010 'pin 6 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
    
    POT1 VAR CMCON0.6 'read potentiometer
    LED VAR GPIO.2 'led pin 5
    loop1 var word 'loop1 counter
    
    bitTest var bit 'Debug test bit.  Set/Clear in ISR and Check in main
    
    
    high LED    'Start with LED ON
    pause 500   'Pause .5 secs so you can see the LED turn on
    
    bitTest = POT1  'Take initial reading of input voltage state
    Loop1 = 0
    
    ENABLE
    main:
    
        IF bitTest = 1 THEN 'input voltage is < reference voltage  
            low LED
            nap 1
        else    'bitTest = 0 - input voltage is > reference voltage
            GOTO FLASH
        ENDIF
            
        GOTO main
    
    
    ENABLE
    FLASH:
        do
            for loop1 = 1 to 1000
                HIGH LED
                PAUSE 1
            NEXT LOOP1
            
            for loop1 = 1 to 1000
                LOW LED
                PAUSE 1
            NEXT LOOP1
            
            IF bitTest = 1 THEN
                GOTO main
            ENDIF
        
        LOOP
      
        
    DISABLE 'disable interrupts in handler
    int1:
        
    if PIR1.3 = 1 then  'Comparator Interrupt fired (must be cleared before resume)
    bitTest = pot1
    ENDIF 
    
    INTCON =%11001000 
    PIR1 = %00000000 'reset CMIF
    RESUME 'where left off
    ENABLE 'enable interrupts
    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