这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » L6219细分技术的实现

共1条 1/1 1 跳转至

L6219细分技术的实现

助工
2014-11-25 20:33:32     打赏

/*
1 L6219的介绍及电路设计
1.1 L6219的性能介绍
L6219是一个有两极的集成电路,它能控制和驱动具有两极步进电机的绕组,
还可以双向控制两个直流电机。L6219可以非常方便地与微处理器连接成控制步进电机的系统,
其电压范围可以从10V~46V,输出的电流可达到750mA,峰值电流高达1A。因为内部具有保护二极管及PWM电流控制,
所以在可靠性和稳定性方面提供了保障。
1.2 L6219电路的具体实现:


1.3 L6219的电路说明
L6219是一个24脚的芯片,功能说明如下:
1.3.1 PCA(Programmable Counter Array)的简介
P89V51RD内部含有一个PCA模块,输出复用P1口的P1.3、P1.4、P1.5、P1.6、P1.7。
PCA有上升下降沿捕捉、定时、高速输出、看门狗、PWM输出5个工作模式,
通过编程控制P1.3——P1.7工作于任意模式。这里只对8位PWM输出模式做详细介绍,其他工作模式可参考相关资料。
1.3.2 PWM相关寄存器的介绍
PCA有一些相关的控制寄存器:计数模式寄存器(CMOD)、计数控制寄存器(CCON)PCA定时器(CH、CL)、
工作模式寄存器(CCAPMn,其中n对应为:0、1、2、3、4)可对这些寄存器编程来控制PWM的输出。
CMOD:计数模式寄存器
CIDL WDTE —— —— —— CPS1 CPS0 ECF


CCON:计数控制寄存器
CF CR —— CCF4 CCF3 CCF2 CCF1 CCF0


CCAPMn:工作模式寄存器
—— ECOMn CAPPn CAPNn MATn TOGn PWMn                                                                             


3细分技术的实现
单片机实现时将0~360℃作细分,一个步距角度是1.8℃,首先根据矢量合成图建立相应的向量表,
程序再根据向量表运行控制马达的旋转,可以是步距的1、2、4、8、16、……2n的倍数。
程序的具体实现如下:


*/

#define CMOD    0XD9 
#define CCAP0G    0XFA 
#define CCAP1G    0XFB 
#define CCAP2G    0XFC 
#define CCAP3G    0XFD 
#define CCAP4G    0XFE 
#define CCAP5G    0XFF 
 
#define CCAP0L    0XEA 
#define CCAP1L    0XEB 
#define CCAO2L    0XEC 
#define CCAP3L    0XED 
#define CCAP4L    0XEE 
#define CCAP5L    0XEF

 // IPH EQU 0B7H
#define CH  0XF9//H
#define CL  0XE9//H
 
#define CCON   0D8H
// CCF0 EQU CCON.0
// CCF1 EQU  CCON.1
// CCF2 EQU CCON.2
// CCF3 EQU CCON.3
// CCF4 EQU CCON.4
// CCF5 EQU CCON.5
sbit  CR   =0XD8^6
// CF EQU CCON.7
 

 

#define PWM0enable(); CCAPM0|=0x42;            //配置P1.3引脚为PWM输出
#define PWM1enable(); CCAPM1|=0x42;            //配置P1.4引脚为PWM输出
#define PWM2enable(); CCAPM2|=0x42;            //配置P1.5引脚为PWM输出
#define PWM3enable(); CCAPM3|=0x42;            //配置P1.6引脚为PWM输出
#define PWM4enable(); CCAPM4|=0x42;            //配置P1.7引脚为PWM输出

#define PWM0disable(); CCAPM0&=0xBD;       //停止P1.3引脚输出PWM
#define PWM1disable(); CCAPM1&=0xBD;       //停止P1.4引脚输出PWM
#define PWM2disable(); CCAPM2&=0xBD;       //停止P1.5引脚输出PWM
#define PWM3disable(); CCAPM3&=0xBD;       //停止P1.6引脚输出PWM
#define PWM4disable(); CCAPM4&=0xBD;       //停止P1.7引脚输出PWM

