delay_1ms();
}
unsignedcharreceive_byte(void)
{
unsignedchari,in_byte;
unsignedcharp;
PORTB=(PORTB&0xfe);//cs=0;
DDRB=(DDRB&0xf7);
PORTB&=0xf7;
//PORTB=(PORTB|0x08);//data=1;
//long_delay();
PORTB=(PORTB&0xfb);//clk=0;
for(i=0;i<8;i++)
{
PORTB=(PORTB|0x04);//clk=1;
long_delay();
in_byte=in_byte*2;
p=(PINB&0x08);
if(p)
{
in_byte=in_byte|0x01;
}
PORTB=(PORTB&0xfb);//clk=0;
long_delay();
}
PORTB=(PORTB&0xf7);//data=0;
PORTB=(PORTB|0X01);//
returnin_byte;
}
voidsend_byte(unsignedcharout_byte)
{
unsignedchari;
PORTB=(PORTB&0xfe);//cs=0;
long_delay();
for(i=0;i<8;i++)
{
if(out_byte&0x80)
{
PORTB=(PORTB|0x08);//data=1;
}
else
{
PORTB=(PORTB&0xf7);//data=0;
}
PORTB=(PORTB|0x04);//clk=1;
short_delay();
PORTB=(PORTB&0xfb);//clk=0;
short_delay();
out_byte=out_byte*2;
}
PORTB=(PORTB|0X01);
}
unsignedcharread7279(unsignedcharcommand)
{
send_byte(command);
return(receive_byte());
}
voidport_init(void)//IO口初始化
{
PORTA=0x00;
DDRA=0x00;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x00;
PORTE=0x00;
DDRE=0x00;
PORTF=0x00;
DDRF=0x04;
PORTG=0x00;
DDRG=0x00;
}
//callthisroutinetoinitializeallperipherals
voidinit_devices(void)
{
//stoperrantinterruptsuntilsetup
CLI();//disableallinterrupts
XDIV=0x00;//xtaldivider
XMCRA=0x00;//externalmemory
port_init();
MCUCR=0x00;
EICRA=0x00;//extendedextints
EICRB=0x00;//extendedextints
EIMSK=0x00;
TIMSK=0x00;//timerinterruptsources
ETIMSK=0x00;//extendedtimerinterruptsources
SEI();//re-enableinterrupts
//allperipheralsarenowinitialized
}
voidmain(void)
{
unsignedchara[10]={0,1,2,3,4,5,6,7,8,9};
charm,j,k,l;
intn=0;
intb=0;
intc=0;
intd=0;
init_devices();//初始化
uart1_init();
DDRB=0xff;//B端口输出
DDRA=0x00;//A端口输入
PORTA=0xff;
send_byte(0xa4);
key_number=0xff;
while
(1)
{
/*uart_send_char(0x01+0X30);//将读取的数据发送到串行调试助手中显示
uart_send_char(0x0A);*/
//send_byte(0xbf);
if(!
(PINA&0x01))
{
send_byte(0x15);
PORTB=PORTB|0x10;
long_delay();
key_number=receive_byte();
uart_send_char(key_number);//将读取的数据发送到串行调试助手中显示
uart_send_char(0x0A);
/*PORTB=PORTB|0x10;*/
send_byte(0xc8);
send_byte(a[n]);
send_byte(0xc9);
send_byte(a[b+2]);
send_byte(0xca);
send_byte(a[c]);
send_byte(0xcb);
send_byte(a[d]);
//PORTB=PORTB|0x10;
if(!
(key_number&0x80))
{
n++;
PORTB=PORTB|0x10;
if(n>9)
{
n=0;
b++;
if(b>9)
{
b=0;
c++;
if(c>9)
{
c=0;
d++;
if(d>9)
{
d=0;
}
}
}
}
send_byte(0xc8);
send_byte(a[n]);
send_byte(0xc9);
send_byte(a[b]);
send_byte(0xca);
send_byte(a[c]);
send_byte(0xcb);
send_byte(a[d]);
/*PORTB=PORTB|0x10;*/
delay(1000);//延时20ms
}
else
{
if(!
(key_number&0xfd))
{
n--;
if(n<0)
{
n=9;
b--;
if(b<0)
{
b=9;
c--;
if(c<0)
{
c=9;
d--;
if(d<0)
{
d=9;
}
}
}
}
send_byte(0xc8);
send_byte(a[n]);
send_byte(0xc9);
send_byte(a[b]);
send_byte(0xca);
send_byte(a[c]);
send_byte(0xcb);
send_byte(a[d]);
/*PORTB=PORTB|0x10;*/
delay(1000);//延时20ms
}
else
{
if(!
(key_number&0xfb))
{
n=0;
b=0;
c=0;
d=0;
send_byte(0xc8);
send_byte(a[n]);
send_byte(0xc9);
send_byte(a[b]);
send_byte(0xca);
send_byte(a[c]);
send_byte(0xcb);
send_byte(a[d]);
}
}
}
}
}
}
四、存在问题及解决办法
基本实现计数之后,在及时转换加减时出现问题,最后发现程序中对键盘的判定不准确,更改后实现了及时转换功能;
在实现及时转换功能之后,转换之后的运算并不能正常输出,之后发现是范围超出,对程序加上限制之后很好地实现了功能;
对于键盘按键判断,利用7279自带功能实现,根据时序图,对7279进行赋值,并将CLK,DATA,KEY分时复用,在一个周期内,先实现输出,后将7279值输入到单片机中。
对于调试采用方法:
将KEY-NUMBER的值利用仿真器,输入到电脑中,实现对值的实时读取与判断程序运行状态,效果好。