我想用16*16的点阵模拟电梯运行时的楼层显示,但是写出来的程序一直显示不出来,按按键也没反应,程序如下:(用4片595控制点阵)
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code NUM_HANG[]={
0x80,0x00,0x40,0x00,0x20,0x00,0x10,0x00,0x08,0x00,0x04,0x00,0x02,0x00,0x01,0x00,
0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x10,0x00,0x08,0x00,0x04,0x00,0x02,0x00,0x01,};
uchar code Table_of_Digits[8][32]=
{//数字8的字码
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC0,0x02,0x20,0x02,0x20,
0x03,0xE0,0x02,0x20,0x02,0x20,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
},
//数字7的字码
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xE0,0x00,0x20,0x00,0x20,
0x00,0x40,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
},
//数字6的字码
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC0,0x02,0x20,0x03,0xC0,
0x02,0x20,0x02,0x20,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
},
//数字5的字码
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xE0,0x02,0x00,0x03,0xC0,
0x00,0x20,0x00,0x20,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
},
//数字4的字码
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0xC0,0x01,0x40,
0x02,0x40,0x07,0xE0,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
},
//数字3的字码
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC0,0x02,0x20,
0x00,0xC0,0x00,0x20,0x02,0x20,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
},
//数字2的字码
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC0,0x02,0x20,0x00,0x20,
0x00,0x40,0x00,0x80,0x01,0x00,0x03,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
},
//数字1的字码
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x03,0x80,0x00,0x80,0x00,0x80,
0x00,0x80,0x00,0x80,0x00,0x80,0x03,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
},
};
uchar i=0,t=0,j=0,a=0,m=0,n=0;
char offset=0;
static int Current_floor=0,Pressed_Key_up=0, Pressed_Key_down=0,b=0,c=0,max=0,min=0;// Current_floor当前楼层 Pressed_Key_up 上升请求 Pressed_Key_down 下降请求
int flag;
int floor_up[8]={9,9,9,9,9,9,9,9};
int floor_down[8]={0,0,0,0,0,0,0,0};
sbit KEY1=P2^0; //定义按键P1口
sbit KEY2=P2^1;
sbit KEY3=P2^2;
sbit KEY4=P2^3;
sbit KEY5=P2^4;
sbit KEY6=P2^5;
sbit KEY7=P2^6;
sbit KEY8=P2^7;
sbit SPEAK=P0^7; //定义蜂鸣器
sbit SCK=P3^6;
sbit LCK=P3^5;
sbit SDI=P3^4;
void Key_up_comparison(); //函数声明,把floor_up[]从小到大排列
void Key_down_comparison(); //把floor_down[]从大到小排列
void max_min_comparison(); //比较出floor_up[]的最大值(max)和flor_down[]的最小值(min)
void HC595_Send(uchar dat1,uchar dat0,uchar dat2,uchar dat3);
void main() //主函数
{
SPEAK=0;
TMOD=0x01; //T0方式
TH0=(-6000)/256; //6ms定时
TL0=(-6000)%256;
IT0=1;
IE=0x83;
TR0=1;
ET0=1;
EA=1;
Pressed_Key_up=floor_up[0];
while(1)
{
Key_up_comparison();
Key_down_comparison();
max_min_comparison();
}
}
void LED_Display() interrupt 1 //进入中断,显示楼层 interrupt1
{
TH0=(-6000)/256; //6ms
TL0=(-6000)%256;
Pressed_Key_up=floor_up[0];
Pressed_Key_down=floor_down[0];
if(flag==1) //上升标志
{
if(Pressed_Key_up!=9)
{
if(Pressed_Key_up>Current_floor)
{
m=(7-Current_floor)+i;
for(n=0;n<32;n=n+2) //一共有16行,所以刷16次
{
HC595_Send(~Table_of_Digits[m][n],~Table_of_Digits[m][n+1],NUM_HANG[n],NUM_HANG[n+1]);
}
if(++i==8)
{
i=0;
}
if(++t==250)
{
t=0;
++Current_floor;
}
}
else if(Pressed_Key_up<Current_floor);
else if(Pressed_Key_up==Current_floor) //到达请求楼层
{
m=(7-Current_floor)+i;
for(n=0;n<32;n=n+2) //一共有16行,所以刷16次
{
HC595_Send(~Table_of_Digits[m][n],~Table_of_Digits[m][n+1],NUM_HANG[n],NUM_HANG[n+1]);
}
if(++i==8)
{
i=0;
}
SPEAK=~SPEAK; //蜂鸣器响
if(++t==250)
{
t=0;
if(Current_floor=Pressed_Key_up)
{
Current_floor=Pressed_Key_up;
if(Current_floor==max)
{
flag=0;
}
floor_up[0]=9;
}
}
}
}
else if(Pressed_Key_up==9) //无按键按下,即无请求,则扫描当前楼层
{
m=(7-Current_floor)+i;
for(n=0;n<32;n=n+2) //一共有16行,所以刷16次
{
HC595_Send(~Table_of_Digits[m][n],~Table_of_Digits[m][n+1],NUM_HANG[n],NUM_HANG[n+1]);
}
if(++i==8)
{
i=0;
}
if(++t==250)
{
t=0;
}
}
}
else if(flag==0)
{
if(Pressed_Key_down!=0) //有下降的请求
{
if(Pressed_Key_down>Current_floor);
else if(Pressed_Key_down<Current_floor) //下降的显示
{
m=(7-Current_floor)+i+offset;
for(n=0;n<32;n=n+2) //一共有16行,所以刷16次
{
HC595_Send(~Table_of_Digits[m][n],~Table_of_Digits[m][n+1],NUM_HANG[n],NUM_HANG[n+1]);
}
if(++i==8)
{
i=0;
}
if(++t==25)
{
t=0;
if(--offset==-8)
{
offset=0;
--Current_floor;
}
}
}
else if(Pressed_Key_down==Current_floor) //到达请求的楼层
{
m=(7-Current_floor)+i;
for(n=0;n<32;n=n+2) //一共有16行,所以刷16次
{
HC595_Send(~Table_of_Digits[m][n],~Table_of_Digits[m][n+1],NUM_HANG[n],NUM_HANG[n+1]);
}
if(++i==8)
{
i=0;
}
SPEAK=~SPEAK; //蜂鸣器响
if(++t==250)
{
t=0;
if(Current_floor=Pressed_Key_down)
{
Current_floor=Pressed_Key_down;
if(Current_floor==min)
{
flag=1;
}
floor_down[0]=0;
}
}
}
}
else if(Pressed_Key_down==0) //无按键按下,即请求,则扫描显示当前楼层
{
m=(7-Current_floor)+i;
for(n=0;n<32;n=n+2) //一共有16行,所以刷16次
{
HC595_Send(~Table_of_Digits[m][n],~Table_of_Digits[m][n+1],NUM_HANG[n],NUM_HANG[n+1]);
}
if(i==8)
{
i=0;
}
if(t==250)
{ t=0;}
}
}
}
void Key_detection() interrupt 0 //按下按键,进入外部中断,将上升请求和下降请求放入数组中去,并且判断标志位flag
{
// P0=0xff;
i=0; //按键消抖
if(KEY1==0) j=8;
else if(KEY2==0) j=7;
else if(KEY3==0) j=6;
else if(KEY4==0) j=5;
else if(KEY5==0) j=4;
else if(KEY6==0) j=3;
else if(KEY7==0) j=2;
else if(KEY8==0) j=1;
if(j>Current_floor) //将上升请求放入数组中
{
if(floor_down[0]==0)
flag=1;
floor_up[b++]=j;
if(b==8)
b=0;
}
else if(j<Current_floor) //将下降请求放入数组
{
if(floor_up[0]==9)
flag=0;
floor_down[c++]=j;
if(c==8)
c=0;
}
else if(j==Current_floor);
}
void Key_up_comparison() //把floor_up[]按从小到大的顺序排列
{
int a1,a2,a3=0;
for(a2=0;a2<7;a2++)
{
for(a1=0;a1<7;++a1)
{
if(floor_up[a1]>floor_up[a1+1])
{
a3=floor_up[a1];
floor_up[a1]=floor_up[a1+1];
floor_up[a1+1]=a3;
}
if(floor_up[a1]==floor_up[a1+1])
floor_up[a1+1]=9;
}
}
}
void Key_down_comparison() //把floor_down[]按从大到小的顺序排列
{
int a1,a2,a3=0;
for(a2=0;a2<7;a2++)
{
for(a1=0;a1<7;++a1)
{
if(floor_down[a]<floor_down[a1+1])
{
a3=floor_down[a1];
floor_down[a1]=floor_down[a1+1];
floor_down[a1+1]=a3;
}
if(floor_down[a1]=floor_down[a1+1])
floor_down[a1+1]=0;
}
}
}
void max_min_comparison() //求出 floor_up[]的最大(max)和floor_down[]的最小(min)
{
int j;
for(j=1;j<7;j++)
if(floor_down[j]==0);
else if(floor_down[j+1]==0)
min=floor_down[j];
else if(floor_down[j]>floor_down[j+1])
min=floor_down[j+1];
else min=floor_down[j];
for(j=1;j<7;j++)
if(floor_up[j]==9);
else if(floor_up[j+1]==9)
max=floor_up[j];
else if(floor_up[j]<floor_up[j+1])
max=floor_up[j+1];
else max=floor_up[j];
}
void HC595_Send(uchar dat1,uchar dat0,uchar dat3,uchar dat2) //显示程序
{
uchar i;
SCK=1;
LCK=1;
for(i=0;i<8;i++)
{
SDI=dat0&0x01; //从高位到低位
dat0>>=1;
SCK=0;
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
}
for(i=0;i<8;i++)
{
SDI=dat1&0x01; //从高位到低位
dat1>>=1;
SCK=0;
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
}
for(i=0;i<8;i++)
{
SDI=dat2&0x01; //从高位到低位
dat2>>=1;
SCK=0;
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
}
for(i=0;i<8;i++)
{
SDI=dat3&0x01; //从高位到低位
dat3>>=1;
SCK=0;
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
}
LCK=0; //下面是输出时钟
_nop_();
_nop_();
LCK=1;
_nop_();
_nop_();
} |