typedef struct { int16 ImeasAGain; // Parameter: gain for Ia (Q13) int16 ImeasAOffset; // Parameter: offset for Ia (Q15) int16 ImeasA; // Output: measured Ia (Q15) int16 ImeasBGain; // Parameter: gain for Ib (Q13) int16 ImeasBOffset; // Parameter: offset for Ib (Q15) int16 ImeasB; // Output: measured Ib (Q15) int16 VdcMeasGain; // Parameter: gain for Vdc (Q13) int16 VdcMeasOffset; // Parameter: offset for Vdc (Q15) int16 VdcMeas; // Output: measured Vdc (Q15) int16 ImeasC; // Output: computed Ic (Q15) Uint16 ChSelect; // Parameter: ADC channel selection
void (*init)(); // Pointer to the init function void (*read)(); // Pointer to the read function } ILEG2DCBUSMEAS;
void F281X_ileg2_dcbus_drv_read(ILEG2DCBUSMEAS *p)
{
int16 DatQ15;
int32 Tmp;
// Wait until ADC conversion is completed
while (AdcRegs.ADCST.bit.SEQ1_BSY == 1)
{};
DatQ15 = AdcRegs.ADCRESULT0^0x8000; // Convert raw result to Q15 (bipolar signal)
Tmp = (int32)p->ImeasAGain*(int32)DatQ15; // Tmp = gain*dat => Q28 = Q13*Q15
p->ImeasA = (int16)(Tmp>>13); // Convert Q28 to Q15
p->ImeasA += p->ImeasAOffset; // Add offset
p->ImeasA *= -1; // Positive direction, current flows to motor
DatQ15 = AdcRegs.ADCRESULT1^0x8000; // Convert raw result to Q15 (bipolar signal)
Tmp = (int32)p->ImeasBGain*(int32)DatQ15; // Tmp = gain*dat => Q28 = Q13*Q15
p->ImeasB = (int16)(Tmp>>13); // Convert Q28 to Q15
p->ImeasB += p->ImeasBOffset; // Add offset
p->ImeasB *= -1; // Positive direction, current flows to motor
DatQ15 = (AdcRegs.ADCRESULT2>>1)&0x7FFF; // Convert raw result to Q15 (unipolar signal)
Tmp = (int32)p->VdcMeasGain*(int32)DatQ15; // Tmp = gain*dat => Q28 = Q13*Q15
if (Tmp > 0x0FFFFFFF) // Limit Tmp to 1.0 in Q28
Tmp = 0x0FFFFFFF;
p->VdcMeas = (int16)(Tmp>>13); // Convert Q28 to Q15
p->VdcMeas += p->VdcMeasOffset; // Add offset
p->ImeasC = -(p->ImeasA + p->ImeasB); // Compute phase-c current
AdcRegs.ADCTRL2.all |= 0x4040; // Reset the sequence
}
上面红字部分,为什么要让得到的结果与Q13格式组合成Q28,然后在转换成Q15,添加了这一部分,有什么作用呢?