这个程序一看就明白,8个按键每按下一个显示想应的数字。
以下是protel仿真的结果。
以下是Studio 6.2跑通的程序:
- /*
- * GccApplication21.c
- *
- * Created: 2014-9-15 18:52:56
- * Author: Administrator
- */
- #include <avr/io.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, //共阳极LED 0~F的段码
- 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
- void delay(uint n)
- { uint i;
- for(i=0;i<n;i++);
- }
- int main(void)
- {
- uchar key;
- DDRC=0xFF;
- PORTC=0xFF;
- DDRD=0x00;
- PORTD=0xFF;
- while(1)
- {
- while(PIND==0xFF); //等待键按下
- {
- delay(2500); // 延时出去抖动
- while(PIND==0xFF);
- {
- key=PIND; //读取键值
- switch(key)
- {
- case 0xfe : PORTC=tab[1]; break;
- case 0xfd : PORTC=tab[2]; break;
- case 0xfb : PORTC=tab[3]; break;
- case 0xf7 : PORTC=tab[4]; break;
- case 0xef : PORTC=tab[5]; break;
- case 0xdf : PORTC=tab[6]; break;
- case 0xbf : PORTC=tab[7]; break;
- case 0x7f : PORTC=tab[8]; break;
- }
- }
- }
- }
- }
|