?
Solved

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

Posted on 2003-03-05
4
Medium Priority
?
185 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
[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
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

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