[英]for loop doesnt read my if statement
我試圖通過光傳感器打開 LED。 但是每次我嘗試運行它時,光傳感器都可以工作,但它不會超過“printf(Voltage..)”這一行。它沒有到達 if 循環。這是為什么?我在每個 if 語句中添加了 printf,所以我知道我可以到達那條線。
int doADCConversion()
{
ADC0->SC1[0] = ADC_SC1_ADCH(19);
while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) == 0)
{
printf("Conversion in progress..\n");
}
return ADC0->R[0];
}
for(;;)
{
temp = doADCConversion();
printf("Voltage = %d\n", temp);
if(temp < 50){
printf("Turn on LED 1/n");
setLED(0);
delay();
}
else if (temp < 100){
printf("Turn on LED 2/n");
setLED(1);
delay();
}
else if (temp < 150){
printf("Turn on LED 3/n");
setLED(2);
delay();
}
else if (temp < 200){
printf("Turn on LED 4/n");
setLED(3);
delay();
}
else if (temp < 250){
printf("Turn on LED 5/n");
setLED(4);
delay();
}
else {
setLED(5);
printf("Turn on LED 6/n");
delay();
}
}
我懷疑當你用"/n"
而不是換行符"\\n"
終止字符串時, printf
輸出保持緩沖。
為什么不簡化它
#define min(a, b) (a < b ? a : b)
while (1) {
temp = doADCConversion();
printf("Voltage = %d\n", temp);
temp = min(5, temp / 50);
printf("Turn on LED %d\n", temp + 1); // newline is `\n`
setLED(temp);
delay();
}
注意:轉義序列與反斜杠 ( \\
) 一起使用。
將您的 for 部分放在主循環或函數中。 否則它會得到優化,因為它從未被使用過,盡管它是有效的 C。
使用識別也很好,所以更容易看到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.