• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Custom making an interrupt

help! can someone check my code? dunno why in the main routine, my if statement is never reached. just hangs there..... thanks.
___________________________________
#include <stdio.h>
#include <conio.h>     // kbhit() getch() inp() outp()
#include <stdlib.h>     // exit(1)
#include <dos.h>     // inportb outportb

void setup(void);
void unsetup(void);

void interrupt setclock();
void interrupt far (*oldVector)();

int Port;
static int k=1;          // so as to determine hi/low

// startup procs.
void setup(void)
{
     int number;                  // sample rate per second
     int ratehi,ratelo;           // hi and lo byte to send to timer
     unsigned rate = 65000;       // set max timer rate 65kHz
     // printf("Rate = %u \n", rate);

     // get and save old interrupt vector
     disable();
     oldVector = getvect(0x1c);
     setvect(0x1c,setclock);
     enable();

     // setup the 8253 timer
     number=65536/(rate/18.2);           // calculate divisor
     ratehi=number&0xff00;               // calculate hi byte for timer
     ratehi=ratehi/256;
     ratelo=number&0x00ff;               // calculate lo byte for timer

     outportb(0x43,0x36);
     outportb(0x40,ratelo);
     outportb(0x40,ratehi);
}

// the NEW interrupt!
// to send the clock signal to d0
void interrupt setclock()
{
     if(k==1)
     {
          outportb(Port,0x01);          // 0000 0001
          //printf("Port is now %d\n", inportb(Port)); cant print will hang
          k = 0;
     }else
     {
          outportb(Port,0);          // 0000 0000
          //printf("Port is now %d\n", inportb(Port));
          k = 1;                                             delay(1);
     }

}

void unsetup(void)
{
     int ratehi,ratelo;           // hi and lo byte to send to timer

     // restore old interrupt vector
     disable();
     setvect(0x1c,oldVector);

     // setup the 8253 timer
     outportb(0x43,0x36);
     outportb(0x40,ratelo);
     outportb(0x40,ratehi);

     enable();

}

void main()
{
     Port=peek(0x40,0x08);       // Parallel port base address - also data pins

     // PortStat = Port +1;
     // PortCtrl = Port +2;

     setup();

     while(!kbhit())
     {
          while((inportb(Port)&(0x01))==0);
          printf("Rising edge detected!\n");

          // CODE never reaches this IF statement!.....
          if (kbhit())   break;
          while((inportb(Port)&(0x01))==1);
          printf("Falling edge detected!\n");
     }

     unsetup();
     exit(0);
}
0
qwertbabe
Asked:
qwertbabe
  • 3
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
Didn't go through much of your code. Are you sure that you need the semi-colon at the end of the nested while statement in main ()?

Mayank.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> while((inportb(Port)&(0x01))==0);

I mean, the semi-colon at the end of this statement?

Mayank.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Well if you do, then maybe this is what is happening:

1. It gets stuck at the first nested while loop as long as the condition is true.

2. Whenever that condition is false, it comes out of the nested while loop and prints "Rising edge detected. ".

3. By the time you hit a key, it has already checked the if () condition and enters the second nested while loop:

while((inportb(Port)&(0x01))==1);

and stays there as long as the condition is true (probably always - that's why its looping there forever).

Mayank.
0
 
qwertbabeAuthor Commented:
yes!... stupid me. too tired i supposed... thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now