CH55X系列指令速度求解

以下有兩段代碼和反匯編結(jié)果對(duì)比:

UINT8 colors[] = {0x00, 0x10, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10};
// WS2812B發(fā)送1Byte數(shù)據(jù)
void WS_SendByte(UINT8 buffer)
{
UINT8 i; // 代碼段一
//static UINT8 i; // 代碼段二
for(i=8;i;--i)
{
if (buffer & 0x80)
{
LED = 1;
++SAFE_MOD;
++SAFE_MOD;
++SAFE_MOD;
}
else LED = 1;
++SAFE_MOD;
++SAFE_MOD;
LED = 0;
buffer <<= 1; // 低電平時(shí)序比較隨意,要求不高
}
}
void main(void)
{
UINT8 i;
SAFE_MOD = 0x55; // 解鎖安全模式
SAFE_MOD = 0xAA;
CLOCK_CFG = 0x86; // 系統(tǒng)時(shí)鐘分頻器4分頻=24MHz
while (1)
{
mDelayuS(1000); // 延時(shí)以便示波器觀察信號(hào)
for (i = 0; i != 9; ++i) WS_SendByte(colors[i]);
}
}

代碼段一反匯編:

1658940673591938.png

代碼段二反匯編:

1658940674837587.png


注:兩段代碼唯一的差別僅僅是一個(gè) static 關(guān)鍵字

實(shí)測(cè)用示波器檢測(cè)LED引腳,9次WS_SendByte()循環(huán)調(diào)用,代碼段一比代碼段二耗時(shí)長(zhǎng)5us。

這個(gè)結(jié)果比較反直覺,畢竟代碼段一的匯編指令更短,我查詢指令周期表,也沒發(fā)現(xiàn)DJNZ會(huì)比JNZ明顯的消耗更多時(shí)間。

所以希望WCH官方能給予解答這個(gè)疑惑。


icon_pdf.gifCH55X指令周期.PDF


There are a few things to consider:

  1. a static for a loop var does not make any sense

  2. you are using large mode, therfore y static i will be placed in xdata (cant use a reg like in non static version)

  3. vars allways have to stored back after each loop in static version


So for optimized speed never use large mode and dont use static for a simple loop var. The Keil compiler is very smart and put your loopvar in R6 therefore can use DJNZ. By forcing it to be static it has to be stored back after every count.

By unrolling that loop it would be even faster, but uses more code


Read some C Book about the usage of the static keyword.

Here a short example;


#define WAIT ++SAFE_MOD;\
? ? ? ? ? ? ++SAFE_MOD;\
? ? ? ? ? ? ++SAFE_MOD \

void WS_SendByte(UINT8 buffer)
{
?LED = !(buffer&0x80);
?WAIT;
?LED = !(buffer&0x40);
?WAIT;
?LED = !(buffer&0x20);
?WAIT;
?LED = !(buffer&0x10);
?WAIT;
?LED = !(buffer&0x08);
?WAIT;
?LED = !(buffer&0x04);
?WAIT;
?LED = !(buffer&0x02);
?WAIT;
?LED = !(buffer &0x01);
?WAIT;
}



Sorry about the question marks. I dont know how to remove them


The question marks just a bug with this website, you can change font to MicrosoftYaHei, that can fix it.


Yes, i select the Large memory.

But i mean when i add "static", assembly code becomes more, but will run faster.


只有登錄才能回復(fù),可以選擇微信賬號(hào)登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频