Advertisement

02.22.2007 at 05:43AM PST, ID: 22406155
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

conversion LARGE_INTEGER to _int64; or operating with LARGE_INTEGER
Tags: large_integer
Hello,
I am trying to use an alternative Sleep() function in C that has a better solution. (The solution of sleep() is only 15 ms). I need 1 ms. My teacher gave me the above code


static double esperaH(double *temp) //sleep in ms
{

bool resultado;
unsigned _int64 *now, *frec,goal;
//LARGE_INTEGER *now, *frec, goal;

now = (unsigned _int64*)calloc(1,sizeof(now));
//now = (LARGE_INTEGER*)calloc(1,sizeof(now));
resultado = QueryPerformanceCounter(now);

frec = (unsigned _int64*)calloc(1,sizeof(frec));
//frec = (LARGE_INTEGER*)calloc(1,sizeof(frec));
resultado=QueryPerformanceFrequency(frec);

goal = *now + (unsigned _int64)((*temp*1.0e-3)*(*frec));

while (goal > *now)
resultado = QueryPerformanceCounter(now);
return ((double)*now/(double)*frec)*1000;
//return ((LARGE_INTEGER)*now/(LARGE_INTEGER)*frec)*1000;

}

if I compile it like this I get the following warnings/errors:
--------------------Configuration: pci 1784 configuration5 - Win32 Debug--------------------
Compiling...
pci 1784 configuration5.cpp
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(110) : warning C4305: 'argument' : truncation from 'const double' to 'float'
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(55) : warning C4101: 'num_contador' : unreferenced local variable
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(387) : warning C4244: 'argument' : conversion from 'unsigned long' to 'unsigned short', possible loss of data
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(426) : warning C4244: 'argument' : conversion from 'const double' to 'unsigned char', possible loss of data
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(728) : error C2664: 'QueryPerformanceCounter' : cannot convert parameter 1 from 'unsigned __int64 *' to 'union _LARGE_INTEGER *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(732) : error C2664: 'QueryPerformanceFrequency' : cannot convert parameter 1 from 'unsigned __int64 *' to 'union _LARGE_INTEGER *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(737) : error C2664: 'QueryPerformanceCounter' : cannot convert parameter 1 from 'unsigned __int64 *' to 'union _LARGE_INTEGER *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

pci 1784 configuration5.obj - 3 error(s), 4 warning(s)


If I compile it using _int

If I use the other code version:

static double esperaH(double *temp) //sleep in ms
{

bool resultado;
//unsigned _int64 *now, *frec,goal;
LARGE_INTEGER *now, *frec, goal;

//now = (unsigned _int64*)calloc(1,sizeof(now));
now = (LARGE_INTEGER*)calloc(1,sizeof(now));
resultado = QueryPerformanceCounter(now);

//frec = (unsigned _int64*)calloc(1,sizeof(frec));
frec = (LARGE_INTEGER*)calloc(1,sizeof(frec));
resultado=QueryPerformanceFrequency(frec);

goal = *now + (LARGE_INTEGER)((*temp*1.0e-3)*(*frec));

while (goal > *now)
resultado = QueryPerformanceCounter(now);
return ((double)*now/(double)*frec)*1000;
//return ((LARGE_INTEGER)*now/(LARGE_INTEGER)*frec)*1000;

}

...I get the following compiler warnings/errors:
--------------------Configuration: pci 1784 configuration5 - Win32 Debug--------------------
Compiling...
pci 1784 configuration5.cpp
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(110) : warning C4305: 'argument' : truncation from 'const double' to 'float'
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(55) : warning C4101: 'num_contador' : unreferenced local variable
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(387) : warning C4244: 'argument' : conversion from 'unsigned long' to 'unsigned short', possible loss of data
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(426) : warning C4244: 'argument' : conversion from 'const double' to 'unsigned char', possible loss of data
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(728) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(732) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(734) : error C2677: binary '*' : no global operator defined which takes type 'union _LARGE_INTEGER' (or there is no acceptable conversion)
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(736) : error C2678: binary '>' : no operator defined which takes a left-hand operand of type 'union _LARGE_INTEGER' (or there is no acceptable conversion)
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(736) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

