|
CC2530控制步進(jìn)電機(jī)正反轉(zhuǎn),調(diào)試通過(guò)。
0.jpg (46.41 KB, 下載次數(shù): 91)
下載附件
2018-3-17 22:09 上傳
分別下載程序到任意一節(jié)點(diǎn)連接電機(jī)如圖所示,觀察電機(jī)運(yùn)行情況,可以修改
ucSpeed 調(diào)整速度哦,代碼分析請(qǐng)看程序中的注釋。
單片機(jī)源程序如下:
- /****************************************************************************
- * 文 件 名: main.c
- * 作 者: Andy
- * 修 訂: 2016-03-27
- * 版 本: 2.0
- * 描 述: 用P04 05 06 07控制步進(jìn)電機(jī)
- ****************************************************************************/
- #include <ioCC2530.h>
- typedef unsigned char uchar;
- typedef unsigned int uint;
- #define A1 P0_4 //定義步進(jìn)電機(jī)連接端口
- #define B1 P0_5
- #define C1 P0_6
- #define D1 P0_7
- //改變這個(gè)參數(shù)可以調(diào)整電機(jī)轉(zhuǎn)速,數(shù)字越小,轉(zhuǎn)速越快,力矩越小
- uchar ucSpeed = 2; //調(diào)整速度 建議在1-10范圍內(nèi)
- uchar FFW[8]={0x90,0x10,0x30,0x20,0x60,0x40,0xc0,0x80};//正轉(zhuǎn)旋轉(zhuǎn)相序表
- uchar REV[8]={0x80,0xc0,0x40,0x60,0x20,0x30,0x10,0x90};//反向旋轉(zhuǎn)相序表
- void MotorData(uchar data)
- {
- A1 = 1&(data>>4);
- B1 = 1&(data>>5);
- C1 = 1&(data>>6);
- D1 = 1&(data>>7);
- }
- //ms延時(shí)函數(shù)
- void Delay_ms(uint x)
- {
- uint i,j;
- for(i=0;i<x;i++)
- for(j=0;j<535;j++);
- }
- //順時(shí)針轉(zhuǎn)動(dòng)
- void MotorFFW(float n)
- {
- unsigned char i;
- unsigned int j;
- int movie_count=(int)(8*64*n);
- for (j=0; j<movie_count; j++)
- {
- for (i=0; i<8; i++)
- {
- MotorData( FFW[i]);//P0 = FFW[i];
- Delay_ms(ucSpeed);
- }
- }
- }
- //逆時(shí)針轉(zhuǎn)動(dòng)
- void MotorREV(float n)
- {
- unsigned char i;
- unsigned int j;
-
- int movie_count=(int)(8*64*n);
- for (j=0; j<movie_count; j++)
- {
- for (i=0; i<8; i++)
- {
- MotorData(REV[i]);
- Delay_ms(ucSpeed);
- }
- }
- }
- /****************************************************************************
- * 名 稱(chēng): InitIO()
- * 功 能: 初始化IO口程序
- * 入口參數(shù): state=0正轉(zhuǎn) 1反轉(zhuǎn)
- ****************************************************************************/
- void ContrlStepMotor(int state, float count)
- {
- if(state == 0)
- {
- MotorFFW(count);
- }
- else
- {
- MotorREV(count);
- }
- }
- /****************************************************************************
- * 名 稱(chēng): InitIO()
- * 功 能: 初始化IO口程序
- * 入口參數(shù): 無(wú)
- * 出口參數(shù): 無(wú)
- ****************************************************************************/
- void InitIO(void)
- {
- P0SEL &= 0x0F; //P04 05 06 07定義為普通IO
- P0DIR |= 0xF0; //P04 05 06 07定義為輸出
- }
- /****************************************************************************
- * 程序入口函數(shù)
- ****************************************************************************/
- void main(void)
- {
- InitIO();
-
- //改變這個(gè)參數(shù)可以調(diào)整電機(jī)轉(zhuǎn)速,數(shù)字越小,轉(zhuǎn)速越快,力矩越小
- ucSpeed = 1; //調(diào)整速度建議在1-10范圍內(nèi),數(shù)字越小轉(zhuǎn)速越快力矩越小
-
- while(1)
- {
- ContrlStepMotor(0, 1);//正轉(zhuǎn)1圈
- Delay_ms(3000);
-
- ContrlStepMotor(1, 1);//反轉(zhuǎn)1圈
- Delay_ms(3000);
- }
-
- }
復(fù)制代碼
所有資料51hei提供下載:
30.控制步進(jìn)電機(jī)正反轉(zhuǎn).rar
(577.05 KB, 下載次數(shù): 51)
2018-3-17 15:57 上傳
點(diǎn)擊文件名下載附件
CC2530控制步進(jìn)電機(jī)正反轉(zhuǎn) 下載積分: 黑幣 -5
|
|