Do you understand the program?

2014-10-23 8:25 pm
I find a program for running 4-phrase stepping motor module 42H2P4412A4 using
8051 MCUdevelopment board,But I didn't understand part of them. The following
is the programme:

#include "reg52.h"

#define speed 38
//Motor
sbit PH1 = P1^0; //定義管腳
sbit PH2 = P1^1;
sbit I01 = P1^2;
sbit I11 = P1^3;
sbit I02 = P1^4;
sbit I12 = P1^5;

unsigned char TableA[] = { 0XF7,0XFB,0XF3}; //A線圈細分表
unsigned char TableB[] = { 0XeF,0XdF,0XcF}; //B線圈細分表

void delay(int time);

/***************************************
函數功能:產生單相四拍脈衝控制步進機 2細分
**************************************/
void Go()
{char i ,temp;
//A
PH1 = 0; //PH1為0 則A線圈為反向電流
for(i = 0; i<3; i++)
{ temp = P1;
P1 = TableA[i];
P1 = P1&temp;
delay(1);
}


PH2 = 0; //PH2為0 則B線圈為反向電流
I02 = 1;
I12 = 1; //輸出0

delay(speed);
//0
PH1 = 0; //PH1為0 則A線圈為反向電流
I01 = 1; //輸出0
I11 = 1;


PH2 = 1; //PH2為1 則B線圈為正電流
for(i = 0; i<3; i++)
{ temp = P1;
P1 = TableB[i];
P1 = P1&temp;
delay(1);
}

delay(speed);
//B
PH1 = 1; //PH1為1 則A線圈為正向電流
for(i = 0; i<3; i++)
{ temp = P1;
P1 = TableA[i];
P1 = P1&temp;
delay(1);
}

PH2 = 1; //PH2為1 則B線圈為正向電流
I02 = 1; //輸出0
I12 = 1;
////
delay(speed);
//0
PH1 = 1; //PH1為1 則A線圈為正向電流
I01 = 1;
I11 = 1;


PH2 = 0; //PH2為0 則B線圈為反向電流
for(i = 0; i<3; i++)
{ temp = P1;
P1 = TableB[i];
P1 = P1&temp;
delay(1);
}
delay(speed);



}
/*******************延時函數****************************/
void delay(int time)
{
int i,j;

for(j=0; j <= time; j++)
for(i =0 ; i <= 120; i++);
}


void main()
{

while(1)
{

Go(); //步進電機運行

}

}

I don't understand why the programmer define i as char, not int. Also, can you tell me the function of TableA and TableB in the programme ie: Reason for defining
the 2 tables and reason for using it.
Thanks

回答 (1)

2014-10-24 8:56 am
✔ 最佳答案
I didn't read your post in great details, but I already got why you don't understand.
(1) To program an MCU, which is very limited in memory and power, you, as a good programmer, should do whatever is most efficient.
Writing in C, over assembly, is already a big trade off. Give up speed, but use much easier C syntax to write.
Bare in mind that every little bit count.
CHAR is a byte, in nearly all implementations.
INT is a word (two bytes) in most implementations.
Using INT to count is a waste if you are only counting from 0 to 3.
Of course, you can define to use INT without any problem, only less effiencent. Other than CHAR, you can also use BYTE or SHORT INT, if they are available in your compiler. Note that SHORT INT may only count up to 127, half is used as negative. You should be very aware of those basic difference.

(2) To understand how to drive a stepper motor, you should read some information on the 4 windings and how to apply current to move the rotor and then lock to the next location. Usually to make it move more smoothly, a half stepping method is used. That is, apply current to those winding accordingly in a pattern, that is defined in Table A and B. You should see the table in BINARY instead of HEX, so you know which pin is ON and OFF, then you will get the pattern.


收錄日期: 2021-04-21 15:09:28
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20141023000051KK00033

檢視 Wayback Machine 備份