- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar display_digit[]={0,0,0};
- uchar code run[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};//A→AB→B→BC→C→CD→D→DA→A
- uchar disbuf[4]={0,0,0,0};
- uchar code LED_0F[] =
- {
- 0x3f,0x06,0x5b,0x4f,0x66,0x6d, 0x7d,0x07,0x7f,0x6f,0x63
- };
- unsigned char K_Value=0;
- unsigned int key_temp1=0;
- unsigned int jiaodu=0;
- uchar num1=0,num2=0,num3=0,num4=0,leiji=0;
- sbit DIO = P0^1; //串行数据输入
- sbit RCLK = P0^2; //时钟脉冲信号——上升沿有效
- sbit SCLK = P0^0; //打入信号————上升沿有效
- bit Flag = 0;
- // 全局变量
- uchar LED[8]; //用于LED的8位显示缓存
- void delay(uint time)
- {
- uint x,y;
- for(x=time;x>0;x--)
- for(y=110;y>0;y--);
- }
- void LED_OUT(uchar X)
- {
- uchar i;
- for(i=8;i>=1;i--)
- {
- if (X&0x80) DIO=1; else DIO=0;
- X<<=1;
- SCLK = 0;
- SCLK = 1;
- }
- }
- /**************************************************************************************
- * 描 述 : LED显示函数
- * 入 参 : 无
- * 返回值 : 无
- **************************************************************************************/
- void LED8_Display (void)
- {
- uchar *led_table; // 查表指针
- uchar i;
- //显示第1位
- led_table = LED_0F + LED[0];
- i = *led_table;
- LED_OUT(i);
- LED_OUT(0xfe);
- RCLK = 0;
- RCLK = 1;
- //显示第2位
- led_table = LED_0F + LED[1];
- i = *led_table;
- LED_OUT(i);
- LED_OUT(0xfd);
- RCLK = 0;
- RCLK = 1;
- //显示第3位
- led_table = LED_0F + LED[2];
- i = *led_table;
- LED_OUT(i);
- LED_OUT(0xfb);
- RCLK = 0;
- RCLK = 1;
- //显示第4位
- led_table = LED_0F + LED[3];
- i = *led_table;
- LED_OUT(i);
- LED_OUT(0xf7);
- RCLK = 0;
- RCLK = 1;
- //显示第6位
- led_table = LED_0F + LED[5];
- i = *led_table;
- LED_OUT(i);
- LED_OUT(0xdf);
- RCLK = 0;
- RCLK = 1;
- //显示第7位
- led_table = LED_0F + LED[6];
- i = *led_table;
- LED_OUT(i);
- LED_OUT(0xbf);
- RCLK = 0;
- RCLK = 1;
- //显示第8位
- led_table = LED_0F + LED[7];
- i = *led_table;
- LED_OUT(i);
- LED_OUT(0x7f);
- RCLK = 0;
- RCLK = 1;
- }
- void zheng()
- {
- static uchar i=0,k;
- if(Flag == 1)
- {
- if(jiaodu<key_temp1)
- {
- while(jiaodu<key_temp1)
- {
- while(i<8)
- {
- P2=run[i];
- delay(15);//转速调节
- if(++k>=2)//1个脉冲转一个步距角
- {
- k = 0;
- ++jiaodu;
- if(jiaodu >= key_temp1)break;
- }
- if(++i>=8)i=0;
- }
- }
- k = 0;
- if(i>=8)i=0;
- while(i<8)
- {
- if(++i>=8)i=0;
- P2=run[i];
- delay(15);//转速调节
- if(++k>=2)//1个脉冲转一个步距角
- {
- k = 0;
- break;
- }
-
- }
- }
- else if(jiaodu > key_temp1)
- {
- while(jiaodu > key_temp1)
- {
- while(i>=0)
- {
-
- P2=run[i];
- delay(15);//转速调节
- if(++k >= 2)//1个脉冲转一个步距角
- {
- k = 0;
- --jiaodu;
- if(jiaodu <= key_temp1)break;
- }
-
- if(i-- <= 0)i=7;
- }
- }
- k = 0;
- if(i < 0)i = 7;
- while(i < 8)
- {
- if(i-- <= 0)i=7;
- P2=run[i];
- delay(15);//转速调节
- if(++k>=2)//1个脉冲转一个步距角
- {
- k = 0;
- break;
- }
-
- }
- }
- leiji=0;num2=0;num3=0;num4=0;
- Flag = 0;
- }
- }
- void Init_Timer0(void)
- {
- TMOD &= 0xF0; //设置定时器模式
- TMOD |= 0x01; //设置定时器模式
- TL0 = 0x30; //设置定时初值
- TH0 = 0xF8; //设置定时初值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- ET0=1; //定时器中断打开
- EA=1; //总中断打开
- }
- /******按键值处理函数********/
- void key(void)
- {
- switch(K_Value)
- {
- case 0:
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- case 8:
- case 9:
- num4=num3;
- num3=num2;
- num2=K_Value;
- leiji++;//记录按下的次数
- key_temp1=num4*100+num3*10+num2;
- break;
- case 10://R
- break;
- case 11://L
- break;
- case 12://ENTER
- if(key_temp1)
- Flag = 1;
- break;
- }
- }
- void main()
- {
- Init_Timer0();
- while(1)
- {
- LED[0]=key_temp1/100%10;
- LED[1]=key_temp1/10%10;
- LED[2]=key_temp1%10;;
- LED[3]=10;
- LED[5]=jiaodu/100%10;
- LED[6]=jiaodu/10%10;
- LED[7]=jiaodu%10;
- zheng();
- if(leiji>2)//按下的次数大于3次时清零十位百位
- {leiji=0;num2=0;num3=0;num4=0;}
- }
- }
- void T0_OverFlow(void) interrupt 1 //定时器0溢出中断,2ms进入一次
- {
- static uint cnt = 0;
- static uchar key_zt = 0;
- unsigned char X,Y,Z;
-
- TH0 = 0xf8; //12m 2000
- TL0 = 0x30;
- if(++cnt >= 5)
- {
- P1=0xff;
- _nop_();_nop_();_nop_();
- cnt = 0;
- P1=0x0f;
- switch(key_zt)
- {
- case 0:
- if(P1!=0x0f)
- key_zt = 1;
- else K_Value = 100;
- break;
-
- case 1:
- if(P1!=0x0f)
- {
- key_zt = 2;
- X=P1; //保存行扫描时有键按下时状态
- P1=0xf0; //列扫描
- Y=P1; //保存列扫描时有键按下时状态
- Z=X|Y; //取出键值
- /*********************************************************************/
- switch ( Z ) //判断键值(那一个键按下)
- {
- case 0xee: K_Value=0; break; //对键值赋值
- case 0xed: K_Value=1; break;
- case 0xeb: K_Value=2; break;
- case 0xe7: K_Value=3; break;
- case 0xde: K_Value=4; break;
- case 0xdd: K_Value=5; break;
- case 0xdb: K_Value=6; break;
- case 0xd7: K_Value=7; break;
- case 0xbe: K_Value=8; break;
- case 0xbd: K_Value=9; break;
- case 0xbb: K_Value=10;break;
- case 0xb7: K_Value=11;break;
- case 0x7e: K_Value=12;break;
- case 0x7d: K_Value=13;break;
- case 0x7b: K_Value=14;break;
- case 0x77: K_Value=15;break;
- }
- key();
- }
- else
- {
- K_Value = 100;
- key_zt = 0;
- }
- case 2:
- if(P1 == 0x0f)
- key_zt = 0;
- break;
- }
- }
- LED8_Display(); //4位数码管显示:4 3 2 1
- }
复制代码
程序无法编译,仿真也不能运行,求大神指点:
矩阵键盘控制步进电机到达设定步距角.rar
(147.17 KB, 下载次数: 5)
|