[Atmel] 用AtmelStudio6.2跑mega16例程(5)

[复制链接]
 楼主| ddllxxrr 发表于 2014-8-31 16:52 | 显示全部楼层 |阅读模式

外部中断0例程:这个原理是INT0中断,在中断程序中让有源蜂鸣发声。

这个是在线仿真的结果,可以看出中断程序已经进入。



程序如下:
  1. /*
  2. * GccApplication8.c
  3. *
  4. * Created: 2014-8-31 10:57:46
  5. *  Author: Administrator
  6. */


  7. #include <avr/io.h>
  8. #include <avr/interrupt.h>
  9. #define uchar unsigned char
  10. #define uint unsigned int
  11. //定义按钮、蜂鸣器及LED与端口的连接
  12. #define  button1     2//PIND|_BV(2)  
  13. #define  sounder     7//PORTD|_BV(7)
  14. #define  redLED1     0//PORTC.0
  15. #define  blueLED1    1//PORTC.1
  16. #define  greenLED1   2//PORTC.2
  17. #define  yellowLED1  3//PORTC.3
  18. #define  redLED2     4//PORTC.4
  19. #define  blueLED2    5//PORTC.5
  20. #define  greenLED2   6//PORTC.6
  21. #define  yellowLED2  7//PORTC.7
  22. void delay(uint k)
  23. {   uint m,n;
  24.         for(m=0;m<k;m++)
  25.         {
  26.                 for(n=0;n<1140;n++)
  27.                 {;}
  28.         }
  29. }
  30. //interrupt[EXT_INT0] void int0_isr(void)
  31. ISR(INT0_vect)
  32. { uint j;
  33.         for(j=0;j<1250;j++)
  34.         {
  35.                 PORTD |=_BV(sounder);
  36.                 delay(2);
  37.                 PORTD ^=_BV(sounder);
  38.                 delay(2);
  39.         }
  40. }
  41. int main(void)
  42. {
  43.         DDRC=0xFF;
  44.         PORTC=0xFF;
  45.         DDRD=0xFF;//before 0xFB
  46.         PORTD=0xFF;
  47.         MCUCR=0x02;
  48.         GICR=0x60;
  49.         //asm("sei");  //SREG=0x80;
  50.         sei();
  51.         while(1)
  52.         {
  53.                 PORTC=0x00;
  54.                 delay(1000);
  55.                 PORTC=0xFF;
  56.                 delay(1000);
  57.         }
  58. }


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2403

主题

6994

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部