For a hardware evaluation project I'm working on, I want to create a device that can be controlled via LabVIEW.
LabVIEW can talk to instruments using serial out of the box, and it knows how to talk Standard Commands for Programmable Instruments (SCPI).

 

scpi PWM On

 

In this blog I build the first hardware command to turn PWM signals on and off.

 

 

One Function Handles 7 PWM Modules

The first command isn't the easiest one. We'll build a function that can talk to any of 7 available PWM modules of the Hercules controller.

SCPI has a construct that allows you to provide a wildcard in the command. That wildcard can be replaced by a number when calling the command from LabVIEW.

 

    {.pattern = "PWM#:STATe", .callback = SCPI_HerculesPwmState,},

 

When you shoot a command where the # is substituded by a number  (i.e.: PWM1:STATe ON), the SCPI_HerculesPwmState() function gets called.

The substituted number is available in that function by calling the API SCPI_CommandNumbers().

With that number, I retrieve the correct PWM hardware register and switch the signal on or off.

 

PWM on off via USB

 

These are the SCPI test calls:

PWM1:STATe ON
PWM1:STATe OFF

 

This is the function that gets called. Don't worry about the magical numbers that are assigned to the PWM register. I'll improve the code while I design the firmware.

param1 holds true if you pass ON or 1 in SCPI command. false if you give OFF or 0.

 

// array of the 7 PWM registers of the Hercules RM46 controller
etpwmBASE_t *pwmRegs[7] ={etpwmREG1, etpwmREG2, etpwmREG3, etpwmREG4, etpwmREG5, etpwmREG6, etpwmREG7} ;

static scpi_result_t SCPI_HerculesPwmState(scpi_t * context) {

    int32_t numbers[1];
    scpi_bool_t param1;
    etpwmBASE_t *pwmReg = NULL;

    // retrieve the PWM channel. Can be 1 - 7
    SCPI_CommandNumbers(context, numbers, 1, 1);

    // todo error if the PWM CHANNEL not between 0 and 7.
    // code below will fail because we'll read invalid memory locations
    pwmReg = pwmRegs[numbers[0]-1];

     /* read first parameter if present */
     if (!SCPI_ParamBool(context, &param1, TRUE)) {
         return SCPI_RES_ERR;
     }
     if (param1) { // switch PWM on
         pwmReg -> TBCTL = ETPWM1_TBCTL_CONFIGVALUE;
     } else { // switch PWM off
         pwmReg -> TBCTL = 131;
     }

    return SCPI_RES_OK;
}

 

That substitution is a powerful construct. With little effort I can control multiple similar modules.

The SCPI library has all the logic that's needed to support this. I just have to use it properly.

Once more it didn't take a lot of code to build flexible functionality.

 

I've attached the CCS project to this blog.

Compile, load to your RM46 LaunchPad and connect with a serial terminal program using 9600, 8, 2, N

 

Put an oscilloscope probe on J11 16 and 18. You'll find two complementary 1MHz PWM signals.

Submit these commands in putty to switch them off and back on:

 

 

 

PWM1:STATe OFF
PWM1:STATe ON

 

In the next blog I'll attach a LabVIEW example that switches the signals.

 

Related Blog

Create a Programmable Instrument with SCPI - Part 1: Parser Library
Create a Programmable Instrument with SCPI - Part 2: Serial over USB
Create a Programmable Instrument with SCPI - Part 3: First Conversation *IDN?
Create a Programmable Instrument with SCPI - Part 4: Error Handling by Default
Create a Programmable Instrument with SCPI - Part 5: First Hardware Commands
Create a Programmable Instrument with SCPI - Part 6: LabVIEW Integration
Create a Programmable Instrument with SCPI - Part 7: Talk to Hardware Registers