pci 1784 configuration5.obj - 3 error(s), 6 warning(s)


I don´t know what to do. The Query-fungtions seem to only accept large_integer, but large_integer cannot operate with +,*,/
Does anyone have a suggestion what could be my error or how I could solve my problem?? I would be so glad since I am very unexperienced in programming.

Judith
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: jutzki
Solution Provided By: Kdo
Participating Experts: 3
Solution Grade: A
Views: 124
Translate:
Loading Advertisement...
02.22.2007 at 06:14AM PST, ID: 18587543

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 06:33AM PST, ID: 18587814

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 06:34AM PST, ID: 18587819

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 06:42AM PST, ID: 18587891

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 06:52AM PST, ID: 18587983

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 07:09AM PST, ID: 18588157

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 07:18AM PST, ID: 18588248

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 07:28AM PST, ID: 18588349

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 07:29AM PST, ID: 18588367

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 07:49AM PST, ID: 18588580

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 07:56AM PST, ID: 18588653

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 08:05AM PST, ID: 18588740

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 08:16AM PST, ID: 18588896

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 08:24AM PST, ID: 18588964

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 08:43AM PST, ID: 18589154

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 08:53AM PST, ID: 18589257

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 09:24AM PST, ID: 18589544

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2007 at 10:22AM PST, ID: 18590006

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.16.2007 at 12:27PM PDT, ID: 19299225

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.13.2007 at 03:47PM PST, ID: 20276501

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.22.2007 at 06:14AM PST, ID: 18587543

Hi Judith,

It's a bit of a challenge to take these error messages (which cover the entire program or module) and relate them to this small piece of code.  Still, I think that I can tie them together.  :)

One of the things that you can do to simplify the program is to use scalar variables instead of pointers within the function.  Here's a quick edit to demonstrate:

static double esperaH(double *temp) //sleep in ms
{

bool resultado;
//unsigned _int64 *now, *frec,goal;
LARGE_INTEGER now, frec, goal;

resultado = QueryPerformanceCounter(&now);

resultado=QueryPerformanceFrequency(&frec);

goal = now + (LARGE_INTEGER)((*temp*1.0e-3)*(frec));

while (goal > now)
resultado = QueryPerformanceCounter(&now);
return ((double)now/(double)frec)*1000;
//return ((LARGE_INTEGER)now/(LARGE_INTEGER)frec)*1000;

}


I'm curious as to why you pass a pointer to a double instead of just passing a double.

Kent

 
02.22.2007 at 06:33AM PST, ID: 18587814

Rank: Master

Hi, Judith,

By looking just to the syntax, all the warnings can be ignored.
The real problem is with  unsigned __int64.
The conversion from unsigned __int64 to double isn't implemmented in the compiler.

Specificaly to Builder C++, seems the question is still open, as you may look at:
http://qc.borland.com/wc/qcmain.aspx?d=2254

The workaround is to use __int64 as signed, if you don't really need the most significant bit.
Let me suggest also to clean the code, as it uses intensively type conversions, with no apparent mandatory reason (as per the piece of code post in the question).

Jose
 
02.22.2007 at 06:34AM PST, ID: 18587819
Hello Kent,
thank you for viewing my question. I am only concerned about the error messages in this part of the program because the rest works...using solution I still get the same error messages...that the operators is not defined for the LARGE_INTEGER_TYPE:


C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(110) : warning C4305: 'argument' : truncation from 'const double' to 'float'
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(55) : warning C4101: 'num_contador' : unreferenced local variable
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(387) : warning C4244: 'argument' : conversion from 'unsigned long' to 'unsigned short', possible loss of data
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(426) : warning C4244: 'argument' : conversion from 'const double' to 'unsigned char', possible loss of data
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(752) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(754) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(756) : error C2677: binary '*' : no global operator defined which takes type 'union _LARGE_INTEGER' (or there is no acceptable conversion)
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(758) : error C2678: binary '>' : no operator defined which takes a left-hand operand of type 'union _LARGE_INTEGER' (or there is no acceptable conversion)
C:\datos\judith\proyecto_judith\pci 1784\pci 1784 configuration5.cpp(758) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

