[Atmel] 用AtmelStudio6.2跑mega16例程(44)工作于异步模式的数字时钟

[复制链接]
1051|0
 楼主| ddllxxrr 发表于 2014-10-31 22:40 | 显示全部楼层 |阅读模式
本例使用T/C2定时器工作于异步模式,由PB6(TOSC1)与PB7(TOSC)外接32768Hz晶振提供时钟,通过设置异步状态可选择T/C2的时钟源,T/C2的时钟频率为32768Hz/64 = 512Hz

以下是Proteus截图:

以下是Studio6.2溢出中断:

程序清单:
  1. /*
  2. * GccApplication7.c
  3. *
  4. * Created: 2014-10-31 21:15:59
  5. *  Author: Administrator
  6. */
  7. #define F_CPU 4000000UL
  8. #include <avr/interrupt.h>
  9. #include <avr/io.h>
  10. #include <util/delay.h>
  11. #include <stdint.h>
  12. const uint8_t SEG_CODE[] = {0x3F,0x06,0x58,0x4F,0x66,0x6D,0x7d,0x07,0x7F,0x6F,0x00};
  13. uint8_t Disp_Buffer[] = {0,0,0x40,0,0,0x40,0,0};
  14. uint8_t Disp_Idx;
  15. uint8_t Key_State = 0xFF;
  16. uint8_t h,m,s;

  17. void Increase_Hour()
  18. {
  19.    if(++h >23) h = 0;
  20.    Disp_Buffer[0] = SEG_CODE[h/10];
  21.    Disp_Buffer[1] = SEG_CODE[h % 10];
  22. }

  23. void Increase_Minute()
  24. {
  25.     if(++m > 59)
  26.         {
  27.            m = 0; Increase_Hour();
  28.         }
  29.         Disp_Buffer[3] = SEG_CODE[m/10];
  30.         Disp_Buffer[4] = SEG_CODE[m % 10];
  31. }

  32. void Increase_Second()
  33. {
  34.     if(++s >59)
  35.         {
  36.                 s = 0; Increase_Minute();
  37.         }       
  38.         Disp_Buffer[6] = SEG_CODE[s/10];
  39.         Disp_Buffer[7] = SEG_CODE[s % 10];
  40. }
  41. int main(void)
  42. {
  43.     DDRA = 0xFF; PORTA = 0xFF;
  44.         DDRD = 0xFF; PORTD = 0xFF;
  45.         DDRB = 0x00; PORTB = 0xFF;
  46.        
  47.         TCCR0 = 0x03;
  48.         TCNT0 = 256 - F_CPU/64.0 * 0.004;
  49.         ASSR = 0x08;
  50.         TCCR2 = 0x04;
  51.         TCNT2 = 0;
  52.         TIMSK =  _BV(TOIE2) | _BV(TOIE0);
  53.        
  54.         h = 12; m=s=0;
  55.         Disp_Buffer[0] = SEG_CODE[h/10];
  56.         Disp_Buffer[1] = SEG_CODE[h%10];
  57.         Disp_Buffer[3] = SEG_CODE[m/10];
  58.         Disp_Buffer[4] = SEG_CODE[m %10];
  59.         Disp_Buffer[6] = SEG_CODE[s/10];
  60.         Disp_Buffer[7] = SEG_CODE[s%10];
  61.         sei();
  62.        
  63.         while(1)
  64.     {
  65.         if(PINB ^Key_State)
  66.                 {
  67.                         _delay_ms(10);
  68.                         if(PINB ^ Key_State)
  69.                         {
  70.                                 Key_State = PINB;
  71.                                 if(!(Key_State&_BV(PB3)))
  72.                                 Increase_Hour();
  73.                                 else
  74.                                 if(!(Key_State&_BV(PB6)))
  75.                                 Increase_Minute();
  76.                         }
  77.                 }
  78.     }
  79. }

  80. ISR(TIMER0_OVF_vect)
  81. {
  82.    static uint8_t Disp_Idx = 0;
  83.    TCNT0 = 256 - F_CPU/64.0*0.004;
  84.    PORTA = 0x00;
  85.    PORTA = Disp_Buffer[Disp_Idx];
  86.    PORTD= ~_BV(Disp_Idx);
  87.    Disp_Idx = (Disp_Idx+1)&0x07;       
  88. }

  89. ISR(TIMER2_OVF_vect)
  90. {
  91.    if(Disp_Buffer[2] == 0x40)
  92.    {
  93.        Disp_Buffer[2] = Disp_Buffer[5] = 0x00;
  94.    }
  95.    else
  96.    {
  97.            Disp_Buffer[2] = Disp_Buffer[5] = 0x40;
  98.            Increase_Second();
  99.    }

  100. }
  101.        
  102.        


本帖子中包含更多资源

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

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

本版积分规则

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

2404

主题

7002

帖子

68

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