1: C example:
在c.h中define:
#ifdef Module_Compressor
void csCompressorPowerOff();
void csColdWaterOff ();
void csColdWaterOn();
void csCompressorError();
void (*csCompressor[ ]) () = {csCompressorPowerOff,csColdWaterOff,csColdWaterOn,csCompressorError};
#else
extern void csCompressorPowerOff ();
extern void csColdWaterOff ();
extern void csColdWaterOn();
extern void csCompressorError();
#endif
/* csCompressorPowerOff(),csColdWaterOff(),ColdWaterOn(),csCompressorError() denote four state machines*/
在main.c中引用:
unsigned char i;
main()
{
……
……
(*csCompressor[i])();
……
……
}
改变i的值实现切换。
2: ASM example:
Toshiba ASM
在code区定义:
rsCompressor:
dw csCompressorPowerOff
dw csColdWaterOff
dw csColdWaterOn
dw csCompressorError
在main program中的引用
定义
dsCompressor dsb 1
ld hl,rsCompressor
ld c,(dsCompressor)
shlc c
call (hl+c) ; Compressor state machine
改变dsCompressor中的值实现切换
用C和ASM实现State Machine切换的方法
最新推荐文章于 2026-06-25 10:01:23 发布
博客给出了C和ASM实现状态机切换的示例。在C中,通过在c.h里定义函数数组,在main.c中引用并改变数组索引值实现切换;在ASM中,在code区定义函数地址,在主程序里引用并改变相关值来实现状态机切换。

9126

被折叠的 条评论
为什么被折叠?



