- #include
- #include
- #define uchar unsigned char
- #define uint unsigned int
- void delay(uint i){while(i--);}
- ***it cs1=P2^0; ***it cs2=P2^1;***it en=P2^2;***it rw=P2^3;
- ***it rs=P2^4; ***it wr=P2^5; ***it LED=P2^6; ***it f=P2^7;
- ***it wl1=P1^0;***it wl2=P1^1;***it wl3=P1^2;***it wl4=P1^3;
- ***it wl5=P1^4;***it wl6=P1^5;***it wl7=P1^6;***it col0=P1^7;
- ***it col1=P3^0;***it col2=P3^1;***it col3=P3^2;
- ***it w1=P3^3; ***it w2=P3^4;***it w3=P3^5;
- ***it w4=P3^6;***it w5=P3^7;
- ***it row0=P0^0;***it row1=P0^1;***it row2=P0^2;***it row3=P0^3;
- #define zuo {w1=0;w2=1;}
- #define you {w1=1;w2=0;}
- #define stop {w1=1;w2=1;}
- #define jia {w3=0;w4=1;}
- #define song {w3=1;w4=0;}
- #define shang {w5=1;}
- #define xia {w5=0;}
- uchar code ledcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
- 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x19,0x10,
- 0xbf,0xff};
- uchar buf[8];
- uchar code zm16x16l[]="产品手动包装控制白黄色工作状态左右移上升下降故障";
- uchar code zm16x16[][32]=
- {
- /*-- 文字: 产 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0x04,0xE4,0x44,0x4C,0x74,0x54,0x45,0x46,0x64,0x54,0x4C,0x44,0x64,0x44,0x00,
- 0x40,0x30,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- /*-- 文字: 品 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0x00,0x00,0x00,0x7E,0x22,0x22,0x22,0x22,0x22,0x22,0x7E,0x00,0x00,0x00,0x00,
- 0x00,0x7F,0x21,0x21,0x21,0x21,0x7F,0x00,0x7F,0x21,0x21,0x21,0x21,0x7F,0x00,0x00,
- /*-- 文字: 手 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0x24,0x24,0x24,0x24,0x24,0x24,0xFE,0x22,0x22,0x22,0x22,0x22,0x20,0x00,0x00,
- 0x02,0x02,0x02,0x02,0x02,0x42,0x82,0x7F,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x00,
- /*-- 文字: 动 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x20,0x24,0x24,0xE4,0x24,0x24,0x24,0x20,0x10,0x10,0xFF,0x10,0x10,0xF0,0x00,0x00,
- 0x08,0x1C,0x0B,0x08,0x0C,0x05,0x4E,0x24,0x10,0x0C,0x03,0x20,0x40,0x3F,0x00,0x00,
- /*-- 文字: 包 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x80,0x40,0x20,0xD8,0x4F,0x4A,0x48,0x48,0x48,0xC8,0x08,0x08,0xFC,0x08,0x00,0x00,
- 0x00,0x00,0x00,0x3F,0x42,0x42,0x42,0x42,0x42,0x43,0x48,0x50,0x4F,0x40,0x70,0x00,
- /*-- 文字: 装 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0x42,0x2C,0x24,0x10,0xFF,0x04,0x64,0xA4,0x24,0x3F,0x24,0x24,0x24,0x04,0x00,
- 0x20,0x21,0x11,0x11,0xF9,0x45,0x23,0x03,0x05,0x09,0x11,0x29,0x45,0xC1,0x41,0x00,
- /*-- 文字: 控 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x08,0x08,0x08,0xFF,0x88,0x48,0x00,0x98,0x48,0x28,0x0A,0x2C,0x48,0xD8,0x08,0x00,
- 0x02,0x42,0x81,0x7F,0x00,0x00,0x40,0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0x40,0x00,
- /*-- 文字: 制 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0x50,0x4F,0x4A,0x48,0xFF,0x48,0x48,0x48,0x00,0xFC,0x00,0x00,0xFF,0x00,0x00,
- 0x00,0x00,0x3F,0x01,0x01,0xFF,0x21,0x61,0x3F,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,
- /*-- 文字: 白 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0x00,0xF8,0x08,0x08,0x0C,0x0B,0x08,0x08,0x08,0x08,0x08,0xF8,0x00,0x00,0x00,
- 0x00,0x00,0x7F,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x7F,0x00,0x00,0x00,
- /*-- 文字: 黄 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x20,0x24,0x24,0xA4,0xA4,0xBF,0xA4,0xE4,0xA4,0xBF,0xA4,0xA4,0x24,0x24,0x20,0x00,
- 0x00,0x80,0x80,0x5F,0x32,0x12,0x12,0x1F,0x12,0x12,0x32,0x5F,0xC0,0x00,0x00,0x00,
- /*-- 文字: 色 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0x40,0x20,0xF0,0x28,0x27,0x24,0xE4,0x24,0x34,0x2C,0xE4,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x3F,0x42,0x42,0x42,0x43,0x42,0x42,0x42,0x43,0x40,0x78,0x00,0x00,
- /*-- 文字: 工 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0x00,0x02,0x02,0x02,0x02,0x02,0xFE,0x02,0x02,0x02,0x02,0x02,0x02,0x00,0x00,
- 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00,
- /*-- 文字: 作 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x80,0x40,0x20,0xF8,0x07,0x22,0x18,0x0C,0xFB,0x48,0x48,0x48,0x68,0x48,0x08,0x00,
- 0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0xFF,0x04,0x04,0x04,0x04,0x06,0x04,0x00,
- /*-- 文字: 状 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x08,0x30,0x00,0xFF,0x20,0x20,0x20,0x20,0xFF,0x20,0xE1,0x26,0x2C,0x20,0x20,0x00,
- 0x04,0x02,0x01,0xFF,0x40,0x20,0x18,0x07,0x00,0x00,0x03,0x0C,0x30,0x60,0x20,0x00,
- /*-- 文字: 态 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0x04,0x04,0x04,0x84,0x44,0x34,0x4F,0x94,0x24,0x44,0x84,0x84,0x04,0x00,0x00,
- 0x00,0x60,0x39,0x01,0x00,0x3C,0x40,0x42,0x4C,0x40,0x40,0x70,0x04,0x09,0x31,0x00,
- /*-- 文字: 左 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x08,0x08,0x08,0x08,0x08,0xC8,0x3F,0x0A,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00,
- 0x40,0x20,0x50,0x4C,0x43,0x41,0x41,0x41,0x7F,0x41,0x41,0x41,0x41,0x40,0x40,0x00,
- /*-- 文字: 右 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x08,0x08,0x08,0x88,0xC8,0x38,0x0F,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00,
- 0x04,0x02,0x01,0x00,0x7F,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x7F,0x00,0x00,0x00,
- /*-- 文字: 移 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x10,0x92,0x72,0xFE,0x91,0x19,0x90,0x88,0x44,0x53,0xA2,0x52,0x0A,0x06,0x00,0x00,
- 0x02,0x01,0x00,0xFF,0x00,0x81,0x88,0x44,0x46,0x29,0x11,0x09,0x05,0x03,0x01,0x00,
- /*-- 文字: 上 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x40,0x40,0x40,0x40,0x40,0x00,0x00,0x00,
- 0x00,0x40,0x40,0x40,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x40,0x40,0x60,0x40,0x00,
- /*-- 文字: 升 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0x40,0x44,0x44,0x44,0xFE,0x42,0x42,0x40,0x40,0xFE,0x40,0x40,0x60,0x40,0x00,
- 0x00,0x40,0x20,0x10,0x0C,0x03,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00,
- /*-- 文字: 下 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0x02,0x02,0x02,0x02,0x02,0x02,0xFE,0x22,0x62,0xC2,0x82,0x02,0x03,0x02,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
- /*-- 文字: 降 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0xFE,0x22,0x5A,0x86,0x00,0x48,0x44,0x2B,0xD2,0x2A,0x26,0x42,0x40,0x40,0x00,
- 0x00,0xFF,0x02,0x04,0x03,0x00,0x0D,0x09,0x09,0xFF,0x09,0x09,0x09,0x08,0x00,0x00,
- /*-- 文字: 故 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x10,0x90,0x90,0xFF,0x90,0x90,0x10,0x80,0xF0,0x1F,0x12,0x10,0xF0,0x10,0x10,0x00,
- 0x00,0x3F,0x10,0x10,0x10,0x5F,0x41,0x20,0x20,0x13,0x0C,0x13,0x20,0x60,0x20,0x00,
- /*-- 文字: 障 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
- 0x00,0xFE,0x22,0x5A,0x86,0x10,0xD2,0x56,0x5A,0x53,0x52,0x5A,0xD6,0x12,0x10,0x00,
- 0x00,0xFF,0x02,0x04,0x13,0x10,0x17,0x15,0x15,0xFD,0x15,0x15,0x17,0x10,0x10,0x00,
- };
- uchar code zm8x16l[]="0123456789";
- uchar code zm8x16[][16]=
- {
- /*-- 文字: 0 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,
- /*-- 文字: 1 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,
- /*-- 文字: 2 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,
- /*-- 文字: 3 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,
- /*-- 文字: 4 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,
- /*-- 文字: 5 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,
- /*-- 文字: 6 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,
- /*-- 文字: 7 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,
- /*-- 文字: 8 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,
- /*-- 文字: 9 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,
- };
- /* 键盘扫描 */
- uchar key()
- {
- uchar k=25;
- row0=0; k=col0==0?0:col1==0?1:col2==0?2:col3==0?3:k; row0=1;
- row1=0; k=col0==0?4:col1==0?5:col2==0?6:col3==0?7:k; row1=1;
- row2=0; k=col0==0?8:col1==0?9:col2==0?10:col3==0?11:k; row2=1;
- row3=0; k=col0==0?12:col1==0?13:col2==0?14:col3==0?15:k; row3=1;
- return k;
- }
- /* 写命令 */
- void wc(uchar com)
- {
- rs=0;rw=0;
- P0=com;
- en=1;en=0;
- }
- /* 写数据 */
- void wd(uchar dat)
- {
- rs=1;rw=0;
- P0=dat;
- en=1;en=0;
- }
- /* 清屏 */
- void clr()
- {
- uchar i,j;
- cs1=cs2=1;
- for(i=0;i<8;i++)
- {
- wc(0xb8+i);
- wc(0x40);
- for(j=0;j<64;j++)wd(0);
- }
- }
- /* 12864初始化 */
- void init()
- {
- wc(0x3f);
- wc(0xc0);
- clr();
- }
- /* 显示任意字模 */
- void showxy(uchar row,uchar col,uchar x,uchar y,uchar *tab)
- {
- bit right;
- uchar i,j;
- if(col<64){right=0;}
- else {col-=64;right=1;}
- for(i=0;i
- {
- if(right)cs1=0,cs2=1;
- else cs1=1,cs2=0;
- wc(0xb8+row+i);
- wc(0x40+col);
- for(j=0;j
- {
- if(col+j<64)wd(tab[i*x+j]);
- else if(right==0)
- {
- cs1=0,cs2=1;
- wc(0xb8+row+i);
- wc(0x40+(col+j-64));
- wd(tab[i*x+j]);
- }
- }
- }
- }
- /* 8x16的索引 */
- uchar enn(uchar *s)
- {
- uchar i;
- for(i=0;zm8x16l[i]!=0;i++)if(zm8x16l[i]==s[0])break;
- return i;
- }
- /* 16x16的索引 */
- uchar cnn(uchar *s)
- {
- uchar i;
- for(i=0;zm16x16l[i]!=0;i+=2)
- if(zm16x16l[i]==*s&&zm16x16l[i+1]==*(s+1))break;
- return i/2;
- }
- /* 显示8x16及16x16的索引 */
- void showstr(uchar row,uchar col,uchar *s)
- {
- uchar i;
- for(i=0;s[i]!=0;i++)
- {
- if(s[i]<0x80)
- {
- showxy(row,col,8,2,zm8x16[enn(&s[i])]);
- col+=8;
- }
- else
- {
- showxy(row,col,16,2,zm16x16[cnn(&s[i])]);
- col+=16;i+=1;
- }
- if(col>120)
- {
- col=0;row+=2;
- }
- }
- }
- /* 数码管的驱动 */
- void dis()
- {
- uchar i,j=0x7f;
- for(i=0;i<8;i++)
- {
- cs1=0;cs2=1;
- P0=ledcode[buf[i]];wr=1;wr=0;
- cs1=1;cs2=0;
- P0=j;wr=1;wr=0;
- delay(40);
- j=(j>>1)|0x80;
- cs1=cs2=0;
- P0=0xff;wr=1;wr=0;
- }
- }
- bit runflag=1,timeflag=1,timesz,keyflag,dian,stopflag=1; //运行,时间,时间设置,按键,小数点,停止的标志
- uchar bs=0,hs=0,sec,min; //白色球,黄色球,秒,分的定义
- uchar n=0,moveflag,flag; //n是机械手工步,moveflag是工作状态标志,flag夹球颜色判断,
- void ka() //键值和控制设置
- {
- uchar i,k;
- if(key()!=25&&keyflag==0)
- {
- keyflag=1;
- if(key()==13&&runflag==1&&timeflag==1)
- {
- while(key()==13);
- bs++; //白色球设置
- if(bs>10)bs=0; //到10就归0
- }
- if(key()==14&&runflag==1&&timeflag==1)
- {
- while(key()==14);
- hs++; //黄色球设置
- if(hs>10)hs=0; //到时就归零
- }
- if(key()==12)timesz=1; //时间设置按钮被按下
- if(key()<10&×z==1) //键盘值小于十
- {
- k=key();
- for(i=0;i<7;i++)buf[i]=buf[i+1]; //buf[7]显示当前数值,将之前的数值往前推
- buf[7]=k;
- }
- else if(key()==10&&dian==1) //按键按下小数点,并且没有重复按下
- {
- dian=0;
- buf[7]=buf[7]+10; //将原来的数值加上十,就成为了有小数点的数
- }
- if(buf[7]<10)dian=1;
- min=buf[5]-10; //分的取值
- sec=buf[6]*10+buf[7]; //秒的取值
- if(key()==11&×z==1&&(bs!=0||hs!=0)&&(sec!=0||min!=0)) //按下运行键,时间被设置完成,球的数量也被设置完成
- {
- runflag=0; //运行标志变为 0 表示在运行
- stopflag=1; //停止标志置 1
- }
- if(runflag==0&&key()==15)stopflag=0; //按下暂停键,停止标志置 0
- if(stopflag==0){stop;runflag=1;} //停止,并且运行键置 1
- }
- //按键松开或没有按下的标志
- if(key()==25)keyflag=0;
- }
- void jxs() //机械手运行函数
- {
- switch(n)
- {
- case 0:
- if(wl5){xia;moveflag=4;break;} //左移并显示
- n=1;
- case 1:
- delay(20000); //延时0.2秒等待机械手判断
- n=2;
- case 2:
- if(wl7==0)flag=0; //flag=0是黄色,白色相反
- else flag=1;
- n=3;
- case 3:
- if(wl6){jia;break;} //夹球
- n=4;
- case 4:
- if(wl6==0){n=5;break;} //夹紧就下一步
- else {moveflag=5;break;} //故障显示
- case 5:
- if(wl4){shang;moveflag=3;break;} //上升并显示
- n=6;
- case 6:
- if(wl3){zuo;moveflag=1;break;} //左移并显示
- stop;
- n=7;
- case 7:
- if(wl6==0){song;break;} //松球
- if(flag==0)hs--; //减少黄球数
- else bs--; //减少白球数
- n=8;
- case 8:
- if(wl1){you;moveflag=2;break;} //右移并显示
- stop;
- if(bs==0&&hs==0){runflag=1;moveflag=6;} //运球完成 开始包装
- n=0;
- }
- }
- void init1() //初始化函数a
- {
- TMOD=0x01;
- EA=ET0=1;
- init();
- showstr(0,0,"产品手动包装控制"); //12864显示
- buf[0]=buf[1]=buf[2]=buf[3]=20; //显示 ---------
- buf[4]=buf[5]=buf[6]=buf[7]=20;
- while(wl1)you; //机械手归位
- stop; //移到一工位停止
- song; //松开爪子
- delay(50000);
- showstr(3,0,"白色"); //12864显示
- showstr(3,64,"黄色");
- }
- void main() //主函数
- {
- uchar z,x; //蜂鸣器及灯的闪烁
- end:
- init1();
- while(1)
- {
- showxy(3,40,8,2,zm8x16[bs%10]); //白球数量显示
- showxy(3,104,8,2,zm8x16[hs%10]); //黄球数量显示
- ka();
- if(runflag==0) //系统运行
- { //定时器开
- TR0=1; //时间设置置 1
- timesz=1; //机械手开启
- jxs();
- buf[5]=min%10+10; //时间倒计时显示
- buf[6]=sec/10%10;
- buf[7]=sec%10;
- /* 工作状态显示 */
- showstr(6,0,"工作状态");
- if(moveflag==1)showstr(6,80,"左移");
- else if(moveflag==2)showstr(6,80,"右移");
- else if(moveflag==3)showstr(6,80,"上升");
- else if(moveflag==4)showstr(6,80,"下降");
- else if(moveflag==5) //故障报警
- {
- showstr(6,80,"故障"); //蜂鸣器以1秒五个周期报警
- delay(10000);
- f=!f;
- }
- if(sec==0&&min==0){runflag=1;stop;} //时间到就停止
- }
- /* 抓球结束 */
- if(moveflag==6) //包装
- {
- showstr(6,80,"包装");
- for(z=0;z<40;z++) //循环二十秒
- {
- delay(25550);
- f=!f; //蜂鸣器一秒一周期
- for(x=0;x<2;x++)
- {
- delay(25550);
- LED=!LED; //LED0一秒两周期
- }
- }
- moveflag=0; //完成所有操作回到初始化
- goto end;
- }
- dis();
- }
- }
- void time() interrupt 1 //定时1
- {
- TH0=0xfc;
- TL0=0x17;
- if(++c==1000) //运行时分秒的运行
- {
- c=0;
- sec--;
- if(sec==0&&min>0)
- {
- sec=60;
- min--;
- }
- }
- }
复制代码
0
|
|
|
|