我在使用C代码时遇到了一些麻烦。我有一个ADC,用于确定是否关闭(跳闸区)我正在使用的PWM。但是我的计算似乎不能按预期工作,因为ADC会在错误的电压电平下关闭PWM。我开始我的变量:用C语言计算C2000设备
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
}
所以我想跳闸PWM如果当前高于9或低于1,这应该分别与< 273(0x111)和> 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;' – pmg
'如果电流高于9或低于1,哪里是单位? “安培”或“毫安”还是什么? – SGG
pmg - 只要我想改变它们,就可以更容易地设置限制。如果不先把它们转换成放大器,样品不会告诉我任何东西。 SGG - 单位是安培。 1/1365是V/sample,-3.462是A/V – user3514815