?
Solved

How to make a loop delay based on this source code?

Posted on 2003-03-05
4
Medium Priority
?
187 Views
Last Modified: 2010-04-01
I need to make this program work in a loop until 'a' becomes a '0'. what loop should i use and how do i use it?
thanks~!

void play_D(void)
{
     unsigned long int a;
     
     time_t start_time, cur_time;

     time(&start_time);
     do
     {
          _outp(DATA_REGISTER,*fout);

          time(&cur_time);
     }
   
     while((cur_time - start_time) < 3);

     a=(*fout>>4);

     _outp(DATA_REGISTER,a);
}
0
Comment
Question by:sadakoblue
  • 2
4 Comments
 

Accepted Solution

by:
IngvarWolf earned 225 total points
ID: 8078122
How about such a loop:

void play_D(void)
{
    unsigned long int a;
    time_t start_time, cur_time;

    do
    {
        time(&start_time);
        do
        {
            _outp(DATA_REGISTER,*fout);
            time(&cur_time);
        } while((cur_time - start_time) < 3);

        a = (*fout >> 4);
        _outp(DATA_REGISTER,a);
    } while (a != 0);
}

Or may be you have some problem, error messages? Please, make clear your problem...
By the way - what is the type of fout?
0
 

Author Comment

by:sadakoblue
ID: 8078352
ohh.. i face no problem.. is i just want to add a loop.
i paste the whole code out.

#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <math.h>
#include <time.h>

#define sys_clk 180000000
#define deno 4294967296

#define BASE_ADDRESS 0x378
#define DATA_REGISTER (BASE_ADDRESS+0)
#define STATUS_REGISTER (BASE_ADDRESS+1)
#define CONTROL_REGISTER (BASE_ADDRESS+2)

long int *fout;
long int p_fout;

void play_D(void)
{
     unsigned long int a;
     
     time_t start_time, cur_time;

     time(&start_time);
     do
     {
          _outp(DATA_REGISTER,*fout);

          time(&cur_time);
     }
   
     while((cur_time - start_time) < 3);

     a=(*fout>>4);

     _outp(DATA_REGISTER,a);
}

void phase()
{
     long int del_phase = (*fout * deno)/sys_clk;
     
     printf("The delta phase is :\t\t\t%d\n", del_phase);
     printf("The delta phase in hexadecimal is: \t%02X\n", del_phase);
}

void main()
{
     fout = &p_fout;

     printf("\n\n\t\t\t\tSending Data\n\n");
     
     printf("Please enter a number for  output frequency\n(Please do not enter a decimal): ");
     scanf("%d", &p_fout);          

     printf("Data port = %02X\n",DATA_REGISTER);
     printf("Status port = %02X\n",STATUS_REGISTER);
     printf("Control port = %02X\n",CONTROL_REGISTER);
     
     phase();

     play_D();
}

i need to sent to data to parallel port.
read the last 4 bits then shift right and read the last 4 bits again. read until the all data become '0'
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8078588
Change fout to be a pointer to and unsigned int.

// long int *fout; // bad robot!
unsigned long* fout; // this will work

Otherwise, if its value happens to start out with a high bit of 1, for instance 0x8000000, then each time you shift it right, that one will be propogated into the vacated bit positions (the technical term is an 'arithmetic shift').  The result is that it will never contain all 0s!  

For instance:

int n= 0x80000000;
int m= n >> 4;
printf( "%x", m ); // output is f8000000
m= m >> 4;  // value is ff800000
m= m >> 4;  // value is fff80000
m= m >> 4;  // value is ffff8000
m= m >> 4;  // value is fffff800
m= m >> 4;  // value is ffffff80
m= m >> 4;  // value is fffffff8
m= m >> 4;  // value is ffffffff
m= m >> 4;  // value is ffffffff (etc... forever!)

//------------------- solution, use unsigned int
unsigned int un= 0x80000000;
m= n >> 4;
printf( "%x", m ); // output is 08000000

m= m >> 4;  // value is 00800000
m= m >> 4;  // value is 00080000
m= m >> 4;  // value is 00008000
m= m >> 4;  // value is 00000800
m= m >> 4;  // value is 00000080
m= m >> 4;  // value is 00000000 zero, at last!

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8124889
What ?!?!
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

621 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