#include <mega16.h>
flash unsigned char led_7[13]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0X07,0X7F,0X6F,0x77,0x7C,0x40};
flash unsigned char position[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
unsigned char dis_buff[8];
unsigned char key_time_counter;
unsigned char posit;
unsigned char key_time_ok;
void display(void)
{
PORTC=0xff;
PORTA=led_7[dis_buff[posit]];
PORTC=position[posit];
if(++posit>=8)posit=0;
}
#pragma interrupt_handler timer0_comp_isr:20
void timer0_comp_isr(void)
{
display();
if(++key_time_counter>=5)
{
key_time_counter=0;
key_time_ok=1;
}
}
# define No_key 255
# define K1_1 1
# define K1_2 2
# define K1_3 3
# define K2_1 4
# define K2_2 5
# define K2_3 6
# define K3_1 7
# define K3_2 8
# define K3_3 9
# define K4_1 10
# define K4_2 0
# define K4_3 11
# define Key_mask 0b00000111
unsigned char read_keyboard()
{
static unsigned char key_state=0,key_value,key_line;
unsigned char key_return=No_key,i;
switch(key_state)
{
case0:
key_line=0b00001000;
for(i=1;i<=4;i++)
{
PORTD=~key_line;
PORTD=~key_line;
key_value=Key_mask&PIND;
if(key_value==Key_mask)
key_line<<=1;
else
{
key_state++;
break;
}
}
break;
case1:
if(key_value==(Key_mask&PIND))
{
switch(key_line|key_value)
{
case 0b00001110: key_return = K1_1;break;
case 0b00001101: key_return = K1_2;break;
case 0b00001011: key_return = K1_3;break;
case 0b00010110: key_return = K2_1;break;
case 0b00010101: key_return = K2_2;break;
case 0b00010011: key_return = K2_3;break;
case 0b00100110: key_return = K3_1;break;
case 0b00100101: key_return = K3_2;break;
case 0b00100011: key_return = K3_3;break;
case 0b01000110: key_return = K4_1;break;
case 0b01000101: key_return = K4_2;break;
case 0b01000011: key_return = K4_3;break;
}
key_state++;
}
else
key_state--;
break;
case2:
PORTD=0b00000111;
PORTD=0b00000111;
if((Key_mask&PIND)==Key_mask)
key_state=0;
break;
}
return key_return;
}
void main(void)
{
unsigned char i,key_temp;
PORTA=0x00;
DDRA=0xFF;
PORTC=0xFF;
DDRC=0xFF;
PORTD=0xFF;
DDRD=0xF8;
TCCR0=0x0B;
TCNT0=0x00;
OCR0=0x7C;
TIMSK=0x02;
for(i=0;i<8;i++)
{dis_buff[i]=12;}
SREG=SREG|0x80;
while(1)
{
if(key_time_ok)
{
key_time_ok=0;
key_temp=read_keyboard();
if(key_temp!=No_key)
{
for(i=0;i<7;i++)
{dis_buff[i]=dis_buff[i+1];}
dis_buff[7]=key_temp;
}
}
};
}
这程序有哪些错可以帮忙纠正吗,硬件要怎么接线
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |