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, okay had another read at first it was just going over my head. Am I right in saying I need to read bit 6 of CMCON0? So CMCON0.6?

    Thanks,
    Rob

  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 !!

    Exactly!

    CMCON0 Register:
    bit 6 COUT: Comparator Output bit.
    This is the logic bit output of the Comparator.

    CM: Comparator Mode = 100
    Name:  Picture1.png
Views: 742
Size:  33.8 KB

    With this setup the comparator is comparing VIN- (CIN-/GP1) to VIN+ (which is not an external pin but it is "From CVREF Module", internal voltage reference)
    So you need to look at the description of COUT in the CMCON0 register listed below AND you need to look at Table 8-1 in the DS.
    This will help you work out what value CMCON0.6 will be based upon a given input on GP1.
    Take note that CINV is a configuration bit within the CMCON0 register that determines the logic polarity of COUT.

    COUT: Possible values
    When CINV = 0:
    1 = VIN+ > VIN-
    0 = VIN+ < VIN

    When CINV = 1:
    1 = VIN+ < VIN-
    0 = VIN+ > VIN
    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 !!

    Brilliant! Thanks for the info again your the best! I should be able to get this working how I want it to now, then & only then will I move onto interupt with it.

    Thanks again,
    Rob

  4. #4
    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

  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 !!

    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

  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, 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

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

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