Menu

Making use of interrupt on pic16f877a

Help
Aaron
2016-07-06
2016-07-07
  • Aaron

    Aaron - 2016-07-06

    Guys am new to gcb, pls can someone put me in d right direction how to call a sub usingi interrupt when an analog reading reaches a specific value

     

    Last edit: Anobium 2016-07-07
    • William Roth

      William Roth - 2016-07-07

      Hi Aaron and welcome.

      Let me take a stab at your question. But first let me say that when asking for advise/help it is a good idea to let us know what specific chip you are referring to. They are not all the same. The mor details you provide the better the help will be.

      I am not aware of any current microcontrollers that support an interrupt on a specific ADC value. This is not a limitation of GCB as it is the microcontroller that determines what interrupts are available.

      But all is not lost ! We can get creative.

      We can set up a comparator to trigger at a voltage level that correlates to the ADC value. Then the output or that comparator can trigger an interrupt. The comparator can be either internal or external to the microcontroller.

      Let us know what microcontroller you have and possibly more help can be provided. In the mean time may I suggest you read the interrupts section of the GCB help as it will tell you what interrupts are available.

      William

       

      Last edit: Anobium 2016-07-07
  • Anobium

    Anobium - 2016-07-06

    Hi.

    A really good start is the demo folder of your installation. See \GreatCowBasic\Demos\Interrupt Solutions\Interrupt driving LED via PWM - 16F886.gcb an example of interrupts.

    Another good example in your context is \GreatCowBasic\Demos\Vendor Boards\Xpress Evaluation Board\26 Reversible LEDS with ADC Variable Delay an Interrupt.gcb

    Can you give me a little more context on what you are trying to do? I am hoping that you have the latest installation installed.

    We can help with a little more info,

     

    Last edit: Anobium 2016-07-07
  • viscomjim

    viscomjim - 2016-07-07

    Was the rest of this thread moved somewhere? I just sent someone to read this and they said it was gone. He was right. Where did it go????

     

    Last edit: Anobium 2016-07-07
  • Anobium

    Anobium - 2016-07-07

    I spent a happy hour today recovering this thread. It was marked as SPxM. :-( If I use that work SPxM is may get marked again.

     

    Last edit: Anobium 2016-07-07
    • Anobium

      Anobium - 2016-07-07

      I am opening up a support ticket. My message just got marked as spxm.

       
  • Anobium

    Anobium - 2016-07-07

    Recreating lost messages - originally posted by William Roth

    With a 20MHz System clock you will likley never get to a precise 20ms interrupt on overflow unless "preset" the timer value.

    With a 16Mhz Clock and Prescale of 1:1 (none) I get about 16.4 ms. So to get 20ms I use a prescale of 1:2 and "preset" the timer to adjust when the interrupt occurs

    GCB does not have a preset option so it is done with "settimer" as the very first line in the ISR. I know that with at 16MHz system clock and a prescale of 1:2 that each timer tick should be ~500 nano seconds ( 1/2 microsecond)

    20ms = 20,000 microseconds 20 ms would also be ~40000 timer ticks. 65530 - 40000 = 25535
    So if I start the timer at 25535, it should overflow 20 ms later.

    So for your 20Mhz clock ...... 1 / 20000000 = 0.00000005 * 2 * 4 = .0.0000004 sec per timer tick (400ns per tick) so you will need .020 / .0000004 ticks (50,0000)
    65535 - 50000 = 15535 starting value.

    Heres my code for 16MHz. (Change for 20Hz Crystal) Use a scope to measure interrupt interval at port b.6.

    ~~~~@
    #chip 16F1829, 16

    Dir PortB.6 out

    on interrupt timer1overflow Call ISR1
    inittimer1(OSC,PS1_2)
    Starttimer 1

    Do

    'wait for interrupt

    Loop

    SUB ISR1
    settimer 1, 25535
    pulseout portb.6, 1 ms
    end sub
    ~~~~~

     

    Last edit: Anobium 2016-07-07
  • Anobium

    Anobium - 2016-07-07

    Original post by viscomjim.

    So for your 20Mhz clock ...... 1 / 20000000 = 0.00000005 * 2 * 4 = .0.0000004 sec per timer tick (400ns per tick) so you will need .020 / .0000004 ticks (50,0000)
    65535 - 50000 = 15535 starting value. <<<<<<< AHHHHHHH!!!!!!

    THANK YOU SOOOOO MUCH!!!!! THAT MAKES SENSE. I finally understand. This should go in the help manual. Very valuable information!!!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.