pci 1784 configuration5.obj - 3 error(s), 6 warning(s)

Qour question: I only copied an existing code...I am afraid my programming knowledge is so poor that I cannot even see a real difference.

Judith
 
02.22.2007 at 06:42AM PST, ID: 18587891
Hello Jose,
I tried to use the signed _int64 instead of unsigned int_64 but it gives me the same errors...
cannot convert parameter 1 from '__int64 *' to 'union _LARGE_INTEGER *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
??
Judith
 
02.22.2007 at 06:52AM PST, ID: 18587983
Hi Judith,

_LARGE_INTEGER is a structure or union.  It is not a base type like int, float, etc.

Dereference the 64-bit values.


static double esperaH(double *temp) //sleep in ms
{

  bool resultado;
  LARGE_INTEGER now, frec, goal;

  resultado = QueryPerformanceCounter(&now);

  resultado=QueryPerformanceFrequency(&frec);

  goal.QuadPart = now.QuadPart + (LONGLONG)(((*temp)*(1.0e-3))*(frec.QuadPart));

  while (goal.QuadPart > now.QuadPart)
    resultado = QueryPerformanceCounter(&now);
  return ((double)now.QuadPart/(double)frec.QuadPart)*1000;
}


Kent
 
02.22.2007 at 07:09AM PST, ID: 18588157
Hi Kent,
thank you very much...now I don´t get errors anymore...
1 more question...when I want to compile the whole program now the compiler tells me the following:

pci 1784 configuration5.obj : error LNK2001: unresolved external symbol _mexPrintf
Debug/pci 1784 configuration5.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

pci 1784 configuration5.exe - 2 error(s), 0 warning(s)

although I put #include "mex.h"....I think I didn´t include a library maybe??
Judith
 
02.22.2007 at 07:18AM PST, ID: 18588248

mexPrintf can come from any of several places.  (MATLAB is probably the biggest reference to it.)

You're going to need to link the correct library.  Not knowing your application, I don't have any idea what the library is.


Kent
 
02.22.2007 at 07:28AM PST, ID: 18588349
#include <Afxwin.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <time.h>
#include <winsock2.h>
#include <mmsystem.h>
#include <math.h>
#include "driver.h"
#include "os.h"
#include "dask.h"
#include "jr3ft.h"
#include "stdafx.h"

#include <windows.h>
#include "mex.h"
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>
#include "ADS1784.h"

void ConfiguraTargetes(void);
void TancaTarjetes(void);
void EscriuTau(int quin, float valor);
long Devicehandler1720;
static double esperaH(double temp);


void main(void)
{      
    BYTE byBoard_ID;
UCHAR byCounter,num_contador;
      DWORD status;
      DWORD lpCntValue;
      CHAR  *texto;

      
    byBoard_ID = 0;
    byCounter = CNT_0;
      
      
    printf(" The card selected is %d   ", byBoard_ID);
      if (byCounter==CNT_0)
      {
         texto="Contador 0";
      }
      printf(" \n\n %s   ", texto);
    ConfiguraTargetes();
      
      printf("empiezo el bucle");
      
      int i;      
      i=0;
      lpCntValue=0;
      while (i<1000) //&& (lpCntValue < 0xfffee920))
      {

            status = P1784CounterRead(byBoard_ID,byCounter,&lpCntValue);
      
      if (status != ERROR_SUCCESS) //ERROR_SUCCESS = function success
    {
            printf("\n\n La lectura es NO OK");
            if (status = CounterNumErr)
            {
                  printf("\n\n Counter number Error");
                  printf("\n Press any key to exit....");
                  getch();//espera que se pulse una tecla e inmediamente después devuelve un valor
                  exit(1); //se puede salir anticipadamente de un programa usando la función exit()
            }
                  else
                  {
                        printf("\n\n Input parameter error!");
                        getch();//espera que se pulse una tecla e inmediamente después devuelve un valor
                        exit(1); //se puede salir anticipadamente de un programa usando la función exit()
                  }
      }
      
      printf("\n\n Counter value read: %x  ", lpCntValue);
      
      EscriuTau(1,0.2);

      //if (lpCntValue >= 0xfec3d74b) //counter too high; girar clockwise in order to disminuir contador
      //      EscriuTau(1,-0.2); //para girar en otro dirección
      //if (lpCntValue<= 0xfec2b441) //counter too little; girar anticlockwise in order to aumentar contador
      //      EscriuTau(1,0.2); //para girar en otro dirección
      
      
            i++;
      

      esperaH(10.0);

      }
      //printf("\n\n Counter value read: %x  ", lpCntValue);
      /*
      printf("\n Press any key to exit....");
      
      getch();
      TancaTarjetes();
      exit(1);
      */

      TancaTarjetes();
      
      
}




