16f84a running interrupt PICBASIC code


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Jul 2008
    Posts
    7

    Default 16f84a running interrupt PICBASIC code

    Please see the attached code.

    It has two parts, one to vary the content of a variable using a single button and second an interrupt run to output RB0 based on the variable content.

    When it is simed on MPLAB, it is quite OK. But when it is run on the chip, it does not work.
    (with WDT switched off)
    However the two parts when compiled as separate files to check the code, they function well on the chip.

    Please help me solve the prob

    Thanks
    Attached Files Attached Files
    Last edited by divaker; - 22nd July 2008 at 15:10. Reason: let me attch the code in text

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    You post your code in PDF format, and the only way anyone can test it is to retype it all over, no thanks.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Joe S. View Post
    You post your code in PDF format, and the only way anyone can test it is to retype it all over, no thanks.
    I post the code from PDF.
    Code:
    
        '16f84a
    
        Symbol OUT_PIN = PORTB.0            ' Output to solenoid RB0
        Symbol LED = PORTB.1                ' Control button to RB1
        Symbol CNTL_BUTTON = PORTB.7        ' Control button to RB7
        
        spacing var byte
        temp var byte
        
        TRISA = $FF
        TRISB = $FC                         ' RB0 and RB1 outputs, others input
        spacing = 15
        LED = 0
        temp = 1
        
        gosub wait_button
        
        LED = 1
        pause 2000                          ' Wait for two seconds
        if CNTL_BUTTON = 1 then spacing = 30: goto direct   ' If button is released before 2 secs
                                                            ' code goes directly into the default spacing
        temp = 0
        gosub wait_button                   ' If button is pressed more than 2 secs,
                                            ' it gets caught here and waits for release
        LED = 0
        pause 1000                          ' Wait for 100 ms
        
    repeata:
        spacing = spacing + 5               ' Increase the spacing count by 2 every time
        toggle LED
        pause 1000
        toggle LED
        if spacing = 45 then spacing = 20   ' Till it reaches maximum and reset to minimum
        temp = 1
        gosub wait_button                   ' Access the push button state and repeat if short press
        pause 2000                          ' Delay for 2 seconds
        if CNTL_BUTTON = 1 then goto repeata
        LED = 1
        OUT_PIN = 0
    'Calculate the value to be loaded into TMR0 for counting at RA4 connected to the encoder output
    'Use a multiplier to convert the spacing in cm to number of pulses to count
    'Say if the distance measuring wheel's dia is 40 cm then its circumference = 3.14*40 appr = 125 cm
    'For one revolution ie 125 cm distance traveled = 500 pulse outputs of encoder
    'Correspondingly 500/125 = 4 pulses/cm distance, therefore set the count as 255 - (spacing * 4)
    direct:
        spacing = 255 - (spacing * 4)
        OPTION_REG = %00110000              ' TMR0 counter on falling edge of RA4
        'Cnt = 0                            ' Clear the event counter, Cnt
        ON INTERRUPT GOTO ISR               ' Interrupt service routine
        TMR0 = spacing
        OUT_PIN = 0
        INTCON = %10100000                  ' Enable TMR0 overflow interrupt
        
    NXT:
        if OUT_PIN = 1 then
            pause 300
            OUT_PIN =0
        endif
        GOTO NXT                            ' Continue
        
    'This is the interrupt service routine, ISR. The program jumps here whenever TMR0 overflow occurs
        DISABLE                             ' Disable interrupts
    ISR:                                    ' Entry point of the ISR
        OUT_PIN = 1
        TMR0 = spacing
        INTCON = %10100000                  ' Enable TMR0 overflow interrupt
        RESUME                              ' Resume main program
        ENABLE                              ' Enable interrupts
        
    wait_button:
        if CNTL_BUTTON && 1 = temp then wait_button
        return
        end

  4. #4
    Join Date
    Jul 2008
    Posts
    7


    Did you find this post helpful? Yes | No

    Default 16f84a running interrupt PICBASIC code

    Quote Originally Posted by Joe S. View Post
    You post your code in PDF format, and the only way anyone can test it is to retype it all over, no thanks.
    Sorry that I had attached it in pdf
    Now I have attached in text.
    Please pardon me.

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by divaker View Post
    When it is simed on MPLAB, it is quite OK. But when it is run on the chip, it does not work.
    And you're sure the PIC is running in the first place?
    How does the 'blinky LED' program run?

  6. #6
    Join Date
    Jul 2008
    Posts
    7


    Did you find this post helpful? Yes | No

    Default Reply - 16f84a running interrupt PICBASIC code 16f84a

    Dear skimask,
    Yes, the "blinky and the button" worked as required right on the PIC16f84.
    Similarly the interrupt driven routine also worked well separately on the PIC
    But when these two are put together on the PIC it odes not work.

    Though the MPLAB simulates it well enough and comes thru'.


    Divaker

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Looks to me like you have to do some big time reorganization of your program...You've got it jumping all over everything...
    Here's how I normally do things...might not be optimal, but it keeps me out of trouble:
    Defines
    Variable declarations
    ASsembly macros
    Skip over Int Handler/Subroutines
    On Interrupt statement
    Int Handler routine
    Subroutines
    PIC, PIC pins and other hardware Setup/Initialization
    Main body

    And besides all that, I don't see where you clear the TMR0 overflow flag like it says you have to do in the datasheet. I see you enable the TMR0 interrupt a couple of times which has the side effect of clearing the flag...but probably not at the right time.

  8. #8
    Join Date
    Jul 2008
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Dear skimask,
    Thanks for your time and the suggestions.
    Let me reorganize and see. Also I will clear the TMR0 approp.
    However, I wonder how come the interrupt routines without the blink button works absolutely alright on the PIC.
    And when I removed the whole button routines and rewrote the code to accept the variable 'spacing' from PORTA through a DIP switch, the code worked without hitch on the PIC.

    Divaer

  9. #9
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by divaker View Post
    However, I wonder how come the interrupt routines without the blink button works absolutely alright on the PIC.
    And when I removed the whole button routines and rewrote the code to accept the variable 'spacing' from PORTA through a DIP switch, the code worked without hitch on the PIC.

    Divaer
    Probably because the blink button code had PAUSEs in it. The 'On Interrupt' doesn't fire if it's blocked by a PAUSE or any other statement that needs to complete before the next statement can execute. More details about 'On Interrupt' in the manual.

Similar Threads

  1. Code works on 16F84A but not on 16F88?
    By pharaohamps in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th November 2009, 18:29
  2. Help with Analog Interrupt
    By brid0030 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th February 2008, 18:14
  3. 16F876A CCP1 Capture/Interrupt Question
    By TDonBass in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 17th January 2008, 03:25
  4. PicBasic code problems with a 16F84A
    By Lauren Barta in forum mel PIC BASIC
    Replies: 3
    Last Post: - 30th May 2006, 22:50
  5. NEWBIE: Some basic questions using interrupts
    By JackPollack in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th March 2006, 02:59

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