Thread: Drawing waveform using turbo c not prefect!!

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    13

    Drawing waveform using turbo c not prefect!!

    Hello,
    I am trying to draw a wave form using turbo c .. I am reading voltage input using an ADC card and plotting the voltage from -10 to +10 .. the code works fine, but the line of program drawn goes out of the borders vertical axis if I change the parameters? I was wondering if there is a way to limit the vertical line drawn so it doesn't go beyond the the table setup by the program? I don't have a problem with the horizontal axes.
    here is the full code that I wrote:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <graphics.h>
    
    #define DAQ1 0x0300  //Channel 1 ADC card
    
    //******************//
    // Global variables //
    //******************//
    
    int graphdriver = DETECT,
        graphmode,
        linein,dev,xaxis,yaxis,key,time;
    
    unsigned int data,data1,data2;
    float Constant = 546.133,zeroline=190;
    
    //***************//
    //     functions   //
    //***************//
    void Display (void)
    {
    cleardevice();
       
       setcolor(14);
       outtextxy(20, 260, "Please use the following keys:");
       setcolor(RED);
       outtextxy(30, 270, "Channel 1");
       outtextxy(30, 280, "Q=5    V/Div");
       outtextxy(30, 290, "W=2    V/Div");
       outtextxy(30, 300, "E=1    V/Div");
       outtextxy(30, 310, "R=0.5  V/Div");
       outtextxy(30, 320, "T=0.2  V/Div");
       outtextxy(30, 330, "Y=0.1  V/Div");
    
       settextstyle(0,0,0);
       setcolor(8);
       outtextxy(5,460, "Press Esc to exit");
       outtextxy(330, 460, "Copyright (C) 2011 Mohammed Almualla");
    
    for(dev=50;dev<530;dev=dev+6)
    	{
           setcolor(8);
    	line(dev,10,dev,250);
    	}
    
    for(dev=10;dev<255;dev=dev+6)
    	{
    	setcolor(8);
    	line(50,dev,530,dev);
    	}
    
    
    setcolor(GREEN);
    for(yaxis=10; yaxis<260; yaxis=yaxis+30)
    	{
    	line(50,yaxis,530,yaxis);
    	}
    for(xaxis=50; xaxis<550; xaxis=xaxis+30)
    	{
    	line(xaxis,10,xaxis,250);
    	}
    }     
    
    void waveform (void)
    {
    float SaveData;
    
    outport(DAQ1);
    delay(1);
    data=inport(DAQ1);
    
    SaveData =data/Constant;
    data2=zeroline- SaveData;
    
    setcolor(YELLOW);
    for(linein=50; linein<541; linein++)
    	{
    	outport(DAQ1);
    	delay(1);
    	data=inport(DAQ1);
    	
    	SaveData =data/Constant;
    	data2=zeroline- SaveData;
    
    	line(linein,data1,linein,data2);
    
    	data1=data2;
    	}                        
    }
    
    //***************//
    //  main Program //
    //***************//
    void main (void)
    {
    initgraph(&graphdriver,&graphmode,"c:");
    
    for(;;)
    	{
    	if (!kbhit())
    		{
    		Display ();
    		waveform ();
    		}
    	else
    		{
    		key=getch();
    		switch(key)
    			{
    			case 'q':case 'Q':
    			Constant=546.133;
    			zeroline=190;
    			break;
    
    			case 'w':case 'W':
    			Constant=218.45;
    			zeroline=280;
    			break;
    
    			case 'e':case 'E':
    			Constant=109.22;
    			zeroline=430;
    			break;
    
    			case 'r':case 'R':
    			Constant=54.6125;
    			zeroline=730;
    			break;
    
    			case 't':case 'T':
    			Constant=21.8453;
    			zeroline=1630;
    			break;
    
    			case 'y':case 'Y':
    			Constant=10.9226;
    			zeroline=3130;
    			break;
    
    
    			case 27:
    				closegraph();
    				exit(0);
    				break;
    			}
    		}
    	if (key==27)
    	break;
    	}
    }
    can't figure out how to limit the vertical axes on the line!
    Last edited by Superm7; 05-08-2011 at 10:08 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's the point of using code tags, but not indenting?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    13
    I don't understand? what do you mean code tags, but not indenting? can you please explain?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void Display (void)
    {
    cleardevice();
    
    for(dev=50;dev<530;dev=dev+6)
    {
    setcolor( 8 ) ;
    line(dev,10,dev,250);
    }
    
    for(dev=10;dev<255;dev=dev+6)
    {
    setcolor( 8 ) ;
    line(50,dev,530,dev);
    }
    That's what you did.
    Code:
    void Display (void)
    {
        cleardevice();
    
        for(dev=50;dev<530;dev=dev+6)
        {
            setcolor( 8 ) ;
            line(dev,10,dev,250);
        }
    
        for(dev=10;dev<255;dev=dev+6)
        {
            setcolor( 8 ) ;
            line(50,dev,530,dev);
        }
    That is indented. See how purdy mine is and how ugly yours is?

    Also, you have a whole slew of functions you aren't showing us here (such as how line actually works).


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    13
    yes I see it .. I'm sorry about that I will fix it now.

    this is all the functions in my program and the line function is actually a built-in function in turbo-c so I don't need to create a function for that.

    I hope this answers your question?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not really, since it doesn't tell us anything about what the arguments are. If you want say, 6 vertical steps max, drawing a vertical diagonal, then you need to make sure your math works in six steps.
    Code:
    int line( int startx, int starty, int endx, int endy, int vsteps, int hsteps )
    {
        int width = endx - startx,
            height = endy - starty,
            row = 0, col = 0;
        for( width /= vsteps, row = starty; row < endy; row += width )
            for( height /= hsteps, col = startx; col < endx; col += height )
            {
                gotoxy( col, row );
                putc( 'x' );
            }
    }
    Or something like that. It's the general idea, I might have mixed up a couple of axes in the middle of that, but that's the general idea of what you want to do. Or at least that's what I think you are trying to do.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    13
    I see your point, but I an trying to draw a line (wave form) emulating an oscilloscope on the screen and I'm reading my input from the ADC card on the PC and I want to limit the line drawn vertically if I change the oscilloscope setting say from 5 volts per devision to 1 volts per devision? I hope this gives you a more clear idea.
    Last edited by Superm7; 05-07-2011 at 09:54 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Is DOS your real operating system?

    If you're running this TurbidCrap inside the DOS emulation of windows, then you're only going to have access to emulated I/O ports as well. Now whether those ports are just faked, or mapped to the real ports under supervision of the real OS is likely to be a guess at this point. It would certainly require a lot more research to make sure the emulation was capable of doing what you want.

    The next huge problem is this
    > #define DAQ1 0x0000 //Channel 1 from ADC card
    Ralf Brown's Files
    According to this information, port 0 is channel 0 of the first DMA controller.

    > outport(DAQ1);
    I thought the outport functions had two parameters, the second being the value you want to output on the port.



    The much better alternative is to just put the crap in the bin, join the rest of the world in this millennium. and get a real compiler for your real OS.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    13
    Yes Dos is my real operating system. I'm not running in an emulator of any sort. I am using a turbo c compiler.

    about the > #define DAQ1 0x0000 //Channel 1 from ADC card. I am just showing it as an example to assign the address of the ADC card to Data Card(DAQ1).

    about the > outport(DAQ1);
    You are right. In order to read data from the card you have to initialize it first by sending one byte, delay and then you read the input.

    outport(DAQ1);
    delay(1);
    data=inport(DAQ1);

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Yes Dos is my real operating system.
    OK, so your compiler choice is at least a sensible one then. 99% of people who come here using TC are running it on top of XP or later.

    You should really find out what port(s) your ADC card really reside.
    Poking around at random in the I/O space of a PC is a good way of crashing the whole machine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    13
    As I said before I am just using it as example here. I know the port I am using its 0x0300, so its (#define DAQ1 0x0300), but I don't think that will make a difference. The program is working fine .. I just want to limit the line drawn vertically so it doean't go beyond a certain pixel. In this case beyond 260 and 530. Just wondering if someone have an idea.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Before you call your line function check the value of the variable, if it is higher than the maximum allowed change the value to the maximum allowed.


    Jim

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It makes a difference, because you're posting only something which "looks like your code" as opposed to being "your actual code". Otherwise, we all spend a few wasted days going over things which have no relevance to your actual problem.

    Like the outport() thing, which also looks paraphrased.
    IIRC, the names of the routines were outportb() and outportw(), depending on whether you were outputting a byte or a word.

    TBH, if your next post isn't your actual code, with corrected outport() calls, then I'll probably just ignore you as being too much effort
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Superm7 View Post
    I see your point, but I an trying to draw a line (wave form) emulating an oscilloscope on the screen and I'm reading my input from the ADC card on the PC and I want to limit the line drawn vertically if I change the oscilloscope setting say from 5 volts per devision to 1 volts per devision? I hope this gives you a more clear idea.
    If you are changing the scale of the aperature then you need to scale your line drawing as well...

    Deviation = measurement * (1/scale)

    that is... on 1 volt per graticule 6v * 1/1 = 6 graticules ...
    on 5 volts per graticule 6v * 1/5 = 1.2 graticules

    (If you're designing an oscilloscope I take it you understand deviation, aperatures and graticules.)

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Like the outport() thing, which also looks paraphrased.
    Actually Borland had the functions/macros outp, outport, outportb, and outw. With outp, and putw defined in conio.h and outport, outportb defined in dos.h. All of these functions/macros take 2 arguments.

    However I agree totally that the OP should be posting the actual code.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ sound and waveform functions
    By markphaser in forum C++ Programming
    Replies: 14
    Last Post: 02-17-2006, 05:48 PM
  2. Drawing Line - turbo C
    By loko in forum C Programming
    Replies: 4
    Last Post: 07-06-2005, 04:21 PM
  3. Display waveform in window
    By lwong in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2003, 12:07 AM
  4. Turbo C++
    By pete777 in forum C++ Programming
    Replies: 5
    Last Post: 02-18-2002, 03:29 PM
  5. Turbo C
    By {Dnw} in forum C Programming
    Replies: 19
    Last Post: 08-18-2001, 05:30 PM