Mega16按键与数码显示程序
时间:11-29
来源:互联网
点击:
#include
#include#define No_key 255 #define K1_11 #define K1_22 #define K1_33 #define K1_4 4 #define K2_15 #define K2_26 #define K2_37 #define K2_4 8 #define K3_19 #define K3_20 #define K3_310 #define K3_4 11 #define K4_112 #define K4_213 #define K4_314 #define K4_4 15 #define Key_mask0b00001111 #define data PORTB.0 #define clk PORTB.1 flash unsigned char led[16]={0x28,0xeb,0x19,0x89,0xca,0x8c,0x0c,0xe9,0x08,0x88, 0xdf,0x4a,0x1c,0x3e,0x7e,0x2a}; unsigned charkey_stime_counter; unsigned char key_temp; bitkey_stime_ok; //D0接键盘,低4位列线输入,高4位行线输出 unsigned char read_keyboard() { static unsigned char key_state = 0, key_value, key_line; unsigned char key_return = No_key,i; switch (key_state) { case 0: key_line = 0b00010000; for (i=1; i<=4; i++)// 扫描键盘 { PORTA = ~key_line;// 输出行线电平 PORTA = ~key_line;// 必须送2次!!! key_value = Key_mask & PINA;// 读列电平 if (key_value == Key_mask) key_line <= 1;// 没有按键,继续扫描 else { key_state++;// 有按键,停止扫描 break;// 转消抖确认状态 } } break; case 1: if (key_value == (Key_mask & PINA))// 再次读列电平, { switch (key_line | key_value)// 与状态0的相同,确认按键 {// 键盘编码,返回编码值 case 0b00011110: key_return = K1_1; break; case 0b00011101: key_return = K1_2; break; case 0b00011011: key_return = K1_3; break; case 0b00010111: key_return = K1_4; break; case 0b00101110: key_return = K2_1; break; case 0b00101101: key_return = K2_2; break; case 0b00101011: key_return = K2_3; break; case 0b00100111: key_return = K2_4; break; case 0b01001110: key_return = K3_1; break; case 0b01001101: key_return = K3_2; break; case 0b01001011: key_return = K3_3; break; case 0b01000111: key_return = K3_4; break; case 0b10001110: key_return = K4_1; break; case 0b10001101: key_return = K4_2; break; case 0b10001011: key_return = K4_3; break; case 0b10000111: key_return = K4_4; break; } key_state++;// 转入等待按键释放状态 } else { key_state--; delay_ms(5); }// 两次列电平不同返回状态0,(消抖处理) break; case 2:// 等待按键释放状态 PORTA = 0b00001111;// 行线全部输出低电平 PORTA = 0b00001111;// 重复送一次 if ( (Key_mask & PINA) == Key_mask) key_state=0;// 列线全部为高电平返回状态0 break; } return key_return; } //向数码管送入数据 void sendbyte(unsigned char byte) { unsigned char num,c; num=led[byte]; for(c=0;c<8;c++) { clk=0; data=num&0x01; clk=1; num>>=1; } } void display(void) { if (key_stime_ok) { key_stime_ok = 0;// 10ms到 key_temp = read_keyboard();// 调用键盘接口函数读键盘 if (key_temp != No_key) {// 有按键按下 sendbyte(key_temp); delay_ms(10); } } } void main(void) { DDRB = 0xFF; //PORTC = 0xFF;// 键盘接口初始化 DDRA = 0xF0;// PD2、PD1、PD0列线,输入方式,上拉有效 // T/C0 初始化 TCCR0=0x0B;// 内部时钟,64分频(4M/64=62.5KHz),CTC模式 TCNT0=0x00; OCR0=0x7C;// OCR0 = 0x7C(124),(124+1)/62.5=2ms TIMSK=0x02;// 允许T/C0比较匹配中断 #asm("sei")// 开放全局中断 while (1) { display(); } } // Timer 0 比较匹配中断服务,2ms定时 interrupt [TIM0_COMP] void timer0_comp_isr(void) { //display();// 调用LED扫描显示 if (++key_stime_counter >=5) { key_stime_counter = 0; key_stime_ok = 1;// 10ms到 } }
Mega16按键数码显 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)