这个是按键中断例程:
以下是仿真截图:
程序很简单,大家看注释即可明白:
- /*
- * GccApplication5.c
- *
- * Created: 2014-8-27 18:51:28
- * Author: Administrator
- */
- #include <avr/io.h>
- #define uchar unsigned char
- #define uint unsigned int
- //延时函数
- void s_ms(unsigned int t)
- {
- for(;t>1;t--)
- {
- }
- }
- int main(void)
- {
- uchar tem,key;
- //端口初始化
- DDRB=0xff;
- PORTB=0xff;
- DDRD=0x00;
- PORTD=0xff;
- while(1)
- {
- tem=PIND; //读取端口b
- if(tem!=0xff) //判断是否有按键按下
- {
- s_ms(500); //排除按键抖动和抗干扰
- key=PIND;
- if(key==tem)
- {
- PORTB=key; //端口D输出按键值
- }
- }
- }
- }
|