Thread: Need help with simple DAQ program

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Question Need help with simple DAQ program

    I've been given a project which, in part, involves some data acquisition coding using a ComputerBoards DAS-08 card. I'm a complete newbie in this area, especially since I graduated in Mechanical Engineering, and not Electrical

    I've been trying to understand how to code, in C or C++, a simple way to output a high signal (+5V) on one of the digital outs (as an example, pin #10 on the DAS08 analog 37-pin connector), but the example programs I've been giving are far more complicated than I need, and as such they're not exactly easy to pick apart.

    I'm capable of some simple C programming (ASCII file I/O, arrays, etc...) but this one has me stumped. I know you have to configure the ports at the beginning of the program using some hex addresses but even this has me confused. I have a feeling the program I need to write is probably about 8 lines long and extremely simple, but getting started is very frustrating.

    If anyone can help me out it would be greatly appreciated!

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Well let me break out my reference book on DAS-08 cards... oh wait, I don't have one.

    Sorry for being facetious, but the odds are that you have more resources than we do on the subject.

    Post some of the difficult code, explain what you are trying to do, and maybe someone will be able to break down the code into easier parts or a working model of what you need.

    I am just telling you this because I have seen similar posts go off into obscurity with no answers because they are too vague or people just do not have the experience.

    Help can only be given if you come to the table with enough information for us to work on.... and you actively participate in the idea exchange.

    Think about it.........
    Blue

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Some example code...

    Thanks for the advice. I didn't know if this was the place to be posting oodles of code or not. But I suppose as it is a programming forum (and now that I've had a chance to view the content of some other posts), there's probably no other way but to dive right in.

    The DAS-08 card is at address 300h. Pin #10 on the J1 analog connector is at, I believe, address base+2, so 302h. I've been given a sample program to use as a guide, and this is what appears near the top:

    #define CONTROL 0x302
    #define COUNTER2 0x306
    #define COUNT_CONTROL 0x307
    #define PORTA 0x308
    #define PORTB 0x309
    #define PORTC 0x30A
    #define CON8255 0x30B

    I believe this portion of the code just gives labels to certain addresses. In my case, I'm interested in the one called CONTROL.

    Later on in the program (which is quite long, and judging by the notations, does quite a few different things), is the effect I need to duplicate. It turns bit #7 on pin #10 to high, then after a delay, turns it back to low:

    // Send CTRL Signal 0x80 = 10000000
    // Set bit #7, pin #10 on the analog connector to high
    outportb(CONTROL, 0x80);

    (Delay part omitted)

    // Send CTRL Signal 0x0 = 00000000
    // Set bit #7, pin #10 on the analog connector to low
    outportb(CONTROL, 0x0);

    The delay portion of code waits for a signal on another channel, then executes the next part. I don't have a need right now to read in data from another channel, I simply just want to get one of those little pins to go to +5 volts for a certain amount of time, then go back to zero.

    Like I mentioned, the code I have to work from is huge. Its far more 'advanced' than I can comprehend at the moment.

    I suppose my questions are:

    1) If I want to set that little pin to +5V for set amount of time, is my code as simple this?

    2) What does 0x80 and 0x0 mean?

    Here is the code I propose:

    #include <stdio.h>
    #include <conio.h>
    #include <dos.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #define CONTROL 0x302;

    using namespace std;

    int main()
    {
    outportb(CONTROL, 0x80);

    cout << "Pin #10 should be at +5V.\n";
    cout << "Hit 'x' to stop signal.\n";

    while (getch()!='x')
    {
    }

    outportb(CONTROL, 0x0);

    return 0;
    }

    Thank you for your help. Oh, and the DAS-08 reference manual, in case it might help, is at: http://www.measurementcomputing.com/.../cio-das08.pdf

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > 2) What does 0x80 and 0x0 mean?

    0x80 = 1000 0000
    0x0 = 0000 0000

    Basically, by sending those values to the ports, you're activating that pin (sending it high). In this case, the 7th bit apparently corresponds to the 10th pin.

    When you send 0x80 out, you turn on pin 10 and all the others off, and 0x0 sends all the pins low, including 10.

    Your code looks fine, from what I can gather.

  5. #5
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    check to see if outportb() is a defined function in the more complex code... if so, you cannot utilize it as such. You also don't need all of those libraries.
    Blue

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > You also don't need all of those libraries.

    Didn't look at that...

    Also - cout is not in any of the libs you listed there....

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Arrow Revised program

    Thank you for the tips. Here is what the program looks like now. I am getting errors on the 'outportb' command. I checked all the contents of all the included files in the sample code I was given, and none made any mention of outportb, so I'm wondering where this command came from.

    Is there an alternate way of writing a value to an address? (as I think thats all that outportb is doing)

    #include <iostream> // Needed for output to screen and input from keyboard
    #include <conio.h> // Needed for getch function
    #define CONTROL 0x302;

    using namespace std; // Use in conjunction with "#include <iostream>" to allow using certain commands

    int main()
    {
    outportb(CONTROL, 0x80);

    cout << "Pin #10 should be at +5V.\n";
    cout << "Hit 'x' to stop signal.\n";

    while (getch()!='x')
    {
    }

    outportb(CONTROL, 0x0);

    return 0;
    }

  8. #8
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Where are you getting the outportb() function?

    Another way to do this since you do not require read and write is to open the com device as a serial input file. Examples of doing this can be found all over these boards in relation to COM Ports which is serial coms.

    You would need to open a HEX address instead of comport name, unless it is a standard com port like com4, com5, etc.

    Post the outportb() function...
    Blue

  9. #9
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    oh and use code tags

    Code:
    [ code ]
       Your code...
    [ /code ]
    
                but with no spaces in the brackets
    Blue

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    The outportb function

    The outportb function was apparently from a really old version of Borland C. The updated function is _outp.

    But getting _outp to work is causing some problems...

    I replaced outportb with _outp on lines 9 and 18 like this:

    _outp(CONTROL, 0x80);

    And gives me the following errors:

    E:\SetPin10High.cpp(9) : error C2143: syntax error : missing ')' before ';'
    E:\SetPin10High.cpp(9) : error C2660: '_outp' : function does not take 1 parameters
    E:\SetPin10High.cpp(9) : error C2143: syntax error : missing ';' before ','
    E:\SetPin10High.cpp(9) : error C2059: syntax error : ')'
    E:\SetPin10High.cpp(18) : error C2143: syntax error : missing ')' before ';'
    E:\SetPin10High.cpp(18) : error C2660: '_outp' : function does not take 1 parameters
    E:\SetPin10High.cpp(18) : error C2143: syntax error : missing ';' before ','
    E:\SetPin10High.cpp(18) : error C2059: syntax error : ')'
    Error executing cl.exe.

    SetPin10High.exe - 8 error(s), 0 warning(s)

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Getting closer...

    Okay, apparently I had an extra semicolon after the #define statement. Now the program runs with no errors/warnings on compiling, but throws up an application error window when trying to execute the _outp statement. It says that the 'exception priviledged instruction 0xc0000096 occured in the application at the location 0x00408bab. I have no idea what this means. I tried //'ing the two _outp statements and the program runs straight through with no errors.

    Does anyone know what the problem is?

    Here's what the code looks like now:

    Code:
    #include <iostream> // Needed for output to screen and input from keyboard
    #include <conio.h> // Needed for getch function
    #define CONTROL 0x302
    
    using namespace std; // Use in conjunction with "#include <iostream>" to allow using certain commands
    
    int main()
    {
    	_outp(CONTROL, 0x80); // Sends 10000000 to 0x302, turning pin #10 to high 
    
    	cout << "Pin #10 should be at +5V.\n";
    	cout << "Hit 'x' to stop signal.\n";
    
    	while (getch()!='x')
    	{
    	}
    
    	_outp(CONTROL, 0x0); // Sets pin #10 back to low
    
    	return 0;
    }

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Win2K was the problem

    In case anyone runs into the same problem I did, I thought I'd keep this thread alive and post an update. Turns out my problem was not so much the code, as it was the fact that I'm running Win2K. Seems that 2K will not let you access the ports without some special software. In my case, it is apparently the Universal Library from Measurement Computing. Whether or not this works, well, we'll find out when the CD arrives this afternoon. I will post the code when (and if) I finally get this to work.

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Ah... the answer

    In keeping with replying with my own threads, here's the answer:

    If you're trying to access ports with Win2k or NT, buy the Universal Library CD from Measurement Computing. It cost me $85 and its extremely easy to use. There are a gazillion sample programs on there that do exactly what they're supposed to do. If you get a funny linking error (well, its not that funny), like it can't seem to link any of the commands, in Visual C++ go to that window thats always on the left, and go to the Files tab, and right click on your program, select Settings. Then a window will pop up, go to the Link tab, and there's a line there that has all the library files that you're trying to link to. Make sure that "../cbw.h" is in that list. (And of course, make sure that the file cbw.h is located up one level than your source code.

    If you try to do this without the Universal Library, and you're not a programming genius, I think you're probably getting in way over your head. I'm a mechanical engineer. Programming is something I know just a little about. I can write little programs that manipulate arrays, if-do-while loops, out to file, in from files, that sorta stuff. Anything beyond that confuses the hell out of me. Buying the disk was way easier than trying to mess around with exception errors and port access any longer.

    If you're trying to do it with Win95 or DOS or something else, there's really no trick to it. You do have to configure the port for input, but all this is very simple and is fully documented in the DAS-08 manual which I believe I put a link to earlier. (up a few messages).

    Hope this helps someone out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. I need help on a formula for this simple program.
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2002, 10:01 PM