我有一个if语句,来判断一个变量在不同大小范围下执行不同操作,感觉if语句逐条判断效率低,能不能,改成switch语句?要怎么改呢?
if(bit_counter<9)
{
bit_set(&DCC_addr,9-bit_counter,data);
}
if(bit_counter>9&&bit_counter<18)
{
bit_set(&D1,18-bit_counter,data);}
if(bit_counter==18&&data==1)
{
DCC_byte_size=2;
dcc_cmd_refresh_flag=1;
data_flag=0;
bit_counter=0;
}
if(bit_counter>18&&bit_counter<27)
{bit_set(&D2,27-bit_counter,data);}
if(bit_counter==27&&data==1)
{
DCC_byte_size=3;
dcc_cmd_refresh_flag=1;
data_flag=0;
bit_counter=0;
// HAL_UART_Transmit_IT(&huart1,&D2,1);
}
if(bit_counter>27&&bit_counter<36)
{bit_set(&D3,36-bit_counter,data);}
if(bit_counter==36&&data==1)
{
DCC_byte_size=4;
dcc_cmd_refresh_flag=1;
data_flag=0;
bit_counter=0;
}
if(bit_counter>36&&bit_counter<45)
{bit_set(&D4,45-bit_counter,data);}
if(bit_counter==45&&data==1)
{
DCC_byte_size=5;
dcc_cmd_refresh_flag=1;
data_flag=0;
bit_counter=0;
}
if(bit_counter>45&&bit_counter<54)
{bit_set(&D5,54-bit_counter,data);}
if(bit_counter==54&&data==1)
{
DCC_byte_size=6;
dcc_cmd_refresh_flag=1;
data_flag=0;
bit_counter=0;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>