void ConfiguraTargetes()
{
      //CONFIGURAR TARJETA PCI 1784:

      BYTE byBoard_ID;

      byBoard_ID = 0;
      //Check how many PCI-1784 card in this system:
      //P1784DevAvailable(DWORD *lpReturnBoardStatus) //evy. in EscriuTau!!!


      //PCI1784 available status.  Bit value : 0 for non avaliable, 1 for available        
      //*lpReturnBoardStatus:  

      //Value   Meaning  
      //Bit 0        For board ID 0
      //Bit 1        For board ID 1
      //Bit 2   For board ID 2
      //.....
 
      //Bit 15  For board ID15



   
   
   
      


      //Device Open:
      //Initial the PCI-1784 and system resource for operation.
      //Before using any P1784 DLL function except P1784DevAvailable,
      //user must use this function first.
      //BYTE byBoard_ID;
      DWORD dwReturnCode;
      dwReturnCode = P1784DevOpen(byBoard_ID);
      //status1784 = P1784DevOpen(BYTE 0); // (BYTE byBoard_ID) O-15

      if (dwReturnCode != ERROR_SUCCESS) //ERROR_SUCCESS = function success
    {
        printf("\n\n Program Fail %4x",dwReturnCode);
        printf("\n Board 0 doesn't exsit !");
        printf("\n Press any key to exit....");
        getch();//espera que se pulse una tecla e inmediamente después devuelve un valor
        exit(1); //se puede salir anticipadamente de un programa usando la función exit()
    }


      //Configure counters:


      //counter mode:
//Select counter mode, PCI1784 counter have 5 modes. X1,X2,X4 is quadrature input mode.
//Quadrature input consists of two square wave inputs(A and B) which are 90 degree out of phase.
      //The PCI1784 counts the square wave transitions and determines the direction by comparing channel A
      //is leading channel B or vice versa.
      
      

      UCHAR byCounter;
      //      Counter 0
      byCounter=CNT_0;

      P1784SelectCntMode(byBoard_ID,byCounter,X4);  //(BYTE byBoard_ID, BYTE byCounter, WORD wMode)
      //P1784SelectCntMode(0,CNT_1,X4);
      //P1784SelectCntMode(0,CNT_2,X4);
      //P1784SelectCntMode(0,CNT_3,X4);
      
      
      //BYTE CNT_0, CNT_1, CNT_2, CNT_3;
      //WORD X4;

      //wMode:
      //DISABLE:PCI1784 will not accept input, but you can still access all its registers.
      //X1:The counter will increment (or decrement) the counter whenever a rising edge occurs on input channel A
       //X2:The counter will increment (or decrement) the counter whenever a rising edge occurs or falling edge
      //   occurs on input channel A
       //X4:The counter will increment (or decrement) the counter whenever a rising or falling edge occurs on input channel A or B
       //_2_PULSE:In 2 pulse mode the PCI1784 uses two input pulses as counting sources: one for clockwise(CW)
      //               and one for counterclockwise(CCW)counting. The counter will decrement whenever a rising edge
      //               occurs on channel A. It will increment whenever a rising edge occurs on channel B
       //_1_PULSE:In pulse/direction mode the PCI1784 uses one input line(A) for pulse input and one line (B) for direction.
      //               If channel B is high (1), the counter will decrement whenever a rising edge occurs on channel A.
      //               If channel B is low (0), the counter will increment whenever a rising edge occurs on channel A.
 

      //CNT_0: Select counter 0
      //CNT_1: Select counter 1
      //CNT_2: Select counter 2
      //CNT_3: Select counter 3




      //counter latch:
      //When you read a counter, you are actually reading a value latched into a buffer.
      //The PCI1784 provides five different latching modes, only one of which is active
      //at any given time. Make sure that you know which latching mode is current one
      //whenever you read the counter. Otherwise, you may read an old value or one that
      //was latched at a different time that you except.
      
      
      P1784SetLatchSource(0,CNT_0,SOFTWARE);//(BYTE byBoard_ID, BYTE byCounter, WORD wLatchSource)  
      //P1784SetLatchSource(0,CNT_1,SOFTWARE);
      //P1784SetLatchSource(0,CNT_2,SOFTWARE);
      //P1784SetLatchSource(0,CNT_3,SOFTWARE);

      //wLatchSource:
      //SOFTWARE:Whenever you read a channels data registers, the counter values will be latched in buffer.
      //               The S/W latch will  only take effect when you read the the counter.
       //INDEX:A rising edge on the channels index input line will latch the channels counter value
       //TIMER:The Card latches the counter value on a rising edge of pulses from the cards on-board timer.
       //DI_0:A rising edge on the boards DI0 line will latch the counter value for the channel.
      //DI_1:A rising edge on the boards DI1 line will latch the counter value for the channel.
      //DI_2:A rising edge on the boards DI2 line will latch the counter value for the channel.
      //DI_3:A rising edge on the boards DI3 line will latch the counter value for the channel.
 


      //counter lock overflow:
      //Set counter locked when counter overflows.
      
      P1784EnableOverFlowLock(0,CNT_0,FALSE);//(BYTE byBoard_ID, BYTE byCounter, BOOL bValue)
      //P1784EnableOverFlowLock(0,CNT_1,FALSE);
      //P1784EnableOverFlowLock(0,CNT_2,FALSE);
      //P1784EnableOverFlowLock(0,CNT_3,FALSE);
      //bValue:
      //Specifies counter locked when counter overflows.
      //If TRUE, counter locked when counter overflows.
      //If FALSE, Counter continues counting (wraps over) when counter overflow.


      //counter lock underflow:
      //Set counter locked when counter underflows.

      P1784EnableUnderFlowLock(0,CNT_0,FALSE);//(BYTE byBoard_ID, BYTE byCounter, BOOL bValue)  
      //P1784EnableUnderFlowLock(0,CNT_1,FALSE);
      //P1784EnableUnderFlowLock(0,CNT_2,FALSE);
      //P1784EnableUnderFlowLock(0,CNT_3,FALSE);

      //bValue:
      //Specifies counter locked when counter underflows.
      //If TRUE, counter locked when counter underflows. If FALSE,
      //Counter continues counting (wraps over) when counter underflows.
      //reset value:

      //Set the reset value when counter reset.
      P1784EnableResetValue(0,CNT_0,FALSE);//(BYTE byBoard_ID, BYTE byCounter, BOOL bValue)
      //P1784EnableResetValue(0,CNT_1,FALSE);
      //P1784EnableResetValue(0,CNT_1,FALSE);
      //P1784EnableResetValue(0,CNT_3,FALSE);

            
      //bValue:
      //The reset value when counter reset.
      //If TRUE, the reset value is 80000000h. If FALSE, it is  00000000h



      //digital filter:

      //digital filter clock:
      //Set digital filter clock frequency.
      DWORD wMode;
      wMode = _8MHZ;
      P1784SelectFilterClock(byBoard_ID,wMode);//(BYTE byBoard_ID, DWORD wMode);  

      //wMode: sampling clock mode; _8MHZ/_4MHZ/_2MHZ/_1MHZ


      //enable digital filter:
      //Set quadrature input (channel A, B) with digital filter.
      
      P1784EnableDigitalFilter(0,CNT_0,TRUE);//(BYTE byBoard_ID, BYTE byCounter, BOOL bValue)  
      //P1784EnableDigitalFilter(0,CNT_1,TRUE);
      //P1784EnableDigitalFilter(0,CNT_2,TRUE);
      //P1784EnableDigitalFilter(0,CNT_3,TRUE);

      //bValue:
      //Specifies quadrature input frequency with digital filter.
      //If TRUE, Quadrature input frequency with digital filter.
      //If FALSE, quadrature input frequency without digital filter



      
      
      
      

      
      
      //Configure Timer:
      


      //Set divide value and timer base of Timer.
      //When you use Timer Latch on a rising edge of pulses from the cards on board timer,
      //You can set timer cycle periods from 0.02 ms to 51 seconds.
      //The cycle time is the product of the timer base period and a multiplier.
      //Timer base periods are 0.02, 0.2, 2, 20 or 200 ms. The multiplier ranges from 1 to 255.
      //The divider can range from 1 to 255.


      P1784SetTimerPeriod(0,0.5,_5KHZ);//(BYTE byBoard_ID, BYTE byDivideValue, BYTE byTimerBase)  
      

      //byDivideValue:The divider value 1-255; to create different timer periods!!
      //byTimerBase:
      //_50KHZ:Timer base periods is 0.02ms
      //_5KHZ :Timer base periods is 0.2ms
      //_500HZ:Timer base periods is 2ms
      //_50HZ :Timer base periods is 20ms
      //_5HZ  :Timer base periods is 200ms
      //example:
      //For example, to set a timer period of 20 ms, you would set the timer base to 1ms and the divider to 20.
      //That is :
      //Timer period = Base Time * divider
      //20 ms = 1 ms * 20




      



      //Configure Comparator:
      
            
      //Read current comparator.
      //DWORD *dwCntValue;


      //P1784ComparatorRead(byBoard_ID,CNT_0,dwCntValue); //(BYTE byBoard_ID, BYTE byCounter, DWORD *dwCntValue)
      //P1784ComparatorRead(byBoard_ID,CNT_1,dwCntValue);
      //P1784ComparatorRead(byBoard_ID,CNT_2,dwCntValue);
      //P1784ComparatorRead(byBoard_ID,CNT_3,dwCntValue);

      //*dwCntValue:The value of the data in current comparator


      
      //Set the comparing condition. Comparing condition consists of 2 parts,
      //P1784SetTriggerComp(0,CNT_0,0,1000);//(BYTE byBoard_ID, BYTE byCounter, BYTE byMode, ULONG dwData)
      //P1784SetTriggerComp(0,CNT_0,0,1000);
      //P1784SetTriggerComp(0,CNT_0,0,1000);
      //P1784SetTriggerComp(0,CNT_0,0,1000);

      //byCounter:CNT_0 or CNT_1 or CNT_2 or CNT_3
      //comparing mode:0 for ">", 1 for "<"      
      //dwData:the value to be compared


      

      

      //Event monitor/interrupt:

      //Enable or Disable the event for 4 counters; starts or stops counter operation
      //P1784EnableEvent(byBoard_ID,CNT_0,1);//(BYTE byBoard_ID, BYTE byCounter, WORD wIntSource)


      //wIntSource:The interrupt event source
      //CNT_OVERFLOW:counter overflow
      //CNT_UNDERFLOW:counter underflow
      //CNT_INDEX:counter index in
      //DI0_TRI:DI0 input, When set this value, the byCounter parameter will be ignore.
      //DI1_TRI:DI1 input, When set this value, the byCounter parameter will be ignore.
      //DI2_TRI:DI2 input, When set this value, the byCounter parameter will be ignore.
      //DI3_TRI:DI3 input, When set this value, the byCounter parameter will be ignore.
      //CNT_OVERCOMP:counter over-compare
      //CNT_UNDERCOMP:counter under-compare
      //INT_TIMER:The card's on-board timer's pulse come
      //INT_ALL:All of the events enabled
 





        

      //You can use the CheckEvent function to monitor the event status.
      //The CheckEvent function is a synchronous method to check the event.
      //You have to specify a period for the time out.
      //When an event occurs, it returns the event status(dwRetEventStatus) immediately.
      //If no event occurs in this period, it returns a time out error.  
      //It uses an efficient polling method to check the event.
      //Your CPU can then simultaneously perform other functions.

      //checks status; when it reaches a count it will return a hardware event

      //DWORD *dwRetEventStatus;
      //DWORD dwMillisecond;
      //P1784CheckEvent(byBoard_ID,dwRetEventStatus,dwMillisecond);
      
      //dwMillisecond: Time-out interval in milliseconds
      //*dwRetEventStatus: Current Event status; The status of interrupt. When some interrupt comes into existence,
      //                               the relative bit of Interrupt status register will become 1, otherwise 0.  
      //The status of interrupt. When some interrupt comes into existence, the relative bit of Interrupt status register
      //will become 1, otherwise 0.  

      //D15  D14  D13  D12  D11  D10  D9  D8  D7  D6  D5  D4  D3  D2  D1  D0  
      //DI3  DI2  DI1  DI0  IX3  IX2  IX1 IX0 UN3 UN2 UN1 UN0 OV3 OV2 OV1 OV0

      //D31  D30  D29  D28  D27  D26  D25 D24  D23  D22  D21  D20  D19  D18  D17  D16  
      //IF                   TM                                   UC3  UC2  UC1  UC0  OC3  OC2  OC1  OC0
 
      //1. OVn = Counter overflow flag
      //2. UNn = counter underflow flag
      //3. IXn = Index input flag
      //4. DIn = Digital input flag
      //5. OCn = Counter over compare register flag
      //6. UCn = Counter under compare register flag
      //7. TM  = Timer pulse flag
      //8. IF = Interrupt flag
      //   (n = 0 to 3)


      


      //Digital Input/Output functions:

      //Digital Input:
      //BYTE *lpReturnValue;
      //P1784DI(0,lpReturnValue);//(BYTE byBoard_ID, BYTE *lpReturnValue)  

      //*lpReturnValue: Data received (0/1) from each 4 input channel

      

      //Set Digital Output Mode:
      //The function is to change the digital output mode into Indicated or normal output, and control the pulse width.
      //BYTE byDOMode;
      //byDOMode = 0; // set DO mode to normal
      //dwReturnCode = P1784SetDOMode(byBoard_ID,byDOMode);
      //P1784SetDOMode(0,0);//(BYTE byBoard_ID, BYTE byDOMode);  

       //byDOMode:
      //D7      D6      D5  D4  D3  D2  D1  D0
      //DM3 DM2 DM1 DM0 LE3 LE2 LE1 LE0
 
      
      //1. LEn = Digital output level control
      //      0        Pulse with counter clock
      //      1        Level with clear interrupt
      //2. DMn = Digital output mode control
      //      0        Normal
      //      1           Indicated
      //      (n = 0 to 3)

      //if(dwReturnCode != ERROR_SUCCESS)
    //{
      //  printf("\n Set DO mode fail %4x",dwReturnCode);
        //printf("\n Press any key to exit....");
        //getch();
        //exit(1);
    //}







      //Get Digital Output Status:
      //P1784GetDOStatus(byBoard_ID,lpReturnValue);

      //*lpReturnValue: Output;The output data status of each channel

      
      //digital output/output data:
      //BYTE byWriteValue;
      //byWriteValue = 1; //in bits: 00000001
      //P1784DO(byBoard_ID,byWriteValue);//(BYTE byBoard_ID, BYTE byWriteValue)
      //byWriteValue:input; output data
 
      // Output data
   
    //while(byWriteValue<=8)
    //{
      //  printf("\nDigital Output Data : 0x%1x",byWriteValue);

        // Write Digital Output Data
        //dwReturnCode = P1784DO(byBoard_ID,byWriteValue);
        //if (dwReturnCode != ERROR_SUCCESS)
        //{
          //  printf("\n\n Program Fail %4x",dwReturnCode);
            //printf("\n Press any to exit....");
            //getch();
            //exit(1);
        //}
        //printf("\n Output data is %d",byDOValue);
        //byWriteValue=byWriteValue<<1; //1 shifts one position to the left: 00000010-->00000100-->00001000 usw.
    //}

    //printf("\n\n Press any key to quit\n");
    //getch();





      


      //CONFIGURAR TARJETA PCI 1720:

      int status1720;
      status1720 = DRV_DeviceOpen(0,&Devicehandler1720); //tarjeta 0 advantechautomation-->device manager --> Advantech device manager




}
 



      





void TancaTarjetes(void)
{      
      
      //PARA