單片機(jī)串口通信控制步進(jìn)電機(jī)
下面是源代碼:
/**
*************************************************************************************************
* @file main.c
* @author xr
* @date 2014年3月18日20:00:03
* @note 步進(jìn)電機(jī)的參數(shù):減速比:1:64步進(jìn)角:5.625/64 啟動(dòng)頻率:>=550 啟動(dòng)時(shí)間最?。?.08ms
* @brief 串口通信控制步進(jìn)電機(jī)單片機(jī)STC89C52RC MCU晶振11.0592MHZ
*************************************************************************************************
*/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long ulong;
//步進(jìn)電機(jī)八拍模式相值編碼 A-AB-B-BC-C-CD-D-DA
uchar code table[] = {
0xE, 0xC, 0xD, 0x9, 0xB, 0x3, 0x7, 0x6
};
uint tmp = 0;
bit dir = 0;
ulong beats = 0;//拍數(shù)
uchar angle = 0;//圈數(shù)
void timer0_Config();
void motor_Config(ulong angle);
void Uart_Config(uint baud);
void main()
{
timer0_Config();//啟動(dòng)時(shí)間設(shè)定為2ms
Uart_Config(9600);
while (1)
{
switch (angle)
{
case 2:
{
motor_Config(2*360);
angle = 0;
break;
}
case 3:
{
motor_Config(3*360);
angle = 0;
break;
}
case 4:
{
motor_Config(4*360);
angle = 0;
break;
}
case 5:
{
motor_Config(5*360);
angle = 0;
break;
}
case 6:
{
motor_Config(6*360);
angle = 0;
break;
}
case 7:
{
motor_Config(7*360);
angle = 0;
break;
}
case 8:
{
motor_Config(8*360);
angle = 0;
break;
}
case 9:
{
P1 |= 0x0F;
beats = 0;
angle = 0;
break;
}
default:
break;
}
}
}
評(píng)論