各位大神,我这个步进电机定义的是2.5周,可以运行起来要4周。烦请各位给指点一二。多谢了。
时间:10-02
整理:3721RD
点击:
- /***************************************
- 用4X4矩阵键盘进行步进电机操作
- ***************************************/
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- long g_Beats = 0;
- void DelayMs(uint ms); //延时函数声明;
- uchar KeyPress(void); //按键按下检测函数声明;
- uchar KeyScan(void); //键值扫描函数声明;
- void OffMotor(void);
- void TurnAngle(long angle);
- void OnMotor(void);
- void OffMotor(void);
- void KeyCode(uchar keycode);
- void Timer0Init(void);
- void main(void)
- {
- Timer0Init();
- while (1)
- {
- if (KeyPress())
- {
- KeyCode(KeyScan());
- }
- }
- }
- //定时器0初始化函数
- void Timer0Init(void)
- {
- TMOD |= 0x01;
- TH0 = 0xFC;
- TL0 = 0x66;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- //延时函数
- void DelayMs(uint ms) //11.0592MHZ 约为1.007MS;
- {
- uint x,y;
- for(x=ms;x>0;x--)
- {
- for(y=113;y>0;y--);
- }
- }
- //按键是否按下检测函数
- uchar KeyPress(void)
- {
- uchar readnum;
- P1 = 0xF0; //P1口的高四位作为输入,则需要对高四位进行初始化;
- readnum = P1; //将端口的状态赋值给一个变量,这样必免直接操作端口时,影响到端口的状态;
- if (readnum == 0xF0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- //按键扫描函数
- uchar KeyScan(void)
- {
- uchar index,keycode,keyvalue;
- DelayMs(20); //延时20MS;
- P1 = 0xFF; //定义键盘的输出状态初始值
- index = 0; //输出行索引;
- if (KeyPress()) //再次判断按键按下
- {
- while (1)
- {
- P1 = (0xF7 >> index) | 0xF0; //I/O端口不能做为中间变量,如temp = (P1 >> index) 单片机随时读取IO的变化,会导致程序有瞬间错误产生;
- index++;
- index = index & 0x03; //要扫描四行中哪一行为输出低电平,当index == 4时,index清零;
- keycode = P1; //当按键按下时,P1端口的高四位为高,若扫描到四行中某一行为0时,此时按键已经按下,P1口的高四位的其中一位将被低四位为0的一行拉低,这样键值就出来了。
- if ((keycode & 0xF0) != 0xF0) //如果按键在按下状态,获得键值停止扫描
- {
- break; //获得键码,跳出扫描;
- }
-
- }
- switch (keycode)
- {
- case 0x77 : keyvalue = 0x31; break;
- case 0x7B : keyvalue = 0x37; break;
- case 0x7D : keyvalue = 0x34; break;
- case 0x7E : keyvalue = 0x30; break;
-
- case 0xB7 : keyvalue = 0x1B; break;
- case 0xBB : keyvalue = 0x38; break;
- case 0xBD : keyvalue = 0x35; break;
- case 0xBE : keyvalue = 0x32; break;
- case 0xD7 : keyvalue = 0x0D; break;
- case 0xDB : keyvalue = 0x39; break;
- case 0xDD : keyvalue = 0x36; break;
- case 0xDE : keyvalue = 0x33; break;
-
- case 0xE7 : keyvalue = 0x27; break;
- case 0xEB : keyvalue = 0x25; break;
- case 0xED : keyvalue = 0x28; break;
- case 0xEE : keyvalue = 0x26; break;
- default : keyvalue = 0xFF;
- while (KeyPress()); //等待按键释放
- }
- }
- else
- {
- keyvalue = 0xFF; //如果没有按键按下时,键值为关闭
- }
- return keyvalue; //返回键值;
- }
- //获取键值函数,
- void KeyCode(uchar keycode)
- {
- static bit direc = 0;
- if ((keycode >= 0x30) && (keycode <=0x39))
- {
- if (direc == 0)
- {
- TurnAngle(360 * (keycode - 0x30));
- }
- else
- {
- TurnAngle(-360 * (keycode - 0x30));
- }
- }
- else if (keycode == 0x26)
- {
- direc = 0;
- }
- else if (keycode == 0x28)
- {
- direc = 1;
- }
- else if (keycode == 0x25)
- {
- TurnAngle(90);
- }
- else if (keycode == 0x27)
- {
- TurnAngle(-90);
- }
- else if (keycode == 0x1B)
- {
- OffMotor();
- }
- }
- //旋转角度函数
- void TurnAngle(long angle)
- {
- EA = 0;
- g_Beats = (angle * 4076) / 360;
- EA = 1;
- }
- //关闭步进电机函数
- void OffMotor(void)
- {
- EA = 0;
- g_Beats = 0;
- EA = 1;
- }
- //定时器0中断函数
- void Timer0IRQ() interrupt 1 //中断函数不需要声明
- {
- TH0 = 0xFC;
- TL0 = 0x66;
- OnMotor();
- }
- //开启步进电机函数
- void OnMotor()
- {
- uchar temp;
- static uchar subscript =0;
- uchar code BeatCode[] = {0x0E,0x0C,0x0D,0x09,
- 0x0B,0x03,0x07,0x06};
- //P0 = 0xFF;
-
- if (g_Beats != 0)
- {
- if (g_Beats > 0)
- {
- subscript++;
- subscript &= 0x07;
- g_Beats--;
- }
- else
- {
- subscript--;
- subscript &= 0x07;
- g_Beats++;
- }
- temp = P0;
- temp = temp & 0xF0;
- P0 = temp | BeatCode[subscript];
- }
- else
- {
- P0 |= 0x0F;
- }
- }
搞程序是要报酬的
报酬可以,你说个价。