|
51單片機 步進電機
- /*******************************************************
- ULN2003驅動5V減速步進電機程序
- Target:STC89C52RC-40C
- Crystal:12MHz
- Author:戰神單片機工作室
- Platform:51&avr單片機最小系統板+ULN2003步進電機驅動套件
- *******************************************************
- 接線方式:
- IN1 ---- P00
- IN2 ---- P01
- IN3 ---- P02
- IN4 ---- P03
- + ---- +5V
- - ---- GND
- *********************/
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define MotorData P0 //步進電機控制接口定義
- uchar phasecw[4] ={0x08,0x04,0x02,0x01};//正轉 電機導通相序 D-C-B-A
- uchar phaseccw[4]={0x01,0x02,0x04,0x08};//反轉 電機導通相序 A-B-C-D
- sbit key1 = P1^0;
- sbit key2 = P1^1;
- //ms延時函數
- void Delay_xms(uint x)
- {
- uint i,j;
- for(i=0;i<x;i++)
- for(j=0;j<112;j++);
- }
- //順時針轉動
- void MotorCW(void)
- {
- uchar i;
- for(i=0;i<4;i++)
- {
- MotorData=phasecw[i];
- Delay_xms(4);//轉速調節
- }
- }
- //逆時針轉動
- void MotorCCW(void)
- {
- uchar i;
- for(i=0;i<4;i++)
- {
- MotorData=phaseccw[i];
- Delay_xms(4);//轉速調節
- }
- }
- //停止轉動
- void MotorStop(void)
- {
- MotorData=0x00;
- }
- //主函數
- void main(void)
- {
- uint i;
- Delay_xms(50);//等待系統穩定
- while(1)
- {
- if(key2 == 1)
- {
- for(i=0;i<20;i++)
- {
- MotorCW(); //順時針轉動
- }
- MotorStop(); //停止轉動
- Delay_xms(500);
-
-
- }
- else if(key1 == 0)
- {
- for(i=0;i<20;i++)
- {
- MotorCCW(); //逆時針轉動
- }
- MotorStop(); //停止轉動
- Delay_xms(500);
-
- }
- else
- {
- MotorStop(); //停止轉動
- Delay_xms(500);
- }
- // MotorStop(); //停止轉動
- // Delay_xms(500);
-
- }
- }
復制代碼
|
-
-
c51.zip
2018-6-29 16:39 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
30.2 KB, 下載次數: 131, 下載積分: 黑幣 -5
評分
-
查看全部評分
|