Interrupt Latency - ASM vs. Basic


Closed Thread
Results 1 to 6 of 6
  1. #1
    barkerben's Avatar
    barkerben Guest

    Default Interrupt Latency - ASM vs. Basic

    Hi,

    I'm planning on usin serial interruots in PICBasic. I'm keeping the main loop as simple as possible to avod delays in servicing the interrupt, and thus minimizing the chance of buffer overrun. I am aos planning to reduce the serial speed to 300 baud. Hopefully, this will be enough. However, I have noticed some people using ASM service routines, using

    DEFINE INTHAND ....

    Rather than ON INTERRUPT

    I assume that the advantage of embedding an ASM routine in the basic is that it is serviced more quickly. Is this the case, and if so, why ....?


    Thanks,

    Ben

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Assembly language is still the fastest way to handle interrupt. It use the internal language of the PIC without any other reference/interpretation. I mean, when you're using PICBASIC command, it can refer to hundreds lines of code before finishing one instruction.

    The PICBASIC interrupt work well to. It's just another way to use interrupts. BTW if you're planning to use Serial interrupt, choose a PIC that have an internal USART. It will provide you an USART interrupt.

    Always keep the main loop instruction as simple as you can. If you must use some delay, do a loop with PAUSEUS or @ NOP. Avoid statement like COUNT, PULSOUT, PULSIN, FREQOUT or so that take time to be finish. There's so many other way to do what they do using the internal TIMER or so. That way, interrupt will be taken faster.

    For all my apps, i always use BASIC interrupt. it's just another way of coding. Hope this help you a bit!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    barkerben's Avatar
    barkerben Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks - basically what I am making is a PC controlled clock output to drive a stepper motor- the PC can set frequency and also a number of control lines on the PIC for controlling the motor.

    To keep the main loop quick, I am using TMR1 interrupts to generate the clock - whenever a hardware interrupt occurs, the idea is the service routine checks the flags to see what type of interrupt it is (Timer or Serial) and responds accordingly. All the main loop does is constantly check a series of IF statements to see if one of the interrupt routines has done something which needs responding to (such as changinf clock freq.)

    Because of the 2 byte buffer serial, I'm worried about overflow, but at 300 baud, it should take just over 50ms for the buffer to fill up, so hopefully I should be ok. I don't know what kind of latency to expect, but I suspect it will be less than 50ms if I am careful.

    Of course, the worst case might be if serial data arrives while a timer interrupt is being serviced, in which case the interruot is missed and we have overlow. Keeping the service routines as short as possible should sort this out I hope.



    I've written most of the cde, and will be trying it out next week when I get back to university - I suspect I may make some more posts then :-) thanks for your help,

    Ben

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    if you're really afraid of the amount of time to get the interrupt, have a HS crystal (20MHZ) or use a PIC that can run up to 40MHZ... But testing in a real world environement will give you the right answer on those delay.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    barkerben's Avatar
    barkerben Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks - I'm using 4Mhz at the moment, but could up the frequency if I have latency problems. I suspect I will be ok in fact - but I will let uou know hw it goes in the lab. The hardware is all built, so a few days coding and I will know how rthings stand!

    I assume the only problem with increasing the crystal frequency is that some of the PICBasic
    default commands assume 4Mhz, so care would be needed when implementing them?

    One final question:

    I realise that, as you say, one command in PICBasic might com pile to many lines of assembler. For this reason, an interrupt service routine written in ASM would prob. be quicker than the same thing written in basic. However, there seems to be a suggestion on various sites that an ASM service routine embedded in a Basic program will be called more quickly - why should this be? Surely the advantages of ASM do not come into play intil the program is already in the service routine...

    Cheers, and have a good New Year,


    Ben

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Post

    I assume the only problem with increasing the crystal frequency is that some of the PICBasic
    default commands assume 4Mhz, so care would be needed when implementing them?
    you'll void some problem when you'll add DEFINE OSC (value) at the begining of your code. But if you plan to use any kind of PULSIN or else... sure you have to be carefull.


    However, there seems to be a suggestion on various sites that an ASM service routine embedded in a Basic program will be called more quickly - why should this be?
    for the same reasons as above. But don't be afraid for now. Try it first. BASIC interrupt is not as slow as you can think. Your coding style can slow the interrupt.

    BTW, feel free to post any kind of code here if you have problem.

    Happy new year to you!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. DT-Ints latency and other interrupt conciderations
    By HenrikOlsson in forum mel PIC BASIC Pro
    Replies: 58
    Last Post: - 12th March 2010, 18:27
  2. 18F2480 asm interrupt
    By Richard Storie in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 5th March 2009, 19:40
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. 16F877a ASM Interrupt Confused
    By modifyit in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 19th September 2005, 03:16
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

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