[Atmel] 用AtmelStudio6.2跑mega16例程(42) 记数器

[复制链接]
957|1
 楼主| ddllxxrr 发表于 2014-10-29 20:58 | 显示全部楼层 |阅读模式
100000秒以内记数器。
本例程是用外部中断,按一下开始记数,再按一下停止,再按一下清零。

以下是Proteus的运行截图:


以下是Studio6.2的程序的截图:


程序清单:
  1. /*
  2. * GccApplication5.c
  3. *
  4. * Created: 2014-10-29 20:19:55
  5. *  Author: Administrator
  6. */

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

  12. const uint8_t SEG_CODE[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
  13. uint8_t Digits_Buffer[] = {0,0,0,0,0,0};
  14.        
  15. void Show_Count_ON_DSY()
  16. {
  17.         uint8_t i;
  18.         for(i=0;i<=5;i++)       
  19.         {
  20.                 PORTC = 0x00;
  21.                 PORTA = ~_BV(5-i);
  22.                 PORTC = SEG_CODE[Digits_Buffer[i]];
  23.                 if(i==1)PORTC |= 0x80;
  24.                 _delay_ms(3);
  25.         }
  26. }
  27. int main(void)
  28. {
  29.     DDRA = 0xFF;PORTA = 0xFF;
  30.         DDRC = 0xFF;PORTC = 0xFF;
  31.         DDRD = 0x00;PORTD = 0xFF;
  32.         MCUCR = 0x02;
  33.         GICR = 0x40;
  34.         TCNT1 = 65536 - F_CPU/8*0.1;
  35.         TIMSK = _BV(TOIE1);
  36.         sei();
  37.         while(1) Show_Count_ON_DSY();
  38. }

  39. ISR(TIMER1_OVF_vect)
  40. {
  41.         uint8_t i;
  42.         TCNT1 = 65536-F_CPU/8*0.1;
  43.         Digits_Buffer[0]++;
  44.         for(i=0;i<=5;i++)
  45.         {
  46.                 if(Digits_Buffer[i] == 10)
  47.                 {
  48.                         Digits_Buffer[i]=0;
  49.                         if(i!=5) Digits_Buffer[i+1]++;
  50.                 }
  51.                 else break;
  52.         }
  53. }

  54. ISR(INT0_vect)
  55. {
  56.         uint8_t i;
  57.         static uint8_t KeyOperation = 0;
  58.         if(++KeyOperation==3)KeyOperation=0;
  59.         switch(KeyOperation)
  60.         {
  61.                
  62.                 case 0:TCCR1B = 0x00;
  63.                 for(i=0;i<6;i++)Digits_Buffer[i] = 0;
  64.                 break;
  65.                 case 1:TCCR1B = 0x02;
  66.                 break;
  67.                 case 2:TCCR1B = 0x00;
  68.                 break;
  69.         }
  70. }

本帖子中包含更多资源

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

×
丶tiantian 发表于 2014-10-30 15:55 | 显示全部楼层
,收藏以下。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

2404

主题

7001

帖子

68

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