|
Keil下AT89C51的软件仿真速度问题。(难道时钟是1.2MHz吗?)
[复制链接]
为AT89C51写了一个延时函数如下:
- void WhileTimeout(unsigned int pTimeout)
- //WhileTimeout函数
- //语法:WhileTimeout(pTimeout);
- //说明:产生pTimeout指定延时,单位为ms。
- //参数:unsigned int pTimeout 单位为ms的延时参数。
- {
- //
- unsigned int tTimerIndex;
- unsigned int tTimerIndex_Length;
- unsigned int tClockIndex;
- unsigned int tClockIndex_Length;
- //
- tTimerIndex_Length=pTimeout;
- tClockIndex_Length=12000; // 1ms=1/12MHz=12000
- //
- for (tTimerIndex=0; tTimerIndex
- {
- for (tClockIndex=0; tClockIndex
- {
- //
- }
- }
- }
复制代码
假定AT89C51以12MHz运行,其中tClockIndex_Length设置为12000,以获得1ms的延时。但实际Debug软件仿真发现这个函数延时在10ms,似乎是在1.2MHz运行。由于单片机还没有搭出来,所以暂时没烧到片里实际测试。不知是仿真速度就是这样?还是我计算有误?
完整程序如下(哪位如果有现成的单片机帮我测试一下就太好了,详情见main函数的说明):
- #include
- //
- void WhileTimeout(unsigned int pTimeout)
- //WhileTimeout函数
- //语法:WhileTimeout(pTimeout);
- //说明:产生pTimeout指定延时,单位为ms。
- //参数:unsigned int pTimeout 单位为ms的延时参数。
- {
- //
- unsigned int tTimerIndex;
- unsigned int tTimerIndex_Length;
- unsigned int tClockIndex;
- unsigned int tClockIndex_Length;
- //
- tTimerIndex_Length=pTimeout;
- tClockIndex_Length=12000; // 1ms=1/12MHz=12000
- //
- for (tTimerIndex=0; tTimerIndex
- {
- for (tClockIndex=0; tClockIndex
- {
- //
- }
- }
- }
- void ProtDevice_LO(unsigned char pConByte, unsigned char pProt)
- //ProtDevice函数
- //语法:ProtDevice(pConByte, pProt);
- //说明:驱动端口(低电平开启)。
- //参数:unsigned char pConByte 端口控制字节(高电平开启)。
- // unsigned char pProt 端口选择(0-3)
- {
- switch(pProt)
- {
- case 0: P0=~pConByte;break;
- case 1: P1=~pConByte;break;
- case 2: P2=~pConByte;break;
- case 3: P3=~pConByte;break;
- }
- }
- void LoopOpen_LO(unsigned int pOpenTimeout, unsigned char pProt)
- //LoopOpen_LO函数
- //语法:LoopOpen_LO(pOpenTimeout, pProt);
- //说明:循环驱动端口(低电平开启)。
- //参数:unsigned int pOpenTimeout 开启时间设置(单位ms)
- // unsigned char pProt 端口选择(0-3)
- {
- unsigned char tConByte;
- unsigned int tLoopIndex;
- unsigned int tLoopIndex_Length;
- tLoopIndex_Length=7;
- //开关0号。
- tConByte=1;
- ProtDevice_LO(tConByte, pProt); //驱动端口
- WhileTimeout(pOpenTimeout);
- //开关1-7号。
- for (tLoopIndex=0; tLoopIndex
- {
- tConByte=tConByte<<1;
- ProtDevice_LO(tConByte, pProt); //驱动端口
- WhileTimeout(pOpenTimeout);
- }
- }
- void main()
- {
- unsigned char tProtA;
- unsigned char tProtB;
- unsigned char tConByte;
- unsigned int tOpenTimeout;
- unsigned int tWhileTimeout;
- tProtA=1;
- tProtB=2;
- tOpenTimeout=50; //按照道理应该是500
- tWhileTimeout=300; //按照道理应该是3000
- tConByte=0;
- ProtDevice_LO(tConByte, tProtA);
- ProtDevice_LO(tConByte, tProtB);
- while(1)
- {
- //第一端口
- LoopOpen_LO(tOpenTimeout, tProtA);
- tConByte=0;
- ProtDevice_LO(tConByte, tProtA);
- //第二端口
- LoopOpen_LO(tOpenTimeout, tProtB);
- tConByte=0;
- ProtDevice_LO(tConByte, tProtB);
- //等待
- WhileTimeout(tWhileTimeout);
- }
- }
复制代码
|
|