本帖最后由 lxe 于 2014-8-4 16:35 编辑
。。码字比较累。。直接上程序。。- #include <iom161v.h>
- #include <macros.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar seg[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7f,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x3F};
- /* 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 */
- uchar key = 0;
- /*******************************************************
- //函数名称:uchar get_key(void)
- //函数功能:获取按键值
- //输入参数:无
- //返回参数:按键值key
- ********************************************************/
- uchar get_key(void)
- {
- //PA 7 6 5 4 3 2 1 0
- PORTA = 0xFE; //PA0拉低 1 1 1 1 1 1 1 0
- if(PINA==0xFE) {key = 0;} //没有按键按下 1 1 1 1 1 1 1 0
- if(PINA!=0xFE) //有按键按下
- {
- if(PINA==0xEE) {key = 16;} //K16按下PA4被拉低 1 1 1 0 1 1 1 0
- if(PINA==0xDE) {key = 12;} //K12按下PA5被拉低 1 1 0 1 1 1 1 0
- if(PINA==0xBE) {key = 8;} //K8按下PA6被拉低 1 0 1 1 1 1 1 0
- if(PINA==0x7E) {key = 4;} //K4按下PA7被拉低 0 1 1 1 1 1 1 0
- }
- //PA 7 6 5 4 3 2 1 0
- PORTA = 0xFD; //PA1拉低 1 1 1 1 1 1 0 1
- if(PINA==0xFD) {key = 0;} //没有按键按下 1 1 1 1 1 1 0 1
- if(PINA!=0xFD) //有按键按下
- {
- if(PINA==0xED) {key = 15;} //K15按下PA4被拉低 1 1 1 0 1 1 0 1
- if(PINA==0xDD) {key = 11;} //K11按下 PA5被拉低 1 1 0 1 1 1 0 1
- if(PINA==0xBD) {key = 7;} //K7按下PA6被拉低 1 0 1 1 1 1 0 1
- if(PINA==0x7D) {key = 3;} //K3按下PA7被拉低 0 1 1 1 1 1 0 1
- }
- //PA 7 6 5 4 3 2 1 0
- PORTA = 0xFB; //PA2拉低 1 1 1 1 1 0 1 1
- if(PINA==0xFB) {key = 0;} //没有按键按下 1 1 1 1 1 0 1 1
- if(PINA!=0xFB) //有按键按下
- {
- if(PINA==0xEB) {key = 14;} //K14按下PA4被拉低 1 1 1 0 1 0 1 1
- if(PINA==0xDB) {key = 10;} //K10按下PA5被拉低 1 1 0 1 1 0 1 1
- if(PINA==0xBB) {key = 6;} //K6按下PA6被拉低 1 0 1 1 1 0 1 1
- if(PINA==0x7B) {key = 2;} //K2按下PA7被拉低 0 1 1 1 1 0 1 1
- }
- //PA 7 6 5 4 3 2 1 0
- PORTA = 0xF7; //PA3拉低 1 1 1 1 0 1 1 1
- if(PINA==0xF7) {key = 0;} //没有按键按下 1 1 1 1 0 1 1 1
- if(PINA!=0xF7) //有按键按下
- {
- if(PINA==0xE7) {key = 13;} //K13按下PA4被拉低 1 1 1 0 0 1 1 1
- if(PINA==0xD7) {key = 9;} //K9按下PA5被拉低 1 1 0 1 0 1 1 1
- if(PINA==0xB7) {key = 5;} //K5按下PA6被拉低 1 0 1 1 0 1 1 1
- if(PINA==0x77) {key = 1;} //K1按下PA7被拉低 0 1 1 1 0 1 1 1
- }
- return key;
- }
- /*******************************************************
- //函数名称:int main(void)
- //函数功能:主函数
- //输入参数:无
- //返回参数:0
- ********************************************************/
- int main(void)
- {
- DDRA |= 0x0F;
- PORTA |= 0xFF;
- DDRB |= 0xFF;
- PORTB |= 0xFF;
- while(1)
- {
- PORTB = seg[get_key()];
- }
- return 0;
- }
复制代码
|