#define MotorIdel            0x00                        //电机空闲模式
#define MotorPowerOn         0x01                       //电机上电
#define MotorNormalState     0x02                       //电机正常运转
#define MotorPowerOff        0x03                       //电机掉电

//定义细分表结构
typedef struct tagSubdivideTable{
       unsigned char mQuardrant;                         //象限0-3
       unsigned char mA_PWM;                             //A相输出的PWM
       unsigned char mB_PWM;                             //B相输出的PWM
}SubdivideTable;

typedef struct tagWorkInformation{
       unsigned int SubdivideTableID;                     //细分表ID
       unsigned char MotorDirect;                         //马达旋转方向
unsigned char SubdivideNumber;                            //细分倍数,这里的细分倍数就是访问细分表 //的步长,如果选用128倍细分,那么//SubdivideNumber=1,选用64倍细分,//SubdivideNumber=2
       unsigned int MotorSteps;                           //马达旋转总的步距,一个步距角1.8度
       unsigned char WorkState;                           //马达工作状态
unsigned char MaxStep;                                    //马达一个脉冲走过的最大步距,对应细分表//的间隔取值是:1、2、4、8、16...2的倍数
}WorkInformation;

  sbit PhaseA   = 0X90^4 
  sbit PhaseB   = 0X90^5   
   
 
#define Quardrant0 1                                        //+iA,+iB         第一象限
#define Quardrant1 2                                        //-iA,+iB         第二象限
#define Quardrant2 3                                        //-iA,-iB         第三象限
#define Quardrant3 4                                        //+iA,-iB         第四象限
WorkInformation MotorWorking;
                                                                              //细分表
