/*
File : bp2595demo.c
Function : voltage ramp demo for CPS model BP2595 power supply
Platform : Windows (console app)
Author : Jim Lamberson
Copyright : (C) CPS, All Rights Reserved.
This file is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
The API is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with the API.
If not, see .
*/
#include
#include
#include "apiBP2595.h"
// Forward declarations
void rampHv(int devaddr, int targ_v, int step_v, int step_ms, int show_meters);
// Storage
static BP2595_STATUS status; // BP2595 status
/////////////////////////////////////////////////////////////////////////////////////
// Main function -- Ramp HV output to +10 kV at 1 kV/s.
int main(int argc, char *argv[])
{
int devaddr = 0; // Address of BP2595 we want to talk to
int devflags; // Detected devices
BP2595_ERR errcode; // Error code returned from API functions
errcode = BP2595_OpenApi(&devflags); // Open API
if (errcode != BP2595_ERR_OK) {
printf("Error: BP2595_OpenApi() returned %d\n", errcode);
return 0;
}
if ((devflags & (1 << devaddr)) == 0) {
printf("Error: device not detected at address %d\n", devaddr);
return 0;
}
errcode = BP2595_OpenDevice(devaddr, &status); // Open BP2595
if (errcode == BP2595_ERR_OK) {
rampHv(devaddr, // Ramp HV output from currently programmed value to +10 kV at 1 kV/s rate (step 200 V every 200 ms):
10000, // ending voltage = +10 kV
200, // voltage step size = 200 V
200, // step interval = 200 ms
0); // don't show meter data
BP2595_CloseDevice(devaddr); // Close BP2595
}
else {
printf("Error: %s\n", BP2595_ErrString(errcode));
}
BP2595_CloseApi(); // Close API
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////
// Ramp the HV output.
// This function uses periodic BP2595 meter reports to pace the voltage ramp.
// This method provides more accurate timing than Sleep() because it doesn't
// accumulate timing errors (due to variable execution time of API functions).
// Arguments:
// devaddr - device address
// targ_v - ending voltage upon ramp completion
// step_v - voltage change per ramp step
// step_ms - number of milliseconds per ramp step
// show_meters - boolean: true = display meters every step; false = don't display meters
void rampHv(int devaddr, int targ_v, int step_v, int step_ms, int show_meters)
{
int setpoint; // Programmed output voltage
int ramp_up; // Boolean: true = ramp up; false = ramp down
BP2595_ERR errcode; // Error code returned from API functions
BP2595_METERS meters; // Meter data
if ((status.StatusSystem.Live & STATE_REMOTE) == 0) { // Abort if BP2595 is operating in local (front panel) control mode
printf("error: BP2595 remote control is disabled in local mode\n");
return;
}
if (status.StatusSystem.Live & STATE_HVEN) { // If HV output is enabled
setpoint = status.SetpointV; // start ramp at currently programmed output voltage.
}
else { // Else
setpoint = 0; // Start ramp at 0 V.
if (status.SetpointV != 0) { // If output is not currently programmed to 0 V
errcode = BP2595_ProgramSetpoint(devaddr, setpoint); // program the output to 0 V before enabling HV out.
if (errcode != BP2595_ERR_OK) {
printf("BP2595_ProgramSetpoint() error: %s\n", BP2595_ErrString(errcode));
return;
}
}
errcode = BP2595_ProgramHvEnable(devaddr, 1); // Enable HV output.
if (errcode != BP2595_ERR_OK) {
printf("Can't enable HV output: %s\n", BP2595_ErrString(errcode));
return;
}
}
errcode = BP2595_SetMeterInterval(devaddr, step_ms); // Start the meter stream running (tell BP2595 to report meter data every step_ms milliseconds).
if (errcode != BP2595_ERR_OK) {
printf("Can't start the meter stream: %s\n", BP2595_ErrString(errcode));
return;
}
ramp_up = (targ_v > setpoint); // calculate ramp direction
if (step_v < 0) // use step_v magnitude
step_v = -step_v;
while (setpoint != targ_v) // Ramp the HV output -----------------------------------------------------------
{
errcode = BP2595_GetMeterUpdate(devaddr, // Wait for next meter report, which signals that it's time for a ramp step:
&meters, // receive meter data in this buffer
step_ms + 500); // allow an extra 500 ms grace period before timeout, in case the system is very busy
if (errcode != BP2595_ERR_OK) {
printf("BP2595_GetMeterUpdate() error: %s\n", BP2595_ErrString(errcode));
break;
}
if (show_meters)
printf("setpoint = %d; meters = %d V, %d uA\n", setpoint, meters.outV, meters.outUA); // display output voltage and current
if (ramp_up) { // Bump setpoint for next step.
setpoint += step_v;
if (setpoint > targ_v)
setpoint = targ_v;
}
else {
setpoint -= step_v;
if (setpoint < targ_v)
setpoint = targ_v;
}
errcode = BP2595_ProgramSetpoint(devaddr, setpoint); // Program the output voltage.
if (errcode != BP2595_ERR_OK) {
printf("BP2595_ProgramSetpoint() error: %s\n", BP2595_ErrString(errcode));
break;
}
}
BP2595_SetMeterInterval(devaddr, 0); // Stop meter stream.
}