2022年单片机C语言中断 .pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2022年单片机C语言中断 .pdf》由会员分享,可在线阅读,更多相关《2022年单片机C语言中断 .pdf(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、单片机 C 语言中断 1电子制作 2009-01-01 18:26 阅读 191 评论 0字号:大中小interrupt 中断的关键字 ,n 是中断号提供中断程序的入口地址。0-INT0 1-T0 2-INT1 3-T1 4-串行中断5-T2 直接访问寄存器和端口定义sfr P0 0 x80 sfr P1 0 x81 sfr ADCON; 0 xDE sbit EA 0 x9F 操作ADCON = 0 x08 ; /* Write data to register */ P1 = 0 xFF ; /* Write data to Port */ io_status = P0 ; /* Read
2、 data from Port */ EA = 1 ; /* Set a bit (enable all interrupts) */ 在使用了 interrupt 1 关键字之后,会自动生成中断向量在 ISR 中不能与其他后台循环代码 (the background loop code) 共享 局部变量因为 连接器会复用在 RAM 中这些变量的位置 ,所以它们会有不同的意义,这取决于当前使用的不同的函数复用变量对RAM 有限的 51 来将 很重要。所以,这些函数希望按照一定的顺序执行而不被中断。timer0_int() interrupt 1 using 2 unsigned char te
3、mp1 ; unsigned char temp2 ; executable C statements ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 10 页 - - - - - - - - - interrupt 声明 表示 向量生成在(8*n 3),这里, n 就是 interrupt 参数后的那个数字这里 ,在 08H 的代码区域生成 LJMP timer0_int 这样一条指令using tells the compiler to switch regist
4、er banks on entry to an interrupt routine. This context switch is the fastest way of providing a fresh registerbank for an interrupt routines local data and is to be preferred to stacking registers for very time-critical routines. Note that interrupts of the same priority can share a register bank,
5、since there is no risk that they will interrupt each other. using 告诉编译器在进入中断处理器去切换寄存器的 bank 。这个 contet 切换是为中断处理程序的局部变量提供一个新鲜的寄存器bank 最快的方式。对时序要求严格的程序,是首选的stack 寄存器(保存寄存器到stack) 方式。注意:同样优先级别的中断可以共享寄存器 bank ,因为他们每次将中断没有危险If a USING 1 is added to the timer1 interrupt function prototype, the pushing of
6、registers is replaced by a simple MOV to PSW to switch registerbanks. Unfortunately, while the interrupt entry is speeded up, the direct register addressing used on entry to sys_interp fails. This is because C51 has not yet been told that the registerbank has been changed. If no working registers ar
7、e used and no other function is called, the optimizer eliminiates teh code to switch register banks. 如果在 timer1 的中断函数原型中使用USING 1, 寄存器的 pushing 将被MOV to PSW 切换寄存器 bank 所替换。不幸的是,当一个中断入口被加速时。用在入口的直接寄存器寻址将失败。这是因为C51 没有告诉寄存器 bank 已经改变。如果不工作的寄存器将被使用,如果没有其他函数被调用,优化器 . Logically, with an interrupt routine,
8、 parameters cannot be passed to it or returned. When the interrupt occurs, compiler-inserted code is run which pushes the accumulator, B,DPTR and the PSW (program status word) onto the stack. Finally, on exiting the interrupt routine, the items previously stored on the stack are restored and the clo
9、sing causes a RETI to be used rather than a normal RET. 逻辑上,一个中断服务程序,不能传递参数进去,也不可返回值。当中断发生时,编译器插入的代码被运行,它将 累加器,B, DPTR 和 PSW(程序状态字)入栈。最后,在退出中断程序时,预先存储在栈中被恢复。最后的结束符号将 插入 RETI 到 中断程序的最后,为了用Keil,C?语言创建一个中断服务程序(ISR),利用 interrupt 关键词和正确的中断号声明一个static void 函数。 Keil,C?编译器自动生成中断向量,以及中断程序的进口、出口代码。Interrupt 函
10、数属性标志着该函数为ISR。可用using 属性指定 ISR 使用哪一个寄存器区,这是可选的。有效的寄存器区范围为1到 3。中断源的矢量位置名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 10 页 - - - - - - - - - 中断源Keil 中断编号矢量地址最高优先级6 0 x0033 外部中断 0 0 0 x0003 定时器 0 溢出1 0 x000B 外部中断 1 2 0 x0013 定时器 1 溢出3 0 x001B 串口4 0 x0023 定时器 2 溢出
11、5 0 x002B DMA 7 0 x003B 硬件断点8 0 x0043 JTAG 9 0 x004B 软件断点10 0 x0053 监视定时器12 0 x0063 1. 函数在调用前定义与在调用后定义产生的代码是有很大差别的(特别是在优化级别大于3 级时)。(本人也不太清楚为什么,大概因为在调用前定义则调用函数已经知道被调用函数对寄存器的使用情况,则可对函数本身进行优化;而在调用后进行定义则函数不知被调用函数对寄存器的使用情况,它默认被调用函数对寄存器( ACC、 B、 DPH 、 DPL 、 PSW 、 R0、 R1、 R2、 R3、R 4 、 R5、, R6、 R7)都已经改变,因此不
12、在这些寄存器中存入有效的数据)2. 函数调用函数时除在堆栈中存入返回地址之外,不在堆栈中保存其它任何寄存器(ACC、 B、 DPH、 DPL、PSW 、 R0、 R1、 R2、 R3、R 4 、 R5、, R6 、 R7)的内容。(除非被调用函数使用了using 特性)3. 中断函数是一个例外,它会计算自身及它所调用的函数对寄存器(ACC、 B、 DPH、 DPL、 PSW、 R0、R1、 R2、 R3、R 4、 R5、, R6、 R7)的改变,并保存相应它认为被改变了的寄存器。4. 使用 C 写程序时,尽量少使用using n (n=0,1,2,3)特性。(这个特性在本人使用的过程中存在一些
13、问题,不知算不算是一个小bug )默认 keil c51 中的函数使用的是0 寄存器组,当中断函数使用using n 时, n = 1,2,3 或许是对的,但n=0时,程序就已经存在了bug (只有中断函数及其所调用的函数并没有改变R0 - R7的值时,这个bug 不会表现出来)一个结论是,在中断函数中如果使用了using n ,则中断不再保存R0-R7 的值。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 10 页 - - - - - - - - - 由此可以推论出,一
14、个高优先级的中断函数及一个低优先级的中断函数同时使用了using n ,(n = 0,1,2,3 )当 n 相同时,这个存在的bug 是多么的隐蔽。(这恰是使人想象不到的)使用不同寄存器组的函数(特殊情况外)不能相互调用using 关键字告诉编译器切换 register bank 如果中断程序不重要,using 关键字能忽略。如果一个函数被从中断程序调用,而此中断强制使用using 当编译一个被调用的函数时,编译器必须告诉它1) 在函数前必须用伪指令#pragma NOAREGS 在进入函数#pragma RESTORE 或者#pragmas AREGS 这样就不会使用绝对地址定位 2)#pr
15、agma REGISTERBANK(n) 用这个指定告诉当前使用的bank 用 NOAREGS指令 移除MOV R7,AR7 中断服务例程/* Timer 0 Overflow Interrupt Service Routine */ timer0_int() interrupt 1 USING 1 unsigned char temp1 ; unsigned char temp2 ; /* executable C statements */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - -
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年单片机C语言中断 2022 单片机 语言 中断
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内