[Atmel] 用AtmelStudio6.2跑mega16例程(34)INT0INT1记数

[复制链接]
 楼主| ddllxxrr 发表于 2014-10-16 22:09 | 显示全部楼层 |阅读模式
本程序,同时允许INT0,INT1中断。GICR = 0XC0也可写作 GICR = _BV(INT0) | _BV(INT1);

MCUCR = 0x0A是将两个中断设为下降沿触发.

以下是仿真的电路图:



以下是Studion6.2编译截图:

以下是程序:
  1. /*
  2. * GccApplication20.c
  3. *
  4. * Created: 2014-10-16 21:18:46
  5. *  Author: Administrator
  6. */

  7. #define F_CPU 0x8000000UL
  8. #include <avr/io.h>
  9. #include <avr/interrupt.h>
  10. #include <util/delay.h>
  11. #include <stdint.h>

  12. #define K1_CLEAR_ON() ((PIND & 0x10)== 0x00)
  13. #define K2_CLEAR_ON() ((PIND & 0x20)== 0x00)

  14. const uint8_t SEG_CODE[] = { 0xc0,0xf9,0xa4,0x80,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
  15. const uint8_t SCAN_BITs[] = {0x20,0x10,0x08,0x04,0x02,0x01};
  16. uint8_t Buffer_Counts[] = {0,0,0,0,0,0};
  17. uint16_t Count_A = 0,Count_B = 0;

  18. void Show_Counts()
  19. {
  20.      uint8_t i;
  21.          
  22.          Buffer_Counts[2] = Count_A/100;
  23.          Buffer_Counts[1] = Count_A %100/10;
  24.          Buffer_Counts[0] = Count_A %10;
  25.          
  26.          if(Buffer_Counts[2]== 0)
  27.          {
  28.                 Buffer_Counts[2] = 10;
  29.                 if(Buffer_Counts[1] == 0) Buffer_Counts[1] = 10;
  30.           }
  31.          
  32.          Buffer_Counts[5] = Count_B/100;
  33.          Buffer_Counts[4] = Count_B % 100 /10;
  34.          Buffer_Counts[3] = Count_B % 10;
  35.          
  36.          if(Buffer_Counts[5] == 0)
  37.      {
  38.                  Buffer_Counts[5] = 10;
  39.                  if(Buffer_Counts[4] == 0)Buffer_Counts[4] = 10;
  40.          }         
  41.      
  42.          for(i=0;i<6;i++)
  43.          {
  44.                  PORTC = SCAN_BITs[i];
  45.                  PORTB = SEG_CODE[Buffer_Counts[i]];
  46.                  _delay_ms(1);
  47.          }
  48. }
  49. int main(void)
  50. {
  51.     DDRB = 0xFF;PORTB = 0xFF;
  52.         DDRC = 0xFF;PORTC = 0xFF;
  53.         DDRD = 0x00;PORTD = 0xFF;
  54.         MCUCR = 0x0A;
  55.         GICR = 0xC0;
  56.         sei();
  57.        
  58.         while(1)
  59.     {
  60.         if(K1_CLEAR_ON())Count_A = 0;
  61.                 if(K2_CLEAR_ON())Count_B = 0;
  62.                 Show_Counts();   
  63.         }
  64. }


  65. ISR(INT0_vect)
  66. {
  67.         Count_A++;
  68. }

  69. ISR(INT1_vect)
  70. {
  71.         Count_B++;
  72. }

本帖子中包含更多资源

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

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

本版积分规则

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

2403

主题

6994

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2403

主题

6994

帖子

68

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