//0-360度的细分,128倍细分表,iA=255*cos(a),iB=255*sin(a)
SubdivideTable code MotorSubdividedTab[]={
       //第一象限,    +iA,    +iB
       Quardrant3,    255, 0,
       Quardrant0,    255, 3,
       Quardrant0,    255, 6,
       Quardrant0,    255, 9,
       Quardrant0,    255, 13,
       Quardrant0,    255, 16,
       Quardrant0,    254, 19,
       Quardrant0,    254, 22,
       Quardrant0,    254, 25,
       Quardrant0,    253, 28,
       Quardrant0,    253, 31,
       Quardrant0,    253, 34,
       Quardrant0,    252, 37,
       Quardrant0,    252, 41,
       Quardrant0,    251, 44,
       Quardrant0,    251, 47,
       Quardrant0,    250, 50,
       Quardrant0,    249, 53,
       Quardrant0,    249, 56,
       Quardrant0,    248, 59,
       Quardrant0,    247, 62,
       Quardrant0,    247, 65,
       Quardrant0,    246, 68,
       Quardrant0,    245, 71,
       Quardrant0,    244, 74,
       Quardrant0,    243, 77,
       Quardrant0,    242, 80,
       Quardrant0,    241, 83,
       Quardrant0,    240, 86,
       Quardrant0,    239, 89,
       Quardrant0,    238, 92,
       Quardrant0,    237, 95,
       Quardrant0,    236, 98,
       Quardrant0,    234, 100,
       Quardrant0,    233, 103,
       Quardrant0,    232, 106,
       Quardrant0,    231, 109,
       Quardrant0,    229, 112,
       Quardrant0,    228, 115,
       Quardrant0,    226, 117,
       Quardrant0,    225, 120,
       Quardrant0,    223, 123,
       Quardrant0,    222, 126,
       Quardrant0,    220, 128,
       Quardrant0,    219, 131,
       Quardrant0,    217, 134,
       Quardrant0,    215, 136,
       Quardrant0,    214, 139,
       Quardrant0,    212, 142,
       Quardrant0,    210, 144,
       Quardrant0,    208, 147,
       Quardrant0,    207, 149,
       Quardrant0,    205, 152,
       Quardrant0,    203, 154,
       Quardrant0,    201, 157,
       Quardrant0,    199, 159,
       Quardrant0,    197, 162,
       Quardrant0,    195, 164,
       Quardrant0,    193, 167,
       Quardrant0,    191, 169,
       Quardrant0,    189, 171,
       Quardrant0,    187, 174,
       Quardrant0,    185, 176,
       Quardrant0,    183, 178,
       Quardrant0,    180, 180,
       Quardrant0,    178, 183,
       Quardrant0,    176, 185,
       Quardrant0,    174, 187,
       Quardrant0,    171, 189,
       Quardrant0,    169, 191,
       Quardrant0,    167, 193,
       Quardrant0,    164, 195,
       Quardrant0,    162, 197,
       Quardrant0,    159, 199,
       Quardrant0,    157, 201,
       Quardrant0,    154, 203,
       Quardrant0,    152, 205,
       Quardrant0,    149, 207,
       Quardrant0,    147, 208,
       Quardrant0,    144, 210,
       Quardrant0,    142, 212,
       Quardrant0,    139, 214,
       Quardrant0,    136, 215,
       Quardrant0,    134, 217,
       Quardrant0,    131, 219,
       Quardrant0,    128, 220,
       Quardrant0,    126, 222,
       Quardrant0,    123, 223,
       Quardrant0,    120, 225,
       Quardrant0,    117, 226,
       Quardrant0,    115, 228,
       Quardrant0,    112, 229,
       Quardrant0,    109, 231,
       Quardrant0,    106, 232,
       Quardrant0,    103, 233,
       Quardrant0,    100, 234,
       Quardrant0,    98,   236,
       Quardrant0,    95,   237,
       Quardrant0,    92,   238,
       Quardrant0,    89,   239,
       Quardrant0,    86,   240,
       Quardrant0,    83,   241,
       Quardrant0,    80,   242,
       Quardrant0,    77,   243,
       Quardrant0,    74,   244,
       Quardrant0,    71,   245,
       Quardrant0,    68,   246,
       Quardrant0,    65,   247,
       Quardrant0,    62,   247,
       Quardrant0,    59,   248,
       Quardrant0,    56,   249,
       Quardrant0,    53,   249,
       Quardrant0,    50,   250,
       Quardrant0,    47,   251,
       Quardrant0,    44,   251,
       Quardrant0,    41,   252,
       Quardrant0,    37,   252,
       Quardrant0,    34,   253,
       Quardrant0,    31,   253,
       Quardrant0,    28,   253,
       Quardrant0,    25,   254,
       Quardrant0,    22,   254,
       Quardrant0,    19,   254,
       Quardrant0,    16,   255,
       Quardrant0,    13,   255,
       Quardrant0,    9,    255,
       Quardrant0,    6,    255,
       Quardrant0,    3,    255,
       //第二象限   -iA,  +iB
       Quardrant0,    0,    255,
       Quardrant1,    3,    255,
       Quardrant1,    6,    255,
       Quardrant1,    9,    255,
       Quardrant1,    13,   255,
       Quardrant1,    16,   255,
       Quardrant1,    19,   254,
       Quardrant1,    22,   254,
       Quardrant1,    25,   254,
       Quardrant1,    28,   253,
       Quardrant1,    31,   253,
       Quardrant1,    34,   253,
       Quardrant1,    37,   252,
       Quardrant1,    41,   252,
       Quardrant1,    44,   251,
       Quardrant1,    47,   251,
       Quardrant1,    50,   250,
       Quardrant1,    53,   249,
       Quardrant1,    56,   249,
       Quardrant1,    59,   248,
       Quardrant1,    62,   247,
       Quardrant1,    65,   247,
       Quardrant1,    68,   246,
       Quardrant1,    71,   245,
       Quardrant1,    74,   244,
       Quardrant1,    77,   243,
       Quardrant1,    80,   242,
       Quardrant1,    83,   241,
       Quardrant1,    86,   240,
       Quardrant1,    89,   239,
       Quardrant1,    92,   238,
       Quardrant1,    95,   237,
       Quardrant1,    98,   236,
       Quardrant1,    100, 234,
       Quardrant1,    103, 233,
       Quardrant1,    106, 232,
       Quardrant1,    109, 231,
       Quardrant1,    112, 229,
       Quardrant1,    115, 228,
       Quardrant1,    117, 226,
       Quardrant1,    120, 225,
       Quardrant1,    123, 223,
       Quardrant1,    126, 222,
       Quardrant1,    128, 220,
       Quardrant1,    131, 219,
       Quardrant1,    134, 217,
       Quardrant1,    136, 215,
       Quardrant1,    139, 214,
       Quardrant1,    142, 212,
       Quardrant1,    144, 210,
       Quardrant1,    147, 208,
       Quardrant1,    149, 207,
       Quardrant1,    152, 205,
       Quardrant1,    154, 203,
       Quardrant1,    157, 201,
       Quardrant1,    159, 199,
       Quardrant1,    162, 197,
       Quardrant1,    164, 195,
       Quardrant1,    167, 193,
       Quardrant1,    169, 191,
       Quardrant1,    171, 189,
       Quardrant1,    174, 187,
       Quardrant1,    176, 185,
       Quardrant1,    178, 183,
       Quardrant1,    180, 180,
       Quardrant1,    183, 178,
       Quardrant1,    185, 176,
       Quardrant1,    187, 174,
       Quardrant1,    189, 171,
       Quardrant1,    191, 169,
       Quardrant1,    193, 167,
       Quardrant1,    195, 164,
       Quardrant1,    197, 162,
       Quardrant1,    199, 159,
       Quardrant1,    201, 157,
       Quardrant1,    203, 154,
       Quardrant1,    205, 152,
       Quardrant1,    207, 149,
       Quardrant1,    208, 147,
       Quardrant1,    210, 144,
       Quardrant1,    212, 142,
       Quardrant1,    214, 139,
       Quardrant1,    215, 136,
       Quardrant1,    217, 134,
       Quardrant1,    219, 131,
       Quardrant1,    220, 128,
       Quardrant1,    222, 126,
       Quardrant1,    223, 123,
       Quardrant1,    225, 120,
       Quardrant1,    226, 117,
       Quardrant1,    228, 115,
       Quardrant1,    229, 112,
       Quardrant1,    231, 109,
       Quardrant1,    232, 106,
       Quardrant1,    233, 103,
       Quardrant1,    234, 100,
       Quardrant1,    236, 98,
       Quardrant1,    237, 95,
       Quardrant1,    238, 92,
       Quardrant1,    239, 89,
       Quardrant1,    240, 86,
       Quardrant1,    241, 83,
       Quardrant1,    242, 80,
       Quardrant1,    243, 77,
       Quardrant1,    244, 74,
       Quardrant1,    245, 71,
       Quardrant1,    246, 68,
       Quardrant1,    247, 65,
       Quardrant1,    247, 62,
       Quardrant1,    248, 59,
       Quardrant1,    249, 56,
       Quardrant1,    249, 53,
       Quardrant1,    250, 50,
       Quardrant1,    251, 47,
       Quardrant1,    251, 44,
       Quardrant1,    252, 41,
       Quardrant1,    252, 37,
       Quardrant1,    253, 34,
       Quardrant1,    253, 31,
       Quardrant1,    253, 28,
       Quardrant1,    254, 25,
       Quardrant1,    254, 22,
       Quardrant1,    254, 19,
       Quardrant1,    255, 16,
       Quardrant1,    255, 13,
       Quardrant1,    255, 9,
       Quardrant1,    255, 6,
       Quardrant1,    255, 3,
       //第三象限   -iA,   -iB
       Quardrant1,    255, 0,
       Quardrant2,    255, 3,
       Quardrant2,    255, 6,
       Quardrant2,    255, 9,
       Quardrant2,    255, 13,
       Quardrant2,    255, 16,
       Quardrant2,    254, 19,
       Quardrant2,    254, 22,
       Quardrant2,    254, 25,
       Quardrant2,    253, 28,
       Quardrant2,    253, 31,
       Quardrant2,    253, 34,
       Quardrant2,    252, 37,
       Quardrant2,    252, 41,
       Quardrant2,    251, 44,
       Quardrant2,    251, 47,
       Quardrant2,    250, 50,
       Quardrant2,    249, 53,
       Quardrant2,    249, 56,
       Quardrant2,    248, 59,
       Quardrant2,    247, 62,
       Quardrant2,    247, 65,
       Quardrant2,    246, 68,
       Quardrant2,    245, 71,
       Quardrant2,    244, 74,
       Quardrant2,    243, 77,
       Quardrant2,    242, 80,
       Quardrant2,    241, 83,
       Quardrant2,    240, 86,
       Quardrant2,    239, 89,
       Quardrant2,    238, 92,
       Quardrant2,    237, 95,
       Quardrant2,    236, 98,
       Quardrant2,    234, 100,
       Quardrant2,    233, 103,
       Quardrant2,    232, 106,
       Quardrant2,    231, 109,
       Quardrant2,    229, 112,
       Quardrant2,    228, 115,
       Quardrant2,    226, 117,
       Quardrant2,    225, 120,
       Quardrant2,    223, 123,
       Quardrant2,    222, 126,
       Quardrant2,    220, 128,
       Quardrant2,    219, 131,
       Quardrant2,    217, 134,
       Quardrant2,    215, 136,
       Quardrant2,    214, 139,
       Quardrant2,    212, 142,
       Quardrant2,    210, 144,
       Quardrant2,    208, 147,
       Quardrant2,    207, 149,
       Quardrant2,    205, 152,
       Quardrant2,    203, 154,
       Quardrant2,    201, 157,
       Quardrant2,    199, 159,
       Quardrant2,    197, 162,
       Quardrant2,    195, 164,
       Quardrant2,    193, 167,
       Quardrant2,    191, 169,
       Quardrant2,    189, 171,
       Quardrant2,    187, 174,
       Quardrant2,    185, 176,
       Quardrant2,    183, 178,
       Quardrant2,    180, 180,
       Quardrant2,    178, 183,
       Quardrant2,    176, 185,
       Quardrant2,    174, 187,
       Quardrant2,    171, 189,
       Quardrant2,    169, 191,
       Quardrant2,    167, 193,
       Quardrant2,    164, 195,
       Quardrant2,    162, 197,
       Quardrant2,    159, 199,
       Quardrant2,    157, 201,
       Quardrant2,    154, 203,
       Quardrant2,    152, 205,
       Quardrant2,    149, 207,
       Quardrant2,    147, 208,
       Quardrant2,    144, 210,
       Quardrant2,    142, 212,
       Quardrant2,    139, 214,
       Quardrant2,    136, 215,
       Quardrant2,    134, 217,
       Quardrant2,    131, 219,
       Quardrant2,    128, 220,
       Quardrant2,    126, 222,
       Quardrant2,    123, 223,
       Quardrant2,    120, 225,
       Quardrant2,    117, 226,
       Quardrant2,    115, 228,
       Quardrant2,    112, 229,
       Quardrant2,    109, 231,
       Quardrant2,    106, 232,
       Quardrant2,    103, 233,
       Quardrant2,    100, 234,
       Quardrant2,    98,   236,
       Quardrant2,    95,   237,
       Quardrant2,    92,   238,
       Quardrant2,    89,   239,
       Quardrant2,    86,   240,
       Quardrant2,    83,   241,
       Quardrant2,    80,   242,
       Quardrant2,    77,   243,
       Quardrant2,    74,   244,
       Quardrant2,    71,   245,
       Quardrant2,    68,   246,
       Quardrant2,    65,   247,
       Quardrant2,    62,   247,
       Quardrant2,    59,   248,
       Quardrant2,    56,   249,
       Quardrant2,    53,   249,
       Quardrant2,    50,   250,
       Quardrant2,    47,   251,
       Quardrant2,    44,   251,
       Quardrant2,    41,   252,
       Quardrant2,    37,   252,
       Quardrant2,    34,   253,
       Quardrant2,    31,   253,
       Quardrant2,    28,   253,
       Quardrant2,    25,   254,
       Quardrant2,    22,   254,
       Quardrant2,    19,   254,
       Quardrant2,    16,   255,
       Quardrant2,    13,   255,
       Quardrant2,    9,    255,
       Quardrant2,    6,    255,
       Quardrant2,    3,    255,
       //第四象限  +iA,   -iB
       Quardrant2,    0,    255,
       Quardrant3,    3,    255,
       Quardrant3,    6,    255,
       Quardrant3,    9,    255,
       Quardrant3,    13,   255,
       Quardrant3,    16,   255,
       Quardrant3,    19,   254,
       Quardrant3,    22,   254,
       Quardrant3,    25,   254,
       Quardrant3,    28,   253,
       Quardrant3,    31,   253,
       Quardrant3,    34,   253,
       Quardrant3,    37,   252,
       Quardrant3,    41,   252,
       Quardrant3,    44,   251,
       Quardrant3,    47,   251,
       Quardrant3,    50,   250,
       Quardrant3,    53,   249,
       Quardrant3,    56,   249,
       Quardrant3,    59,   248,
       Quardrant3,    62,   247,
       Quardrant3,    65,   247,
       Quardrant3,    68,   246,
       Quardrant3,    71,   245,
       Quardrant3,    74,   244,
       Quardrant3,    77,   243,
       Quardrant3,    80,   242,
       Quardrant3,    83,   241,
       Quardrant3,    86,   240,
       Quardrant3,    89,   239,
       Quardrant3,    92,   238,
       Quardrant3,    95,   237,
       Quardrant3,    98,   236,
       Quardrant3,    100, 234,
       Quardrant3,    103, 233,
       Quardrant3,    106, 232,
       Quardrant3,    109, 231,
       Quardrant3,    112, 229,
       Quardrant3,    115, 228,
       Quardrant3,    117, 226,
       Quardrant3,    120, 225,
       Quardrant3,    123, 223,
       Quardrant3,    126, 222,
       Quardrant3,    128, 220,
       Quardrant3,    131, 219,
       Quardrant3,    134, 217,
       Quardrant3,    136, 215,
       Quardrant3,    139, 214,
       Quardrant3,    142, 212,
       Quardrant3,    144, 210,
       Quardrant3,    147, 208,
       Quardrant3,    149, 207,
       Quardrant3,    152, 205,
       Quardrant3,    154, 203,
       Quardrant3,    157, 201,
       Quardrant3,    159, 199,
       Quardrant3,    162, 197,
       Quardrant3,    164, 195,
       Quardrant3,    167, 193,
       Quardrant3,    169, 191,
       Quardrant3,    171, 189,
       Quardrant3,    174, 187,
       Quardrant3,    176, 185,
       Quardrant3,    178, 183,
       Quardrant3,    180, 180,
       Quardrant3,    183, 178,
       Quardrant3,    185, 176,
       Quardrant3,    187, 174,
       Quardrant3,    189, 171,
       Quardrant3,    191, 169,
       Quardrant3,    193, 167,
       Quardrant3,    195, 164,
       Quardrant3,    197, 162,
       Quardrant3,    199, 159,
       Quardrant3,    201, 157,
       Quardrant3,    203, 154,
       Quardrant3,    205, 152,
       Quardrant3,    207, 149,
       Quardrant3,    208, 147,
       Quardrant3,    210, 144,
       Quardrant3,    212, 142,
       Quardrant3,    214, 139,
       Quardrant3,    215, 136,
       Quardrant3,    217, 134,
       Quardrant3,    219, 131,
       Quardrant3,    220, 128,
       Quardrant3,    222, 126,
       Quardrant3,    223, 123,
       Quardrant3,    225, 120,
       Quardrant3,    226, 117,
       Quardrant3,    228, 115,
       Quardrant3,    229, 112,
       Quardrant3,    231, 109,
       Quardrant3,    232, 106,
       Quardrant3,    233, 103,
       Quardrant3,    234, 100,
       Quardrant3,    236, 98,
       Quardrant3,    237, 95,
       Quardrant3,    238, 92,
       Quardrant3,    239, 89,
       Quardrant3,    240, 86,
       Quardrant3,    241, 83,
       Quardrant3,    242, 80,
       Quardrant3,    243, 77,
       Quardrant3,    244, 74,
       Quardrant3,    245, 71,
       Quardrant3,    246, 68,
       Quardrant3,    247, 65,
       Quardrant3,    247, 62,
       Quardrant3,    248, 59,
       Quardrant3,    249, 56,
       Quardrant3,    249, 53,
       Quardrant3,    250, 50,
       Quardrant3,    251, 47,
       Quardrant3,    251, 44,
       Quardrant3,    252, 41,
       Quardrant3,    252, 37,
       Quardrant3,    253, 34,
       Quardrant3,    253, 31,
       Quardrant3,    253, 28,
       Quardrant3,    254, 25,
       Quardrant3,    254, 22,
       Quardrant3,    254, 19,
       Quardrant3,    255, 16,
       Quardrant3,    255, 13,
       Quardrant3,    255, 9,
       Quardrant3,    255, 6,
       Quardrant3,    255, 3,
       };
