Beginner in need of help !!


Closed Thread
Results 1 to 40 of 72

Hybrid View

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

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


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    I will drop the pause below the osc value, thanks for that tip
    Like Tabsoft wrote, you generally want your DEFINEs at the top of the program but it's for readabillity. In reallity it makes no difference because they are not runtime commands.

    Another little note
    Code:
            for loop1 = 1 to 1000
                HIGH LED
                PAUSE 1
            NEXT LOOP1
    What's the purpose of setting the LED high inside the loop?
    There's nothing inherently wrong in doing it but it does take (a little) bit of time.

    Also, now that you're starting to get the hang of it you might want to consider ditching the HIGH/LOW commands and simply set the pin directly, LED=1, LED=0. For this to work you need to clear the corresponding TRIS-bit first (TRISO.2 = 0 ' Set GPIO.2 to output). Doing it this way executes faster and consumes less codespace since it doesn't waste resources clearing the TRIS bit each and every time (which is what HIGH/LOW does for you).

    Again, you're not doing anything "wrong" - it all works fine, I'm just pointing out a couple of things you could improve and take with you to the next project.

    /Henrik.

  3. #3
    Join Date
    Jan 2015
    Posts
    45


    Did you find this post helpful? Yes | No

    Default Re: Beginner in need of help !!

    Thanks guys, I will have a play

    Rob

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