CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: timer interrupt

  1. #1
    Join Date
    Dec 2001
    Location
    Maryland
    Posts
    13

    timer interrupt

    i'm trying to use a timer interrupt to perform a task at a fixed period, i'm having some trouble setting the vector:

    #include <stdio.h>
    #include <dos.h>

    #ifdef __cplusplus
    #define __CPPARGGS ....
    #else
    #define __CPPARGS
    #endif

    void setup_int(void);
    void interrupt isr1(__CPPARGS);
    void restore(void);
    void interrupt (far *old_int8_vect)(__CPPARGS);

    int x = 0;
    int interrupt_flag = 0;


    void main() {
    unsigned int i;
    outportb(0x43, 0x36);
    printf("square wave set");
    setup_int();


    for(i=0; i <= 32000; i++) {
    if(interrupt_flag == 1) {
    printf("INTERRUPT!");
    interrupt_flag = 0;
    x = 0;
    } else {
    printf("line: %d \n", x);
    x++;
    }
    }

    restore();
    }

    void setup_int(void) {
    disable();
    old_int8_vect = getvect(8);
    setvect(8, isr1);
    printf("interrupt setup");
    enable();
    }

    void interrupt isr1(__CPPARGS) {
    disable();
    interrupt_flag = 1;
    old_int8_vect();
    enable();
    }

    void restore(void) {
    disable();
    setvect(8, old_int8_vect);
    enable();
    }

    any comments, what's wrong with this code? eventually it's going to be used in a control system application so restoring the isr isn't really required, and messing with the clock isn't going to cause a problem.

    thanks a lot
    -ian

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    What platform are you running this on? A lot of the old-style
    interrupt code won't work with windows. I didn't even look at
    the code, thinking this might be the case.

  3. #3
    Join Date
    Dec 2001
    Location
    Maryland
    Posts
    13

    platform

    is dos 6.2.2

  4. #4
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645
    What is the problem that you are seeing?

    Are you getting a compile or link error?
    Are you getting a runtime error? What is the error? Which line?
    Is the ISR getting called?

  5. #5
    Join Date
    Dec 2001
    Location
    Maryland
    Posts
    13

    the problem

    sorry, that was dumb on my part.

    code compiles ok, but just prints some gibberesh to the screen then crashes, honestly i'm not sure if the isr is being called or not (is there an easy way to know?)

    what i thought would happen is numbers would get displayed, counting up, until the interrupt fired when it would say "interrupt" and start over.

    thanks for your help!

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: the problem

    Originally posted by Ianwalsh
    sorry, that was dumb on my part.

    code compiles ok, but just prints some gibberesh to the screen then crashes, honestly i'm not sure if the isr is being called or not (is there an easy way to know?)

    what i thought would happen is numbers would get displayed, counting up, until the interrupt fired when it would say "interrupt" and start over.

    thanks for your help!
    Isn't it dangerous to call printf() during an interrupt? From what I remember, you shouldn't make any calls to 'C' library functions that may do any I/O.

    I have some very old timer interrupt code, and I distinctly remember that there are functions you shouldn't call during an interrupt (there is an "InDos" flag, if I recall, that if it is ON, you can't make certain function calls).

    If you can get a copy, get the book "PC Intern" by Michael Tischer. Here is a link:

    http://www.abacuspub.com/catalog/b304.htm

    If you are trying to write your own timer interrupt, and you don't have previous source code to learn from, you more than likely won't get too far.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645
    Paul is correct about the printf inside the interrupt handler.

    However, your printf is in the setup_int() not the ISR.

    Do you know which printf is barfing?

    What is the output that you've seen?
    Does the setvect() work?
    Are you seeing the "INTERRUPT" ever?
    Do you see the "interrupt setup"?
    Do you see the counter counting any?

    Unfortunately, I don't have a DOS setup to work with. I'll have to
    rely on what you are seeing.

  8. #8
    Join Date
    Apr 2003
    Posts
    1,755

    Smile

    I tried your code and it gives my interrupt a "Stack overflow" error. What I did is I deleted the far in the old interrupt declaration
    Code:
    void interrupt (*old_int8_vect)(__CPPARGS);
    And there is no error anymore.

    Hope it will work for you

  9. #9
    Join Date
    Dec 2001
    Location
    Maryland
    Posts
    13
    thanks for your help, i'm looking into getting a copy of that book.

    when i run the program i see the interrupt setup, and the counter, but never the "INTERRUPT!"

    seems like this should be somthing obvious...
    thanks again
    -ian

  10. #10
    Join Date
    Apr 2003
    Posts
    1,755

    Smile

    Sorry, I tested my code in win98. I made it show the "INTERRUPT!" in win98, but when I tested what you mentioned in DOS, I cannot see it anymore.

    I tried to change the interrupt number to 9 (keyboard interrupt) just to make sure if the problem is in the code. While the program is running, I'm pressing the keyboard and I can see, the "INTERRUPT!" string.

    With what I've done, I concluded that there is no problem with the code anymore. The problem now is that the interrupt 8 is not being called by the DOS system anymore. Maybe they changed the interrupt # (or any other reason).

    Try to look at some new books on DOS about interrupt #s.

  11. #11
    Join Date
    Dec 2001
    Location
    Maryland
    Posts
    13

    success!

    Beauty! that worked, at least i can see the keyboard interrupt.

    thanks a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured