[Atmel] 用AtmelStudio6.2跑mega16例程(33)按键发声

[复制链接]
 楼主| ddllxxrr 发表于 2014-10-14 20:51 | 显示全部楼层 |阅读模式
这个程序就是按下一个键就发一种声音, 以下是仿真:

以下是Studio6.2编译通过截图:


以下是程序:
  1. /*
  2. * GccApplication18.c
  3. *
  4. * Created: 2014-10-14 20:30:19
  5. *  Author: Administrator
  6. */

  7. #define __DELAY_BACKWARD_COMPATIBLE__
  8. #define  F_CPU 0x8000000UL

  9. #include <avr/io.h>
  10. #include <util/delay.h>
  11. #include <stdint.h>

  12. #define  BEEP() (PORTD ^= 0x80)

  13. #define  K1_DOWN() ((PINB & 0x10)== 0x00)
  14. #define  K2_DOWN() ((PINB & 0x20)== 0x00)
  15. #define  K3_DOWN() ((PINB & 0x40)== 0x00)
  16. #define  K4_DOWN() ((PINB & 0x80)== 0x00)

  17. void Play(uint8_t ddd)
  18. {
  19.         uint8_t i;
  20.         for(i=0;i<100;i++)
  21.         {
  22.                 BEEP();_delay_ms(ddd);
  23.         }
  24. }


  25. int main(void)
  26. {
  27.     uint8_t Pre_key = 0xFF;
  28.         DDRB = 0x00;PORTB = 0xFF;
  29.         DDRD = 0xFF;
  30.         while(1)
  31.         {
  32.                 while(Pre_key == PINB);
  33.                 Pre_key = PINB;
  34.                 if(K1_DOWN()) Play(1);
  35.                 if(K2_DOWN()) Play(2);
  36.                 if(K3_DOWN()) Play(3);
  37.                 if(K4_DOWN()) Play(4);
  38.                
  39.         }
  40.        
  41. }


本帖子中包含更多资源

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

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

本版积分规则

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

2403

主题

6994

帖子

68

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