这是我的反向停车项目......这段代码没有IF语句。当我在代码中放入IF语句时,我的硬件没有响应。 (无效主要的IF声明......) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
感谢您的帮助!
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD2_bit;
sbit LCD_D5 at RD3_bit;
sbit LCD_D6 at RD4_bit;
sbit LCD_D7 at RD5_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
//inicializacija LCD modula
char message1[] = "Distance:";
char *dis = "000.0";
unsigned int dist;
float temp_value;
float x;
float y;
void Display_Distance() {
if (dist/10000)
dis[0] = dist/10000 + 48; //stotice
else
dis[0] = ' ';
dis[1] = (dist/1000)%10 + 48; // desetica
dis[2] = (dist/100)%10 + 48; // enica
dis[4] = (dist/10)%10 + 48; // decimalka
Lcd_Out(2, 1, dis); // izpis distance na LCD
}
void main() {
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,message1);
Lcd_Chr(2,7,'C');
Lcd_Chr(2,8,'m');
do {
x = (float)ADC_Read(2); //vrednost iz analognega kanala, kamor je senzor priklopljen
y = (((x*5)/1023)); //pretvorba v volte
if(y>0){
temp_value = (((log10(y))/(log10(0.98)))+62.6); //funkcija grafa
if(y<=0.8 && y>0.65)
temp_value = temp_value+10;
else if(y<=0.65 && y>0.6)
temp_value = temp_value+17.5;
else if(y<=0.6 && y>0.55)
temp_value = temp_value+13;
else if(y<=0.55 && y>0.5)
temp_value = temp_value+3.5;
else if(y>=2 && y<2.5)
temp_value = temp_value+1.3;
else if(y>=2.5 && y<2.8)
temp_value = temp_value+4.5;
temp_value=temp_value*10000;
dist = temp_value/1000;
dist = dist*10;
Display_Distance();
Delay_ms(1000);
}
} while(1);
}
答案 0 :(得分:0)
必须在我的一个项目中处理这个问题,如果没有括号,if语句将无法正确分支。解决方案是在if / else语句之后使用大括号,即使后面只有一个语句。这是编译器的一个错误。
if(y<=0.8 && y>0.65)
temp_value = temp_value+10;
else if(y<=0.65 && y>0.6)
temp_value = temp_value+17.5;
变为
if(y<=0.8 && y>0.65){
temp_value = temp_value+10;
}
else if(y<=0.65 && y>0.6){
temp_value = temp_value+17.5;
}
如果程序崩溃,请检查生成的HEX / ASM。