本帖最后由 chengdeshi 于 2018-10-21 19:17 编辑
#include "reg51.h"
#include "intrins.h"
#include "absacc.h"
#define uchar unsigned char
#define uint unsigned int
#define PA XBYTE[0X1fFF]
#define PB XBYTE[0x3fff]
#define PC XBYTE[0X5fff]
#define PD XBYTE[0X7fff]
bit szflag=1,runflag=1,stopflag=1,moveflag=1;
uchar n,num,sec,n=0,t;
uint runcount,endcount,c=0;
sbit sw=P1^0; sbit f=P1^4; sbit cp=P1^1;
sbit dir=P1^2; sbit rl=P1^3;
#define mc {cp=1;delay(50);cp=0;}
sbit row0=P3^0;sbit row1=P3^1;sbit col0=P3^2;
sbit col1=P3^3;sbit col2=P3^4;sbit col3=P3^5;
bdata uchar jxb;
sbit wl1=jxb^0;
sbit wl2=jxb^1;
sbit wl3=jxb^2;
sbit wl4=jxb^3;
sbit wl5=jxb^4;
sbit wl6=jxb^5;
sbit wl7=jxb^6;
sbit w1=P2^0;
sbit w2=P2^1;
sbit w3=P2^2;
sbit w4=P2^3;
sbit w5=P2^4;
#define zuo {w1=0;w2=1;}
#define you {w1=1;w2=0;}
#define stop {w1=w2=1;}
#define shang {w5=1;}
#define xia {w5=0;}
#define jia {w3=0;w4=1;}
#define song {w3=1;w4=0;}
bdata uchar aj;
sbit sb1=aj^0;
sbit sb2=aj^1;
sbit sb3=aj^2;
sbit sb4=aj^3;
sbit sb5=aj^4;
sbit sb6=aj^5;
sbit sb7=aj^6;
sbit sb8=aj^7;
void delay(uint i){while(--i);}
//字模数组,一个8*8字符占1行
uchar code zm[][8] =
{
0x08,0xF8,0x04,0xF4,0x12,0xF1,0x10,0xF0, //-0- 复
0x20,0xF0,0x30,0x48,0x84,0x42,0x20,0x1C,
0x10,0x3F,0x00,0x0F,0x08,0x0F,0x08,0x0F,
0x00,0x0F,0x04,0x02,0x01,0x02,0x0C,0x70,
0x10,0x10,0x10,0x08,0xE8,0x0C,0x4A,0x49, //-1- 位
0x88,0x88,0x08,0x08,0x08,0xF8,0x08,0x08,
0x01,0x02,0x02,0x10,0x3F,0x00,0x10,0x10,
0x08,0x08,0x09,0x05,0x24,0x7F,0x00,0x00,
0x10,0x10,0xC8,0x04,0x12,0x10,0xE8,0x0C, //-2- 待
0x0A,0xE9,0x08,0x48,0x88,0x88,0x08,0x08,
0x02,0x12,0x3F,0x02,0x02,0x22,0x7F,0x08,
0x28,0x7F,0x08,0x08,0x08,0x08,0x0A,0x04,
0x08,0x08,0x88,0x88,0xBF,0x88,0x8C,0x9C, //-3- 机
0xAA,0x8A,0x89,0x88,0x88,0x48,0x28,0x18,
0x00,0x08,0x1F,0x08,0x08,0x08,0x08,0x08,
0x08,0x08,0x08,0x08,0x48,0x48,0x70,0x00,
0x40,0x80,0xFE,0x02,0x01,0xF8,0x00,0x00, //-4- 完
0xFE,0x20,0x20,0x20,0x10,0x10,0x08,0x06,
0x00,0x00,0x7F,0x40,0x24,0x0F,0x00,0x10,
0x3F,0x01,0x01,0x01,0x41,0x41,0x7E,0x00,
0x00,0x00,0x00,0xFC,0x04,0x04,0x04,0x7C, //-5- 成
0x44,0x44,0x44,0x54,0x24,0x02,0x81,0x00,
0x01,0x05,0x09,0x3F,0x01,0x01,0x21,0x22,
0x12,0x12,0x0C,0x04,0x46,0x49,0x50,0x60,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //-6- 不显示
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x63,0x73,0x6B,0x6B, // -0-
0x67,0x63,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x1C,0x1E,0x18,0x18,0x18, // -1-
0x18,0x18,0x18,0x7E,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x60,0x30,0x18,0x0C, // -2-
0x06,0x03,0x63,0x7F,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x60,0x60,0x3C,0x60, // -3-
0x60,0x60,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x30,0x38,0x3C,0x36,0x33,0x7F, // -4-
0x30,0x30,0x30,0x78,0x00,0x00,0x00,0x00,
0x00,0x00,0x7F,0x03,0x03,0x03,0x3F,0x70, // -5-
0x60,0x60,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x1C,0x06,0x03,0x03,0x3F,0x63, // -6-
0x63,0x63,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x7F,0x63,0x60,0x60,0x30,0x18, // -7-
0x0C,0x0C,0x0C,0x0C,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x63,0x63,0x3E,0x63, // -8-
0x63,0x63,0x63,0x3E,0x00,0x00,0x00,0x00,
0x00,0x00,0x3E,0x63,0x63,0x63,0x7E,0x60, // -9-
0x60,0x60,0x30,0x1E,0x00,0x00,0x00,0x00,
};
uchar buf[8]; //缓存
void disp3216(){
uchar i,n;
uint row=0x01;
for(i=0;i<16;i++){
n=i<8?0:1;
P0=zm[buf[0+4*n]][i-8*n]; col0=1;col0=0;
P0=zm[buf[1+4*n]][i-8*n]; col1=1;col1=0;
P0=zm[buf[2+4*n]][i-8*n]; col2=1;col2=0;
P0=zm[buf[3+4*n]][i-8*n]; col3=1;col3=0;
P0=row%0x100; row0=1;row0=0;
P0=row/0x100; row1=1;row1=0;
delay(50); row=_irol_(row,1);
P0=0;
row0=row1=col0=col1=col2=col3=1;
row0=row1=col0=col1=col2=col3=0;
}
}
/* 送8*16字模到缓存 */
/* 参数:c为位置,n为8*16字符编号 */
void s8x16(uchar c,uchar n){
c%=4; //限制取值0~3
n=n*2+28; //计算字模实际行号
buf[c]=n; //上半部分
buf[c+4]=n+1; //下半部分
}
/* 送16*16字模到缓存 */
/* 参数:c为位置,n为16*16字符编号 */
void s16x16(uchar c,uchar n){
c%=3; //限制取值0~2
n=n*4+0; //计算字模实际行号
buf[c]=n;buf[c+1]=n+2; //上半部分
buf[c+4]=n+1;buf[c+5]=n+3; //下半部分
}
void init()
{
uint i,q;
s16x16(0,0);
s16x16(2,1);
for(i=0;i<100;i++)disp3216();
jxb=PB;
while(wl1==1){you;jxb=PB;}
stop;
song;
shang;
dir=1;
while(rl==0)mc;
dir=0;
for(q=0;q<1500;q++)mc;
runcount=0;
s16x16(0,2);
s16x16(2,3);
}
void bj(void)
{
for(;runcount<endcount;runcount++)
{
dir=0;
mc;
}
for(;runcount>endcount;runcount--)
{
dir=1;
mc;
}
}
void jxs()
{
switch(n)
{
case 0:
if(runflag==0)n=1;TR0=1;break;
case 1:
if(wl6==0&&wl7==0)
{
if(moveflag==0){n=2;moveflag=1;}
else
{
if(wl2)
{
zuo;
moveflag=0;
n=2;
}
}
stop;
break;
}
case 2:
xia;
n=3;
break;
}
}
uchar j=1;
void main()
{
TMOD=0x01;
EA=ET0=1;
PD=0x8b;
if(sw==0)
{
init();
while(1)
{
jxb=PB;
aj=PC;
if(sb1==0&&runflag==1)
{
szflag=0; //设置键
}
if(szflag==0)
{
s8x16(0,j/10%10); //设置显示
s8x16(1,j%10);
s16x16(2,6);
}
if(szflag==0&&sb2==0) //加一
{
delay(20000);
j++;
}
else if(szflag==0&&sb4==0) //加十
{
delay(20000);
j+=10;
}
else if(szflag==0&&sb6==0) //减一
{
delay(20000);
j--;
}
else if(szflag==0&&sb8==0) //减十
{
delay(20000);
j-=10;
}
if(sb3==0)szflag=1; //确认键
if(szflag==1&&j>0&&sb7==0)runflag=0; //运行键
if(j>50)
{
j=50; //总箱数在1~50
f=1; //无效蜂鸣器响0.5秒
delay(55550);
}
else f=0;
if(runflag==0)
{
jxs();
s8x16(0,j/10%10);
s8x16(1,j%10);
s8x16(2,sec/10%10);
s8x16(3,sec%10);
}
if(runflag==0&&sb5==0){stopflag=0;runflag=1;}
bj(); //步进电机控制
disp3216();
}
}
/*else
{
stop;
shang;
s16x16(0,6);
s16x16(2,6);
} */
}
void time0() interrupt 1
{
TH0=0xfc;
TL0=0x17;
if(c++==1000)
{
c=0;
sec++;
if(sec==60)sec=0;
}
}
|