我的 C 代码有问题。我有一个 ADC,它将用于确定是否关闭(跳闸区)我正在使用的 PWM。但我的计算似乎没有按预期工作,因为 ADC 在错误的电压电平下关闭了 PWM。我将变量初始化为:
float32 current = 5;
Uint16 shutdown = 0;
然后我计算为:
// Save the ADC input to variable
adc_info->adc_result0 = AdcRegs.ADCRESULT0>>4; //bit shift 4 steps because adcresult0 is effectively a 12-bit register not 16-bit, ADCRESULT0 defined as Uint16
current = -3.462*((adc_info->adc_result0/1365) - 2.8);
// Evaluate if too high or too low
if(current > 9 || current < 1)
{
shutdown = 1;
}
else
{
shutdown = 0;
}
之后我使用这个 if 语句:
if(shutdown == 1)
{
EALLOW; // EALLOW protected register
EPwm1Regs.TZFRC.bit.OST = 1; // Force a one-shot trip-zone interrupt
EDIS; // end write to EALLOW protected register
}
因此,如果电流高于 9 或低于 1,我想触发 PWM,这应该分别与 <273 (0x111) 和 >3428 (0xD64) 的 adc 结果一致。ADC 值分别对应于电压 0.2V 和 2.51V。ADC 在电压 0 和 3V 之间以 12 位精度进行测量。
然而,这种情况并非如此。相反,跳闸区在大约 1V 和 2.97V 时关闭。那么我做错了什么?