四线双极性步进马达时序程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/*
LB11847 步进电机驱动
*/
#include

//代表输出电流值
//分别代表
// 17.39% 26.08% 34.78% 43.48% 52.17% 60.87% 69.56% 73.91% 78.26% 82.61% 86.95% 91.30% 95.65% 100%
//对应端口
// IB4 IB3 IB2 IB1 IA4 IA3 IA2 IA1
// RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0
const unsigned char TableA[] = {0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x0F,0x0F,0x0f,0x0f,0x0f,0x0f};// 电流输出值对应
const unsigned char TableB[] = {0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0,0xF0,0xF0,0xf0,0xf0,0xf0,0xf0};
// 正转反转时许设定
// 1.单相通电
// 正转时序
// A/ B A B/
// 反转时序
// B/ A B A/
// 2.两相通电
// 正转时序
// A/B AB AB/ A/B/
// 反转时序
// A/B/ AB/ AB A/B
// 3.半步方式
// 正转时序
// A/ A/B B AB A AB/ B/ A/B/
// 反转时序
// A/B/ B/ AB/ A AB B A/B A/
// 步进电机对应
// 从带蓝边开始 依次 A B A/ B/
// 操作时序
// PHASE ENABLE OUTAorB OUTA/orB/
// H L H L
// L L L H
// - H OFF OFF
const unsigned char PositiveTable[]={0x08,0x06,0x09,0x02}; // 正转时序
const unsigned char ReverseTable[]={0x02,0x09,0x06,0x08}; // 反响时序

const unsigned char PositiveTable1[]={0x04,0x05,0x01,0x00}; // 正转时序
const unsigned char ReverseTable1[]={0x00,0x01,0x05,0x04}; // 反响时序

const unsigned char PositiveTable2[] ={0x08,0x04,0x06,0x05,0x09,0x01,0x02,0x00}; // 8拍正转时序
const unsigned char ReverseTable2[]={0x00,0x02,0x01,0x09,0x05,0x06,0x04,0x08};
#define Timer1_Int 0xFF80 // 定时器1初始化值

const unsigned char PositiveTable3[]={0x08,0x06,0x09,0x02}; // 正转时序

#define PHASEA RC0 // 输出管脚使能
#define ENABLEA RC1
#define PHASEB RC2
#define ENABLEB RC3

unsigned char Point_CurrentA; // A相电流输出指针
unsigned char Point_CurrentB; // B相电流输出指针

unsigned char Point_Running; // 转动指针

unsigned char Delay_Counter; // 延时计数器
unsigned int Timer_Add;

unsigned char Add_Pluse;

volatile bit A_Add;
volatile bit A_Plus;
volatile bit B_Add;
volatile bit B_Plus;

volatile bit Positive_Reverse_Flage; // 正反标志
volatile bit Positive_ReverseA;
volatile bit Positive_ReverseB;

//#define A_Add 1 // A相电流加操作
//#define A_Pluse 2 // A相电流减操作
//#define B_Add 3 // B相电流加操作
//#define B_Pluse 4 // B相电流减操作


void Pic_Int();

void delay(unsigned int asd)
{
unsigned int i;
for(i=0;i{}
}
//*****************************************
//中断函数
//*****************************************
void interrupt SDI()
{
if(TMR1IF) // 定时器中断
{
TMR1IF = 0;
if(Timer_Add<0xFF60)
{
//Timer_Add++;
}
TMR1H = (unsigned char)(Timer_Add>>4); // 定时器计时初始化
TMR1L = (unsigned char)(Timer_Add&0x00ff);

Delay_Counter++;
if(Delay_Counter>=1);
{
Delay_Counter = 0;

PORTC = PositiveTable[Point_Running]; // 正转时


PORTB = (unsigned char)((TableA[Point_CurrentA])|(TableB[Point_CurrentB]));
if(A_Add)
{
Point_CurrentA+=4;
}

if(A_Plus)
{
Point_CurrentA-=4;
}

if(B_Plus)
{
Point_CurrentB-=4;
}
if(B_Add)
{
Point_CurrentB+=4;
}
//Point_Running++;
if(A_Add)
{
if(Point_CurrentA == 16)
{
A_Add = 0;
A_Plus = 1;
if(Positive_ReverseA)
{Point_Running++;}
Positive_ReverseA = 0;
}
}
if(A_Plus)
{
if(Point_CurrentA == 0)
{
A_Add = 1;
A_Plus = 0;
if(Positive_ReverseA)
{Point_Running++;}
Positive_ReverseA = 0;
}
}
if(B_Add)
{
if(Point_CurrentB==16)
{
B_Add = 0;
B_Plus = 1;
if(Positive_ReverseB)
{Point_Running++;}
Positive_ReverseB = 0;
}
}
if(B_Plus)
{
if(Point_CurrentB==0)
{
B_Add = 1;
B_Plus = 0;
if(Positive_ReverseB)
{Point_Running++;}
Positive_ReverseB = 0;
}
}
if(Positive_Reverse_Flage==0) // 正转
{
if(Positive_ReverseA==0)
{
Positive_ReverseB = 1;
}
if(Positive_ReverseB==0)
{
Positive_ReverseA = 1;
}
}


if(Point_Running>4)
{
Point_Running = 0;
}
}
}
}
//*****************************************
//主函数
//*****************************************
void main()
{
Pic_Int(); // 初始化
//PHASEB = 1;
//ENABLEB = 0;
while(1)
{

}
}

//*****************************************
//初始化函数
//*****************************************
void Pic_Int()
{
ADCON1 = 0x07; // 关闭AD转换器
INTCON= 0x00; // 关闭中断
TRISB = 0x00; // RB口设置为输出
PORTB = 0xff; // 全部电流输出

TRISC = 0xF0; // 低四位设置为输出引脚
ENABLEB = 1;
ENABLEA = 1;

T1CON = 0x00; // 定时器1初始化
TMR1IE = 1;
Timer_Add = Timer1_Int;

TMR1H = (unsigned char)(Timer_Add>>4); // 定时器计时初始化
TMR1L = (unsigned char)(Timer_Add&0x00ff);
GIE = 1; // 中断开始
PEIE = 1;
TMR1ON= 1; // 定时器运行
Point_CurrentA = 0; // 指针初始化
Point_CurrentB = 0;
Point_Running = 0;
Delay_Counter = 0;

Positive_Reverse_Flage = 0; // 正转标志
Point_CurrentA = 16; // 正转电流A相处于最大
Point_CurrentB = 0; // 正转电流B相处于最小

Positive_ReverseA = 1;
Positive_ReverseB = 0;
A_Plus = 1; // 起始B进行加操作
A_Add = 0;
B_Add = 1; // B加操作
B_Plus = 0;
}

相关文档
最新文档