AVR驱动软驱电机程序

电子元件 2年前 (2022) aysz01
0

时代变了,软驱没有用了,但是里面的电机是比较好玩的,拿出来研究研究。

CODE:
/*用mega32三个口线驱动软驱电机.
PA2接LB1833 ENA12 脚;
PA1接LB1833 IN1 脚;
PA0接LB1833 IN2 脚.
*************************************************** /
//ICC-AVRapplication builder : 2005-5-20 10:40:30
// Target : M32
// Crystal: 3.6864Mhz

#include
#include

unsigned char np;

//步进电机运行数据表
const unsigned char motortb[]={0x05,0x07,0x06,0x04,0x05,0x07,0x06,0x04};

void delay ( unsigned char t );// 每步延时的子程序
void a_step( unsigned char d, unsigned char t );//步进电机走一步d=0 正转d=1 反转

void port_init( void )
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0xFF;
DDRB = 0x00;
PORTC = 0xFF;
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0x00;
}

//call this routine to initialise all peripherals
void init_devices( void )
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();

MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
}

void delay ( unsigned char t )// 每步延时的子程序
{
unsigned char i;
unsigned int j;
for (i = 0 ; i < t ; i++ )
{
for ( j = 0 ;j < 800 ; j++ )
;
}
}
void a_step ( unsigned char d, unsigned char t) //步进电机走一步d=0 正转d=1 反转 t // 越大走得越慢
{
if ( d & 0x01 )
{
if ( np == 0 )
{
np = 7;
}
else
{
np--;
}
}
else
{
if ( np == 7 )
{
np = 0;
}
else
{
np++;
}
}
PORTA = motortb[np];
delay(t);
}
void a_turn (unsigned char d, unsigned char t)// 步进电机走一圈
{
unsigned char i;
for ( i = 0 ; i < 96 ; i++ )
{
a_step ( d, t );
}
}

void main ( void )
{
np = 4;
while (1)
{
a_turn ( 1, 1 );
}

}

版权声明:aysz01 发表于 2022-08-06 15:51:30。
转载请注明:AVR驱动软驱电机程序 | 电工学习网

暂无评论

暂无评论...