【发布时间】:2014-05-22 12:57:38
【问题描述】:
我的 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,这应该分别与 3428 (0xD64) 的 adc 结果一致。 ADC 值分别对应于电压 0.2V 和 2.51V。 ADC 在电压 0 和 3V 之间以 12 位精度进行测量。
但是,事实并非如此。相反,跳闸区在大约 1V 和 2.97V 时关闭。那我做错了什么?
【问题讨论】:
-
为什么不直接使用
adc_info->adc_result0呢?if ((273.0 <= adc_info->adc_result0) && (adc_info->adc_result0 <= 3428.0)) shutdown = 0; else shutdown = 1; -
if current is above 9 or below 1 which单位在哪里?Amps或milli amps还是什么? -
pmg - 如果我想更改限制,设置限制会更容易。如果不先将它们转换为安培,这些样本不会告诉我任何事情。 SGG - 单位是安培。 1/1365 是 V/样本,-3.462 是 A/V