我正在研究一個嵌入式系統項目,我嘗試解調來自IR傳感器(使用「脈寬調製」)的數據。下面的代碼只是等待紅外傳感器開始脈衝,然後測量每個高低脈衝的寬度(持續時間)。從0遞增整數變爲負數
在這段代碼中,我有一個循環,我遞增的整數
irPulseSet irReadPulse()
{
irPulseSet outputPulseSet;
int pulseCount = 0;
int finished = 0;
while(1)
{
int lowPulse = 0;
int highPulse = 0;
while(bit_is_clear(irSensorPin , irSensorPinNo))
{
highPulse++;
_delay_us(20);
}
while(!bit_is_clear(irSensorPin , irSensorPinNo))
{
if (lowPulse > 3250)
{
finished = 1;
break;
}
lowPulse++;
_delay_us(20);
}
if (finished)
break;
outputPulseSet.pulses[pulseCount][0] = highPulse * 20;
outputPulseSet.pulses[pulseCount][1] = lowPulse * 20;
pulseCount++;
}
// Assign pulse count to output pulse set
outputPulseSet.pulseCount = pulseCount;
return outputPulseSet;
}
因爲這是一個嵌入式系統項目,我的資源是有限的,我做我調試了一臺液晶顯示器(作爲電路不能連接到計算機)
打印每個脈衝
int irPrintPulses(irPulseSet pulseSet)
{
int counter;
for(counter = 0; counter <= pulseSet.pulseCount; counter++)
{
LCDClearScreen();
char printStr[100];
sprintf(printStr , "H%iL%i;%i " , pulseSet.pulses[counter][0] , pulseSet.pulses[counter][1] , counter );
LCDSendString(printStr);
_delay_ms(1000);
}
_delay_ms(5000);
LCDClearScreen();
LCDSendString("Finished pulse print!");
_delay_ms(1000);
LCDClearScreen();
return 0;
}
的lowPulse
INT似乎有時是否定的(例如g值爲-1054
)。我完全感到困惑,因爲它首先被定義爲0
,我所做的所有事情都是增加它,那它怎麼會變成負值?
如果您將'highPulse'和'lowPulse'重新聲明爲局部變量,它有什麼區別嗎?他們的類型是什麼? –
@StephenC它們是'int'類型的,我剛剛嘗試過在本地聲明,它仍然變爲負數。 – user2166351
你怎麼知道lowPulse是一個負值?你能發佈整個代碼嗎? –