📄 ad_dacon.c
字号:
ssSetInputPortWidth(S, 0, NUM_DA_CHAN); // input: vector (..., port, width)
ssSetInputPortDirectFeedThrough(S, 0, 1); // direct feedthrough (only executs after update of inputs)
ssSetInputPortRequiredContiguous(S, 0, 1); // ports to be stored contiguously in memory
break;
default:
#ifndef MATLAB_MEX_FILE // Matlab MEX-file
abort_LED (27);
#endif
break;
}
----------------*/
ssSetNumSampleTimes (S, 1); // only 'one' sampletime in this S-Function
ssSetNumIWork (S, 0); // number of integer work vector elements
ssSetNumRWork (S, 0); // number of real work vector elements
ssSetNumPWork (S, 0); // number of pointer work vector elements
}
/* Function: mdlInitializeSampleTimes =========================================
*
*/
static void mdlInitializeSampleTimes (SimStruct *S)
{
ssSetSampleTime(S, 0, SAMPLE_TIME); // this S-Function only has 'one' sampletime -> index '0'
ssSetOffsetTime(S, 0, 0.0);
}
/* Function: mdlStart =========================================================
*
*/
#define MDL_START
static void mdlStart(SimStruct *S)
{
unsigned int i;
// no DA converter on the PhyCore167 (FW-06-01)
// Reset_DA (); /* Reset all D/A converters to 0 V */
// currently unused (only AD) -> BLOCK dependency disabled... (FW-07-01)
/*---------------
switch(BLOCK)
{
case AD_CONV:
----------------*/
#ifndef MATLAB_MEX_FILE
// reset ADstore memory
for(i=0; i<NUM_AD_CHAN; i++) ADstore[i] = 0;
// Initialize A/D converter:
if (NUM_AD_CHAN == 1)
ADCON = 0x0000; // channel 0: Single Channel Single Conversion
else
ADCON = 0x0020 + (NUM_AD_CHAN-1); // NUM_AD_CHAN-1...0: Auto Scan Single Conversion
ADCIC = 0x005C; // set interrupt priority (ILVL: 7, GLVL: 0), ADCIE = 1
ADST(1); // start A/D conversion
//#ifdef TIMING
// set P3.3 (timing -> scope)
//P3 |= 0x0008; // indicate 'A/D' started
//#endif
#endif
/*---------------
break;
case DA_CONV:
#ifndef MATLAB_MEX_FILE
// D/A initialisation code to be placed here...
#endif
break;
default:
#ifndef MATLAB_MEX_FILE
abort_LED (28);
#endif
break;
}
----------------*/
}
/*
* mdlOutputs - compute the outputs
*
* In this function, you compute the outputs of your S-function
* block. The outputs are placed in the y variable.
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
unsigned int i;
//unsigned int wert_da;
//static float outbit;
#ifndef MATLAB_MEX_FILE
// currently unused (only AD) -> BLOCK dependency disabled... (FW-07-01)
/*---------------
switch (BLOCK)
{
case AD_CONV:
----------------*/
// return current set of converted values
for (i = 0; i < NUM_AD_CHAN; i++)
*(real_T *)(&ssGetOutputPortRealSignal(S,0)[i])
= (real_T)((float)(ADstore[i])/AD_RESOLUTION * MAX_AD_VOLTAGE);
// restart A/D converter
ADST(1);
//#ifdef TIMING
// set P3.3 (timing -> scope)
//P3 |= 0x0008; // indicate: A/D started
//#endif
/*---------------
break;
case DA_CONV:
#define MAX_DA_VALUE MAX_DA_VOLTAGE*(DA_RESOLUTION-1)/(float)DA_RESOLUTION
for (i = 0; i < ssGetNumInputPorts(S); i++)
{
outbit = (float)(*(real_T *)&ssGetInputPortRealSignal(S,0)[i]);
// Limit the given D/A voltage to [0,MAX_DA_VOLTAGE) V:
if (outbit > MAX_DA_VALUE) outbit = (float)MAX_DA_VALUE;
if (outbit < 0) outbit = 0.0;
// Convert the limited voltage to a n-bit value for the D/A converter:
wert_da = (unsigned int)(outbit*DA_RESOLUTION/MAX_DA_VOLTAGE + 0.5) & (DA_RESOLUTION-1);
switch (i)
{
case 0:
EnableDA0_Ext(1); // Enable D/A Channel 0 (External)
break;
case 1:
EnableDA1_Ext(1); // Enable D/A Channel 0 (External)
break;
default:
abort_LED (29); // can not happen...
break;
}
// When accessing latch P7 (which is used for programming
// the D/A converter AD7847 of the kitCON-166 extension board)
// every value has to be written twice:
DA_Port = (wert_da | 0x1000<<i); // Activate WR and CSA or
DA_Port = (wert_da | 0x1000<<i); // CSB respectively
DA_Port = (wert_da | 0x4000 | 0x1000<<i); // Positive edge at WR =>
DA_Port = (wert_da | 0x4000 | 0x1000<<i); // Accept data at DA0/DA1
DA_Port = (wert_da | 0x7000); // Deactivate WR, CSA, and CSB
DA_Port = (wert_da | 0x7000);
}
break;
default:
abort_LED (29);
break;
}
----------------*/
#endif
}
/*
* mdlTerminate - called when the simulation is terminated.
*
* In this function, you should perform any actions that are necessary
* at the termination of a simulation. For example, if memory was allocated
* in mdlInitializeConditions, this is the place to free it.
*/
static void mdlTerminate(SimStruct *S)
{
// currently unused (only AD) -> BLOCK dependency disabled... (FW-07-01)
/*---------------
switch (BLOCK)
{
case AD_CONV:
----------------*/
#ifndef MATLAB_MEX_FILE
ADST(0); // Stop A/D converter
//#ifdef TIMING
// clear P3.3 (timing -> scope)
//P3 &= 0xFFF7;
//#endif
#endif
/*---------------
break;
case DA_CONV:
// Reset_DA(); // Reset all D/A converters to 0 V
break;
default:
#ifndef MATLAB_MEX_FILE
abort_LED (31);
#endif
break;
}
----------------*/
}
// the define 'MATLAB_MEX_FILE' has to be specified when recompiling this module to a DLL.
// this is only required if the format of the call-up parameters is modified... (FW-06-01)
#ifdef MATLAB_MEX_FILE
#include "simulink.c"
#else
#include "cg_sfun.h"
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -