[Atmel] 用AtmelStudio6.2跑mega16例程(40) 叮咚门呤

[复制链接]
 楼主| ddllxxrr 发表于 2014-10-27 20:13 | 显示全部楼层 |阅读模式
定时器初始定时为700us,按键使能定时器溢出中断后,在前300ms时间内的第次中断触发时间间隔都是0.7ms使得DoorBell()可以输出1000/(0.7*2)=714Hz的频率,在后500ms内的每次中断触发时间间隔为1ms(初值为-1000),DoorBell()输出1000/(1.0*2)=500Hz的频率。

Proteus 仿真的波形:咚

Proteus仿真的波形:叮

Studio6.2编译通过截图:


程序清单:
  1. /*
  2. * GccApplication3.c
  3. *
  4. * Created: 2014-10-27 19:50:51
  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. #define DoorBell()(PORTD^=0x01)  
  13. #define Key_DOWN()((PINB & 0x80) == 0x00)
  14. volatile uint16_t soundDelay;
  15. int main(void)
  16. {
  17.     DDRB = 0x00;PORTB = 0xFF;
  18.         DDRD = 0xFF;
  19.         TCCR1B = 0x01;
  20.         TCNT1 = -700;
  21.         sei();
  22.        
  23.        
  24.         while(1)
  25.     {
  26.        if(Key_DOWN())
  27.            {
  28.               TIMSK = _BV(TOIE1);  //允许T1定时器溢出中断
  29.                   soundDelay = -700;
  30.                   _delay_ms(400);
  31.                   soundDelay = -1000;
  32.                   _delay_ms(600);
  33.                   TIMSK = 0x00;
  34.           
  35.            }
  36.     }
  37. }

  38. ISR(TIMER1_OVF_vect)
  39. {
  40.         DoorBell();
  41.         TCNT1 = soundDelay;
  42. }



本帖子中包含更多资源

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

×
jiang390625 发表于 2014-10-29 15:53 | 显示全部楼层
支持下,
sssha 发表于 2014-10-29 16:01 | 显示全部楼层
不错,学习了。
okyouwin 发表于 2014-10-29 16:22 | 显示全部楼层
学习下AtmelStudio还不太熟。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

2403

主题

6994

帖子

68

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

2403

主题

6994

帖子

68

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