|
該設(shè)計(jì)可實(shí)現(xiàn)步進(jìn)電機(jī)的速度及角度控制,
并在LCD1602上顯示速度和角度
還可以利用PCF8591采集到的的信號(hào)實(shí)現(xiàn)角度控制
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
仿真.PNG (121.71 KB, 下載次數(shù): 74)
下載附件
2021-4-21 10:20 上傳
相關(guān)單片機(jī)代碼如下:- #include "MAIN.H"
- #include "LCD1602.h"
- #include "PCF8591.h"
- /*******************************************************************************
- * 設(shè)計(jì)名稱: 兩相步進(jìn)電機(jī)
- ******************************************************************************/
- unsigned char code FFW_X[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //X反轉(zhuǎn)順序
- unsigned char code FFZ_X[8]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01}; //X正轉(zhuǎn)順序
- unsigned char code FFW_Y[8]={0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90}; //Y反轉(zhuǎn)順序
- unsigned char code FFZ_Y[8]={0x90,0x80,0xc0,0x40,0x60,0x20,0x30,0x10}; //Y正轉(zhuǎn)順序
- unsigned char Speed = 30,Direction = 1,X_Y = 0,stop = 1,mode = 0;
- unsigned int k, temp;
- int Angle = 0;
- /*******************************************************************************
- * 函 數(shù) 名 : main
- * 函數(shù)功能 : 主函數(shù)
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void main(void)
- {
- System_Init();
- while(1)
- {
- temp = Read_PCF(); // 讀取AD轉(zhuǎn)換后的數(shù)字量
- PCF_Output(temp); // DA轉(zhuǎn)換,模擬量輸出
- //將數(shù)字量轉(zhuǎn)換為十進(jìn)制,*100 + 0.5將小數(shù)轉(zhuǎn)換為整數(shù),便于顯示和存儲(chǔ)
- k = (float)temp * 5 / 256 * 100 + 0.5; // 將得到的結(jié)果存入數(shù)組
- Key_Scan();
- if(mode == 0)Motor();
- if(mode == 1)
- {
- Angle = k/61*45;
- if(Angle >= 0)
- {
- LCD1602_Location(2,7);
- LCD1602_Write_Num(Angle);
- if(X_Y==0)
- GPIO_MOTOR = FFW_Y[k/62];
- else
- GPIO_MOTOR = FFW_X[k/62];
- }
- Delay(20);
- }
- }
- }
- void System_Init(void)
- {
- Init_PCF();
- Init_LCD1602();
- LCD1602_Location(1,1);
- LCD1602_write_word("Welcome To Use!");
- Delay(1000);
- LCD1602_Clear();
- LCD1602_Location(1,1);
- LCD1602_write_word("Speed:");
- LCD1602_Write_Num(Speed);
- LCD1602_Location(2,1);
- LCD1602_write_word("Angle:");
- LCD1602_write_word("Null");
- LCD1602_Location(1,11);
- LCD1602_write_word("Stop");
- LCD1602_Location(2,12);
- LCD1602_write_word("X+");
- LCD1602_Location(2,15);
- LCD1602_write_word("O");
- }
- /*******************************************************************************
- * 函 數(shù) 名: Motor
- * 函數(shù)功能: 電機(jī)旋轉(zhuǎn)函數(shù)
- * 輸 入: 無
- * 輸 出: 無
- *******************************************************************************/
- void Motor()
- {
- unsigned char i;
- if(Speed>0)
- for(i=0;i<8;i++)
- {
- if(Direction==1&&X_Y==0&&stop==0) //正轉(zhuǎn)&Y電機(jī)&電機(jī)沒按下停止
- GPIO_MOTOR = FFW_Y[i];
- if(Direction==2&&X_Y==0&&stop==0) //反轉(zhuǎn)&Y電機(jī)&電機(jī)沒按下停止
- GPIO_MOTOR = FFZ_Y[i];
- if(Direction==1&&X_Y!=0&&stop==0) //正轉(zhuǎn)&X電機(jī)&電機(jī)沒按下停止
- GPIO_MOTOR = FFW_X[i];
- if(Direction==2&&X_Y!=0&&stop==0) //反轉(zhuǎn)&X電機(jī)&電機(jī)沒按下停止
- GPIO_MOTOR = FFZ_X[i];
- Delay(1000/Speed); //調(diào)節(jié)轉(zhuǎn)速
- }
- }
- /*******************************************************************************
- * 函 數(shù) 名 : Delay
- * 函數(shù)功能 : 延時(shí)
- * 輸 入 : t
- * 輸 出 : 無
- *******************************************************************************/
- void Delay(unsigned int t)
- {
- unsigned int k;
- while(t--)
- {
- for(k=0; k<80; k++);
- }
- }
- /*******************************************************************************
- * 函 數(shù) 名 : Key_Scan
- * 函數(shù)功能 : 按鍵
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void Key_Scan(void)
- {
- unsigned int i,j;
- if(K1==0) //檢測按鍵K1是否按下
- {
- Delay(1); //消除抖動(dòng)
- if(K1==0)
- {
- if(stop == 1)
- {
- stop=0;
- LCD1602_Location(1,11);
- LCD1602_write_word("Move");
- LCD1602_Location(2,7);
- LCD1602_write_word("Null");
- }
- else
- {
- stop=1; //stop為控制電機(jī)停止的標(biāo)志位,但它為1時(shí),電機(jī)停止
- LCD1602_Location(1,11);
- LCD1602_write_word("Stop");
- }
- }
- while((i<300)&&(K1==0))//檢測按鍵是否松開
- {
- Delay(1);
- i++;
- }
- i=0;
- }
-
- if(K2==0) //檢測按鍵K1是否按下
- {
- Delay(1); //消除抖動(dòng)
- if(K2==0)
- {
- Direction=1; //Direction 轉(zhuǎn)向標(biāo)志位,為2時(shí)為正轉(zhuǎn)
- LCD1602_Location(2,13);
- LCD1602_write_word("+");
- }
- while((i<200)&&(K2==0))//檢測按鍵是否松開
- {
- Delay(1);
- i++;
- }
- i=0;
- }
- /********************檢測按鍵K2是否按下***********************/
- if(K3==0) //檢測按鍵K2是否按下
- {
- Delay(1); //消除抖動(dòng)
- if(K3==0)
- {
- Direction=2; //Direction 轉(zhuǎn)向標(biāo)志位,為2時(shí)為反轉(zhuǎn)
- LCD1602_Location(2,13);
- LCD1602_write_word("-");
- }
- while((i<200)&&(K3==0))//檢測按鍵是否松開
- {
- Delay(1);
- i++;
- }
- i=0;
- }
- /********************檢測按鍵K3是否按下***********************/
- if(K4==0)//檢測按鍵K3是否按下
- {
- Delay(1);//消除抖動(dòng)
- if(K4==0)
- {
- if(Speed < 100)
- Speed += 5;
- LCD1602_Location(1,7);
- LCD1602_Write_Num(Speed);
- }
- while((i<200)&&(K4==0))//檢測按鍵是否松開
- {
- Delay(1);
- i++;
- }
- i=0;
- }
- /********************檢測按鍵K3是否按下***********************/
- if(K5==0) //檢測按鍵K4是否按下
- {
- Delay(1); //消除抖動(dòng)
- if(K5==0)
- {
- if(Speed > 0)
- Speed -= 5;
- LCD1602_Location(1,7);
- LCD1602_Write_Num(Speed);
- }
- while((i<200)&&(K5==0))//檢測按鍵是否松開
- {
- Delay(1);
- i++;
- }
- i=0;
- }
- if(K6==0)//檢測按鍵K1是否按下
- {
- Delay(1);//消除抖動(dòng)
- if(K6==0)
- {
- // Direction=0; //X_Y 電機(jī)的參數(shù) X_Y=0為Y電機(jī) X_Y=1為X電機(jī)
- if(X_Y==0) //當(dāng)切換電機(jī)時(shí),對(duì)Direction=0;對(duì)標(biāo)志位清零,防止上個(gè)電機(jī)的轉(zhuǎn)向影響本次所控電機(jī)
- {
- X_Y=1;
- LCD1602_Location(2,12);
- LCD1602_write_word("Y");
- }
- else
- {
- X_Y=0;
- LCD1602_Location(2,12);
- LCD1602_write_word("X");
- }
- }
- while((i<400)&&(K6==0))//檢測按鍵是否松開
- {
- Delay(1);
- i++;
- }
- i=0;
- }
- if(K7==0)//檢測按鍵K1是否按下
- {
- Delay(1);//消除抖動(dòng)
- if(K7==0)
- {
- if(Angle >= 0)
- {
- if(Direction==1&&stop==1) //正轉(zhuǎn)&Y電機(jī)&電機(jī)沒按下停止
- {
- if(j >= 8)j -= 8;
- Angle += 45;
- if(Angle >= 360)Angle -= 360;
- j++;
- LCD1602_Location(2,7);
- LCD1602_Write_Num(Angle);
-
- if(X_Y==0)
- GPIO_MOTOR = FFW_Y[j];
- else
- GPIO_MOTOR = FFW_X[j];
- }
-
- if(Direction==2&&stop==1) //反轉(zhuǎn)&Y電機(jī)&電機(jī)沒按下停止
- {
- if(j > 0)
- {
- Angle -= 45;
- if(Angle <= 0)Angle = 0;
- j--;
- LCD1602_Location(2,7);
- LCD1602_Write_Num(Angle);
- if(X_Y==0)
- GPIO_MOTOR = FFW_Y[j];
- else
- GPIO_MOTOR = FFW_X[j];
- }
- else
- j = 0;
- }
- }
- Delay(20);
- }
- while((i<2000)&&(K7==0))//檢測按鍵是否松開
- {
- Delay(1);
- i++;
- }
- i=0;
- }
- if(K8==0)//檢測按鍵K1是否按下
- {
- Delay(1);//消除抖動(dòng)
- if(K8==0)
- {
- mode = 1;
- LCD1602_Location(2,15);
- LCD1602_write_word("C");
- }
- while((i<500)&&(K8==0))//檢測按鍵是否松開
- {
- Delay(1);
- i++;
- }
- i=0;
- }
- if(K9==0)//檢測按鍵K1是否按下
- {
- Delay(1);//消除抖動(dòng)
- if(K9==0)
- {
- mode = 0;
- LCD1602_Location(2,15);
- LCD1602_write_word("O");
- }
- while((i<500)&&(K9==0))//檢測按鍵是否松開
- {
- Delay(1);
- i++;
- }
- i=0;
- }
- }
復(fù)制代碼
51hei.png (6.92 KB, 下載次數(shù): 59)
下載附件
2021-4-21 16:12 上傳
全部資料51hei下載地址:
兩相步進(jìn)電機(jī).zip
(220.49 KB, 下載次數(shù): 118)
2021-4-21 10:22 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|