[Atmel] 用AtmelStudio6.2跑mega16例程(14):查询按键

[复制链接]
1047|0
 楼主| ddllxxrr 发表于 2014-9-15 19:38 | 显示全部楼层 |阅读模式
这个程序一看就明白,8个按键每按下一个显示想应的数字。

以下是protel仿真的结果。


以下是Studio 6.2跑通的程序:

  1. /*
  2. * GccApplication21.c
  3. *
  4. * Created: 2014-9-15 18:52:56
  5. *  Author: Administrator
  6. */


  7. #include <avr/io.h>
  8. #define uchar unsigned char
  9. #define uint unsigned int
  10. uchar tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,  //共阳极LED  0~F的段码
  11. 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
  12. void delay(uint n)
  13. {   uint i;
  14.         for(i=0;i<n;i++);
  15. }
  16. int main(void)
  17. {
  18.         uchar key;
  19.         DDRC=0xFF;
  20.         PORTC=0xFF;
  21.         DDRD=0x00;
  22.         PORTD=0xFF;
  23.         while(1)
  24.         {
  25.                 while(PIND==0xFF);                //等待键按下
  26.                 {
  27.                         delay(2500);                    // 延时出去抖动
  28.                         while(PIND==0xFF);
  29.                         {
  30.                                 key=PIND;                         //读取键值
  31.                                 switch(key)
  32.                                 {
  33.                                         case 0xfe :   PORTC=tab[1]; break;
  34.                                         case 0xfd :   PORTC=tab[2]; break;
  35.                                         case 0xfb :          PORTC=tab[3]; break;
  36.                                         case 0xf7 :   PORTC=tab[4]; break;
  37.                                         case 0xef :   PORTC=tab[5]; break;
  38.                                         case 0xdf :   PORTC=tab[6]; break;
  39.                                         case 0xbf :   PORTC=tab[7]; break;
  40.                                         case 0x7f :   PORTC=tab[8]; break;
  41.                                 }
  42.                         }
  43.                 }
  44.         }
  45. }


本帖子中包含更多资源

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

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

本版积分规则

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

2404

主题

7001

帖子

68

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