sprintf(szVal, "%0.3f" , val); LCD_DisString((i%9)+1, 19, szVal); Pro_szUnit(DBData[i],i); if (isDraw == 1) val = LCD_BitModify(val,draw); ikey = Lcd_Recvfun2(); switch(ikey) { case LCD_KEY_UP: bit = 0; if (not == 2) { if (draw > page + 1 && draw <= line) draw = draw - 1; } break; case LCD_KEY_DOWN: bit = 0; if (not == 2) { if(draw < i) if (draw < line) draw = draw + 1; } break; case LCD_KEY_ADD: break; case LCD_KEY_DECREASE: break; case LCD_KEY_Left: if (not == 1) { change = 0; if(page < 9) continue; if(page >= 9) { page = page - 9; line = line - 9; } } break; case LCD_KEY_RIGHT: if (not == 1) { change = 0; if(line >= num) continue; if(page < num) { page = page + 9; line = line + 9; } } break; case LCD_KEY_ENTER: not = 2; bit = 0; break; case LCD_KEY_CANCEL: change = 0; if(not > 0) --not; draw = 0; if (not == 0) return; break; default: break; } } 这段代码在死循环中运行,其中的LCD_BitModify函数也有死循环,怎么使它们互不干扰(不使用线程)
时间: 2024-04-05 13:31:46 浏览: 56
可以使用时间片轮转的方式来解决这个问题。在主循环中设置一个计时器,每隔一定时间就切换到另一个函数的执行。具体的实现方式可以使用一个标志变量来表示当前应该执行的函数,然后在计时器中切换标志变量的值。这样就可以让两个函数轮流执行,而不会互相干扰。当然,这个方法需要注意时间片的设置,以保证两个函数的执行时间不会过长,否则会影响系统的响应速度。
相关问题
优化这段代码 if( DBData[i] >= RT1064KZZ_GL1_ALM && DBData[i] <= RT1064KZZ_KZHL && DBData[i] != RT1064KZZ_MODE && DBData[i] != RT1064KZZ_UAB_CH && DBData[i] != RT1064KZZ_UBC_CH && DBData[i] != RT1064KZZ_FBS && DBData[i] != RT1064KZZ_FBS_MODE) { (isDraw ? LCD_DisString_Not((i%LISTOFFSET)+1, 20,(char *)gcszOnOff[(int)val]) : LCD_DisString((i%LISTOFFSET)+1, 20,(char *)gcszOnOff[(int)val]) ); } else if(DBData[i] == RT1064KZZ_MODE) { (isDraw ? LCD_DisString_Not((i%LISTOFFSET)+1, 20,(char *)ModeName[(int)val]) : LCD_DisString((i%LISTOFFSET)+1, 20,(char *)ModeName[(int)val]) ); } else if (DBData[i] == RT1064KZZ_FBS || DBData[i] == RT1064KZZ_FBS_MODE ) { (isDraw ? LCD_DisString_Not((i%LISTOFFSET)+1, 20,(char *)FBS_NAME[(int)val]) : LCD_DisString((i%LISTOFFSET)+1, 20,(char *)FBS_NAME[(int)val]) ); } else if(DBData[i] == RT1064KZZ_UAB_CH || DBData[i] == RT1064KZZ_UBC_CH || DBData[i] == RT1064_DZ_CHZCS) { sprintf(szVal, "%0.f" , val); LCD_DisString((i%LISTOFFSET)+1, 19 , szVal); if(DBData[i] == RT1064_DZ_CHZCS) LCD_DisString((i%LISTOFFSET)+1, 24, (char *)"次"); if (isDraw == 1) len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit); } else { sprintf(szVal, "%0.3f" , get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])); LCD_DisString((i%LISTOFFSET)+1, 19, szVal); if (DBData[i] >= RT1064_YS_GL1 && DBData[i] <= RT1064_YS_FBS_JY) LCD_DisString((i%9)+1, 25, (char *)"S"); else if ((DBData[i] >= RT1064_DZ_GL1 && DBData[i] <= RT1064_DZ_I02) || ((DBData[i] >= RT1064_DZ_PHASE_I && DBData[i] <= RT1064_DZ_I0DLT) && DBData[i] != RT1064_DZ_YL_HAR && DBData[i] != RT1064_DZ_LMJ) || DBData[i] == RT1064_DZ_SD || DBData[i] == RT1064_DZ_I0HJS || DBData[i] == RT1064_DZ_FC_CHZ || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)|| DBData[i] == RT1064_DZ_FBS_OL) { LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"A"); } else if (DBData[i] == RT1064_DZ_LMJ || DBData[i] == RT1064_DZ_JC || DBData[i] == RT1064_DZ_GYJC) { LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"°"); } else if (DBData[i] == RT1064_DZ_YL_HAR) LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"%"); else if (DBData[i] == RT1064_DZ_IDS ||DBData[i] == RT1064_DZ_OPENCS || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)) { LCD_DisString((i%LISTOFFSET)+1, 24, (char *)"次"); } else if (DBData[i] == RT1064_DZ_DP || DBData[i] == RT1064_DZ_GP) { LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"HZ"); } else LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"V"); if (isDraw == 1)len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit); }
这段代码可以优化的地方有:
1. 将重复的代码提取出来,避免重复书写。例如,多次调用 `LCD_DisString` 可以提取出来作为一个函数,减少代码重复。
2. 将一些条件语句的判断条件提取出来,避免重复书写。例如,多次使用 `DBData[i]` 的值进行判断,可以将它提取出来,避免重复书写。
3. 使用 switch 语句来替换多个 if-else 语句,使代码更加清晰。
4. 对于 `sprintf(szVal, "%0.3f" , get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i]))` 这一行代码,可以考虑使用 snprintf 函数来避免缓冲区溢出。
下面是一个优化后的代码示例:
```
void displayData(int i, int isDraw, float val, char* szVal, int len) {
if (DBData[i] >= RT1064KZZ_GL1_ALM && DBData[i] <= RT1064KZZ_KZHL && DBData[i] != RT1064KZZ_MODE && DBData[i] != RT1064KZZ_UAB_CH && DBData[i] != RT1064KZZ_UBC_CH && DBData[i] != RT1064KZZ_FBS && DBData[i] != RT1064KZZ_FBS_MODE) {
displayString(i, isDraw, val, gcszOnOff, (i % LISTOFFSET) + 1, 20);
} else if (DBData[i] == RT1064KZZ_MODE) {
displayString(i, isDraw, val, ModeName, (i % LISTOFFSET) + 1, 20);
} else if (DBData[i] == RT1064KZZ_FBS || DBData[i] == RT1064KZZ_FBS_MODE) {
displayString(i, isDraw, val, FBS_NAME, (i % LISTOFFSET) + 1, 20);
} else if (DBData[i] == RT1064KZZ_UAB_CH || DBData[i] == RT1064KZZ_UBC_CH || DBData[i] == RT1064_DZ_CHZCS) {
snprintf(szVal, sizeof(szVal), "%0.f", val);
LCD_DisString((i % LISTOFFSET) + 1, 19, szVal);
if (DBData[i] == RT1064_DZ_CHZCS) {
LCD_DisString((i % LISTOFFSET) + 1, 24, (char *)"次");
}
if (isDraw == 1) {
len = LCD_BitNot(UNIT_PROTECT, UNIT_GAP_RT1064, gapid, DBData[i], i, bit);
}
} else {
snprintf(szVal, sizeof(szVal), "%0.3f", get_ActionDZInfo_val(UNIT_GAP_RT1064, gapid, DBData[i]));
LCD_DisString((i % LISTOFFSET) + 1, 19, szVal);
if (DBData[i] >= RT1064_YS_GL1 && DBData[i] <= RT1064_YS_FBS_JY) {
LCD_DisString((i % 9) + 1, 25, (char *)"S");
} else if ((DBData[i] >= RT1064_DZ_GL1 && DBData[i] <= RT1064_DZ_I02) || ((DBData[i] >= RT1064_DZ_PHASE_I && DBData[i] <= RT1064_DZ_I0DLT) && DBData[i] != RT1064_DZ_YL_HAR && DBData[i] != RT1064_DZ_LMJ) || DBData[i] == RT1064_DZ_SD || DBData[i] == RT1064_DZ_I0HJS || DBData[i] == RT1064_DZ_FC_CHZ || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)|| DBData[i] == RT1064_DZ_FBS_OL) {
LCD_DisString((i % LISTOFFSET) + 1, 25, (char *)"A");
} else if (DBData[i] == RT1064_DZ_LMJ || DBData[i] == RT1064_DZ_JC || DBData[i] == RT1064_DZ_GYJC) {
LCD_DisString((i % LISTOFFSET) + 1, 25, (char *)"°");
} else if (DBData[i] == RT1064_DZ_YL_HAR) {
LCD_DisString((i % LISTOFFSET) + 1, 25, (char *)"%");
} else if (DBData[i] == RT1064_DZ_IDS || DBData[i] == RT1064_DZ_OPENCS || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)) {
LCD_DisString((i % LISTOFFSET) + 1, 24, (char *)"次");
} else if (DBData[i] == RT1064_DZ_DP || DBData[i] == RT1064_DZ_GP) {
LCD_DisString((i % LISTOFFSET) + 1, 25, (char *)"HZ");
} else {
LCD_DisString((i % LISTOFFSET) + 1, 25, (char *)"V");
}
if (isDraw == 1) {
len = LCD_BitNot(UNIT_PROTECT, UNIT_GAP_RT1064, gapid, DBData[i], i, bit);
}
}
}
void displayString(int i, int isDraw, float val, char** stringArray, int x, int y) {
(isDraw ? LCD_DisString_Not(x, y, (char *)stringArray[(int)val]) : LCD_DisString(x, y, (char *)stringArray[(int)val]));
}
```
优化这段代码 if( DBData[i] >= RT1064KZZ_GL1_ALM && DBData[i] <= RT1064KZZ_KZHL && DBData[i] != RT1064KZZ_MODE && DBData[i] != RT1064KZZ_UAB_CH && DBData[i] != RT1064KZZ_UBC_CH && DBData[i] != RT1064KZZ_FBS && DBData[i] != RT1064KZZ_FBS_MODE) { (isDraw ? LCD_DisString_Not((i%LISTOFFSET)+1, 20,(char *)gcszOnOff[(int)val]) : LCD_DisString((i%LISTOFFSET)+1, 20,(char *)gcszOnOff[(int)val]) ); } else if(DBData[i] == RT1064KZZ_MODE) { (isDraw ? LCD_DisString_Not((i%LISTOFFSET)+1, 20,(char *)ModeName[(int)val]) : LCD_DisString((i%LISTOFFSET)+1, 20,(char *)ModeName[(int)val]) ); } else if (DBData[i] == RT1064KZZ_FBS || DBData[i] == RT1064KZZ_FBS_MODE ) { (isDraw ? LCD_DisString_Not((i%LISTOFFSET)+1, 20,(char *)FBS_NAME[(int)val]) : LCD_DisString((i%LISTOFFSET)+1, 20,(char *)FBS_NAME[(int)val]) ); } else if(DBData[i] == RT1064KZZ_UAB_CH || DBData[i] == RT1064KZZ_UBC_CH || DBData[i] == RT1064_DZ_CHZCS) { sprintf(szVal, "%0.f" , val); LCD_DisString((i%LISTOFFSET)+1, 19 , szVal); if(DBData[i] == RT1064_DZ_CHZCS) LCD_DisString((i%LISTOFFSET)+1, 24, (char *)"次"); if (isDraw == 1) len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit); } else { sprintf(szVal, "%0.3f" , get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])); LCD_DisString((i%LISTOFFSET)+1, 19, szVal); if (DBData[i] >= RT1064_YS_GL1 && DBData[i] <= RT1064_YS_FBS_JY) LCD_DisString((i%9)+1, 25, (char *)"S"); else if ((DBData[i] >= RT1064_DZ_GL1 && DBData[i] <= RT1064_DZ_I02) || ((DBData[i] >= RT1064_DZ_PHASE_I && DBData[i] <= RT1064_DZ_I0DLT) && DBData[i] != RT1064_DZ_YL_HAR && DBData[i] != RT1064_DZ_LMJ) || DBData[i] == RT1064_DZ_SD || DBData[i] == RT1064_DZ_I0HJS || DBData[i] == RT1064_DZ_FC_CHZ || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)|| DBData[i] == RT1064_DZ_FBS_OL) { LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"A"); } else if (DBData[i] == RT1064_DZ_LMJ || DBData[i] == RT1064_DZ_JC || DBData[i] == RT1064_DZ_GYJC) { LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"°"); } else if (DBData[i] == RT1064_DZ_YL_HAR) LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"%"); else if (DBData[i] == RT1064_DZ_IDS ||DBData[i] == RT1064_DZ_OPENCS || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)) { LCD_DisString((i%LISTOFFSET)+1, 24, (char *)"次"); } else if (DBData[i] == RT1064_DZ_DP || DBData[i] == RT1064_DZ_GP) { LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"HZ"); } else LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"V"); if (isDraw == 1)len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit); }
这段代码可以优化的地方比较多,建议按照以下几个方面进行调整:
1. 将重复的代码片段提取出来,封装成函数,提高代码复用性和可读性;
2. 尽可能地使用 switch-case 语句替换 if-else 语句,提高代码执行效率;
3. 对于需要频繁操作的字符串拼接操作,可以使用 sprintf 函数来代替,提高代码执行效率。
下面是一个可能的优化版本:
```c
void draw_LCD(int i, int isDraw, float val, int gapid, int bit) {
char szVal[10];
switch (DBData[i]) {
case RT1064KZZ_MODE:
if (isDraw) {
LCD_DisString_Not((i % LISTOFFSET) + 1, 20, (char *)ModeName[(int)val]);
} else {
LCD_DisString((i % LISTOFFSET) + 1, 20, (char *)ModeName[(int)val]);
}
break;
case RT1064KZZ_FBS:
case RT1064KZZ_FBS_MODE:
if (isDraw) {
LCD_DisString_Not((i % LISTOFFSET) + 1, 20, (char *)FBS_NAME[(int)val]);
} else {
LCD_DisString((i % LISTOFFSET) + 1, 20, (char *)FBS_NAME[(int)val]);
}
break;
case RT1064KZZ_UAB_CH:
case RT1064KZZ_UBC_CH:
case RT1064_DZ_CHZCS:
sprintf(szVal, "%0.f", val);
LCD_DisString((i % LISTOFFSET) + 1, 19, szVal);
if (DBData[i] == RT1064_DZ_CHZCS) {
LCD_DisString((i % LISTOFFSET) + 1, 24, (char *)"次");
}
if (isDraw) {
LCD_BitNot(UNIT_PROTECT, UNIT_GAP_RT1064, gapid, DBData[i], i, bit);
}
break;
default:
sprintf(szVal, "%0.3f", get_ActionDZInfo_val(UNIT_GAP_RT1064, gapid, DBData[i]));
LCD_DisString((i % LISTOFFSET) + 1, 19, szVal);
if (DBData[i] >= RT1064_YS_GL1 && DBData[i] <= RT1064_YS_FBS_JY) {
LCD_DisString((i % 9) + 1, 25, (char *)"S");
} else if ((DBData[i] >= RT1064_DZ_GL1 && DBData[i] <= RT1064_DZ_I02) ||
((DBData[i] >= RT1064_DZ_PHASE_I && DBData[i] <= RT1064_DZ_I0DLT) &&
DBData[i] != RT1064_DZ_YL_HAR && DBData[i] != RT1064_DZ_LMJ) ||
DBData[i] == RT1064_DZ_SD || DBData[i] == RT1064_DZ_I0HJS || DBData[i] == RT1064_DZ_FC_CHZ ||
(DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0) || DBData[i] == RT1064_DZ_FBS_OL) {
LCD_DisString((i % LISTOFFSET) + 1, 25, (char *)"A");
} else if (DBData[i] == RT1064_DZ_LMJ || DBData[i] == RT1064_DZ_JC || DBData[i] == RT1064_DZ_GYJC) {
LCD_DisString((i % LISTOFFSET) + 1, 25, (char *)"°");
} else if (DBData[i] == RT1064_DZ_YL_HAR) {
LCD_DisString((i % LISTOFFSET) + 1, 25, (char *)"%");
} else if (DBData[i] == RT1064_DZ_IDS || DBData[i] == RT1064_DZ_OPENCS ||
(DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)) {
LCD_DisString((i % LISTOFFSET) + 1, 24, (char *)"次");
} else if (DBData[i] == RT1064_DZ_DP || DBData[i] == RT1064_DZ_GP) {
LCD_DisString((i % LISTOFFSET) + 1, 25, (char *)"HZ");
} else {
LCD_DisString((i % LISTOFFSET) + 1, 25, (char *)"V");
}
if (isDraw) {
LCD_BitNot(UNIT_PROTECT, UNIT_GAP_RT1064, gapid, DBData[i], i, bit);
}
break;
}
}
```
调用方式如下:
```c
for (int i = 0; i < DBData.size(); i++) {
if (DBData[i] >= RT1064KZZ_GL1_ALM && DBData[i] <= RT1064KZZ_KZHL && DBData[i] != RT1064KZZ_MODE &&
DBData[i] != RT1064KZZ_UAB_CH && DBData[i] != RT1064KZZ_UBC_CH && DBData[i] != RT1064KZZ_FBS &&
DBData[i] != RT1064KZZ_FBS_MODE) {
draw_LCD(i, isDraw, val, gapid, bit);
} else {
draw_LCD(i, isDraw, val, gapid, bit);
}
}
```
阅读全文