delay with MOV/LOOP

Typical Pentium software delay loops can be written using MOV and LOOP instructions.
For example, the following instruction sequence can be used for a delay loop: MOV CX,countDELAY: LOOP DELAY
The initial loop counter value of “count” can be calculated using the cycles required to execute the following Pentium instructions:MOV reg/imm (1 cycle)LOOP label (5/6 cycles)
Note that the Pentium LOOP instruction requires two different execution times. LOOP requires six cycles when the Pentium branches if the CX is not equal to zero after autodecrementing CX by 1. However, the Pentium goes to the next instruction and does not branch when CX = 0 after autodecrementing CX by 1, and this requires five cycles.
This means that the DELAY loop will require six cycles for (count - 1) times, and the last iteration will take five cycles.
For a 100-MHz Pentium clock, each cycle is 10 ns. For 2 ms, total cycles =2ms/10ns= 200,000. The loop will require six cycles for (count - 1) times when CX + 0, and five cycles will be required when no branch is taken (CX = 0). Thus, total cycles including the MOV = 1 + 6 x (count - 1) + 5 = 200,000. Hence, count = 33,333,0. Therefore, CX must be loaded with 33,33310

#include <STC15.H> // 全局变量 int flag = 1; bit key_pressed = 0; // 按键按下标志 // 10ms延时函数 void Delay10ms() //@12.000MHz { unsigned char i, j; i = 117; j = 184; do { while (--j); } while (--i); } // 关闭所有LED void All_LED_Off() { P1 = 0xFF; P2 = 0xFF; P3 = 0xFF; P4 = 0xFF; } // 外部中断1服务函数(P3.3) void exint1() interrupt 2 { EX1 = 0; // 暂时关闭外部中断1,防止重复进入 Delay10ms(); // 消抖延时 if(P33 == 0) // 确认按键确实按下 { key_pressed = 1; // 设置按键按下标志 } // 等待按键释放 while(P33 == 0) { Delay10ms(); } EX1 = 1; // 重新开启外部中断1 IE1 = 0; // 清除外部中断1标志 } // 初始化中断 void Init_Interrupt() { // 设置外部中断1(P3.3)为下降沿触发 IT1 = 1; // 1: 下降沿触发, 0: 低电平触发 // 使能外部中断1 EX1 = 1; // 开启总中断 EA = 1; } // 显示模式1 void Display_Mode1() { // 第一组显示 P3 = 0xFC; P1 = 0x7F; P4 = 0x7F; // 显示1秒,期间持续检测按键标志 { unsigned char i; for(i = 0; i < 100; i++) { Delay10ms(); if(key_pressed) // 检测按键标志 { flag = !flag; key_pressed = 0; // 清除按键标志 All_LED_Off(); return; } } } // 第二组显示 All_LED_Off(); P2 = 0x7F; // 显示1秒,期间持续检测按键标志 { unsigned char i; for(i = 0; i < 100; i++) { Delay10ms(); if(key_pressed) // 检测按键标志 { flag = !flag; key_pressed = 0; // 清除按键标志 All_LED_Off(); return; } } } // 第三组显示 All_LED_Off(); P1 = 0xBF; P4 = 0xBF; // 显示1秒,期间持续检测按键标志 { unsigned char i; for(i = 0; i < 100; i++) { Delay10ms(); if(key_pressed) // 检测按键标志 { flag = !flag; key_pressed = 0; // 清除按键标志 All_LED_Off(); return; } } } All_LED_Off(); } void main() { // 端口模式设置 P1M0 = 0x00; P1M1 = 0x00; P2M0 = 0x00; P2M1 = 0x00; P3M0 = 0x00; P3M1 = 0x00; P4M0 = 0x00; P4M1 = 0x00; // 初始化所有LED关闭 All_LED_Off(); // 初始化中断 Init_Interrupt(); while(1) { // 根据标志位执行不同操作 if(flag) { Display_Mode1(); // 显示模式1 } else { // 关闭模式 All_LED_Off(); Delay10ms(); // 在关闭模式下也检测按键 if(key_pressed) { flag = !flag; key_pressed = 0; // 清除按键标志 } } } }请在stc15基础上写出汇编语言,并且给出延时时间的具体计算过程
10-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值