Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

pwm

Dear Sirs:
 
Please, Firs,I don't know If it is  the adequete  forum    but  I   expect here  I can  get   some hint   about    my   question.....I have a problem and  don't know if this have solution  with only one PCI  6602 card.
I  need to  make "simultaneous" tasks such  as   quadrature encoder reading, PWM   generation with
variable duty cycle ,  and also I need   to  write   and  read  from digital I/O 
lines.

I  wrote  the  following    code   ,  but as I indicate, I can't  get   the things  works as  I expect.
First  I get  the PWM works,but then when I start the  task for reading I/O  lines the PWM generation stops.
So what I must to have present about working with many tasks in the PCI6602  card??
Thank you  for  your  replies.
 
Regards
 
My code is  here:
 
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <conio.h>
#include "NIDAQmx.h"
#pragma link "nidaqmx.lib"
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) FncError(); else
#define  DAQmxErrChkOut(functionCall) if( DAQmxFailed(error=(functionCall)) ) FncErrorOut(); else
#define  DAQmxErrChkIn(functionCall) if( DAQmxFailed(error=(functionCall)) ) FncErrorIn(); else
char        errBuff[2048]={'\0'};
int  error=0;
TaskHandle taskHandlePWM=0,taskHandleOut=0,readhandlez=0;
void FncErrorIn(){
        if( DAQmxFailed(error) )
  DAQmxGetExtendedErrorInfo(errBuff,2048);
        if( readhandlez!=0 ) {
   }
 if( DAQmxFailed(error) )
 printf("%s",errBuff);
  }
void FncErrorOut(){
        if( DAQmxFailed(error) )
  DAQmxGetExtendedErrorInfo(errBuff,2048);
        if( taskHandleOut!=0 ) {
 }
 if( DAQmxFailed(error) )
 printf("%s",errBuff);
}
void FncError(){ 
        if( DAQmxFailed(error) )
  DAQmxGetExtendedErrorInfo(errBuff,2048);
        if( taskHandlePWM!=0 ) {
 }
 if( DAQmxFailed(error) )
 printf("%s",errBuff);
}
int main(int argc, char* argv[])
{
//  uInt8       palabra_enable[3]={0,1,1};
  char K=0,i;
  int32 read;
  int32 bytesPerSamp;
  uInt8 datain[8]={0,0,0,0,0,0,0,0};
  DAQmxErrChk (DAQmxCreateTask("",&taskHandlePWM));
  DAQmxErrChkIn(DAQmxCreateTask("",&readhandlez));
//--------------------------------------------------
 DAQmxErrChk (  DAQmxCreateCOPulseChanFreq (taskHandlePWM, "Dev1/ctr2","",DAQmx_Val_Hz , DAQmx_Val_High, 0, 1, 0.9));
 DAQmxErrChk (DAQmxCfgImplicitTiming(taskHandlePWM,DAQmx_Val_ContSamps,1000));
 DAQmxErrChk ( DAQmxStartTask(taskHandlePWM));
 
 // I CHANGE THE DutyCycle. TO 10%
 
 DAQmxErrChk(DAQmxWriteCtrFreqScalar(taskHandlePWM, true, DAQmx_Val_WaitInfinitely, 1, 0.1, NULL));
 
 //WELL...up to this point  the PULSE TRAIN is being generated......I SEE THE LED IS OSCILLATING AS I EXPECT IT.
 getche();
 
 //-------------------------------------------
 //BUT FROM HERE  THE LED where I AM SEEING THE PULSE TRAIN TURNS OFF...THE PULSE TRAIN
 //IS NO MORE BEING GENERATED...WHY??? and HOW TO SOLVE IT?
 
 
//DAQmxErrChkIn( DAQmxCreateDIChan (readhandlez, "Dev1/port0/line4:5", "", DAQmx_Val_ChanForAllLines));
DAQmxErrChkIn( DAQmxCreateDIChan (readhandlez, "Dev1/port0/line4:5", "", DAQmx_Val_ChanPerLine));
DAQmxErrChkIn( DAQmxStartTask(readhandlez));
DAQmxErrChkIn( DAQmxReadDigitalLines (readhandlez, 1, 10.0, DAQmx_Val_GroupByChannel , datain, 8, &read, &bytesPerSamp, NULL));
while(!kbhit()){
  DAQmxErrChkIn(DAQmxReadDigitalLines (readhandlez, 1, 10.0, DAQmx_Val_GroupByChannel , datain, 8, &read, &bytesPerSamp, NULL));
  for(i=0;i<=7;i++){
     gotoxy(10+i,10);
     printf("%d",datain[7-i]); //<-----I PRINT ON THE SCREEN THE READ BITS, THIS WORKS FINE
  }
 }
 getch();
 // I STOP THE TASKS
 DAQmxStopTask(taskHandlePWM);
 DAQmxClearTask(taskHandlePWM);
 DAQmxStopTask(readhandlez);
 DAQmxClearTask(readhandlez);
 }
//---------------------------------------------------------------------------
0 Kudos
Message 1 of 3
(3,810 Views)
Hi efreyre,

You can do it with only one 6602. You need to create tree separate task. I take a look at your code and I have some suggestions to you. It can help you developing your application:

1 - Use subroutines to organize the code. You are creating a number of tasks and it can make the code difficult to read.
2 - Create and start all tasks in the beginning of the application. After all task were started, you can read/write to/from them.

To test your specific problem, I would create two separate programs. One doing PWM generation and another reading the digital lines. After testing the two programs separated, you can run the two programs and figure out what happen.

Another troubleshoot step is to use MAX to test your board. You probably has MAX installed (if you are using Windows). You only need to start MAX (Measurement and Automation Explorer) going to Start>> All Programs>> National Instruments>> Measurement and Automation. In MAX, go to My System>> Devices and Interfaces>> NI-DAQmx Devices>> PCI-6602. Right click the board and select Test Panels...

With MAX Test Panels you can generate a digital signal and read from the Digital lines. It would assure that it isn't a hardware issue.

Best Regards,
Alisson Kokot
NI BRAZIL
0 Kudos
Message 2 of 3
(3,675 Views)

Hello

Thread has been out for a while, yet as I read, I remembered some things about order of starting tasks on counter cards. You have to make sure the digital task is started prior to the counter out because some of the lines are shared among ctr and DIO. When initializing counters first, subsequent DIO tasks may overwrite Counter allocations.

0 Kudos
Message 3 of 3
(2,828 Views)