?
Solved

Custom making an interrupt

Posted on 2003-03-19
4
Medium Priority
?
274 Views
Last Modified: 2010-05-18
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
Comment
Question by:qwertbabe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8165669
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8165672
>> while((inportb(Port)&(0x01))==0);

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

Mayank.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 60 total points
ID: 8165685
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
 

Author Comment

by:qwertbabe
ID: 8165948
yes!... stupid me. too tired i supposed... thanks.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question