修改头文件和硬件连接。
#include <iom128v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
//DO数据
uchar scan[9]={0x01,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
//存放DI数据
uchar re_code[9];
uint m;
//共阴数码管段码
uchar ddd[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
//位码
uchar www[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void port_init(void)
{
DDRE=0xff;
PORTE=0x00; //段选
DDRB=0xff;
PORTB=0xff; //位选
//PD0->DAT输入;PD1->CMD输出;PD2->CS输出;PD3->CLK输出
DDRD =(0<<PD0)|(1<<PD1)|(1<<PD2)|(1<<PD3) ;
PORTD =0;
}
void delay(uint n)
{
uint i;
for(i=0;i<n;i++);
}
uchar scanout(uchar command) //向手柄发送命令并接受反馈的子程序
{
uchar i,j=1;
uchar res=0;
for(i=0;i<8;i++) //逐位接收
{
if(command&0x01) cmd_set;
else cmd_clr;
command=command>>1;
delay(1);
clk_clr;
delay(1); //这里延时很关键,太多了手柄就反应迟钝了
if(PIND&0x01) res=res+j; //这里就开始同步接收DI值了
j=j<<1;
clk_set;
delay(2);
}
return res;
}
void Read_PS2(void) //向手柄发送DO并将反馈DI存入re_code[ ]中
{
uchar i;
cs_clr;
for(i=0;i<9;i++) re_code=scanout(scan);
cs_set;
}
void main(void)
{
port_init();
delay(50);
while(1)
{
Read_PS2();
delay(200);
m=re_code[3]*256+re_code[4];
PORTE=ddd[m/256/16];
PORTB=www[3];
delay(50);
PORTE=ddd[m/256%16];
PORTB=www[2];
delay(50);
PORTE=ddd[m%256/16];
PORTB=www[1];
delay(50);
PORTE=ddd[m%256%16];
PORTB=www[0];
delay(50);
PORTE=ddd[out[7]/16];
PORTB=www[7];
delay(50);
PORTE=ddd[out[7]%16];
PORTB=www[6];
delay(50);
PORTE=ddd[out[8]/16];
PORTB=www[5];
delay(50);
PORTE=ddd[out[8]%16];
PORTB=www[4];
delay(50);
}
} |