//internal  clock,Fosc/6,PCA interrupt disable
//system oscillator frequency 12M, 1/6 * 12M=0.5us
/************************************************************/
void ConfigPWM()
{                                 //CIDL WDTE               CPS1 CPS0  ECF
       CMOD=0x02;        //     0    0    0    0    0    0    1    0
       CL=0;
       CH=0;
       PWM3enable();      // CEX0(P1.3) 8 bit PWM Model,join B winding
       PWM4enable();      // CEX1(P1.4) 8 bit PWM Model,join A winding
       CR=1;                   // turn the PCA counter on
}
/************************************************************/
//马达一个脉冲走过的最大步距,对应细分表的间隔
//取值是:1、2、4、8、16...2的倍数
/************************************************************/
void SetMaxStep(unsigned char Steps)
{
       MotorWorking.MaxStep=Steps;  
}
/************************************************************/
//马达走动一步
//参数:Direct=1,0-->3象限逆时针走细分表
//
/************************************************************/
void MotorRunOneStep(unsigned char Direct)
{
       if(Direct)
       {     //逆时针走细分表
              //下一细分表ID等于当前细分表ID加细分倍数
              MotorWorking.SubdivideTableID+=MotorWorking.SubdivideNumber;
              if(MotorWorking.SubdivideTableID%128==0)
                     MotorWorking.MotorSteps-=1;     //细分表走完128小步,马达旋转1.8度,走了1步
              if(MotorWorking.SubdivideTableID>=512)          //细分表走完归零
                     MotorWorking.SubdivideTableID=0;
       }
       else
       {     //顺时针走细分表
              if(MotorWorking.SubdivideTableID==0)             //下一步细分表ID等于当前细分表ID减细分倍数
                     MotorWorking.SubdivideTableID=512-MotorWorking.SubdivideNumber;
              else
                     MotorWorking.SubdivideTableID-=MotorWorking.SubdivideNumber;
              if(MotorWorking.SubdivideTableID%128==0)
                     MotorWorking.MotorSteps-=1;     //细分表走完128小步,马达旋转1.8度,走了1步
       }
}
/************************************************************/
//马达根据细分ID输出电流
/************************************************************/
void MotorGetEnergy(unsigned int SubdivideID)
{
       SubdivideTable *pSubdivideTable;
       pSubdivideTable=&(MotorSubdividedTab+SubdivideID);
       switch(pSubdivideTable->mQuardrant)
       {
              case 1:                  //+iA,+iB
                     PhaseA=1;
                     PhaseB=1;
                     break;
              case 2:                  //-iA,+iB
                     PhaseA=0;
                     PhaseB=1;
                     break;
              case 3:                  //-iA,-iB
                     PhaseA=0;
                     PhaseB=0;
                     break;
              case 4:                  //+iA,-iB
                     PhaseA=1;
                     PhaseB=0;
                     break;
              default:
                     break;
       }
       CCAP3L=0;
       CCAP3G=255-pSubdivideTable->mB_PWM;
       CCAP4L=0;                                         //CL<CCAPxL  PWMx=0
       CCAP4G=255-pSubdivideTable->mA_PWM;              //CL>=CCAPxL  PWMx=1
}
/************************************************************/
void MotorRun(unsigned char Direct,unsigned int Steps)
{
       unsigned int StepCount;
       unsigned char Subdivide=1;
       switch(MotorWorking.WorkState)
       {
              case MotorIdel:
                     MotorWorking.MotorDirect=Direct;
                     MotorWorking.MotorSteps=Steps;
                     MotorWorking.SubdivideNumber=1;                         //马达128倍细分
                     MotorWorking.WorkState=MotorPowerOn;
         case MotorPowerOn:       //马达旋转步距角比较小时,在上电过程中完成,速度不上到正常旋转中
                     while(Subdivide<MotorWorking.MaxStep)
                     {
                            StepCount=65;                            //马达走64个小步,速度上一个台阶
                            while(--StepCount)
                            {
                                   MotorRunOneStep(MotorWorking.MotorDirect);
                                   MotorGetEnergy(MotorWorking.SubdivideTableID);
                            }
                            Subdivide<<=1;
                            MotorWorking.SubdivideNumber=Subdivide;
                     }//当马达速度达到最大速度时进入正常运转状态
                     MotorWorking.WorkState=MotorNormalState;
              case MotorNormalState:
                     StepCount=MotorWorking.MotorSteps;       //这里的StepCount保存的是马达还有多少个步距角
                     while(StepCount>4)                                 //马达最后一圈进入减速状态
                     {
                                   MotorRunOneStep(MotorWorking.MotorDirect);
                                   MotorGetEnergy(MotorWorking.SubdivideTableID);
                                   StepCount=MotorWorking.MotorSteps;
                     }
                     MotorWorking.WorkState=MotorPowerOff;
              case MotorPowerOff:
                     StepCount=MotorWorking.MotorSteps;
                     if(MotorWorking.MotorSteps==4)
                     {
                            Subdivide=MotorWorking.MaxStep>>1;
                            while(Subdivide)
                            {
                                   StepCount=65;                            //马达走64个小步,速度上一个台阶
                                   while(--StepCount)
                                   {
                                          MotorRunOneStep(MotorWorking.MotorDirect);
                                          MotorGetEnergy(MotorWorking.SubdivideTableID);
                                   }
                                   Subdivide>>=1;
                                   MotorWorking.SubdivideNumber=Subdivide;
                            }                               //当马达速度达到最大速度时进入正常运转状态
                     }
                     MotorWorking.WorkState=MotorIdel;
                     break;
                     default:
                     break;
       }
}

 


共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]