以下是全部的程序 LCD数据写入的部分我标记了下,求大大们帮个忙
- #include <reg52.h> //此文件中定义了单片机的一些特殊功能寄存器
- #define uchar unsigned char
- #define uint unsigned int
- #define GPIO_DIG P0 //lcd
- #define GPIO_KEY P1 //矩阵键盘
- #define Coil_A1 {A1=1;B1=0;C1=0;D1=0;}//A相通电,其他相断电
- #define Coil_A2 {A1=1;B1=1;C1=0;D1=0;}//AB相通电,其他相断电
- #define Coil_B1 {A1=0;B1=1;C1=0;D1=0;}//B相通电,其他相断电
- #define Coil_B2 {A1=0;B1=1;C1=1;D1=0;}//BC相通电,其他相断电
- #define Coil_C1 {A1=0;B1=0;C1=1;D1=0;}//C相通电,其他相断电
- #define Coil_C2 {A1=0;B1=0;C1=1;D1=1;}//CD相通电,其他相断电
- #define Coil_D1 {A1=0;B1=0;C1=0;D1=1;}//D相通电,其他相断电
- #define Coil_D2 {A1=1;B1=0;C1=0;D1=1;}//DA相通电,其他相断电
- #define LCD_IO P0
- typedef unsigned int u16; //对数据类型进行声明定义
- typedef unsigned char u8;
- sbit jiaoliuzhengzhuan=P2^3;
- sbit jiaoliufanzhuan=P2^4;
- sbit k1=P3^0;
- sbit k2=P3^1;
- sbit A1=P3^2; //定义步进电机连接端口
- sbit B1=P3^3;
- sbit C1=P3^4;
- sbit D1=P3^5;
- int zqianwei,zbaiwei,zshiwei,zgewei;
- int zjiaodu=0;
- int b=0,c=0,d=0;
- int j=0,i=0,k=0;
- int jiaodu=180;
- int shiwei;
- int shanshuo;
- int gewei;
- int e,v,n;
- int baiwei;
- int weight=0;
- int z=0;
- int KeyValue; //用来存放读取到的键值
- sbit sheng1=P1^4;
- sbit sheng2=P1^5;
- sbit LCD_RS = P2^6;
- sbit LCD_RW = P2^5;
- sbit LCD_EN = P2^7;
- char code LCD_line2[] = "Weight: 0kg ";
- char Timer_buf[] = "00";
- char code LCD_line1[] = "jiaodu: 0 du ";
- char weight_1[] = "";
- unsigned char SetPlace;
- unsigned char Speed;
- /*******************************************************************************
- * 函 数 名 : delay
- * 函数功能 : 延时函数,i=1时,大约延时10us
- *******************************************************************************/
- void delay1(uint z)
- {
- uint x, y;
- for(x = z; x > 0; x--) for(y = 100; y > 0; y--);
- }
- //---------------------------------------------------
- void W_LCD_Com(uchar com) //写指令
- {
- LCD_RS = 0; LCD_IO = com; // LCD_RS和R/W都为低电平时,写入指令
- LCD_EN = 1; delay1(5); LCD_EN = 0; //用EN输入一个高脉冲
- }
- //---------------------------------------------------
- void W_LCD_Dat(uchar dat) //写数据
- {
- LCD_RS = 1; LCD_IO = dat; // LCD_RS为高、R/W为低时,写入数据
- LCD_EN = 1; delay1(5); LCD_EN = 0; //用EN输入一个高脉冲
- }
- //---------------------------------------------------
- void W_LCD_STR(uchar *s) //写字符串
- {
- while(*s) W_LCD_Dat(*s++);
- }
- //---------------------------------------------------
- void W_BUFF(void) //填写显示缓冲区 LCD数据写入
- {gewei=jiaodu%10;
- baiwei=jiaodu/100;
- shiwei=(jiaodu/10)%10;
- weight_1[2] = gewei % 10+ 48;
- weight_1[1] = shiwei % 10+ 48;
- weight_1[0] = baiwei % 10+ 48;
- W_LCD_Com(0x80 + 7); W_LCD_STR(weight_1);
- zgewei=zjiaodu%10;
- zbaiwei=zjiaodu/100;
- zshiwei=(zjiaodu/10)%10;
- zqianwei=zjiaodu/1000;
- Timer_buf[1] = zbaiwei % 10 + 48;Timer_buf[0] = zqianwei % 10+ 48;
- Timer_buf[2] = zshiwei % 10 + 48;Timer_buf[3] = zgewei % 10 + 48;
- W_LCD_Com(0xc0+7);W_LCD_STR(Timer_buf);
- }
- //---------------------------------------------------
- //---------------------------------------------------
- void Init()
- {
- LCD_RW = 0;
- W_LCD_Com(0x38); delay1(50);
- W_BUFF();
- W_LCD_Com(0x01);
- W_LCD_Com(0x80); W_LCD_STR(LCD_line1);
- W_LCD_Com(0xC0); W_LCD_STR(LCD_line2);
- }
- //---------------------------------------------------
- void delay(u16 i)
- {
- while(i--);
- }
- /*******************************************************************************
- * 函 数 名 : KeyDown
- * 函数功能 : 检测有按键按下并读取键值
- * 输 入 : 无
- * 输 出 : 无
- *******************************************************************************/
- void KeyDown(void)
- {
- char a=0;
- GPIO_KEY=0x0f;
- if(GPIO_KEY!=0x0f)//读取按键是否按下
- {
- delay(1000);//延时10ms进行消抖
- if(GPIO_KEY!=0x0f)//再次检测键盘是否按下
- {
- //测试列
- GPIO_KEY=0X0F;
- switch(GPIO_KEY)
- {
- case(0X07): KeyValue=0;break;
- case(0X0b): KeyValue=1;break;
- case(0X0d): KeyValue=2;break;
- case(0X0e): KeyValue=3;break;
- }
- //测试行
- GPIO_KEY=0XF0;
- switch(GPIO_KEY)
- {
- case(0X70): KeyValue=KeyValue;break;
- case(0Xb0): KeyValue=KeyValue+4;break;
- case(0Xd0): KeyValue=KeyValue+8;break;
- case(0Xe0): KeyValue=KeyValue+12;break;
- }
- while((a<50)&&(GPIO_KEY!=0xf0)) //检测按键松手检测
- {
- delay(1000);
- a++;
- }
- }
- }
- }
- zhengzhuan()
- {
- while(k!=j)
- {
- Coil_A1 //遇到Coil_A1 用{A1=1;B1=0;C1=0;D1=0;}代替
- delay(200); //改变这个参数可以调整电机转速 ,
- k--;
- if(k==j)
- break;
- Coil_A2
- delay(200);
- k--;
- if(k==j)
- break;
- Coil_B1
- delay(200);
- k--;
- if(k==j)
- break;
- Coil_B2
- delay(200);
- k--;
- if(k==j)
- break;
- Coil_C1
- delay(200);
- k--;
- if(k==j)
- break;
- Coil_C2
- delay(200);
- k--;
- if(k==j)
- break;
- Coil_D1
- delay(200);
- k--;
- if(k==j)
- break;
- Coil_D2
- delay(200);
- k--;
- if(k==j)
- break;
- }}
- fanzhuan()
- {
- while(k!=j)
- {
- Coil_D2 //遇到Coil_A1 用{A1=1;B1=0;C1=0;D1=0;}代替
- delay(200); //改变这个参数可以调整电机转速 ,
- k--;
- if(k==j)
- break;
- Coil_D1
- delay(200);
- k--;
- if(k==j)
- break;
- Coil_C2
- delay(200);
- k--;
- if(k==j)
- break;
- Coil_C1
- delay(200);
- k--;
- if(k==j)
- break;
- Coil_B2
- delay(200);
- k--;
- if(k==j)
- break;
- Coil_B1
- delay(200);
- k--;
- if(k==j)
- break;
- Coil_A2
- delay(200);
- k--;
- if(k==j)
- break;
- Coil_A1
- delay(200);
- k--;
- if(k==j)
- break;
- }
- }
- void anjian()
- {if(KeyValue==6&&b==1&&c==1&&d==1)//电机正转
- {k=jiaodu*11+jiaodu/2;i=i+k;zhengzhuan();KeyValue=20;}
- if(KeyValue==7&&b==1&&c==1&&d==1)//电机反转
- {k=jiaodu*12;if(i<=0){
- delay(1000);i=0;k=0;}
- i=i-k;fanzhuan();
- KeyValue=20;}
- if(KeyValue==5)
- {b=1;c=1;d=1;shanshuo=0;KeyValue=20;z=5;}
- if(KeyValue==0)
- {z++;
- if (z>=7)
- z=0;
- KeyValue=20;}//百位选择
- if(z==0)
- {b=1;c=0;d=0;e=1;v=1;n=1;shanshuo=1;}
- if(z==1)//十位选择
- {b=0;c=1;d=0;e=1;v=1;n=1;shanshuo=2;}
- if(z==2)//个位选择
- {b=0;c=0;d=1;e=1;v=1;n=1;shanshuo=3;}
- if(z==3)//千位选择
- {b=0;c=0;d=0;e=1;v=0;n=0;shanshuo=4;}
- if(z==4)//百位选择
- {b=1;c=0;d=0;e=0;v=0;n=0;shanshuo=5;}
- if(z==5)//十位选择
- {b=0;c=1;d=0;e=0;v=0;n=1;shanshuo=6;}
- if(z==6)//个位选择
- {b=0;c=0;d=1;e=0;v=1;n=1;shanshuo=7;}
- if(KeyValue==1)
- {jiaoliuzhengzhuan=1;jiaoliufanzhuan=0;}
- if(KeyValue==2)
- {jiaoliufanzhuan=1;jiaoliuzhengzhuan=0;}
- if(KeyValue==9)
- {jiaoliufanzhuan=1;jiaoliuzhengzhuan=1;}
- if(KeyValue==3&&b==1&&c==0&&d==0&&e==1&&v==1&&n==1)//百位+1
- {jiaodu=jiaodu+100;KeyValue=20;}
- if(KeyValue==4&&b==1&&c==0&&d==0&&e==1&&v==1&&n==1)//百位-1
- {jiaodu=jiaodu-100;KeyValue=20;}
- if(KeyValue==3&&b==0&&c==1&&d==0&&e==1&&v==1&&n==1)//十位+1
- {jiaodu=jiaodu+10;KeyValue=20;}
- if(KeyValue==4&&b==0&&c==1&&d==0&&e==1&&v==1&&n==1)//十位-1
- {jiaodu=jiaodu-10;KeyValue=20;}
- if(KeyValue==3&&b==0&&c==0&&d==1&&e==1&&v==1&&n==1)//个位+1
- {jiaodu=jiaodu+1;KeyValue=20;}
- if(KeyValue==4&&b==0&&c==0&&d==1&&e==1&&v==1&&n==1)//个位-1
- {jiaodu=jiaodu-1;KeyValue=20;}
- if(KeyValue==3&&b==1&&c==0&&d==0&&e==0&&v==0&&n==0)//百位+1
- {zjiaodu=zjiaodu+100;KeyValue=20;}
- if(KeyValue==4&&b==1&&c==0&&d==0&&e==0&&v==0&&n==0)//百位-1
- {zjiaodu=zjiaodu-100;KeyValue=20;}
- if(KeyValue==3&&b==0&&c==1&&d==0&&e==0&&v==0&&n==1)//十位+1
- {zjiaodu=zjiaodu+10;KeyValue=20;}
- if(KeyValue==4&&b==0&&c==1&&d==0&&e==0&&v==0&&n==1)//十位-1
- {zjiaodu=zjiaodu-10;KeyValue=20;}
- if(KeyValue==3&&b==0&&c==0&&d==1&&e==0&&v==1&&n==1)//个位+1
- {zjiaodu=zjiaodu+1;KeyValue=20;}
- if(KeyValue==4&&b==0&&c==0&&d==1&&e==0&&v==1&&n==1)//个位-1
- {zjiaodu=zjiaodu-1;KeyValue=20;}
- if(KeyValue==3&&b==0&&c==0&&d==0&&e==1&&v==0&&n==0)//千位+1
- {zjiaodu=zjiaodu+1000;KeyValue=20;}
- if(KeyValue==4&&b==0&&c==0&&d==0&&e==1&&v==0&&n==0)//千位-1
- {zjiaodu=zjiaodu-1000;KeyValue=20;}
- if(KeyValue==8){if(i<=0)i=0;k=i;
- i=i-k;fanzhuan();
- KeyValue=20;}
- if(k1==0)
- {KeyValue=6;}
- if(k2==0)
- {KeyValue=7;}
- }
- /*******************************************************************************
- * 函 数 名 : main
- * 函数功能 : 主函数
- * 输 入 : 无
- * 输 出 : 无
- *******************************************************************************/
- void main()
- {
- delay(100);
- Init();
- shanshuo=0;
- delay(100);
- weight=30;
- P1=0x00;
- while(1)
- { KeyDown();
- anjian();
- weight=i/1900;
- switch (shanshuo)
- {case 0:W_LCD_Com(0x0c);W_BUFF();break;
- case 1: W_LCD_Com(0x87);
- W_LCD_Com(0x0f);delay(1000); W_BUFF();break;
- case 2:W_LCD_Com(0x88);
- W_LCD_Com(0x0f);delay(1000); W_BUFF();break;
- case 3:W_LCD_Com(0x89);
- W_LCD_Com(0x0f);delay(1000); W_BUFF();break;
- case 4:W_LCD_Com(0xc7);
- W_LCD_Com(0x0f);delay(1000); W_BUFF();break;
- case 5:W_LCD_Com(0xc8);
- W_LCD_Com(0x0f);delay(1000); W_BUFF();break;
- case 6:W_LCD_Com(0xc9);
- W_LCD_Com(0x0f);delay(1000); W_BUFF();break;
- case 7:W_LCD_Com(0xca);
- W_LCD_Com(0x0f);delay(1000); W_BUFF();break;
- default :W_LCD_Com(0x0c);W_BUFF();break;
- }//写显示
- }
- }
复制代码
|