[Atmel] 用AtmelStudio6.2跑mega16例程(31)键控LED

[复制链接]
 楼主| ddllxxrr 发表于 2014-10-9 20:51 | 显示全部楼层 |阅读模式
本帖最后由 ddllxxrr 于 2014-10-9 20:53 编辑

本程序通过四个按键来控制两排LED的显示,每次只有一个灯亮。

分别控制着第一排的上下和第二排水的上下。

程序如下:

  1. /*
  2. * GccApplication14.c
  3. *
  4. * Created: 2014-10-9 20:27:09
  5. *  Author: Administrator
  6. */

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

  11. int8_t i=0,j=0;

  12. void Move_LED()
  13. {
  14.    if((PINB &0x01)==0x00) i= (i-1) & 0x07;
  15.    else if ((PINB & 0x02)==0x00) i=(i+1) &0x07;
  16.    else if ((PINB & 0x04)==0x00) j = (j-1)&0x07;
  17.    else if ((PINB & 0x08)==0x00) j = (j+1)&0x07;
  18.    
  19.    PORTC = ~(1<<i);
  20.    PORTD = ~(1<<j);
  21. }

  22. int main(void)
  23. {
  24.     int8_t Recent_Key = 0x00;
  25.         DDRB = 0x00;PORTB=0xFF;
  26.         DDRC = 0xFF;PORTC=0xFE;
  27.         DDRD = 0xFF;PORTD=0xFE;
  28.         while(1)
  29.     {
  30.         if(PINB!= Recent_Key)
  31.                 {
  32.                    Recent_Key = PINB;
  33.                    Move_LED();
  34.                    _delay_ms(10);
  35.                 }
  36.     }
  37. }
仿真截图:



编译通过截图:



本帖子中包含更多资源

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

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

本版积分规则

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

2403

主题

6994

帖子

68

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

2403

主题

6994

帖子

68

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