本帖最后由 ydatou 于 2025-8-30 08:48 编辑
我的做法·是定义3个常量。
系统时钟频率
指令机器周期数
定时器机器周期数
有了这3个常量可以适配任何51mcu,不用改代码,只改这3个常量。
下面的配置是十速的51mcu。
- #define FOSC 7.3728//11.0592 //
- #define MACHINE_NUM 2 //定时器
- #define M_NUM 2 //程序 SH88F2051 程序是1T,定时器1T/12T可选
- #define COVERT(T) ( (T*FOSC+M_NUM) / (M_NUM*2) -1 )
- #define _COUNT_560uS_ ((uint16)(-560L*FOSC/MACHINE_NUM))//560uS
- #define _COUNT_560uS_LO (_COUNT_560uS_ & 0x00FF)
- #define _COUNT_560uS_HI (_COUNT_560uS_ >> 8)
- #define _COUNT_1mS_ ((uint16)(-1000L*FOSC/MACHINE_NUM))//1000uS
- #define _COUNT_1mS_LO (_COUNT_1mS_ & 0x00FF)
- #define _COUNT_1mS_HI (_COUNT_1mS_ >> 8)
- #define DELAY_uS(DlyTime) do{uint8 nCount;nCount=MIN(MAX(COVERT(DlyTime),1),256);do{}while(--nCount);}while(0)
- //Note:实际延时效果C51 12M 4<= DlyTime <= 514(uS)
- //Note:实际延时效果88F54 16.6M 1<= DlyTime <= 31(uS)
- //#if (M_NUM==1)
- // #define delay_1mS() do{ uint8 x=30; do{ DELAY_uS(33.0); }while(--x); }while(0)
- //#else
- // #if(M_NUM==12)
- // #define delay_1mS() do{ DELAY_uS(333.0);DELAY_uS(333.0); DELAY_uS(333.0); }while(0)
- // #else
- // #define delay_1mS() do{ uint8 x=10; do{ DELAY_uS(99.0); }while(--x); }while(0)
- // #endif
- //#endif
- //#define delay_mS(mSEC) do{uint16 ms=mSEC; do{delay_1mS();WDT_reset();T2CON=0x04;}while(--ms); }while(0)
复制代码
|