#include <iom16v.h>
#include <macros.h>
#include "iodata.h"
unsigned int location_x=0,location_y=50;
void motoX_ctrl(void)
{
unsigned char speed;
speed=adc_x>>3;
if (speed>0x4b)
{
motoX_forward(0x7f-speed);
location_x++;
}
if (speed<0x3b)
{
motoX_backward(speed-5);
location_x--;
}
}
void motoX_forward(unsigned char speed)
{
a3972_X(0b01111111,0b01111111); //ab
delay_temp(speed);
a3972_X(0b00111111,0b01111111); //-ab
delay_temp(speed);
a3972_X(0b00111111,0b00111111); //-a-b
delay_temp(speed);
a3972_X(0b01111111,0b00111111); //a-b
delay_temp(speed);
}
void motoX_backward(unsigned char speed)
{
a3972_X(0b01111111,0b00111111); //a-b
delay_temp(speed);
a3972_X(0b00111111,0b00111111); //-a-b
delay_temp(speed);
a3972_X(0b00111111,0b01111111); //-ab
delay_temp(speed);
a3972_X(0b01111111,0b01111111); //ab
delay_temp(speed);
}
void motoY_ctrl(void)
{
unsigned char speed;
speed=adc_y>>3;
if (speed>0x4b)
{
if (location_y>99) location_y=100;
else
{
motoY_forward(0x7f-speed);
location_y++;
}
}
if (speed<0x3b)
{
if (location_y<2) location_y=1;
else
{
motoY_backward(speed-5);
location_y--;
}
}
}
void motoY_forward(unsigned char speed)
{
a3972_Y(0b01111111,0b01111111); //ab
delay_temp(speed/2);
a3972_Y(0b01001111,0b01111111); //ab
delay_temp(speed/2);
a3972_Y(0b00001111,0b01111111); //ab
delay_temp(speed/2);
a3972_Y(0b00111111,0b01111111); //-ab
delay_temp(speed/2);
a3972_Y(0b00111111,0b01001111); //-ab
delay_temp(speed/2);
a3972_Y(0b00111111,0b00001111); //-ab
delay_temp(speed/2);
a3972_Y(0b00111111,0b00111111); //-a-b
delay_temp(speed/2);
a3972_Y(0b00001111,0b00111111); //-a-b
delay_temp(speed/2);
a3972_Y(0b01001111,0b00111111); //-a-b
delay_temp(speed/2);
a3972_Y(0b01111111,0b00111111); //a-b
delay_temp(speed/2);
a3972_Y(0b01111111,0b00001111); //a-b
delay_temp(speed/2);
a3972_Y(0b01111111,0b01001111); //a-b
delay_temp(speed/2);
}
void motoY_backward(unsigned char speed)
{
a3972_Y(0b01111111,0b00111111); //a-b
delay_temp(speed/2);
a3972_Y(0b01001111,0b00111111); //a-b
delay_temp(speed/2);
a3972_Y(0b00001111,0b00111111); //a-b
delay_temp(speed/2);
a3972_Y(0b00111111,0b00111111); //-a-b
delay_temp(speed/2);
a3972_Y(0b00111111,0b00001111); //-a-b
delay_temp(speed/2);
a3972_Y(0b00111111,0b00100111); //-a-b
delay_temp(speed/2);
a3972_Y(0b00111111,0b01111111); //-ab
delay_temp(speed/2);
a3972_Y(0b00001111,0b01111111); //-ab
delay_temp(speed/2);
a3972_Y(0b01001111,0b01111111); //-ab
delay_temp(speed/2);
a3972_Y(0b01111111,0b01111111); //ab
delay_temp(speed/2);
a3972_Y(0b01111111,0b01001111); //ab
delay_temp(speed/2);
a3972_Y(0b01111111,0b00001111); //ab
delay_temp(speed/2);
}
//
//A3972控制程序,bridge1\bridge2为两个线圈不同的开关状态
//
void a3972_X(unsigned char bridge1,unsigned char bridge2)
{
A3972_CLK_low;
A3972X_ST_low;
a3972_word0(bridge1,bridge2);
A3972X_ST_high;
// A3972X_ST_low;
}
void a3972_Y(unsigned char bridge1,unsigned char bridge2)
{
A3972_CLK_low;
A3972Y_ST_low;
a3972_word0(bridge1,bridge2);
A3972Y_ST_high;
// A3972Y_ST_low;
}
void a3972_word0(unsigned char bridge1,unsigned char bridge2)
{
A3972_DAT_high; //ITRIP = VDAC/4RS
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //Internal 2V
A3972_CLK_high;
A3972_CLK_low;
if (bridge2&0x80) A3972_DAT_high; //Bridge 2 mode
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge1&0x80) A3972_DAT_high; //Bridge 1 mode
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge2&0x40) A3972_DAT_high; //Bridge 2 phase
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge1&0x40) A3972_DAT_high; //Bridge 1 phase
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge2&0x20) A3972_DAT_high; //Bridge2, DAC, bit6
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge2&0x10) A3972_DAT_high; //Bridge2, DAC, bit5
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge2&0x08) A3972_DAT_high; //Bridge2, DAC, bit4
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge2&0x04) A3972_DAT_high; //Bridge2, DAC, bit3
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge2&0x02) A3972_DAT_high; //Bridge2, DAC, bit2
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge2&0x01) A3972_DAT_high; //Bridge2, DAC, bit1
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge1&0x20) A3972_DAT_high; //Bridge1, DAC, bit6
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge1&0x10) A3972_DAT_high; //Bridge1, DAC, bit5
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge1&0x08) A3972_DAT_high; //Bridge1, DAC, bit4
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge1&0x04) A3972_DAT_high; //Bridge1, DAC, bit3
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge1&0x02) A3972_DAT_high; //Bridge1, DAC, bit2
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
if (bridge1&0x01) A3972_DAT_high; //Bridge1, DAC, bit1
else A3972_DAT_low;
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //word select=0
A3972_CLK_high;
A3972_CLK_low;
}
//
//A3972初始化程序,根据不同工作模式而定
//
void a3972X_init(void)
{
A3972_CLK_low;
A3972X_ST_low;
a3972_word1();
A3972X_ST_high;
// A3972X_ST_low;
}
void a3972Y_init(void)
{
A3972_CLK_low;
A3972Y_ST_low;
a3972_word1();
A3972Y_ST_high;
// A3972Y_ST_low;
}
void a3972_word1(void)
{
A3972_DAT_high; //idle mode
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //Synchronous Rectification 15 (1=passive 0=active)
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //Synchronous Rectification 14
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //Oscillator Control C1
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //Oscillator Control C0
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //fast-decay time MSB
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_high; //fast-decay time bit2
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //fast-decay time bit1
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //fast-decay time LSB
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //off-time MSB
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //off-time bit3
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_high; //off-time bit2
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //off-time bit1
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //off-time LSB
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_high; //blank-time MSB
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_low; //blank-time LSB
A3972_CLK_high;
A3972_CLK_low;
A3972_DAT_high; //word select=1
A3972_CLK_high;
A3972_CLK_low;
}
谢谢!