AVR驅(qū)動軟驅(qū)電機程序
時代變了,軟驅(qū)沒有用了,但是里面的電機是比較好玩的,拿出來研究研究。
CODE:/*用mega32三個口線驅(qū)動軟驅(qū)電機.
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;
//步進電機運行數(shù)據(jù)表
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 正轉(zhuǎn)d=1 反轉(zhuǎn)
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 正轉(zhuǎn)d=1 反轉(zhuǎn) 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 );
}
}